有什么方法可以将PyTorch中可用的预训练模型下载到特定路径? [英] Is there any way I can download the pre-trained models available in PyTorch to a specific path?

查看:720
本文介绍了有什么方法可以将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 在他的答案:

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.

这可以如下进行:

import torch 
import torchvision
import os

# Suppose you are trying to load pre-trained resnet model in directory- models\resnet

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中可用的预训练模型下载到特定路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