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)
相关推荐
独隅4 小时前
EasyOCR跨框架部署:从PyTorch到TensorFlow Lite的转换全面指南
人工智能·pytorch·tensorflow
威迪斯特5 小时前
Ubuntu的apt命令详解:系统管理的核心工具
运维·服务器·ubuntu·apt·下载·包管理·维护
key_3_feng5 小时前
HarmonyOS 6.0 元服务(Meta Ability)深度设计方案
pytorch·深度学习·harmonyos·元服务
皮肤科大白8 小时前
X-AnyLabeling +9.5 G Medsam3全流程接入笔记
pytorch·笔记·深度学习
盼小辉丶8 小时前
PyTorch强化学习实战(2)——强化学习环境库Gymnasium
pytorch·深度学习·强化学习
kishu_iOS&AI9 小时前
深度学习 —— 正则化&批量归一化BN
人工智能·pytorch·python·深度学习
独隅12 小时前
将MAE模型从PyTorch无缝迁移到TensorFlow Lite的完整实践指南
人工智能·pytorch·tensorflow
Jmayday21 小时前
Pytorch:张量创建及运算
人工智能·pytorch·python
ACCELERATOR_LLC21 小时前
【DataWhale组队学习】DIY-LLM Task2 PyTorch 与资源核算
人工智能·pytorch·深度学习·大模型
张老师带你学1 天前
Unity 科幻武器系列
科技·游戏·unity·模型·游戏美术