Test only one image using a list, result is bad

username: Xiaoyu_Song
cooked: <p>Hi, Can i know that if I don not use the Dataset and Dataloader, I just load the image like a list and do the transforms etc. Will the test result be affected?</p>
<p>The definition of a TestLoader is a must?</p>
<p>Cause in my code, the training and validation accuracy could reade 95% but when i inference only one image, the accuracy is like only 10%.</p>
<p>Any idea?</p>
<p>Here is a snap of my testing code</p>
<pre><code class="lang-auto">for img_name in img_list:
        img_path = os.path.join(test_dir, img_name)
        img_arr = cv2.imread(img_path,1)
        img_tensor = torch.from_numpy(img_arr)
        compose_img = transforms.Compose([transforms.ToPILImage(),
                                         transforms.Resize((112,112),interpolation=Image.NEAREST),
                                         transforms.ToTensor(),
                                         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
        img_tensor = compose_img(img_tensor)
        print('Predicting image: ' + img_name)
        with torch.no_grad():
            img_tensor = img_tensor.unsqueeze(0)

            output = model(img_tensor)
            m = nn.Softmax(dim = 1)
            output_a = m(output)
            preds = torch.argmax(output_a.data, 1)
            print(preds)

</code></pre>

username: tom
cooked: <aside class="quote no-group" data-username="Xiaoyu_Song" data-post="1" data-topic="98012">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xiaoyu_song/48/33419_2.png" class="avatar"> Xiaoyu_Song:</div>
<blockquote>
<p><code>cv2</code></p>
</blockquote>
</aside>
<p>A common pitfall with cv2 is that it is BGR instead of RGB. If you trained using PIL, I would recommend to do <code>img_tensor = torch.from_numpy(img_arr)[:,:, ::-1]</code>.<br>
Another is to put the network into eval mode (<code>model.eval()</code>). You need to do this after loading the model, in particular if you have BatchNorm.</p>

username: Xiaoyu_Song
cooked: <p>Hi <a class="mention" href="/u/tom">@tom</a>, thank you for your reply and suggestion. My training data is also RGB images, and I set the model.eval() already. Should i still use <code>img_tensor = torch.from_numpy(img_arr)[:,:, -1]</code> ?</p>

username: tom
cooked: <p>By BGR I mean that OpenCV returns the image with the order of the three channels reversed compared to what PIL does. So yes, try it and see if it helps.</p>

username: Xiaoyu_Song
cooked: <p>Hi, it shows me the following error, seems the normalize has some issue</p>
<pre><code class="lang-auto">File "test_xy_final.py", line 62, in &lt;module&gt;
    main()
  File "test_xy_final.py", line 43, in main
    img_tensor = compose_img(img_tensor)
  File "/opt/conda/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 61, in __call__
    img = t(img)
  File "/opt/conda/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 166, in __call__
    return F.normalize(tensor, self.mean, self.std, self.inplace)
  File "/opt/conda/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 217, in normalize
    tensor.sub_(mean[:, None, None]).div_(std[:, None, None])
RuntimeError: output with shape [1, 112, 112] doesn't match the broadcast shape [3, 112, 112]

</code></pre>

username: tom
cooked: <p>Ah, sorry, it should have been [:, :, ::-1] but that doesn’t work in PyTorch but you need <code>.flip(2)</code> (after that, the three image channels should be switched), you can use matplotib’s pyplot.imshow to see whether it looks OK.</p>

username: Xiaoyu_Song
cooked: <p>thank you. I’ll try this. Can you explain why during training, I use cv2, the validation result is good. Some mechanism in pytorch? And also, if i want to finetune a public model, the RGB, BGR should be consist with their training data ( their defined dataloader), right?</p>

username: tom
cooked: <p>Oh, if you used cv2 in the training, you might have also trained with BGR (e.g. the original detectron was, I think). I had understood your reply as that you used the usual TorchVision pipeline which uses RGB images. I should have asked better, sorry.<br>
In general, the TorchVision models work with RGB, so most pretrained models do.</p>