0
$\begingroup$

Hi I'm trying to train a cnn model with transfer learning, and I am not able to get a good test accuracy (14%) - I don't know why it doesn't work for me.

import os
import torch
from torchvision import datasets
import torchvision.transforms as transforms

transform_train = transforms.Compose([
                    transforms.Resize(256),
                    transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
                    transforms.RandomHorizontalFlip(),
                    transforms.RandomResizedCrop(224, scale=(0.08,1), ratio=(1,1)), 
                    transforms.ToTensor(),
                    transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])
                    ])
transform_valid = transforms.Compose([
                    transforms.Resize((224, 224)),
                    transforms.ToTensor(),
                    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
                    ])
transform_test = transforms.Compose([
                    transforms.Resize((224, 224)),
                    transforms.ToTensor(),
                    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
                    ])
image_datasets = {
    'train' : datasets.ImageFolder(root='/data/dog_images/train',transform=transform_train),
    'valid' : datasets.ImageFolder(root='/data/dog_images/valid',transform=transform_valid),
    'test' : datasets.ImageFolder(root='/data/dog_images/test',transform=transform_test)
}

loaders_transfer = {
    'train' : torch.utils.data.DataLoader(image_datasets['train'],batch_size = 1,shuffle=True),
    'valid' : torch.utils.data.DataLoader(image_datasets['valid'],batch_size = 1),
    'test' : torch.utils.data.DataLoader(image_datasets['test'],batch_size = 1)    
}

import torchvision.models as models
import torch.nn as nn

model_transfer = models.densenet161(pretrained=True)

for param in model_transfer.parameters():
    param.requires_grad = False

model_transfer.classifier = nn.Linear(model_transfer.classifier.in_features, 133)

# if GPU is available, move the model to GPU
if use_cuda:
    model_transfer.cuda()
print(model_transfer)

criterion_transfer = nn.CrossEntropyLoss()
optimizer_transfer = optim.SGD(model_transfer.classifier.parameters(), lr=0.001, momentum=0.9)

# train the model
model_transfer = train(25, loaders_transfer, model_transfer, optimizer_transfer, criterion_transfer, use_cuda, 'model_transfer.pt')

This is what the training looks like, and I was hoping it would decrease more but it doesn't.

Epoch: 1    Training Loss: 6.038606     Validation Loss: 6.012529
Validation loss decreased (inf --> 6.012529).  Saving model ...
Epoch: 2    Training Loss: 6.047534     Validation Loss: 6.301253
Epoch: 3    Training Loss: 6.030610     Validation Loss: 5.974661
Validation loss decreased (6.012529 --> 5.974661).  Saving model ...
Epoch: 4    Training Loss: 6.015132     Validation Loss: 5.595847
Validation loss decreased (5.974661 --> 5.595847).  Saving model ...
Epoch: 5    Training Loss: 6.039560     Validation Loss: 5.453747
Validation loss decreased (5.595847 --> 5.453747).  Saving model ...
Epoch: 6    Training Loss: 6.013939     Validation Loss: 6.033586
Epoch: 7    Training Loss: 5.994193     Validation Loss: 6.240430
Epoch: 8    Training Loss: 6.002292     Validation Loss: 5.159647
Validation loss decreased (5.453747 --> 5.159647).  Saving model ...
Epoch: 9    Training Loss: 5.990530     Validation Loss: 4.857103
Validation loss decreased (5.159647 --> 4.857103).  Saving model ...
Epoch: 10   Training Loss: 5.974143     Validation Loss: 5.731185
Epoch: 11   Training Loss: 5.973516     Validation Loss: 4.829700
Validation loss decreased (4.857103 --> 4.829700).  Saving model ...
Epoch: 12   Training Loss: 5.987331     Validation Loss: 4.828186
Validation loss decreased (4.829700 --> 4.828186).  Saving model ...
Epoch: 13   Training Loss: 5.984856     Validation Loss: 4.959522
Epoch: 14   Training Loss: 5.960883     Validation Loss: 4.358726
Validation loss decreased (4.828186 --> 4.358726).  Saving model ...
Epoch: 15   Training Loss: 5.979597     Validation Loss: 4.607085
Epoch: 16   Training Loss: 5.941909     Validation Loss: 5.294339
Epoch: 17   Training Loss: 5.955883     Validation Loss: 4.874300
Epoch: 18   Training Loss: 5.953255     Validation Loss: 4.728502
Epoch: 19   Training Loss: 5.934913     Validation Loss: 4.391431
Epoch: 20   Training Loss: 5.930152     Validation Loss: 5.499924
Epoch: 21   Training Loss: 5.938586     Validation Loss: 4.285192
Validation loss decreased (4.358726 --> 4.285192).  Saving model ...
Epoch: 22   Training Loss: 5.910895     Validation Loss: 4.970409
Epoch: 23   Training Loss: 5.920622     Validation Loss: 5.218917
Epoch: 24   Training Loss: 5.908489     Validation Loss: 4.656822
Epoch: 25   Training Loss: 5.906709     Validation Loss: 4.235758
Validation loss decreased (4.285192 --> 4.235758).  Saving model ...

I feel like I'm missing something, but I'm new to pytorch. Any help would be appreciated!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.