Is it possible to load a pytorch model (from a .pth
file, containing architecture+state_dict) without torchvision as a dependency?
import os
import torch
assert os.path.exists(r'.\vgg.pth')
model = torch.load(r'.\vgg.pth')
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-e26863d95688> in <module>
2 import torch
3 assert os.path.exists(r'.\vgg.pth')
----> 4 model = torch.load(r'.\vgg.pth')
~\Anaconda3\envs\pytorch_save\lib\site-packages\torch\serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
590 opened_file.seek(orig_position)
591 return torch.jit.load(opened_file)
--> 592 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
593 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
594
~\Anaconda3\envs\pytorch_save\lib\site-packages\torch\serialization.py in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
849 unpickler = pickle_module.Unpickler(data_file, **pickle_load_args)
850 unpickler.persistent_load = persistent_load
--> 851 result = unpickler.load()
852
853 torch._utils._validate_loaded_sparse_tensors()
ModuleNotFoundError: No module named 'torchvision'
I have looked into torch/serialization.py
, but I see no reason why it would need torchvision. The imports in this file are as follows:
import difflib
import os
import io
import shutil
import struct
import sys
import torch
import tarfile
import tempfile
import warnings
from contextlib import closing, contextmanager
from ._utils import _import_dotted_name
from ._six import string_classes as _string_classes
from torch._sources import get_source_lines_and_file
from torch.types import Storage
from typing import Any, BinaryIO, cast, Dict, Optional, Type, Tuple, Union, IO
import copyreg
import pickle
import pathlib