Replies: 1 comment 2 replies
-
Seems like your model is underfitting and kinda stuck in a local minimum and is bouncing back and forth. I will recommend try using Adam optimizer instead of SGD and leave the weight decay parameters default unless you wanna regularize. Also, try different learning rates and see what works. Also, I wouldn't recommend using transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) if your dataset is quite small . |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
###############code below##################
device = "cuda" if torch.cuda.is_available() else "cpu"
import torch
from torch import nn
from torchvision import transforms
from torch.utils.data import DataLoader
from torchvision import datasets
import os
class TinyVGG_change(nn.Module):
def init(self,
input_shape: int,
hidden_units: int,
output_shape: int) -> None:
super().init()
self.Conv_black_1=nn.Sequential(
nn.Conv2d(
in_channels=input_shape,
out_channels=hidden_units,
kernel_size=3, #3x3
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_black_2=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)
)
)
def forward(self, x):
x=self.Conv_black_1(x)
x=self.Conv_black_2(x)
x=self.classifier(x)
train_transform_trivial = transforms.Compose([
transforms.Resize((64, 64)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomRotation(5), # Reduce rotation from 10 to 5 degrees
transforms.RandomAffine(degrees=0, translate=(0.05, 0.05)), # Less translation
transforms.ColorJitter(brightness=0.05, contrast=0.05, saturation=0.05), # Less jitter
transforms.RandomResizedCrop(128, scale=(0.9, 1.0)), # Reduce cropping
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
test_transform_simple = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor()]
)
train_data_augmented = datasets.ImageFolder(root=train_dir,
transform=train_transform_trivial,
target_transform=None)
test_data_simple = datasets.ImageFolder(root=test_dir,
transform=test_transform_simple,
target_transform=None)
BATCH_SIZE=32
NUM_WORKERS=os.cpu_count()
torch.manual_seed(42)
torch.cuda.manual_seed(42)
train_dataloader_augmented = DataLoader(dataset=train_data_augmented,
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)
train_dataloader_augmented, test_dataloader_simple
NUM_EPOCHS =40
model_1=TinyVGG_change(
input_shape=3,
hidden_units=32,
output_shape=len(train_data_augmented.classes)
).to(device)
loss=nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model_1.parameters(), lr=0.001, momentum=0.9, weight_decay=0.0005)
from timeit import default_timer as timer
start_time=timer()
model_1_results=train(model=model_1,

train_dataloader=train_dataloader_augmented,
test_dataloader=test_dataloader_simple,
optimizer=optimizer,
loss_fn=loss,
epochs=NUM_EPOCHS,
device=device)
end_time=timer()
print(f"Total training time: {end_time-start_time:.3f} seconds")
`
Beta Was this translation helpful? Give feedback.
All reactions