Facebook released deep learning toolkit PyTorch Hub, make it easier to reproduce paper

Recently, PyTorch community released a deep learning toolkit PyTorchHub, machine learning help workers to work faster and achieve reproducible important papers. PyTorchHub consists of a pre-training model warehouses, designed for reproducibility as well as new research studies to improve. It also built on Google Colab support, and with Papers With Code integration. Currently PyTorchHub includes a series of image classification, segmentation, generation and conversion related models.

Reproducibility is a basic requirement in many areas of research, which of course includes field research based on machine learning techniques. However, many machine learning relevant papers or not to reproduce, or difficult to reproduce. With the continued growth in the number of papers, including the current on the arXiv pre-print and tens of thousands of papers submitted to the conference papers, research work Reproducibility is becoming increasingly important. While many papers have trained with the code and model, but such help is obviously very limited, step readers find their own way of reproduction process is still in great demand. Let's look at how to complete the work quickly and release model reproduced by PyTorch Hub of the weapon.

image

How to quickly release model

This section introduces the model for publishers is how quickly and efficiently will join PyTorch Hub own model library. PyTorch Hub support model by adding a simple hubconf.py file pre-training (weight training and pre-defined model) posted to GitHub repository. This model provides a list of its dependencies list. Some examples can be torchvision , huggingface-BERT and gan-model-zoo to find the repository.

Pytoch community gives an example of hubconf.py torchvision file:

Copy the code 
 
 
# Optional list of dependencies required by the package
 
dependencies = ['torch']
   
 
from torchvision.models.alexnet import alexnet
 
Torchvision.Models.Densenet From Import Densenet121, Densenet169, Densenet201, Densenet161
 
from torchvision.models.inception import inception_v3
 
from torchvision.models.resnet import resnet18, resnet34, resnet50, resnet101, resnet152, resnext50_32x4d, resnext101_32x8d
 
from torchvision.models.squeezenet import squeezenet1_0, squeezenet1_1
 
from torchvision.models.vgg import vgg11, vgg13, vgg16, vgg19, vgg11_bn, vgg13_bn, vgg16_bn, vgg19_bn
 
from torchvision.models.segmentation import fcn_resnet101, deeplabv3_resnet101
 
from torchvision.models.googlenet import googlenet
 
from torchvision.models.shufflenetv2 import shufflenet_v2_x0_5, shufflenet_v2_x1_0
 
from torchvision.models.mobilenet import mobilenet_v2

In torchvision, the model has the following features:

  • Each model files can be executed independently or achieve a certain function
  • PyTorch package without any addition (in the hubconf.py encoded as dependencies [ 'torch'])
  • They do not need a separate entry points, because the model when creating seamlessly out of the box.

PyTroch community believes minimize package dependencies can reduce the difficulties encountered when the user loads the model. Here they gave a more complex example --HuggingFace's BERT model, its hubconf.py as follows:

Copy the code 
 
 
dependencies = ['torch', 'tqdm', 'boto3', 'requests', 'regex']
   
 
from hubconfs.bert_hubconf import (
 
bertTokenizer,
 
bertModel,
 
bertForNextSentencePrediction,
 
bertForPreTraining,
 
bertForMaskedLM,
 
bertForSequenceClassification,
 
bertForMultipleChoice,
 
bertForQuestionAnswering,
 
bertForTokenClassification
 
)

此外,对于每个模型,PyTorch 官方提到都需要为其创建一个入口点。下面是一个用于指定 bertForMaskedLM 模型的入口点的代码片段,这部分代码完成的功能是返回加载了预训练参数的模型。

复制代码
 
 
def bertForMaskedLM(*args, **kwargs):
 
"""
 
BertForMaskedLM includes the BertModel Transformer followed by the
 
pre-trained masked language modeling head.
 
Example:
 
...
 
"""
 
model = BertForMaskedLM.from_pretrained(*args, **kwargs)
 
return model

这些入口点可以看成是复杂的模型结构的一种封装形式。它们可以在提供简洁高效的帮助文档的同时完成下载预训练权重的功能(例如,通过 pretrained = True),也可以集成其他特定功能,例如可视化。

通过 hubconf.py,模型发布者可以在 Github 上基于template提交他们的合并请求。PyTorch 社区希望通过 PyTorch Hub 创建一系列高质量、易复现且效果好的模型以提高研究工作的复现性。因此,PyTorch 会通过与模型发布者合作的方式以完善请求,并有可能会在某些情况下拒绝发布一些低质量的模型。一旦 PyTorch 社区接受了模型发布者的请求,这些新的模型将会很快出现在 PyTorch Hub 的网页上以供用户浏览。

