Argument 'input' (position 1) must be Tensor, not str


I am loading images into a custom Dataset and trying to classify them. When I pass the image through my neural net, I get

TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not str

You can see a short notebook reproducing the problem here.

Can anyone spot what I am doing wrong?


1 Like

Can you print the inputs? Maybe they are still a dict or a tuple of key and value and you are trying to feed the key (which is a string) to the network?

In the last cell in that notebook, you have the following line:

   inputs, labels = data

since data is a dictionary, that assignment will only assigns the keys. I made an example dictionary d with two elements:

>>> d = {'a':1, 'b':2}
>>> x, y = d
>>> x, y
('a', 'b')

Yet, another problem is in your dataset class, where in __getitem__ you defined sample as a dictionary, and then pass a dictionary to self.transform() function. The transformation functions only accept PIL Image as input not dictionary.


Thanks that solved it for me.

@vmirly1 what would be the proper way to set up the sample in __getitem__ for the example provided in this question? To me, it looks like @Jacob_K did the same thing as this example from pytorch. In the pytorch example, they use sample = {'image': image, 'landmarks': landmarks} in creating the dataset class, and Jacob used sample = {'image': image, 'label': label}.
Sorry if this is an obvious answer, but Iā€™m new to pytorch and having a pretty hard time loading image files from a csv. @Jacob_K would it be possible to post the code that you eventually got to work for this problem.