Errror while predicting classes in fashion mnist

Daneshwari_Kankanwad: <p>I’m trying to predict cloth categories from fashion-mnist images. I referred code from <a href="https://boscoj2008.github.io/customCNN/" rel="noopener nofollow ugc">here</a>. I have created the whole model with architecture as in pic attached below and to get image pixels for prediction once the model is trained, I used the below code. The ‘images’ loaded is of size (10000,784) so I reshaped and created a new numpy.ndarray with shape (10000,28,28,1). I took 1st image from this(that is of type ndarray) and when I was trying to convert it to tensor(using transforms.ToTensor()) and then use it for prediction, I was facing dimension issues. So I referred this <a href="https://discuss.pytorch.org/t/pytorch-1-0-how-to-predict-single-images-mnist-example/32394">discussion</a>. But I’m facing a new error. I’m stuck at the last step and am unable to use the model to predict cloth category for a single image. Please suggest a solution, since I have tried every possible solution. Also please point where I might be wrong.</p>
<p><span class="hashtag-raw">#for</span> fashion-mnist images, got pixels as (10000,784) from here<br>
from mnist import MNIST<br>
mndata = MNIST(‘./FashionMNIST/raw/’)<br>
images, labels = mndata.load_testing()</p>
<p><span class="hashtag-raw">#tried</span> this code from the discussion but its throwing another error<br>
single_loaded_img = test_pred_data_loader.dataset.data[0]<br>
single_loaded_img = single_loaded_img[None, None]<br>
pred = cnn_model(single_loaded_img)</p>
<h1><a name="architecture-diagram-1" class="anchor" href="#architecture-diagram-1"></a>architecture diagram-</h1>
<p>Network(<br>
(conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))<br>
(batchN1): BatchNorm2d(6, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(conv2): Conv2d(6, 12, kernel_size=(5, 5), stride=(1, 1))<br>
(fc1): Linear(in_features=192, out_features=120, bias=True)<br>
(batchN2): BatchNorm1d(120, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(fc2): Linear(in_features=120, out_features=60, bias=True)<br>
(out): Linear(in_features=60, out_features=10, bias=True)<br>
)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/2/e2beb47cbb4cba189ad67bb177ae95ec68b0cd6c.png" data-download-href="https://discuss.pytorch.org/uploads/default/e2beb47cbb4cba189ad67bb177ae95ec68b0cd6c" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/2/e2beb47cbb4cba189ad67bb177ae95ec68b0cd6c_2_690x309.png" alt="image" data-base62-sha1="wlSuv5SvpIMDCfP74o2DArsVhJG" width="690" height="309" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/2/e2beb47cbb4cba189ad67bb177ae95ec68b0cd6c_2_690x309.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/2/e2beb47cbb4cba189ad67bb177ae95ec68b0cd6c_2_1035x463.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/2/e2beb47cbb4cba189ad67bb177ae95ec68b0cd6c_2_1380x618.png 2x" data-dominant-color="3F3F40"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1639×736 140 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>As the error message points out, the <code>numpy</code> array is invalid as the input to a conv layer, so you would need to transform them to PyTorch tensors.<br>
Add <code>transform=torchvision.transforms.ToTensor()</code> to the dataset and it should work.</p>