Improvement simple CNN

username: zahorodnii
cooked: <p>Hello<br>
I am new to the study of neural networks.</p>
<p>I am trying to improve the CNN based on <a href="https://www.kaggle.com/miljan/stanford-dogs-dataset-traintest" rel="nofollow noopener">stanford dogs dataset</a> kaggle dataset, but biggest Accuracy is 12%.<br>
I don’t know what to do, please tell me how improve my CNN, or what to change.<br>
Thanks</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torchvision import datasets, transforms
import os

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 68, 3)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(68, 128, 3)
        self.conv3 = nn.Conv2d(128, 128, 3)
        self.fc1 = nn.Linear(128 * 26 * 26, 5000)
        self.fc2 = nn.Linear(5000, 4000)
        self.fc3 = nn.Linear(4000, 120)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        #print(x.shape)
        x = x.view(-1, 128 * 26 * 26)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


batch_size=50
epochs=5
lr=0.01
log_interval = 40
classes=os.listdir("/kaggle/input/stanford-dogs-dataset-traintest/cropped/test")
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")



torch.manual_seed(1)

transform = transforms.Compose([
    transforms.ToTensor(), 
    #transforms.Normalize((0.1307,), (0.3081,)),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
])

traindataset = torchvision.datasets.ImageFolder(root='/kaggle/input/stanford-dogs-dataset-traintest/cropped/train', transform=transform)
testdataset = torchvision.datasets.ImageFolder(root='/kaggle/input/stanford-dogs-dataset-traintest/cropped/test', transform=transform)

train_loader = torch.utils.data.DataLoader(traindataset, batch_size=batch_size, shuffle=True, num_workers=1)
test_loader = torch.utils.data.DataLoader(testdataset, batch_size=batch_size, shuffle=False, num_workers=1)


import torchvision.models as models

#model = models.densenet121().to(device)

model = Net().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.9)
scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=0.01, max_lr=0.1)

#x = next(iter(test_loader))
#model.forward(x[0])

def train(model, train_loader, optimizer, epoch, log_interval):
    model.train()
    avg_loss = 0
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target .to(device)
        optimizer.zero_grad() 
        output = model(data)
        loss = criterion(output, target)
        loss.backward()
        optimizer.step()
        avg_loss+=loss.item()    
        scheduler.step()
        
        if batch_idx % log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))
    avg_loss/=len(train_loader.dataset)
    return avg_loss

def test(model, test_loader, epoch):
    model.eval()
    test_loss = 0
    correct = 0
    class_correct = list(0. for i in range(len(classes)))
    class_total = list(0. for i in range(len(classes)))
    with torch.no_grad():
        for data in test_loader:
            images, labels = data      
            images, labels = images.to(device), labels.to(device)
            output = model(images)
            test_loss += criterion(output, labels).item() # sum up batch loss            
            _, predicted = torch.max(output, 1)
            correct += (predicted == labels).sum().item()            
            c = (predicted == labels).squeeze()
            for i in range(batch_size):
                if len(labels) &gt; i:
                    #print(i)
                    #print(labels)
                    label = labels[i]
                    class_correct[label] += c[i].item()
                    class_total[label] += 1
            

    test_loss /= len(test_loader.dataset)
    
    for i in range(len(classes)):
        if epoch &gt;= 30:
            print('Accuracy of %5s : %2d %%' % (classes[i], 100 * class_correct[i] / class_total[i]))

    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))
    accuracy = 100. * correct / len(test_loader.dataset)
    
    return test_loss,accuracy


train_losses = []
test_losses = []
accuracy_list = []
for epoch in range(1, epochs + 1):
    trn_loss = train(model, train_loader, optimizer, epoch, log_interval)
    test_loss, accuracy = test(model, test_loader, epoch)
    train_losses.append(trn_loss)
    test_losses.append(test_loss)
    accuracy_list.append(accuracy)

</code></pre>
<pre><code class="lang-auto">Train Epoch: 1 [0/12000 (0%)]	Loss: 4.787573
Train Epoch: 1 [2000/12000 (17%)]	Loss: 4.757991
Train Epoch: 1 [4000/12000 (33%)]	Loss: 4.487531
Train Epoch: 1 [6000/12000 (50%)]	Loss: 4.559648
Train Epoch: 1 [8000/12000 (67%)]	Loss: 4.353319
Train Epoch: 1 [10000/12000 (83%)]	Loss: 4.515636

Test set: Average loss: 0.0860, Accuracy: 345/8580 (4%)

Train Epoch: 2 [0/12000 (0%)]	Loss: 4.300442
Train Epoch: 2 [2000/12000 (17%)]	Loss: 4.482271
Train Epoch: 2 [4000/12000 (33%)]	Loss: 4.314884
Train Epoch: 2 [6000/12000 (50%)]	Loss: 4.437295
Train Epoch: 2 [8000/12000 (67%)]	Loss: 4.060574
Train Epoch: 2 [10000/12000 (83%)]	Loss: 4.061174

Test set: Average loss: 0.0834, Accuracy: 545/8580 (6%)

Train Epoch: 3 [0/12000 (0%)]	Loss: 4.054270
Train Epoch: 3 [2000/12000 (17%)]	Loss: 4.039870
Train Epoch: 3 [4000/12000 (33%)]	Loss: 4.119386
Train Epoch: 3 [6000/12000 (50%)]	Loss: 3.935216
Train Epoch: 3 [8000/12000 (67%)]	Loss: 4.114314
Train Epoch: 3 [10000/12000 (83%)]	Loss: 4.203525

Test set: Average loss: 0.0804, Accuracy: 665/8580 (8%)

Train Epoch: 4 [0/12000 (0%)]	Loss: 3.573288
Train Epoch: 4 [2000/12000 (17%)]	Loss: 3.669470
Train Epoch: 4 [4000/12000 (33%)]	Loss: 3.616935
Train Epoch: 4 [6000/12000 (50%)]	Loss: 3.620514
Train Epoch: 4 [8000/12000 (67%)]	Loss: 3.837484
Train Epoch: 4 [10000/12000 (83%)]	Loss: 3.677999

Test set: Average loss: 0.0792, Accuracy: 787/8580 (9%)

Train Epoch: 5 [0/12000 (0%)]	Loss: 3.164417
Train Epoch: 5 [2000/12000 (17%)]	Loss: 2.834144
Train Epoch: 5 [4000/12000 (33%)]	Loss: 3.117025
Train Epoch: 5 [6000/12000 (50%)]	Loss: 3.289179
Train Epoch: 5 [8000/12000 (67%)]	Loss: 3.233259
Train Epoch: 5 [10000/12000 (83%)]	Loss: 3.406269

Test set: Average loss: 0.0789, Accuracy: 961/8580 (11%)
</code></pre>