Hi,
I trained a model using DataParallel wrapper and then was trying to run inference in a machine without gpu and it does not work. Could somebody help me to check, here is the code and error:
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
import requests
from PIL import Image as PILImage
metadata = {
"image_size": {"width": 512, "height": 512},
"training_name": "resnet50_classifier",
"mean": [0.4847615957260132, 0.4710499048233032, 0.36006495356559753],
"std": [0.20003080368041992, 0.19453473389148712, 0.17458273470401764]
}
def get_image(image_url):
image_response = requests.get(image_url, stream=True)
image = PILImage.open(image_response.raw)
image_channels = len(image.getbands())
image_width, image_height = image.size
return image, image_width, image_height
mean = metadata.get("mean", None)
stddev = metadata.get("std", None)
target_size = (512, 512)
pil_image, _, _ = get_image(url)
transform = torchvision.transforms.Compose(
[torchvision.transforms.Resize(target_size), torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean, stddev)]
)
def _load_image( pil_image):
image = transform(pil_image)
image = image.float()
image = image.unsqueeze_(0)
image = image.to(device)
return image
image = _load_image(pil_image)
model = torch.load(path_to_model, map_location=torch.device("cpu"))
model(image)
RuntimeError Traceback (most recent call last)
in
----> 1 model(image)
~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
→ 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
~/.local/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py in forward(self, *inputs, **kwargs)
153 raise RuntimeError("module must have its parameters and buffers "
154 "on device {} (device_ids[0]) but found one of "
→ 155 “them on device: {}”.format(self.src_device_obj, t.device))
156
157 inputs, kwargs = self.scatter(inputs, kwargs, self.device_ids)
RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cpu