报错

device:GPU CPU

?1:RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

sol1:用CUDA训练的模型,测试时要指定设备为CUDA

  1. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  2. # img = transform(img)后
  3. img = img.to(device)
  4. # or
  5. with torch.no_grad():
  6. output = model(img.to(device))

sol2:调用训练好的模型时,使用CPU

  1. model = torch.load("pretrained_demo_10.pth",map_location=torch.device("cpu"))