import torch import torch.nn as nn import torch.optim as optim from torch.optim import lr_scheduler import numpy as np from torchvision import datasets, models, transforms import time import os import copy device = torch.device("mps" if torch.has_mps else "cpu") mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) data_transforms = { 'train': transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean, std) ]), 'val': transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean, std) ]) } # import data data_dir = 'data/hymenoptera_data' sets = ['train', 'val'] # Here I load the dataset in a folder with a specific structure (https://pytorch.org/vision/stable/generated/torchvision.datasets.ImageFolder.html) image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val']} # Here a generate the data loaders, given the data folders, it loads the images from disk to memory in batches of 4 dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=2, shuffle=True, num_workers=0) for x in ['train', 'val']} dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']} class_names = image_datasets['train'].classes print(class_names) def train_model(model, criterion, optimizer, scheduler, num_epochs=25): since = time.time() best_model_weights = copy.deepcopy(model.state_dict()) best_acc = 0.0 for epoch in range(num_epochs): print(f'Epoch {epoch}/{num_epochs - 1}') print('-' * 10) for phase in ['train', 'val']: if phase == 'train': # sets the model in training mode. It basically changes the attribute training to true, this impacts # layers such as batch norm and dropout. At training time they're used, but at eval they're skipped # https://stackoverflow.com/questions/51433378/what-does-model-train-do-in-pytorch model.train() else: model.eval() running_loss = 0.0 running_corrects = 0 # iterate on data for inputs, labels in dataloaders[phase]: inputs = inputs.to(device) labels = labels.to(device) # forward step # track history only in training mode. In evaluation I dont need to track the gradients in the # computational graph with torch.set_grad_enabled(phase == 'train'): outputs = model(inputs) _, preds = torch.max(outputs, 1) # takes the index of the maximum value loss = criterion(outputs, labels) if phase == 'train': optimizer.zero_grad() loss.backward() optimizer.step() running_loss += loss.item() * inputs.size(0) if phase == 'train': scheduler.step() epoch_loss = running_loss / dataset_sizes[phase] epoch_acc = running_corrects / dataset_sizes[phase] print('{} Loss: {:.4f} Acc: {:.4f}'.format( phase, epoch_loss, epoch_acc)) # deep copy the model if phase == 'val' and epoch_acc > best_acc: best_acc = epoch_acc best_model_wts = copy.deepcopy(model.state_dict()) #### Finetuning the convnet #### # Load a pretrained model and reset final fully connected layer. # Im retraining the whole model (the resnet) and all its layers on the new dataset. # The difference is that Im starting from an already trained network! So I have already the weights in place, # Im changing them a bit for my new problem! model = models.resnet18(pretrained=True) num_ftrs = model.fc.in_features # Here the size of each output sample is set to 2. # Alternatively, it can be generalized to nn.Linear(num_ftrs, len(class_names)). model.fc = nn.Linear(num_ftrs, 2) model = model.to(device) criterion = nn.CrossEntropyLoss() # Observe that all parameters are being optimized optimizer = optim.SGD(model.parameters(), lr=0.001) # StepLR Decays the learning rate of each parameter group by gamma every step_size epochs # Decay LR by a factor of 0.1 every 7 epochs # Learning rate scheduling should be applied after optimizerâ s update # e.g., you should write your code this way: # for epoch in range(100): # train(...) # validate(...) # scheduler.step() # the scheduler updates the learning rate in time! # Every 7 epochs I divide by 10 the learning rate step_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1) model = train_model(model, criterion, optimizer, step_lr_scheduler, num_epochs=25)