Skip to content

vikasreddy11/TransferLearning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Transfer Learning - Image Classification

Image classification using CNN and Transfer Learning with PyTorch on cat vs dog

Files

File Description
convolution.py CNN from scratch
feature_extraction.py Feature Extraction
fine_tunning.py Fine Tuning

Model Architecture

CNN from Scratch

Input (3×224×224)
    ↓
Conv2d(3→32) + ReLU + MaxPool
    ↓
Conv2d(32→64) + ReLU + MaxPool
    ↓
Conv2d(64→128) + ReLU + MaxPool
    ↓
Linear(128×28×28 → 512) + ReLU + Dropout
    ↓
Linear(512 → 2)

ResNet50 Transfer Learning

ResNet50 Backbone (frozen)
    ↓
Linear(2048→512) + ReLU + Dropout
    ↓
Linear(512→2)

Results

Model AVG Accuracy
CNN from Scratch 69.52%
Feature Extraction 98.778%
Fine Tuning 99.195%
Model BEST Accuracy
CNN from Scratch 80.97%
Feature Extraction 99.16%
Fine Tuning 99.51%

Training curves

  • The graph shows accuracy increasing and loss decreasing over 10 epochs.
  • Train and Val curves are close together which means no overfitting.

CNN training graph conv_train

Feature Extraction feature_E_training

Fine Tunning Fine_training

Confusion matrix

  • Diagonal shows correct predictions.
  • Some confusion between cat and dog which is expected as they look similar.

CNN confusion matrix convConfusion

Feature Extraction confusion matrix featureConfusion

Fine Tunning confusion matrix fineConfusion

Predictions

  • Green title = correct prediction
  • Red title = wrong prediction

CNN cnnpredicted

Feature Extraction predicted_feature

Fine Tunning predicted_fine

What I learned

CNN from Scratch

  • Built a convolution layers to extract features from the images
  • MaxPooling reduces the size of the image after each layer
  • Dropout prevents overfitting

Transfer Learning

  • ResNet50 pretrained on ImageNet already knows edges, shapes, textures
  • No need to train from scratch — saves time and data

Feature Extraction

  • I used resnet50
  • Forze all backbone layers(requires_grad=False)
  • Only trained the new classifier head
  • Fast training only small number of parameters update

Fine Tuning

  • Froze backbone first, trained head
  • Then unfroze last block (layer4) for resnet50

How to Run

Install dependencies

pip install torch torchvision matplotlib seaborn scikit-learn

Run CNN from scratch

python convolution.py

Run Feature Extraction

python feature_extraction.py

Run Fine Tuning

python fine_tunning.py

Author

Vikas Reddy

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages