Testing 1 Image VS 1 Dataset

Hello all,

I have a working Pytorch ConvNet program. It tests Datasets and runs rather slow, but effectively.

Would anyone know how to test images separately? I want to test 1 image instead of n number of images.

Thank you

You can load and process a single image, e.g. via PIL.Image.open and torchvision.transforms.
Once you have created the image tensor for this single input image, you can add a batch dimension via x = x.unsqueeze(0) and pass it to the model.
Let me know, if this works for you.

Thank you for the tips, ptrblck. I’m going to test it out right now.

What does x.unsqueeze(0) do? (What would happen if I didn’t use this function call and passed my image to my current model?)

unsqueeze adds a dimension at the specified position (here 0 in this example).

1 Like

Appreciate the help, harsha_g :smile:.

I’m getting the error: Attribute Error: ‘Compose’ object has no attribute ‘unsqueeze’.

Here’s my code for testing my image on the model, any help would be great!

‘’’

Load and Process a Single Image

new_image = Image.open(‘new_image.jpg’)

transformed_image = transforms.Compose([

transforms.ToPILImage(),

transforms.Resize((28, 28)),

transforms.RandomGrayscale(p = 1.0), # Transforms the image to a gray-scale

transforms.ToTensor(),

])

Add a Batch Dimension

transformed_image = transformed_image.unsqueeze(0)

check_image(transformed_image, model)

transformed_image is the transformation, not the actual transformed image.
You would have to define the transformation first and pass the input image to it.
Use this code instead:

new_image = Image.open(‘new_image.jpg’)

transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize((28, 28)),
    transforms.RandomGrayscale(p = 1.0), # Transforms the image to a gray-scale
    transforms.ToTensor(),
])

transformed_image = transform(new_image)
transformed_image = transformed_image.unsqueeze(0)
check_image(transformed_image, model)

This is the error that I am getting as a result of the code:
‘’’

Traceback (most recent call last):

File “/Users/Josh/Desktop/convolutional_neural_networks/pytorch_convolutional_neural_network_mnist.py”, line 233, in
transformed_image = transform(new_image)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torchvision/transforms/transforms.py”, line 61, in call
img = t(img)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torchvision/transforms/transforms.py”, line 127, in call
return F.to_pil_image(pic, self.mode)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torchvision/transforms/functional.py”, line 100, in to_pil_image
raise TypeError(‘pic should be Tensor or ndarray. Got {}.’.format(type(pic)))
TypeError: pic should be Tensor or ndarray. Got <class ‘PIL.JpegImagePlugin.JpegImageFile’>.
‘’’

Am I doing something wrong?

Ah sorry, ToPILImage is of course not needed, if you already provide the input as a PIL.Image.
I just copied your transformation without checking it. Just remove transforms.ToPILImage() and rerun the code.

1 Like

I see now.

Thanks again for your help, ptrblck!

This is so frustrating…

I’m still getting an error, which doesn’t make a whole lot of sense considering the fact I turned the colored image into Grayscale.

Traceback (most recent call last):
  File "/Users/Josh/Desktop/convolutional_neural_networks/pytorch_convolutional_neural_network_mnist.py", line 243, in <module>
    check_image(transformed_image, model)
  File "/Users/Josh/Desktop/convolutional_neural_networks/pytorch_convolutional_neural_network_mnist.py", line 210, in check_image
    scores = model(image)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/Users/Josh/Desktop/convolutional_neural_networks/pytorch_convolutional_neural_network_mnist.py", line 55, in forward
    x = activation_functions.relu(self.convolutional_input_layer(x))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 349, in forward
    return self._conv_forward(input, self.weight)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 345, in _conv_forward
    return F.conv2d(input, weight, self.bias, self.stride,
RuntimeError: Given groups=1, weight of size [8, 1, 3, 3], expected input[1, 3, 28, 28] to have 1 channels, but got 3 channels instead

Any help appreciated!

RandomGrayscale will return a 3 channel image as described in the docs:

If input image is 1 channel: grayscale version is 1 channel - If input image is 3 channel: grayscale version is 3 channel with r == g == b

Use transforms.Grayscale(num_output_channels=1), if you want to get a single channel output.

1 Like

Great! Your solution is working well.

I have one more thing before this thread can be locked: ‘I got an answer which is tensor([3]). How can I convert this to a string with the classification of the fourth index?’

Thank you for your help.