Testing 1 Image VS 1 Dataset

username: Josh_Lazor
cooked: <p>Hello all,</p>
<p>I have a working Pytorch ConvNet program. It tests Datasets and runs rather slow, but effectively.</p>
<p>Would anyone know how to test images separately? I want to test 1 image instead of n number of images.</p>
<p>Thank you</p>

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

username: Josh_Lazor
cooked: <p>Thank you for the tips, ptrblck. I’m going to test it out right now.</p>
<p>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?)</p>

username: harsha_g
cooked: <p><em>unsqueeze</em> adds a dimension at the specified position (here 0 in this example).</p>

username: Josh_Lazor
cooked: <p>Appreciate the help, harsha_g <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":smile:">.</p>
<p>I’m getting the error: <strong>Attribute Error: ‘Compose’ object has no attribute ‘unsqueeze’.</strong></p>
<p>Here’s my code for testing my image on the model, any help would be great!</p>
<p>‘’’</p>
<h1>Load and Process a Single Image</h1>
<p>new_image = Image.open(‘new_image.jpg’)</p>
<p>transformed_image = transforms.Compose([</p>
<pre><code>transforms.ToPILImage(),

transforms.Resize((28, 28)),

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

transforms.ToTensor(),
</code></pre>
<p>])</p>
<h1>Add a Batch Dimension</h1>
<p>transformed_image = transformed_image.unsqueeze(0)</p>
<p>check_image(transformed_image, model)</p>

username: ptrblck
cooked: <p><code>transformed_image</code> is the transformation, not the actual transformed image.<br>
You would have to define the transformation first and pass the input image to it.<br>
Use this code instead:</p>
<pre><code class="lang-python">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)
</code></pre>

username: Josh_Lazor
cooked: <p>This is the error that I am getting as a result of the code:<br>
‘’’</p>
<h1>Traceback (most recent call last):</h1>
<p>File “/Users/Josh/Desktop/convolutional_neural_networks/pytorch_convolutional_neural_network_mnist.py”, line 233, in <br>
transformed_image = transform(new_image)<br>
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torchvision/transforms/transforms.py”, line 61, in <strong>call</strong><br>
img = t(img)<br>
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torchvision/transforms/transforms.py”, line 127, in <strong>call</strong><br>
return F.to_pil_image(pic, self.mode)<br>
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torchvision/transforms/functional.py”, line 100, in to_pil_image<br>
raise TypeError(‘pic should be Tensor or ndarray. Got {}.’.format(type(pic)))<br>
TypeError: pic should be Tensor or ndarray. Got &lt;class ‘PIL.JpegImagePlugin.JpegImageFile’&gt;.<br>
‘’’</p>
<p>Am I doing something wrong?</p>

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

username: Josh_Lazor
cooked: <p>I see now.</p>
<p>Thanks again for your help, ptrblck!</p>

username: Josh_Lazor
cooked: <p>This is so frustrating…</p>
<p>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.</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/Users/Josh/Desktop/convolutional_neural_networks/pytorch_convolutional_neural_network_mnist.py", line 243, in &lt;module&gt;
    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
</code></pre>
<p>Any help appreciated!</p>

username: ptrblck
cooked: <p><code>RandomGrayscale</code> will return a 3 channel image as described in the docs:</p>
<blockquote>
<p>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</p>
</blockquote>
<p>Use <code>transforms.Grayscale(num_output_channels=1)</code>, if you want to get a single channel output.</p>

username: Josh_Lazor
cooked: <p>Great! Your solution is working well.</p>
<p>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?’</p>
<p>Thank you for your help.</p>