Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Tensorflow be used to extract features with the help of pre-trained model using Python?
TensorFlow can be used to extract features with the help of pre-trained models using a feature extractor model, which is previously defined and is used in the KerasLayer method. This approach leverages transfer learning to utilize knowledge from models trained on large datasets.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
Understanding Transfer Learning
The intuition behind transfer learning for image classification is that if a model is trained on a large and general dataset, this model can effectively serve as a generic model for the visual world. It learns feature maps, which means users don't have to start from scratch by training a large model on a large dataset.
TensorFlow Hub is a repository that contains pre-trained TensorFlow models. TensorFlow can be used to fine-tune learning models.
Setting Up the Environment
First, we need to import the required libraries and set up our environment:
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
# Create sample image batch (32 images of 224x224x3)
image_batch = tf.random.normal((32, 224, 224, 3))
print("Image batch shape:", image_batch.shape)
Image batch shape: (32, 224, 224, 3)
Extracting Features Using Pre-trained Model
We use MobileNet V2 from TensorFlow Hub as our feature extractor. This pre-trained model converts images into feature vectors:
print("Extracting features")
feature_extractor_model = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
# Create the feature extractor layer
feature_extractor_layer = hub.KerasLayer(
feature_extractor_model,
input_shape=(224, 224, 3),
trainable=False
)
# Extract features from the image batch
feature_batch = feature_extractor_layer(image_batch)
print("Feature batch shape:", feature_batch.shape)
Extracting features Feature batch shape: (32, 1280)
How Feature Extraction Works
The feature extractor transforms each input image (224×224×3) into a 1280-dimensional feature vector. These features represent high-level patterns learned by the pre-trained model:
- Input: 32 images of shape (224, 224, 3)
- Output: 32 feature vectors of shape (1280,)
- The model is frozen (
trainable=False) to preserve pre-trained weights
Using Features for Classification
These extracted features can be used as input to a custom classifier:
# Create a simple classification model using extracted features
model = tf.keras.Sequential([
feature_extractor_layer,
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax') # 10 classes
])
# Compile the model
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
print("Model summary:")
model.summary()
Model summary: Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= keras_layer (KerasLayer) (None, 1280) 2257984 _________________________________________________________________ dense (Dense) (None, 128) 163968 _________________________________________________________________ dense_1 (Dense) (None, 10) 1290 ================================================================= Total params: 2,423,242 Trainable params: 165,258 Non-trainable params: 2,257,984 _________________________________________________________________
Conclusion
TensorFlow Hub's pre-trained models enable efficient feature extraction without training from scratch. The KerasLayer wrapper makes it easy to integrate pre-trained models into custom architectures, significantly reducing training time and computational requirements.