用户工作流

对于想使用 PyTorch Hub 对别人的工作进行复现的用户,PyTorch Hub 提供了以下几个步骤:1)浏览可用的模型;2)加载模型;3)探索已加载的模型。下面让我们来浏览几个例子。

浏览可用的入口点

用户可以使用 torch.hub.list() API 列出仓库中的所有可用入口点。

复制代码
 
 
>>> torch.hub.list('pytorch/vision')
 
>>>
 
[ 'alexnet',
 
'deeplabv3_resnet101',
 
'densenet121',
 
...
 
'vgg16',
 
'vgg16_bn',
 
'vgg19',
 
'vgg19_bn']

注意,PyTorch Hub 还允许辅助入口点(除了预训练模型),例如,用于 BERT 模型预处理的 bertTokenizer,它可以使用户工作流程更加顺畅。

加载模型

对于 PyTroch Hub 中可用的模型,用户可以使用 torch.hub.load() API 加载模型入口点。此外,torch.hub.help() API 可以提供有关如何实例化模型的有用信息。

复制代码
 
 
print(torch.hub.help('pytorch/vision', 'deeplabv3_resnet101'))
 
model = torch .hub.load('pytorch/vision', 'deeplabv3_resnet101', pretrained=True)

由于仓库的持有者会不断添加错误修复以及性能改进,PyTorch Hub 允许用户通过调用以下内容简单地获取最新更新:

复制代码
 
 
model = torch.hub.load( ..., force_reload=True)

这一举措可以有效地减轻仓库持有者重复发布模型的负担,从而使他们能够更专注于自己的研究工作。同时,也确保了用户可以获得最新版本的模型。

此外,对于用户来说,稳定性也是一个重要问题。因此,某些模型所有者会从特征的分支或标签为他们提供服务,以确保代码的稳定性。例如,pytorch_GAN_zoo 会从 hub 分支为他们提供服务:

复制代码
 
 
model = torch.hub.load('facebookresearch/pytorch_GAN_zoo:hub', 'DCGAN', pretrained=True, useGPU=False)

这里,传递给 hub.load() 的 * args,** kwargs 用于实例化模型。在上面的示例中,pretrained = True 和 useGPU = False 被传递给模型的入口点。

探索已加载的模型

从 PyTorch Hub 加载模型后,用户可以使用以下工作流查看已加载模型的可用方法,并更好地了解运行它所需的参数。

其中,dir(model) 可以查看模型中可用的方法。下面是 bertForMaskedLM 的一些方法:

复制代码
 
 
>>> dir(model)
 
>>>
 
[ 'forward'
 
...
 
'to'
 
'state_dict',
 
]

help(model.forward)则会提供使已加载的模型运行时所需参数的视图:

复制代码
 
 
>>> help(model.forward)
 
>>>
 
Help on method forward in module pytorch_pretrained_bert.modeling:
 
forward(input_ids, token_type_ids=None, attention_mask=None, masked_lm_labels=None)
 
...

更多细节可以查看BERTDeepLabV3页面:

其他探索方式与相关资源

PyTorch Hub 中提供的模型也支持 Colab,并且会直接链接在 Papers With Code 上,用户只需单击链接即可开始使用:

image

PyTorch 提供了一些相关资源帮助用户快速上手 PyTorch Hub:

FAQ

Q: If we want to contribute in a Hub has been a model, but maybe my model has higher accuracy, I should contribute it?
A: Yes, please submit your model, Hub next step is to develop the best model to demonstrate the voting system.

Q: Who is responsible for keeping the model right PyTorch Hub weight?
A: As a contributor, you are responsible for keeping the model weights. You can host your model in your favorite cloud storage, or if it comply with the limits, you can host your model on GitHub. If you can not keep the weight, please contact us to submit questions by way of Hub warehouse.

Q: What if my model uses data privatized train how to do? I should also contribute to this model it?
A: Please do not submit your model! PyTorch Hub open source research center and expand the use of public data sets to train these models. If you submit a request to merge the private model, we will ask you to resubmit using public data model training.

Q: I downloaded the model stored?
A: We follow the XDG base directory specification, and follow the general standard cache files and directories. These positions used in the following order:

  • Call hub.set_dir (<PATH_TO_HUB_DIR>)
  • If the environment variables TORCH_HOME, compared with $ TORCH_HOME / hub.
  • If you set the environment variable XDG_CACHE_HOME, compared with $ XDG_CACHE_HOME / torch / hub.
  • ~/.cache/torch/hub

related suggestion:

Guess you like

Origin www.cnblogs.com/jfdwd/p/11262511.html