aaron-xichen / pytorch-playground

Base pretrained models and datasets in pytorch (MNIST, SVHN, CIFAR10, CIFAR100, STL10, AlexNet, VGG16, VGG19, ResNet, Inception, SqueezeNet)
MIT License
2.62k stars 612 forks source link

Load pretrained model on CPU only machine #60

Closed boyang9602 closed 1 year ago

boyang9602 commented 1 year ago

I tried to load pretrained MNIST model on a CPU only machine, however, I met

RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location='cpu' to map your storages to the CPU.

So I added a parameter cuda with a default value True when creating models. This parameter is only used when loading pretrained models from model_zoo.

When load model from model_zoo.load_url, I set the map_location to "cuda" if cuda=True in the parameter, else "cpu"

map_location (optional) – a function or a dict specifying how to remap storage locations (see torch.load)

from https://pytorch.org/docs/stable/model_zoo.html