Img should be PIL Image. Got <class 'torch.Tensor'>

test_transform = transforms.Compose({
    transforms.Resize(320),
    transforms.CenterCrop(250),
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.ToTensor(),
    normalize
})
testdata = DogsDataset(sub, '/content/drive/My Drive/Kaggle competitions/dog_breed dataset/test/',test_transform)
testloader = DataLoader(testdata,batch_size=24)
def test_submission(model):
    since = time.time()
    sub_output = []
    model.train(False)
    for inputs, labels in testloader:
        inputs = inputs.to(device)
        outputs = model(inputs)
        sub_output.append(outputs.data.cpu().numpy())
    sub_output = np.concatenate(sub_output)
    for idx,row in enumerate(sub_output.astype('float')):
        sub_output[idx] = np.exp(row)/np.sum(np.exp(row))
    output.loc[:,1:] = sub_output
    print()
    time_elapsed = time.time() - since
    print('Run complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))
if torch.cuda.is_available():
    device = 'cuda'
else:
    device = 'cpu'
model = model.to(device)
test_submission(model)

I am implementing this test function for dog breed identification.

On running this, I am getting the following error and can’t get where I am wrong


TypeError: img should be PIL Image. Got <class 'torch.Tensor'>

Because you are using torch.Tensor() in transforms. Can you share your Dataoader function.

I have not used custom dataloader

I think I got my mistake. Actually I have done some problem with cpu and gpu.

1 Like