top of page

Caltech101| Airplanes,Motorbikes & Schooners - Image Classifier using PyTorch

NIRAJ KUMAR

Problem Statement: To build an Image classifier for Caltech 101 Dataset : Caltech101 | Airplanes, Motorbikes & Schooners. Classify the images into three classes airplanes, Motorbikes and schooners

Image Source: After loading sample data from Dataset


I have referenced the [1] PyTorch tutorial code for building this image classifier and code to split the data and create dataser is used from this [2] link.


Code:

Loading Dataset : Mounting google drive as my data is stored in drive and navigating to that folder


Getting details of dataset size and number of images in each folders

Printing the details of Caltech 101 Dataset

Splitting dataset into training and validation set: The data is split in ratio of 80:20 for training and validation

Code provides user an option to pass parameters like AugmentationList to transform the images in the dataset, batchSize, learningRate, select optimizer either "SGD" or "ADAM", number of epochs, training models(I have created 4 models and passed it as list)

show_Training_info() : Loads sample images from the dataset and displays the associated class

Sample Images and their respective class.

Image Augmentation: AugmentationList passed as a parameter create_aug_dataset() to apply augmentation like resize, horizontal flip on the images.

augmentationList = [[transforms.Resize((240,240)),ToTensor()],
                    [transforms.Resize((300,300)),transforms.GaussianBlur(1,sigma=(0.1,0.5)),transforms.RandomRotation(5),
                     transforms.RandomHorizontalFlip(),transforms.ToTensor()]]

Convolution Neural Network : CNN is deep learning algorithm for processing data like images and identifies various features present in it which helps in the classification of the object.


There are four models used in this notebook having different numbers of Conv2d layers, AdaptiveAvgPool2d and fully connected layers.

Loss function and optimizer: CrossEntropyLoss is used in this code for all the models. For optimizer we can either use "SGD" or "ADAM" as parameter to use the respective optimizer in train_model() function along with learning rate like (0.001,0.005).


Training Model: Train the model on the training dataset. We can pass epoch_val for number of epochs in train_model(). The training time of all four models is calculated and a graph is created for the same.


Accuracy of Model on Validation Set: The trained model is then tested on validation set to find the accuracy of the model and modification is made to the model's hyperparameters as well as the images are augmented to test the accuracy of model on different augmented image data set with various hyperparameters changes applied to all for CNN Models.


A bar graph is created to show the accuracy of models for each class.



A bar chart is created to compare the accuracy of all four models on each dataset plot_graph() is used for it.



Experiments: I have performed various experiment to test the accuracy of each model on different dataset. Training time were not calculated for first 3 experiments

as present in this link

Note: Since the number of schooners images were only 63 when compared to motorbikes and airplanes having approx(800) images the class accuracy of schooners was very less in majority of my experiments.

Exp 1: For the first experiment the images were resized to 240x240,

batch size = 4

No of epoch for model 0: 2

No of epoch for model 1: 5

No of epoch for model 2: 10

No of epoch for model 3: 10


Model Accuracy :

Model 1 : 47%

Model 2 : 85%

Model 3 : 86%

Model 4 : 47%





Class-wise Accuracy of Model 2:

Accuracy of Motorbikes: 96.5%

Accuracy of airplanes: 80.5%

Accuracy of schooner: 50.0%








Exp 2: Image size:100x100, batch size was changed to 10 for this experiment

Model Accuracy :

Model 1 : 47%

Model 2 : 88%

Model 3 : 89%

Model 4 : 89%




Now after getting high accuracy lets start image augmentation for the images, select different optimizer and learning rate along with different epochs and batch sizes as present in GitHub Link

Exp 4:

Input Parameters:

learningRate = 0.001
batch_size = 10
epoch_val = 5
# sample 1: augmentationList = [transforms.Resize((300,300)),transforms.GaussianBlur(1,sigma=(0.1,0.5)),transforms.RandomRotation(5),transforms.RandomHorizontalFlip(),transforms.ToTensor()]
# sample 2: augmentationList = [Resize((240,240)),ToTensor()]
augmentationList = [[transforms.Resize((240,240)),ToTensor()],
                    [transforms.Resize((300,300)),transforms.GaussianBlur(1,sigma=(0.1,0.5)),transforms.RandomRotation(5),
                     transforms.RandomHorizontalFlip(),transforms.ToTensor()]]
train_model(n,"ADAM",learningRate,epoch_val,net,dataloaders))

Model Accuracy :

Parameter1 : Image Augment = [transforms.Resize((240,240)),ToTensor()]

