if torch.cuda.is_available():
model = model.cuda()
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
model.load_state_dict(torch.load(modelPath))
你的主机没有GPU,但是读入的预训练模型是使用GPU训练的,所以报错了,只需要在读取模型的torch.load加上参数map_location=torch.device('cpu'),大概是这样:
model = torch.load(model_path , map_location=torch.device('cpu'))
报错提示其实已经给出了解决方法:
If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.