How to solve RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

64 Views Asked by At

I am using Fastai for multi-categorical image classification.Here is the full code:

from fastasi.vision.all import *
import numpy as np
import pandas as pd

path = untar_data(URLs.PASCAL_2007

df = pd.read_csv(path/'train.csv')

def get_x(r): return r['fname']

def get_y(r): return r['labels']

def splitter(df):
    train = df.index[~df['is_valid']].tolist()
    valid = df.index[df['is_valid']].tolist()
    return train, valid

dblock = DataBlock(blocks = (ImageBlock, MultiCategoryBlock),
                  splitter = splitter,
                  get_x = get_x,
                  get_y = get_y,
                  item_tfms = RandomResizedCrop(128, min_scale = 0.35))
dls = dblock.dataloaders(df)

learn = cnn_learner(dls, resnet18)
x,y = dls.train.one_batch()
activs = learn.model(x)
activs.shape

when I run this code, the above error occurs. By the way, I am using Kaggle notebook and GPU. The error occurs after activs = learn.model(x). Anyone to help

1

There are 1 best solutions below

1
khushi-411 On

You are getting this error because your input tensor is in the cuda device and the weight tensor is in the CPU device.

To resolve this issue you need to move the data to the GPU by changing the lines to:

x, y = dls.train.one_batch().cuda()
activs = learn.model(x).cuda()

Hope this resolves your problem. Thanks!