Model stopped learning after running several times

Yangyi_Shi: <p>I am training the model to learn to recognize alphabet pictures (Size: 80 by 80). They are stored in 26 sub-folders.<br>
It was able to run and gave me an accuracy of 90% when testing. However, after running the training code below for a few times (each time train on a new set of alphabet images to get a new model), the model suddenly stopped learning. When I try to run it with data that I used for the 1st time (the data that gave me 90%), the accuracy became only 3.84%. It is no longer learning, no matter what data I give it, it stayed 3.84% forever. I really don’t understand why. I checked the code a few times but still cannot find the reason.</p>
<p>This is the model.</p>
<p>import torch.nn as nn</p>
<p>class CNN4CAM(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(CNN4CAM, self).<strong>init</strong>()<br>
self.conv = nn.Sequential(<br>
nn.Conv2d(1, 32, 3, 1, 1),<br>
nn.ReLU(True),<br>
nn.Conv2d(32, 64, 3, 1, 1),<br>
nn.ReLU(True),<br>
nn.MaxPool2d(2, 2),</p>
<pre><code>        nn.Conv2d(64, 128, 3, 1, 1),
        nn.ReLU(True),
        nn.Conv2d(128, 256, 3, 1, 1),
        nn.ReLU(True),
        nn.MaxPool2d(2, 2),

        nn.Conv2d(256, 512, 3, 1, 1),
        nn.ReLU(True),
        nn.Conv2d(512, 1024, 3, 1, 1),
        nn.ReLU(True),
        nn.MaxPool2d(2, 2)

    )
    self.avg_pool = nn.AvgPool2d((10, 10))  # img_size // 8
    self.classifier = nn.Linear(1024, 26)

def forward(self, x):
    features = self.conv(x)
    flatten = self.avg_pool(features).view(features.size(0), -1)
    output = self.classifier(flatten)
    return output, features
</code></pre>
<p>This is the code for training</p>
<p>import cv2<br>
import torch<br>
import numpy as np<br>
from torch.nn import functional as F<br>
import torch.nn as nn<br>
from newCNNmodel import CNN4CAM<br>
import torchvision.transforms as transforms<br>
from torchvision import datasets</p>
<p>transform = transforms.Compose([<br>
transforms.Grayscale(),<br>
transforms.ToTensor()<br>
])<br>
train_dir = ‘E:/alpTrain/blur0trainingSh’<br>
train_set = datasets.ImageFolder(train_dir, transform)<br>
train_loader = torch.utils.data.DataLoader(train_set, batch_size=10, shuffle=True)</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
cnn = CNN4CAM().to(device)<br>
criterion = nn.CrossEntropyLoss().to(device)<br>
optimizer = torch.optim.Adam(cnn.parameters(), lr=0.001)</p>
<p>print(‘Training Starts.’)<br>
EPOCH = 5</p>
<p>cnn.train()<br>
for epoch in range(EPOCH):<br>
epoch_loss = 0<br>
for i, (images, labels) in enumerate(train_loader):<br>
images, labels = images.to(device), labels.to(device)<br>
optimizer.zero_grad()<br>
outputs, _ = cnn(images)<br>
loss = criterion(outputs, labels)<br>
loss.backward()<br>
optimizer.step()<br>
epoch_loss += loss.item()</p>
<pre><code>avg_epoch_loss = epoch_loss / len(train_loader)
print('Epoch [%d/%d], Loss %.4f' % (epoch+1, EPOCH, avg_epoch_loss))</code></pre>

ptrblck: <p>I cannot see any obvious errors in your code.<br>
Did you change anything in the code or your machine setup before the accuracy was reduced?<br>
If not I would guess that your training might be sensitive to the seed and would recommend to play around with some hyperparameters such as the learning rate to stabilize it.</p>

Yangyi_Shi: <p>Thank you for your response. I talked to my friends yesterday about this and we found the solution exactly as you said. I decreased or to 0.0001 and increased epoch from 5 to 20.</p>