TD 4 Computer Vision
TD 4 Computer Vision
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
3.2 VGG16
5 Performance Comparison
Create a comparison table of:
6 Visualization
Plot:
• Confusion matrices
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
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?