Do and don’t when using transformation to improve CNN deep learning model
By: Chien Vu
Tags: Pytorch, Machine Learning, Data Science, Computer Vision

Evaluate Transformation techniques on CNN model 📄

Let’s discuss the effect of Transformation technique on CNN model. The dataset used in this experiment is Dog Breed Identification from Kaggle competition which provided a strictly canine subset of ImageNet in order to practice fine-grained image categorization. This dataset includes 120 breeds of dogs with total 10,222 images. We split data to train/valid at 0.8/0.2 ratio.

In this article, pre-trained ResNet [2] (Residual Networks) model will be used as backbone for Classification CNN model. ResNet is one of the SOTA pre-trained models that reveals good results on a wide range of Computer Vision tasks. Instead of learning input features of image directly with a function H(x) (A stacking of multi-layer perceptron networks), the main idea of ResNet is to provide a residual function that can reframe H(x) = F(x)+x, where F(x) and x represent the stacked non-linear layers and the identity function (input=output) respectively. This idea can solve the degradation problem of deep neuron networks (vanishing gradient) because it is easier to optimize the residual mapping function F(x) than the original one.

First, we will load the original data.

# Create a custom Dataset class to read in data from dataframe

class BreedDogDataset(Dataset):

def __init__(self, dataframe, root_dir, transform=None):

self.dataframe = dataframe

self.root_dir = root_dir

self.transform = transform



def __len__(self):

return (len(self.dataframe))



def __getitem__(self, idx):

img_name = os.path.join(self.root_dir,

self.dataframe.iloc[idx, 0])

image = Image.open(img_name)

target = self.dataframe.iloc[idx, 1]

target_processed = breeds_processed_dict[target]

if self.transform:

image = self.transform(image)

sample = (image, target_processed)

return sample transform = transforms.Compose([transforms.Resize((255, 255)),

transforms.ToTensor()]) train_ds = BreedDogDataset(df_train,TRAIN_DIR, transform=transform)

val_ds = BreedDogDataset(df_val,TRAIN_DIR, transform=transform)

Then we create a model like this structure:

# load pre-trained model

conv_base = models.resnet50(pretrained=True) # create a new model class

class Model(nn.Module):

def __init__(self, base_model, output):

super(Model, self).__init__()

self.base_model = base_model

self.output = output

self.fc1 = nn.Linear(n_outs, 512)

self.fc2 = nn.Linear(512, output)

self.dropout = nn.Dropout(0.5)



def forward(self, image):

x = self.base_model(image)

x = self.dropout(x)

x = F.relu(self.fc1(x))

x = self.dropout(x)

outs = self.fc2(x)

return outs

One of the most important hyperparameters to tune in a deep learning model is the learning rate. Picking the right learning rate is very important, if it’s too high, the model will face with diverge problem but if it’s too low, the model will take a lot of time to converge. The idea is that we can find a good learning rate by training the model for a few hundred iterations and then based on the learning curve, we pick up a good learning rate. There are a lot of Learning Rate Scheduling techniques, for example, Power scheduling, Exponential scheduling, 1Cycle Scheduling, constant scheduling,… In this experiment, we will apply 1Cycle Scheduling. In Leslie Smith’s paper, this technique was proved to push up remarkably the training time. You can check out this blog for more explanation.

Learning curve of the model

According to the above learning curve result, it shows that the lower bound is 0.9E-04, I rounded values to 1.0E-03. Now, I can fine-tune the pre-trained ResNet50 on the new image dataset by defining a range of learning rates using the rule of thumb: starting at a rate that is one order lower than the learning rate where loss is minimum and ending at the “secure” rate 1E-03.

After training with 5 epochs, we got the accuracy in valid set is 83.284 %

Train loss and valid loss are shown below. It’s seen that both values decreased. From the 4th to 5th epoch, both loss and accuracy were almost constant. It means the model has learned relatively everything from data, all the good information that helps the model to distinguish breed dog was captured.