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)
相关推荐
往日情怀酿做酒 V17639296381 小时前
pytorch的介绍以及张量的创建
人工智能·pytorch·python
石迹耿千秋6 小时前
tensor
pytorch
2401_878624797 小时前
pytorch 神经网络
人工智能·pytorch·神经网络
Gyoku Mint8 小时前
深度学习×第7卷:参数初始化与网络搭建——她第一次挑好初始的重量
人工智能·pytorch·rnn·深度学习·神经网络·算法·机器学习
boooo_hhh9 小时前
第35周—————糖尿病预测模型优化探索
pytorch·深度学习·机器学习
我爱一条柴ya11 小时前
【AI大模型】RAG系统组件:向量数据库(ChromaDB)
数据库·人工智能·pytorch·python·ai·ai编程
仗剑_走天涯17 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
2401_878624791 天前
pytorch 自动微分
人工智能·pytorch·python·机器学习
水龙吟啸1 天前
从零开始搭建深度学习大厦系列-2.卷积神经网络基础(5-9)
人工智能·pytorch·深度学习·cnn·mxnet
慕婉03071 天前
深度学习中的常见损失函数详解及PyTorch实现
人工智能·pytorch·深度学习