Transfer Learning with Pytorch

Zeyad Yasser
3 min readFeb 16, 2019

--

I recently took the Stanford CNN course cs231n, and wanted to apply what I learned on a project and dive into Pytorch’s inner workings.

I like software design and making automation tools so I decided to make hybrid tool between Software Engineering & Deep Learning. I made very easy to use Transfer Learning command line tool (pyclassify) https://github.com/zeyadyasser/pyclassify.

  • Usage
  • Under the hood

Usage

Obviously you should install Pytorch first from https://pytorch.org/, then install pyclassify.

pip install git+https://github.com/zeyadyasser/pyclassify

Next you should prepare your data, make two folder ‘train’ & ‘val’, each of those should contain image folder for each class you have.

root/train/dog/xxx.png
root/train/cat/yyy.png
root/val/dog/xxx.png
root/val/cat/yyy.png

To train run:

pyclassify_train
--checkpoint-dir=path/to/checkpoint # Resume from here
--save-dir=path/to/checkpoint # Directory to save checkpoint
--data-dir=path/to/data # Directory of data
--epochs=30
--batch-size=64
--num-workers=4
--device=cuda
--backend-model=squeeze_net
--model-name=cat_dog_classifier
--lr=0.001
--momentum=0.9
--weight-decay=0.0002

To classify run:

pyclassify_run
path/to/model_checkpoint
path/to/img
--device=cuda

Example

I tried my new tool it to retrain SqueezeNet on Dogs vs. Cats dataset from Kaggle.

It has 25,000 images split equally between cats and dogs. I split the data into two folder ‘train’ & ‘val’, each has two folders ‘dog’ & ‘cat’.

The data folder structure is:

path/to/data/train/dog # 10,000 dog images
path/to/data/train/cat # 10,000 cat images
path/to/data/val/dog # 2,500 dog images
path/to/data/val/cat # 2,500 cat images

Then I ran the training command:

pyclassify_train
--save-dir=path/to/checkpoint # Directory to save checkpoint
--data-dir=path/to/data # Directory of data
--device=cuda
--model-name=cat_dog_classifer

Training:

Epoch: [1][1/625]       Loss (1.2942)   Acc (37.500%)
Epoch: [1][17/625] Loss (0.5352) Acc (72.243%) Epoch: [1][33/625] Loss (0.4060) Acc (80.019%)
.
.
.
Epoch: [1][609/625] Loss (0.2024) Acc (91.154%)
Epoch: [1][625/625] Loss (0.2002) Acc (91.265%)
Running Validation...
Validation: Loss (0.0809) Acc (96.860%)
Checkpoint save at C:\Users\zeyady98\Desktop\model_checkpoint\model.pt

Classification:

I used this image

who’s a good boy
pyclassify_run
path/to/model_checkpoint
path/to/img
--device=cuda
Model: "cat_dog_classifer" is loaded
dog

Under the hood

Backend Models:

pyclassify currently only supports SqueezeNet as a backend but I intend to add other models like Inception and ResNet and even other computer vision tasks like object detection.

I used existing SqueezeNet model in Pytorch from torchvision.models, and disabled gradients on all layers of the model.

I took the output from last Conv Layer in the model and fed it to a new Sequential Layer that maps from the Conv Layer to the classes specified in the data folder.

The forward pass gets the features from the frozen Conv Layers and then passes them to the new layer.

Here the new SqueezeNet Class:

Feeding the data

I used Pytorch’s ImageFolder and DataLoader for efficient loading from disk.

I would be glad if this tool was of any benefit to you.

--

--