My Pytorch Training Pipeline in Image Classification
Tags: python, machinelearning, deeplearning, pytorch
Published: 2022-06-11 10:21:12

### _On this project, I create a custom dataset of 5 male models and conduct a full Pytorch training pipeline. I use a pretrained model and transfer learning, as well as do hyper-parameter search to help increase the accuracy._

&nbsp;

### ✍️ Analysis and Evaluation:

- _Full Pytorch Training Pipeline on Image Classification [part1](https://blogbybao.wordpress.com/2022/02/13/full-pytorch-training-pipeline-on-image-classification-task/)_

- _Full Pytorch Training Pipeline on Image Classification [part2](https://blogbybao.wordpress.com/2022/02/14/full-pytorch-training-pipeline-on-image-classification-task-part2/)_

📺 My implementations are based off [Aladdin Persson](https://www.youtube.com/playlist?list=PLhhyoLH6IjfxeoooqP9rhU3HJIAVAJ3Vz) and [Python Engineer](https://www.youtube.com/playlist?list=PLqnslRFeH2UrcDBWF5mfPGpqQDSta6VK4).

&nbsp;

### My [repository](https://qbaocaca.github.io/boys_classifier/) contains:
- A training script (using pretrained vgg16 and transfer learning)
- A script for Hyper-parameter Search
- A script for loading the model for either resumed training or inference
- [A trained model] (45% accuracy) 😬
- Some helper functions
- A dataset

&nbsp;

## **📰 Dataset Structure**

    train/val
    |___chau_minh_chi
           |___chau_minh_chi_01.jpg
              |___chau_minh_chi_02.jpg
              ...
    |___keita_machida
           |___keita_machida_01.jpg
              |___keita_machida_02.jpg
              ...
&nbsp;

## **Small batch visualization**

![small](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbabtz0tdx2208t15obi.JPG) 

&nbsp;

## **Large batch visualization**

![large](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/417xq61wkcffycla5mkp.JPG)

&nbsp;

## **Visualize the number of classes**

![class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a02jfe06e5y5viv7nwng.JPG)

&nbsp;

## **😑 Hyper-parameter Search**

![Hyper-parameter search](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9d8qja337w3mlqolxd34.JPG)

&nbsp;

## **💥 Training Epochs**

```
==> Saving new best
Epoch 1/25
Step 34/34, train Loss = 1.84,  train Acc = 0.29
Step 20/20, val loss = 1.58,  val acc = 0.25
Time spent for this epoch -----> 0m 32s

==> Saving new best
Epoch 2/25
Step 34/34, train Loss = 0.91,  train Acc = 0.63
Step 20/20, val loss = 1.53,  val acc = 0.43
Time spent for this epoch -----> 0m 13s

==> Validation accuracy did not improve.
Epoch 3/25
Step 34/34, train Loss = 0.61,  train Acc = 0.82
Step 20/20, val loss = 1.67,  val acc = 0.27
Time spent for this epoch -----> 0m 8s
```
&nbsp;

## **😌 Visualize Loss and Accuracy**

![Loss and accuracy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tl01wsucwjmjhnm4y23q.JPG)

&nbsp;

## **😅 Accuracy of Each Class**

```
Test Acc
Got 13/30 correct samples over 43.33%
Accuracy of timmy_xu: 33.33%
Accuracy of corbyn_besson: 62.50%
Accuracy of keita_machida: 16.67%
Accuracy of wang_kai: 30.00%
Accuracy of chau_minh_chi: 100.00%
```
&nbsp;

## **😞 Classification Report and Confusion Matrix Heatmap**

![cm](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2y9gikfyurhoee94kn5.PNG)

&nbsp;

## **😍 Predict a single image**

![single](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iirlsc1b31vsycanljxv.PNG)

&nbsp;
 
## **😐 Prediction In Batch**

![In batch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6v22s0hfjkonpihgrjer.JPG)