5 Best Ways to Extract Features Using Pre-trained Models in TensorFlow with Python

Rate this post

๐Ÿ’ก Problem Formulation: Deep learning practitioners often need to extract meaningful features from images to support various tasks such as classification, recognition, or transfer learning. Leveraging pre-trained models like those provided by TensorFlow can significantly reduce computational resources and improve performance. This article illustrates how to use pre-trained models in TensorFlow to extract features from input images, where the desired output is a set of feature vectors.

Method 1: Using TensorFlow Hub for Feature Vector Extraction

TensorFlow Hub provides a library for reusable machine learning modules, including pre-trained models for image feature extraction. By using these modules, one can efficiently extract high-level feature descriptors from images.

Here’s an example:

import tensorflow_hub as hub
import tensorflow as tf

module_handle = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
module = hub.load(module_handle)
features = module(image_tensor) # image_tensor is a batch of images in tensor format.

print(features)

The output would be a tensor containing the extracted feature vectors for each image in the batch.

This code snippet demonstrates how to load a MobileNetV2 model as a feature extractor from TensorFlow Hub. The model takes an image_tensor as input and outputs the corresponding feature vectors.

Method 2: Utilizing TensorFlow Keras Applications

TensorFlow’s tf.keras.applications provides a series of pre-trained models which can be used directly for feature extraction or fine-tuning. These models come ready-to-use with pre-loaded weights.

Here’s an example:

from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Model

base_model = ResNet50(weights='imagenet', include_top=False)
model = Model(inputs=base_model.input, outputs=base_model.get_layer('activation_49').output)

img = preprocess_input(image_array) # image_array is the input image in array format.
features = model.predict(img)

print(features)

The output will be the activation values from the ‘activation_49’ layer of ResNet50 which serves as the feature vector.

This code snippet uses ResNet50 to extract the feature vectors. The include_top=False argument specifies that we are not interested in the fully connected layers at the top of the network, just the convolutional features.

Method 3: Feature Extraction via TensorFlow Slim

TensorFlow Slim is an easy to use library for defining, training and evaluating complex models in TensorFlow. Pre-trained models can be loaded with TensorFlow Slim for easy feature extraction.

Here’s an example:

import tensorflow as tf
import tf_slim as slim
from tf_slim.nets import vgg

input_tensor = tf.placeholder(tf.float32, shape=(None,224,224,3))
vgg = vgg.vgg_16(input_tensor)

with slim.arg_scope(vgg_arg_scope()):
    logits, endpoints = vgg(input_tensor, is_training=False)
    features = endpoints['vgg_16/fc7']

sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(load_vgg_16_checkpoint_fn(sess))

feature_vector = sess.run(features, feed_dict={input_tensor: image_batch})

The feature_vector holds the extracted features from the ‘fc7’ layer of the VGG-16 model for an image_batch.

The example uses TensorFlow Slim to load a VGG-16 model and includes the definition of an input placeholder. It extracts features from the ‘fc7’ layer, generally known to carry strong feature representations.

Method 4: Custom Feature Extractor by Modifying Pre-trained Models

One can create a custom feature extractor by modifying a pre-trained model. For example, removing the output layers and appending new layers as needed to adapt the model for a specific task.

Here’s an example:

from tensorflow.keras.applications import InceptionV3
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.models import Model

base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x) # Adding a global spatial average pooling layer
model = Model(inputs=base_model.input, outputs=x)

img = preprocess_input(image_array)
features = model.predict(img)

print(features)

The variable features contains the global average-pooled features for the input image.

This code snippet shows how to build a custom feature extractor on top of InceptionV3 by adding a global average pooling layer. Such feature extractors are useful for reducing the dimensionality of features while retaining spatial information.

Bonus One-Liner Method 5: Quick Feature Extraction with Keras’ Preprocessing

Keras’ preprocessing utilities offer a quick way to use pre-trained models for a single line feature extraction from images.

Here’s an example:

features = tf.keras.applications.inception_v3.preprocess_input(image_batch)

This line returns the preprocessed image batch ready for feature extraction with InceptionV3.

Although not a feature extraction method per se, this preprocessing step is crucial and often overlooked. It rescales and normalizes the image pixels to match the distribution expected by InceptionV3’s original training dataset.

Summary/Discussion

  • Method 1: TensorFlow Hub. Strengths: Hassle-free integration, variety of models available. Weaknesses: Internet connectivity required to download modules.
  • Method 2: Keras Applications. Strengths: Easy-to-use API, fine-tuning capabilities. Weaknesses: Limited to models available in Keras.
  • Method 3: TensorFlow Slim. Strengths: Flexible architecture, can handle legacy models. Weaknesses: Slimmer community support and less intuitive for beginners.
  • Method 4: Custom Feature Extractor. Strengths: Highly customizable for specific tasks. Weaknesses: Requires deeper knowledge of model architectures and potential for overfitting.
  • Method 5: Preprocessing. Strengths: Quick and easy, essential for accurate feature extraction. Weaknesses: Just preprocessing, not actual feature extraction.