=============Iteration  0 ============= Augmentation Iteration 0 Image Augmentation parameters [Resize(size=(240, 240), interpolation=bilinear, max_size=None, antialias=None), ToTensor()] Total Number of Images 1661 Total Number of Images in Training Dataset  1328 Total Number of Images in Validation Dataset  333 Dataset Information  Dataset ImageFolder     Number of datapoints: 1661     Root location: caltech101_classification     StandardTransform Transform: Compose(                Resize(size=(240, 240), interpolation=bilinear, max_size=None, antialias=None)                ToTensor()            ) torch.Size([6, 3, 240, 240]) torch.Size([6]) ('Motorbikes', 'airplanes', 'schooner') 

airplanes Motorbikes airplanes Motorbikes airplanes Motorbikes [epoch : 1, mini-batches :   100] loss: 1.141 [epoch : 1, mini-batches :   200] loss: 0.869 [epoch : 2, mini-batches :   100] loss: 0.599 [epoch : 2, mini-batches :   200] loss: 0.455 Finished Training Model  0 Training Time for Model 0  : 51.16920018196106 ========================================== Accuracy of the  0  Network on the validation images: 86 % ========================================== =============Plot graph for model  0 ============= Accuracy for class: Motorbikes is 83.0 % Accuracy for class: airplanes is 96.3 % Accuracy for class: schooner is 0.0 % 

========================================== [epoch : 1, mini-batches :   100] loss: 1.061 [epoch : 1, mini-batches :   200] loss: 0.585 [epoch : 2, mini-batches :   100] loss: 0.380 [epoch : 2, mini-batches :   200] loss: 0.378 Finished Training Model  1 Training Time for Model 1  : 45.64894127845764 ========================================== Accuracy of the  1  Network on the validation images: 81 % ========================================== =============Plot graph for model  1 ============= Accuracy for class: Motorbikes is 96.2 % Accuracy for class: airplanes is 71.8 % Accuracy for class: schooner is 0.0 % 

========================================== [epoch : 1, mini-batches :   100] loss: 0.977 [epoch : 1, mini-batches :   200] loss: 0.697 [epoch : 2, mini-batches :   100] loss: 0.849 [epoch : 2, mini-batches :   200] loss: 0.852 Finished Training Model  2 Training Time for Model 2  : 34.2151403427124 ========================================== Accuracy of the  2  Network on the validation images: 47 % ========================================== =============Plot graph for model  2 ============= Accuracy for class: Motorbikes is 100.0 % Accuracy for class: airplanes is 0.0 % Accuracy for class: schooner is 0.0 % 

========================================== [epoch : 1, mini-batches :   100] loss: 1.071 [epoch : 1, mini-batches :   200] loss: 0.874 [epoch : 2, mini-batches :   100] loss: 0.850 [epoch : 2, mini-batches :   200] loss: 0.880 Finished Training Model  3 Training Time for Model 3  : 183.1270306110382 ========================================== Accuracy of the  3  Network on the validation images: 47 % ========================================== =============Plot graph for model  3 ============= Accuracy for class: Motorbikes is 100.0 % Accuracy for class: airplanes is 0.0 % Accuracy for class: schooner is 0.0 % 

========================================== Model Accuracy Graph 

Model Run Time Graph 

=============Iteration  1 ============= Augmentation Iteration 1 Image Augmentation parameters [Resize(size=(300, 300), interpolation=bilinear, max_size=None, antialias=None), GaussianBlur(kernel_size=(1, 1), sigma=(0.1, 0.5)), RandomRotation(degrees=[-5.0, 5.0], interpolation=nearest, expand=False, fill=0), RandomHorizontalFlip(p=0.5), ToTensor()] Total Number of Images 1661 Total Number of Images in Training Dataset  1328 Total Number of Images in Validation Dataset  333 Dataset Information  Dataset ImageFolder     Number of datapoints: 1661     Root location: caltech101_classification     StandardTransform Transform: Compose(                Resize(size=(300, 300), interpolation=bilinear, max_size=None, antialias=None)                GaussianBlur(kernel_size=(1, 1), sigma=(0.1, 0.5))                RandomRotation(degrees=[-5.0, 5.0], interpolation=nearest, expand=False, fill=0)                RandomHorizontalFlip(p=0.5)                ToTensor()            ) torch.Size([6, 3, 300, 300]) torch.Size([6]) ('Motorbikes', 'airplanes', 'schooner') 

Motorbikes airplanes Motorbikes Motorbikes Motorbikes Motorbikes [epoch : 1, mini-batches :   100] loss: 1.122 [epoch : 1, mini-batches :   200] loss: 0.641 [epoch : 2, mini-batches :   100] loss: 0.475 [epoch : 2, mini-batches :   200] loss: 0.508 Finished Training Model  0 Training Time for Model 0  : 41.1795015335083 ========================================== Accuracy of the  0  Network on the validation images: 82 % ========================================== =============Plot graph for model  0 ============= Accuracy for class: Motorbikes is 73.1 % Accuracy for class: airplanes is 98.1 % Accuracy for class: schooner is 0.0 % 

