Is there any way I can download the pre-trained models available in PyTorch to a specific path?(有什么方法可以将 PyTorch 中可用的预训练模型下载到特定路径?)
问题描述
我指的是可以在这里找到的模型:https://pytorch.org/docs/stable/torchvision/models.html#torchvision-models
I am referring to the models that can be found here: https://pytorch.org/docs/stable/torchvision/models.html#torchvision-models
推荐答案
作为,@dennlinger 在他的中提到href="https://stackoverflow.com/a/52631758/8085890">答案:torch.utils.model_zoo
,在您加载预训练模型时被内部调用.
As, @dennlinger mentioned in his answer : torch.utils.model_zoo
, is being internally called when you load a pre-trained model.
更具体地说,每次加载预训练模型时都会调用方法:torch.utils.model_zoo.load_url()
.相同的文档中提到:
More specifically, the method: torch.utils.model_zoo.load_url()
is being called every time a pre-trained model is loaded. The documentation for the same, mentions:
model_dir
的默认值为 $TORCH_HOME/models
其中$TORCH_HOME
默认为 ~/.torch
.
The default value of
model_dir
is$TORCH_HOME/models
where$TORCH_HOME
defaults to~/.torch
.
默认目录可以用 $TORCH_HOME
覆盖环境变量.
The default directory can be overridden with the $TORCH_HOME
environment variable.
这可以按如下方式完成:
This can be done as follows:
import torch
import torchvision
import os
# Suppose you are trying to load pre-trained resnet model in directory- models
esnet
os.environ['TORCH_HOME'] = 'models\resnet' #setting the environment variable
resnet = torchvision.models.resnet18(pretrained=True)
我通过在 PyTorch 的 GitHub 存储库中提出问题来遇到上述解决方案:https://github.com/pytorch/vision/issues/616
I came across the above solution by raising an issue in the PyTorch's GitHub repository: https://github.com/pytorch/vision/issues/616
这导致了文档的改进,即上述解决方案.
This led to an improvement in the documentation i.e. the solution mentioned above.
这篇关于有什么方法可以将 PyTorch 中可用的预训练模型下载到特定路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有什么方法可以将 PyTorch 中可用的预训练模型下


基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01