0

Edit Update:

After I made the Batch_Size = 128 and I added new Layer

 self.conv_block3 = nn.Sequential(nn.Conv2d(in_channels=hidden_units,out_channels=hidden_units,kernel_size=3,stride=1,padding=1), nn.ReLU(), nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3,stride=1,padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2) ) 

I face weird something after 2 epoch the train and test loss look like this:

Epoch: 1 | train_loss: 1.0392 | train_acc: 0.5169 | test_loss: 0.4287 | test_acc: 0.8576 Epoch: 2 | train_loss: 0.2700 | train_acc: 0.9096 | test_loss: 0.2665 | test_acc: 0.9110 

I'm new to PyTorch and trying to build multiclass image classification with 4 classes, what is wrong with my model, or how to improve it, please?

1- All classes have 75 train images and 25 test images. 2- I don't use any augmentation

That's my input shape and full code:

Create simple transform

simple_transform = transforms.Compose([ transforms.Grayscale(num_output_channels=1), transforms.Resize(size=(128, 128)), transforms.ToTensor(), ]) train_data_simple = datasets.ImageFolder(root=train_dir, transform=simple_transform) test_data_simple = datasets.ImageFolder(root=test_dir, transform=simple_transform) 

Turn dataset into DataLoader

BATCH_SIZE = 32 NUM_WORKERS = 2 train_dataloader_simple = DataLoader(dataset=train_data_simple, batch_size=BATCH_SIZE, num_workers=NUM_WORKERS, shuffle=True) test_dataloader_simple = DataLoader(dataset=test_data_simple, batch_size=BATCH_SIZE, num_workers=NUM_WORKERS, shuffle=False) 

Creating class model

class TingVGG(nn.Module): def __init__(self, input_shape: int, hidden_units: int, output_shape: int) -> None: super().__init__() self.conv_block1 = nn.Sequential(nn.Conv2d(in_channels=input_shape,out_channels=hidden_units,kernel_size=3,stride=1,padding=1), nn.ReLU(), nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3,stride=1,padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2) ) self.conv_block2 = nn.Sequential(nn.Conv2d(in_channels=hidden_units,out_channels=hidden_units,kernel_size=3,stride=1,padding=1), nn.ReLU(), nn.Conv2d(in_channels=hidden_units, out_channels=hidden_units, kernel_size=3,stride=1,padding=1), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2) ) self.classifier = nn.Sequential(nn.Flatten(), nn.Linear(in_features=hidden_units*32*32 ,out_features=output_shape)) def forward(self, x: torch.Tensor): x = self.conv_block1(x) #print(x.shape) x = self.conv_block2(x) #print(x.shape) x = self.classifier(x) #print(x.shape) return x 

Create and initialize of TinyVGG

model_0 = TingVGG(input_shape=1, # Number of channels in the input image (c, h, w) -> 3 hidden_units=20, output_shape=len(train_data.classes)).to(device) 

Setup the loss function and optimizer

loss_fn = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(params= model_0.parameters(), lr= 0.001) 

I will attach the epochs and loss and accuracy as screenshots ThanksLoss and accuracy epoches

1
  • Your model gives 90% over 25 test images. What thing do you think to go wrong? Commented Apr 2, 2023 at 10:10

2 Answers 2

1

Here are some general approaches to improving a model performance:

  1. Experiment with hyperparameters, for example: try different learning rates, different batch sizes, a different optimizer
  2. Try Data Augmentation, especially if you have a small dataset, also to avoid overfitting
  3. Try a simpler model if your dataset is small.

In your case, based on the code you have provided, here are few things you can try:

  1. Definitely add data augmentation to increase your dataset, try using Roboflow to add some augmentation like Grayscaling, flips, noise, etc. along with some preprocessing.

  2. Change your Batch size, in your code: BATCH_SIZE = 32, it's 32, bigger batch size is for faster computation, and for accuracy reduce it to like 2 or 4.

  3. Change resize to 640 in the following line, so that it learns on the size closer to the original image, no need to resize that much unless it's a lot of data: transforms.Resize(size=(128, 128))

  4. Experiment with the learning rate as well.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your comment, I can't add Data Augmentation because the model is about whether the images need to be rotated or not and if there is a way to cancel rotation from it... If I added more images it will be okay? like 2000 images for every class?
I added 3rd layer and made the batch_size=128 but the epochs I guess weird Epoch: 1 | train_loss: 1.0392 | train_acc: 0.5169 | test_loss: 0.4287 | test_acc: 0.8576 Epoch: 2 | train_loss: 0.2700 | train_acc: 0.9096 | test_loss: 0.2665 | test_acc: 0.9110
I would really recommend you to reduce your batch size and increase number of epochs, try batch_size=4 or 8, epoch =10-100, loss reduces and accuracy increases with more epochs
1

Your test loss increases and there is a gap between train acc and test acc. This indicates that your model overfits to the training data, you may make your model smaller/add regularization/add dropout to see the performance. I recommend you to read this paper for more: https://arxiv.org/pdf/1803.09820.pdf.

2 Comments

thanks for your comment, I can't add Data Augmentation because the model is about whether the images need to be rotated or not and if there is a way to cancel rotation from it... If I added more images it will be okay? like 2000 images for every class?
if the task is about rotation, i guess it's fine to add data augmentation. for example, you could just rotate the images you already have in your dataset. there is a chance that your valid acc increases.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.