Search code examples
neural-networkdeep-learningconv-neural-networkpytorchtorch

PyTorch - RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'weight'


I load my previously trained model and want to classify a single (test) image from the disk through this model. All the operations in my model are carried out on my GPU. Hence, I move the numpy array of the test image to GPU by calling cuda() function. When I call the forward() function of my model with the numpy array of the test image, I get the RuntimeError: Expected object of backend CPU but got backend CUDA for argument #2 'weight'.

Here is the code I use to load the image from disk and call the forward() function:

test_img = imageio.imread('C:\\Users\\talha\\Desktop\\dog.png')
test_img = imresize(test_img, (28, 28))
test_tensor = torch.from_numpy(test_img)
test_tensor = test_tensor.cuda()
test_tensor = test_tensor.type(torch.FloatTensor)
log_results = model.forward(test_tensor)

Software Environment:

torch: 1.0.1

GPU: Nvidia GeForce GTX 1070

OS: Windows 10 64-bit

Python: 3.7.1


Solution

  • Convert to FloatTensor before sending it over the GPU.

    So, the order of operations will be:

    test_tensor = torch.from_numpy(test_img)
    
    # Convert to FloatTensor first
    test_tensor = test_tensor.type(torch.FloatTensor)
    
    # Then call cuda() on test_tensor
    test_tensor = test_tensor.cuda()
    
    log_results = model.forward(test_tensor)