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)
相关推荐
伊织code12 小时前
PyTorch API 9 - masked, nested, 稀疏, 存储
pytorch·python·ai·api·-·9·masked
萧霍之14 小时前
基于onnxruntime结合PyQt快速搭建视觉原型Demo
pytorch·python·yolo·计算机视觉
伊织code21 小时前
PyTorch API 1 - 概述、数学运算、nn、实用工具、函数、张量
人工智能·pytorch·python·深度学习·ai·api
伊织code1 天前
PyTorch API 4 - 分布式通信、分布式张量
pytorch·python·ai·api·-·4·分布式通信、分布式张量
海天一色y2 天前
Pycharm(二十)张量的运算与操作
pytorch·深度学习·pycharm
蹦蹦跳跳真可爱5892 天前
Python----神经网络(基于AlexNet的猫狗分类项目)
人工智能·pytorch·python·深度学习·神经网络·分类
伊织code2 天前
PyTorch API 8 - 工具集、onnx、option、复数、DDP、量化、分布式 RPC、NeMo
pytorch·python·ai·api·-·8
Listennnn2 天前
PyTorch 中如何针对 GPU 和 TPU 使用不同的处理方式
人工智能·pytorch·python
大G哥2 天前
19_大模型微调和训练之-基于LLamaFactory+LoRA微调LLama3
人工智能·pytorch·python·深度学习·计算机视觉
ayiya_Oese2 天前
[数据处理] 6. 数据可视化
人工智能·pytorch·python·深度学习·机器学习·信息可视化