Awfull result on testing with same data

H_MP: <p>High, I’ve trained a multi-classification model with MNIST-like image and I’ve reach 90% accuaracy on validation after 200 epoch. but when It comes to inference, my model just predict the images randomly. The images is exactly  same as images in validation set. here you can find my network architecture<a href="https://discuss.pytorch.org/t/linear-activation-function/158275">https://discuss.pytorch.org/t/linear-activation-function/158275</a><br>
This is my training error<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/a/3a2cec9f01e2982fa55433cffc7af309a0fb1403.png" data-download-href="https://discuss.pytorch.org/uploads/default/3a2cec9f01e2982fa55433cffc7af309a0fb1403" title="training-error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/a/3a2cec9f01e2982fa55433cffc7af309a0fb1403_2_666x500.png" alt="training-error" data-base62-sha1="8iDWtuRw9NOAol6B0DV3EfqH44j" width="666" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/a/3a2cec9f01e2982fa55433cffc7af309a0fb1403_2_666x500.png, https://discuss.pytorch.org/uploads/default/original/3X/3/a/3a2cec9f01e2982fa55433cffc7af309a0fb1403.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/a/3a2cec9f01e2982fa55433cffc7af309a0fb1403.png 2x" data-dominant-color="F4F7F3"><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">training-error</span><span class="informations">800×600 47.5 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>
<p>here is my code:<br>
net: <code>Block</code></p>
<pre><code class="lang-auto">class Block(nn.Module):
    def __init__(self, in_channels, out_channels, exp=1, stride=1, type=''):
        super(Block, self).__init__()
        self.t = type
        self.stride = stride
        self.inc, self.outc = in_channels, out_channels
        self.exp = exp
        self.blockc = nn.Sequential(
            nn.Conv2d(self.inc, self.inc* self.exp, kernel_size=1),
            nn.BatchNorm2d(self.inc * self.exp),
            nn.ReLU6(inplace=True), 
            nn.Conv2d(self.inc * self.exp, self.inc * self.exp, kernel_size=3, groups= self.inc * self.exp, stride= self.stride, padding=1),
            nn.BatchNorm2d(self.inc * self.exp),
            nn.ReLU6(inplace=True),
            nn.Conv2d(self.inc * self.exp, self.outc, kernel_size=1),
            nn.BatchNorm2d(self.outc))
    def forward(self, x):
        out = self.blockc(x)
        if self.t == 'A':
            out = torch.add(out,x)
        return out 
</code></pre>
<p><code>Overall Network</code></p>
<pre><code class="lang-auto">class Model(nn.Module):
    def __init__(self):
        
        super(Model, self).__init__()
        self.conv2d1 = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=8, kernel_size=3,padding=1, stride=2),
        nn.BatchNorm2d(8),
        nn.ReLU6(inplace=True))
        self.stage1 = nn.Sequential(
            Block(8, 8, exp=1, stride=2, type='C'))
        self.stage2 = nn.Sequential(
            Block(8, 16, exp=2, stride=2, type='C'),
            Block(16, 16, exp=2, type='A'))
        self.stage3 = nn.Sequential(
            Block(16, 24, exp=2, stride=2, type='C'),
            Block(24, 24, exp=2, type='A'))
        self.post_block2 = nn.Sequential(
            Block(24, 32, exp=2, type='B'))
        self.gap = nn.AdaptiveAvgPool2d((1,1))
        self.drop = nn.Dropout()
        self.head =nn.Sequential(
        nn.Linear(32, 10)) 

    def forward(self, x):
        out = self.conv2d1(x)
        out = self.stage1(out)
        out = self.stage2(out)
        out = self.stage3(out)
        out = self.post_block2(out)
        out = self.gap(out)
        out = out.view(-1, 32)
        out = self.drop(out)
        out = self.head(out)
        return out
</code></pre>
<p>Is there any wrong with the code?<br>
I’m working on it for a week, any idea would so helpful. thanks</p>

ptrblck: <p>Are you calling <code>model.eval()</code> for the validation and inference run?<br>
Based on your your first plot it seems the validation error might be quite high and noisy but so do you get the 90% accuracy for the the last few epochs or are you just cherry-picking the best val. loss?</p>

H_MP: <p>Thank you for the response<br>
yes, Actually I solved my question after serveral days, my problem wa due to preprocessing stage<br>
I needed to convert my image to grayscale and 32x32<br>
mistake:</p>
<pre><code class="lang-auto">img = cv2.imread() 
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (32,32)
</code></pre>
<p>But the correct answer was to use transforms:</p>
<pre><code class="lang-auto">img = cv2.imread(path, 0) # convert to grayscale while reading image
img = CustomTransform(img)
</code></pre>