========================================== [epoch : 1, mini-batches :   100] loss: 0.819 [epoch : 1, mini-batches :   200] loss: 0.518 [epoch : 2, mini-batches :   100] loss: 0.440 [epoch : 2, mini-batches :   200] loss: 0.388 Finished Training Model  1 Training Time for Model 1  : 78.87123203277588 ========================================== Accuracy of the  1  Network on the validation images: 91 % ========================================== =============Plot graph for model  1 ============= Accuracy for class: Motorbikes is 93.8 % Accuracy for class: airplanes is 96.3 % Accuracy for class: schooner is 0.0 % 

========================================== [epoch : 1, mini-batches :   100] loss: 1.015 [epoch : 1, mini-batches :   200] loss: 0.702 [epoch : 2, mini-batches :   100] loss: 0.555 [epoch : 2, mini-batches :   200] loss: 0.468 Finished Training Model  2 Training Time for Model 2  : 60.669975996017456 ========================================== Accuracy of the  2  Network on the validation images: 90 % ========================================== =============Plot graph for model  2 ============= Accuracy for class: Motorbikes is 95.0 % Accuracy for class: airplanes is 95.0 % Accuracy for class: schooner is 0.0 % 

========================================== [epoch : 1, mini-batches :   100] loss: 1.047 [epoch : 1, mini-batches :   200] loss: 0.879 [epoch : 2, mini-batches :   100] loss: 0.842 [epoch : 2, mini-batches :   200] loss: 0.844 Finished Training Model  3 Training Time for Model 3  : 208.49237489700317 ========================================== Accuracy of the  3  Network on the validation images: 48 % ========================================== =============Plot graph for model  3 ============= Accuracy for class: Motorbikes is 100.0 % Accuracy for class: airplanes is 0.0 % Accuracy for class: schooner is 0.0 % 

========================================== Model Accuracy Graph 



Parameter 2 :

Transform: Compose( Resize(size=(300, 300), interpolation=bilinear, max_size=None, antialias=None) GaussianBlur(kernel_size=(1, 1), sigma=(0.1, 0.5)) RandomRotation(degrees=[-5.0, 5.0], interpolation=nearest, expand=False, fill=0) RandomHorizontalFlip(p=0.5) ToTensor() ) torch.Size([6, 3, 300, 300]) torch.Size([6])


Exp 5:

Input Parameters:

batch_size = 10
epoch_val = 5
augmentationList = [[transforms.Resize((240,240)),ToTensor()],[transforms.Resize((300,300)),transforms.GaussianBlur(1,sigma=(0.1,0.5)),transforms.RandomRotation(5),transforms.RandomHorizontalFlip(),transforms.ToTensor()]]
train_model(n,"ADAM",0.001,epoch_val,net,dataloaders)

Model Accuracy and Runtime for Parameter 1 in Augmentation List:

Model Accuracy and Runtime for Parameter 2 in Augmentation List:

Exp 6:

Input Parameters:

batch_size = 10
epoch_val = 10
augmentationList = [[transforms.Resize((120,120)),ToTensor()],
                    [transforms.Resize((120,120)),transforms.GaussianBlur(1,sigma=(0.1,0.5)),transforms.RandomRotation(30),                    transforms.RandomHorizontalFlip(),transforms.RandomInvert(),transforms.RandomAffine(10),transforms.RandomCrop((10,10)),transforms.ToTensor()]]
train_model(n,"SGD",0.005,epoch_val,net,dataloaders)

Model Accuracy and Runtime for Parameter 1 in Augmentation List:

Model Accuracy and Runtime for Parameter 2 in Augmentation List:

Result:

  • Increasing the number of layers in the CNN model doesn't increase the accuracy which can be seen in model 3 for majority of experiment due to overfitting.

  • Model 1 and 2 performs better when adam optimizer is used in comparison to sgd

  • When learning rate changed from 0.001 to 0.005 the model accuracy decreased.


NoteBook:


My Contribution:

  • Created custom function to run the models on different parameters like optimizer(adam,sgd), learning rate(0.001,0.005), augmentationlist (transforms.Resize((120,120)),transforms.GaussianBlur(1,sigma=(0.1,0.5)),transforms.RandomRotation(30), transforms.RandomHorizontalFlip(),transforms.RandomInvert(),transforms.RandomAffine(10),transforms.RandomCrop((10,10))).

  • Created models different layers to check the accuracy of each model.

  • Augmentation of image dataset and testing models accuracy against each dataset.

  • Created bar chart to visualize the different models accuracy and training runtime.

References:

32 views

Recent Posts

See All

Comments


bottom of page