RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #2 'weight'

I’m at a loss at how to ensure that the data and the model are both on the GPU. I tried multiple approaches.

This is the code:

TODO: Display an image along with the top 5 classes

Get and process a Test Image

torch.set_default_tensor_type(‘torch.cuda.FloatTensor’)

test_image_index = 28
test_image = test_dir + “/” + str(test_image_index) + “/image_05230.jpg”
img = process_image(test_image)
#test_image = images.to(‘cuda’)

Display test image, with Label as title

label = cat_to_name.get(str(test_image_index))
print(label)
ax = imshow(img, ax=plt).title(label)

Run image through model

probs, classes = predict(test_image, model)
print(“Probs:”,probs)
print(“Class:”,classes)

This is the error:

RuntimeError Traceback (most recent call last)
in ()
14
15 # Run image through model
—> 16 probs, classes = predict(test_image, model)
17 print(“Probs:”,probs)
18 print(“Class:”,classes)

in predict(image_path, model, topk)
8 # Process image and run through model
9 img = process_image(image_path).unsqueeze(0)
—> 10 outputs = model.forward(img)
11
12

/opt/conda/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/models/densenet.py in forward(self, x)
218
219 def forward(self, x):
–> 220 features = self.features(x)
221 out = F.relu(features, inplace=True)
222 out = F.avg_pool2d(out, kernel_size=7, stride=1).view(features.size(0), -1)

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
–> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
89 def forward(self, input):
90 for module in self._modules.values():
—> 91 input = module(input)
92 return input
93

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
–> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)

/opt/conda/lib/python3.6/site-packages/torch/nn/modules/conv.py in forward(self, input)
299 def forward(self, input):
300 return F.conv2d(input, self.weight, self.bias, self.stride,
–> 301 self.padding, self.dilation, self.groups)
302
303

RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #2 ‘weight’

It looks like your input tensor is still on the CPU.
You should push your data as well as the model onto the device:

data = data.to('cuda')
target = target.to('cuda')
model = model.to('cuda') # assignment not really necessary here

If you want to check the current device of the tensor, just use print(tensor.device).

8 Likes