0% found this document useful (0 votes)
2 views4 pages

TD 4 Computer Vision

This document outlines a deep learning homework project focused on comparing the performance of various pre-trained models for flower classification using the Oxford Flowers-102 dataset. It includes steps for setup, dataset preparation, model implementation (ResNet50, VGG16, EfficientNetB0, Vision Transformer, and YOLOv8), training, evaluation, and performance comparison. Additionally, it poses questions for analysis regarding model accuracy, resource requirements, and training dynamics.

Uploaded by

youness.bht2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

TD 4 Computer Vision

This document outlines a deep learning homework project focused on comparing the performance of various pre-trained models for flower classification using the Oxford Flowers-102 dataset. It includes steps for setup, dataset preparation, model implementation (ResNet50, VGG16, EfficientNetB0, Vision Transformer, and YOLOv8), training, evaluation, and performance comparison. Additionally, it poses questions for analysis regarding model accuracy, resource requirements, and training dynamics.

Uploaded by

youness.bht2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Deep Learning Homework: Pre-Trained Models Comparison

Flower Classification Task

Project Overview
Compare the performance of different pre-trained models on the Oxford Flowers-102 dataset. Implement
transfer learning with various architectures and analyze their results.

1 Setup
Install required packages:
1 pip install tensorflow torch torchvision ultralytics matplotlib numpy

2 Dataset Preparation
Load and preprocess the Flowers-102 dataset (new rapid technique to read the data inside the folders for
tensorflow models):
1 import tensorflow as tf
2 from tensorflow . keras . preprocessing . image import ImageDataGenerator
3
4 # Data augmentation
5 train_datagen = ImageDataGenerator (
6 rescale =1./255 ,
7 rotation_range =20 ,
8 wid th_shi ft_range =0.2 ,
9 he ig ht _s hift_range =0.2 ,
10 horizontal_flip = True ,
11 validation_split =0.2
12 )
13
14 # Load dataset
15 train_generator = train_datagen . flow_from_directory (
16 ’ archive / data / ’ ,
17 target_size =(224 , 224) ,
18 batch_size =32 ,
19 class_mode = ’ categorical ’ ,
20 subset = ’ training ’
21 )
22
23 val_generator = train_datagen . flow_from_directory (
24 ’ archive / data / ’ ,
25 target_size =(224 , 224) ,
26 batch_size =32 ,
27 class_mode = ’ categorical ’ ,
28 subset = ’ validation ’
29 )

Use the code below to access the details of the data and print them.
1 # Print dataset details
2 print ( " \ n \033[1 mDataset Information :\033[0 m " )
3 print ( f " Training samples : { train_generator . samples } " )
4 print ( f " Validation samples : { val_generator . samples } " )
5 print ( f " Number of classes : { len ( train_generator . class_indices ) } " )
6 print ( f " Class names : { list ( train_generator . class_indices . keys () ) [:5]}... " ) # Show first
5
7
8 # Visualize sample images
9 plt . figure ( figsize =(10 , 10) )
10 for images , labels in train_generator :
11 for i in range (9) :
12 ax = plt . subplot (3 , 3 , i + 1)
13 plt . imshow ( images [ i ])
14 class_idx = np . argmax ( labels [ i ])
15 class_name = list ( train_generator . class_indices . keys () ) [ class_idx ]
16 plt . title ( f " Class : { class_name } " )
17 plt . axis ( " off " )
18 break # Only show first batch
19 plt . suptitle ( " Sample Training Images " , y =1.02)
20 plt . tight_layout ()
21 plt . show ()
22
23 # Plot class distribution
24 class_counts = np . bincount ( train_generator . labels )
25 plt . figure ( figsize =(12 , 4) )
26 plt . bar ( range ( len ( class_counts ) ) , class_counts )
27 plt . title ( " Class Distribution " )
28 plt . xlabel ( " Class ID " )
29 plt . ylabel ( " Number of Samples " )
30 plt . show ()

3 Model Implementations
3.1 ResNet50

1 from tensorflow . keras . applications import ResNet50


2 from tensorflow . keras import layers , models
3
4 base_model = ResNet50 ( weights = ’ imagenet ’ , include_top = False , input_shape =(224 , 224 , 3) )
5 base_model . trainable = False # Freeze base model
6
7 model = models . Sequential ([
8 base_model ,
9 layers . G lo ba lAv er ag ePo ol in g2D () ,
10 layers . Dense (256 , activation = ’ relu ’) ,
11 layers . Dense (5 , activation = ’ softmax ’) # 5 is the number of classes
12 ])
13
14 model . compile ( optimizer = ’ adam ’ ,
15 loss = ’ c at e g o ri c a l _c r o ss e n t ro p y ’ ,
16 metrics =[ ’ accuracy ’ ])

3.2 VGG16

1 from tensorflow . keras . applications import VGG16


2
3 base_model = VGG16 ( weights = ’ imagenet ’ , include_top = False , input_shape =(224 , 224 , 3) )
4 for layer in base_model . layers [: -4]: # Freeze all but last 4 layers
5 layer . trainable = False
6
7 # Same classifier as above
3.3 EfficientNetB0

1 from tensorflow . keras . applications import EfficientNetB0


2
3 base_model = EfficientNetB0 ( weights = ’ imagenet ’ , include_top = False , input_shape =(224 , 224 ,
3) )
4 base_model . trainable = False
5
6 # Same classifier as above

3.4 Vision Transformer (ViT)

1 from transformers import ViTFeatureExtractor , T F V i T F o r I m a g e C l a s s i f i c a t i o n


2
3 model = T F V i T F o r I m a g e C l a s s i f i c a t i o n . from_pretrained (
4 ’ google / vit - base - patch16 -224 ’ ,
5 num_labels =5 ,
6 i g n o r e _ m i sm a tc he d _s iz e s = True
7 )

3.5 YOLOv11 (Classification)


We need to reorganize the folder structure for training the model. The directory architecture should be
structured as follows:
archive/
train/
class 1/
class 2/
class 3/
class 4/
class 5/
test/
class 1/
class 2/
class 3/
class 4/
class 5/
1 from ultralytics import YOLO
2
3 # Load pre - trained YOLOv8 classification model
4 model = YOLO ( ’ yolo11n - cls . pt ’) # nano version
5
6 # Train ( will automatically adapt to 102 classes )
7 results = model . train (
8 data = ’ archive ’ ,
9 epochs =50 ,
10 imgsz =224 ,
11 batch =32
12 )

4 Training and Evaluation


Train each model and record metrics:
1 # For TensorFlow models
2 history = model . fit (
3 train_generator ,
4 epochs =20 ,
5 validation_data = val_generator
6 )
7
8 # For YOLOv8
9 metrics = model . val () # Validation metrics

5 Performance Comparison
Create a comparison table of:

• Training time per epoch

• Final validation accuracy

• Model size (parameters)

• Inference time per image

6 Visualization
Plot:

• Accuracy/loss curves for each model

• Confusion matrices

• Sample predictions with confidence scores

7 Questions
1. Which model achieved the highest accuracy? Why do you think this is?

2. Compare the resource requirements (memory, compute) of CNN-based vs. transformer-based models

3. How did YOLOv8’s classification performance compare to traditional classification models?

4. What differences did you observe in training dynamics between the models?

5. If you were to deploy this in a mobile app, which model would you choose and why?

6. Experiment with unfreezing different numbers of layers - how does this affect performance?

7. Compare the models’ performance on different flower categories - are some consistently harder to
classify?

You might also like