This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Defining a trainer function which would train and validate the model | |
def trainer(dataloader_dict, model, loss_fn, optimizer, epochs = 1, log_interval = 1): | |
print('Training started...') | |
train_losses = [] | |
val_losses = [] | |
batch_train_losses = [] | |
batch_val_losses = [] | |
for epoch in range(epochs): | |
print('epoch >>> {}/{}'.format(epoch + 1, epochs)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch.optim as optim | |
from torch.optim import lr_scheduler | |
# Now we define the Loss Function | |
loss_fn = nn.CrossEntropyLoss() | |
# Define the optimizer | |
lr = 0.00001 | |
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr = lr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for name, param in model.named_parameters(): | |
print('name: {} has requires_grad: {}'.format(name, param.requires_grad)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We would freeze all but the last few layers (layer4 and fc) | |
for name, param in model.named_parameters(): | |
if ('layer4' in name) or ('fc' in name): | |
param.requires_grad = True | |
else: | |
param.requires_grad = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for name, module in model.named_children(): | |
print('name: ', name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Now let's have a look at the requires_grad attributes for all the parameters | |
for name, param in model.named_parameters(): | |
print('name: {} has requires_grad: {}'.format(name, param.requires_grad)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
from torchvision import models | |
# Defining the model | |
model = models.resnet34(pretrained = True) | |
# The original architecture of resnet34 has 1000 neurons(corresponding to 1000 classes on which it was originally trained on) in the final layer. | |
# So we need to change the final layer according to the number of classes that we have in our dataset | |
print('model.fc before: ', model.fc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Defining dataloaders which would return data in batches | |
batch_size = 64 | |
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size = batch_size, shuffle = True) | |
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size = batch_size, shuffle = False) | |
print('number of batches in train_loader with a batch_size of {}: {}'.format(batch_size, len(train_loader))) | |
print('number of batches in val_loader with a batch_size of {}: {}'.format(batch_size, len(val_loader))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's start by loading in the image data | |
import torch | |
from torchvision import datasets, transforms | |
# Defining the transforms that we want to apply to the data. | |
# Resizing the image to (224,224), | |
# Randomly flipping the image horizontally(with the default probability of 0.5), | |
# Converting the image to Tensore (converting the pixel values btween 0 and 1), | |
# Normalizing the 3-channel data using the 'Imagenet' stats | |
data_transforms = transforms.Compose([ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
%matplotlib inline | |
import os, glob, sys | |
def q(text = ''): # Just an exit function | |
print(text) | |
sys.exit() | |
# Input data files are available in the "/kaggle/input/" directory. |