Multiclass image classification using Transfer learning in python

Multiclass image classification using Transfer learning in python

Transfer learning is a machine learning technique where a pre-trained model is refined to solve another similar task. For image classification, popular deep learning architectures like VGG, ResNet, and Inception have been pre-trained on large datasets such as ImageNet. Using transfer learning, one can leverage these pre-trained models to solve specific classification problems with smaller datasets.

Here's a step-by-step guide to perform multiclass image classification using transfer learning with TensorFlow and Keras:

  • Import Required Libraries:
import tensorflow as tf from tensorflow.keras.applications.inception_v3 import InceptionV3 from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense, GlobalAveragePooling2D from tensorflow.keras.preprocessing.image import ImageDataGenerator 
  • Load the Base Model:

We'll use the InceptionV3 model here. You can replace it with other models like VGG16, ResNet50, etc.

base_model = InceptionV3(weights='imagenet', include_top=False) 
  • Add Custom Layers:
x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(NUM_CLASSES, activation='softmax')(x) # Assuming NUM_CLASSES is the number of classes you have model = Model(inputs=base_model.input, outputs=predictions) 
  • Freeze Layers from the Base Model:
for layer in base_model.layers: layer.trainable = False 
  • Compile the Model:
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 
  • Prepare Your Data:

Use ImageDataGenerator for data augmentation:

train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory('path_to_training_data', target_size=(150, 150), batch_size=32, class_mode='categorical') validation_generator = test_datagen.flow_from_directory('path_to_validation_data', target_size=(150, 150), batch_size=32, class_mode='categorical') 
  • Train the Model:
model.fit(train_generator, epochs=10, validation_data=validation_generator) 
  • Optional: Fine-Tuning:

Once you've trained the top layers, you can unfreeze some layers from the base model and fine-tune them:

# Unfreeze some layers for layer in model.layers[:249]: layer.trainable = False for layer in model.layers[249:]: layer.trainable = True # Recompile and train with a slower learning rate model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) model.fit(train_generator, epochs=10, validation_data=validation_generator) 

And that's it! You've successfully set up a multiclass image classification using transfer learning. Adjust hyperparameters, training epochs, layers, and architectures as needed to optimize the model further for your specific dataset.


More Tags

keypress constraint-validation-api android-gradle-plugin tidyselect sha256 findbugs hadoop-yarn javax cython bootstrap-grid

More Programming Guides

Other Guides

More Programming Examples