为什么 PyTorch 无法从 Torch Hub 实例化这个神经网络?

Why PyTorch fails to instantiate this neural network from Torch Hub?

我在 Ubuntu、运行 PyTorch 1.7.1 和 torchvision 0.8.2:

print(torch.__version__)
print(torchvision.__version__)

>> 1.7.1
>> 0.8.2

当我尝试实例化 resnet18 时:

model_resnet18 = torch.hub.load('pytorch/vision', 'resnet18', pretrained=True)

它失败了:

Using cache found in /home/stark/.cache/torch/hub/pytorch_vision_master
Traceback (most recent call last):
  File "/home/stark/Work/test/binary_classifier.py", line 253, in <module>
    build_and_train()
  File "/home/stark/Work/test/binary_classifier.py", line 174, in build_and_train
    model_resnet18 = torch.hub.load('pytorch/vision', 'resnet18', pretrained=True)
  File "/home/stark/anaconda3/envs/torch-env/lib/python3.8/site-packages/torch/hub.py", line 370, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "/home/stark/anaconda3/envs/torch-env/lib/python3.8/site-packages/torch/hub.py", line 396, in _load_local
    hub_module = import_module(MODULE_HUBCONF, hubconf_path)
  File "/home/stark/anaconda3/envs/torch-env/lib/python3.8/site-packages/torch/hub.py", line 71, in import_module
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/stark/.cache/torch/hub/pytorch_vision_master/hubconf.py", line 14, in <module>
    from torchvision.models.mobilenetv2 import mobilenet_v2
ModuleNotFoundError: No module named 'torchvision.models.mobilenetv2'

我正在使用 Python 3.8 并且已经在使用最新的 PyTorch 版本,那么我还能做些什么来解决这个问题?

非常感谢任何指点!

您可以使用 torchvisionAPI 代替:

model_resnet18 = torchvision.models.resnet18(pretrained=True)