pytorch 模型下载,from torchvision.datasets.utils import download_url不能下载模型,如何代理

1. torchvision.datasets.utils.download_url的代理

找到对应的文件/root/data1/anaconda3/envs/decouple_diffusion/lib/python3.12/site-packages/torchvision/datasets/utils.py

修改前:

python 复制代码
def _get_redirect_url(url: str, max_hops: int = 3) -> str:
    initial_url = url
    headers = {"Method": "HEAD", "User-Agent": "USER_AGENT"}

    for _ in range(max_hops + 1):
        with urllib.request.urlopen(urllib.request.Request(url, headers=headers)) as response:
            if response.url == url or response.url is None:
                return url

            url = response.url
    else:
        raise RecursionError(
            f"Request to {initial_url} exceeded {max_hops} redirects. The last redirect points to {url}."
        )

修改后:

python 复制代码
def _get_redirect_url(url: str, max_hops: int = 3) -> str:
    initial_url = url
    headers = {"Method": "HEAD", "User-Agent": "USER_AGENT"}

    # 设置代理
    proxy_handler = urllib.request.ProxyHandler({
        'http': 'http://192.168.155.245:19970',
        'https': 'http://192.168.155.245:19970'  # 如果需要支持 HTTPS
    })
    opener = urllib.request.build_opener(proxy_handler)
    urllib.request.install_opener(opener)

    for _ in range(max_hops + 1):
        with urllib.request.urlopen(urllib.request.Request(url, headers=headers)) as response:
            if response.url == url or response.url is None:
                return url

            url = response.url
    else:
        raise RecursionError(
            f"Request to {initial_url} exceeded {max_hops} redirects. The last redirect points to {url}."
        )

修改点说明:

  1. ProxyHandler 配置代理:

    设置 http 和 https 的代理地址。

  2. build_opener 和 install_opener:

    使用 build_opener 构建带有代理的处理器。
    使用 install_opener 让后续的 urlopen 请求使用代理。

  3. 兼容 HTTPS 请求:

    如果目标 URL 包括 HTTPS,确保设置 https 的代理。

2. 推荐:如果不想修改安装包的内部文件,可以在程序运行最开始设置

例如运行python main.py,在main.py 主文件最开始设置:

python 复制代码
import urllib.request

# 设置代理
proxy_handler = urllib.request.ProxyHandler({
    'http': 'http://192.168.155.245:19970',
    'https': 'http://192.168.155.245:19970'  # 如果需要支持 HTTPS
})
opener = urllib.request.build_opener(proxy_handler)
urllib.request.install_opener(opener)

3. from_pretrained 的代理

python 复制代码
vae = AutoencoderKL.from_pretrained(f"stabilityai/sd-vae-ft-{args.vae}",proxies={'http': 'http://192.168.155.xxx:19970','https': 'http://192.168.155.xxx:19970'}).to(device)
相关推荐
hopsky6 小时前
经典Transformer的PyTorch实现
pytorch·深度学习·transformer
brave and determined7 小时前
CANN训练营 学习(day7)昇腾AI训练全流程实战:从模型迁移到性能优化的深度指南
pytorch·ai·ai训练·昇腾ai·msprobe·模型性能调优·训练配置
baby_hua7 小时前
20251011_Pytorch从入门到精通
人工智能·pytorch·python
道19938 小时前
PyTorch 高级进阶教程之深度实战实例(四)
人工智能·pytorch·python
Francek Chen9 小时前
【自然语言处理】应用02:情感分析:使用循环神经网络
人工智能·pytorch·rnn·深度学习·神经网络·自然语言处理
拾贰_C9 小时前
【pytorch | torchvision | datasets】ImageFolder()类
人工智能·pytorch·python
Blossom.1189 小时前
Transformer时序预测实战:用PyTorch构建股价预测模型
运维·人工智能·pytorch·python·深度学习·自动化·transformer
baby_hua9 小时前
20251031_三天速通PyTorch
人工智能·pytorch·python
weixin_404679319 小时前
pytorch nn.Parameter self.register_parameter() 区别
人工智能·pytorch·python·深度学习·机器学习
好风凭借力,送我上青云10 小时前
Pytorch经典卷积神经网络-----激活函数篇
人工智能·pytorch·深度学习·算法·矩阵·cnn