Tensorflow
Inception ResNet v2
Deep Learning
Pre-trained Models
Neural Networks

Using pre-trained inception_resnet_v2 with Tensorflow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

InceptionResNetV2 is a large image model that works well for transfer learning when you need stronger feature extraction than a very small backbone can provide. In TensorFlow, the easiest path is to load the pre-trained model from tf.keras.applications, preprocess images with the matching utility, and then either use it as a frozen feature extractor or fine-tune some of the deeper layers. Most issues come from using the wrong input size, skipping the model-specific preprocessing, or fine-tuning too early.

Load the Pre-Trained Model Correctly

TensorFlow exposes the model directly through Keras applications.

python
1import tensorflow as tf
2
3base_model = tf.keras.applications.InceptionResNetV2(
4    include_top=False,
5    weights="imagenet",
6    input_shape=(299, 299, 3),
7    pooling="avg",
8)
9
10print(base_model.output_shape)

Important choices here:

  • 'weights="imagenet" loads the pre-trained weights'
  • 'include_top=False removes the original ImageNet classifier'
  • 'input_shape=(299, 299, 3) matches the expected image shape'
  • 'pooling="avg" gives you one feature vector per image'

If you use the original classifier head, keep include_top=True and do not change the number of classes. For transfer learning, include_top=False is usually the right starting point.

Always Use the Matching Preprocessing Function

Every Keras application model expects input data in a specific range and format. For InceptionResNetV2, use its own preprocessing helper.

python
1import numpy as np
2import tensorflow as tf
3
4image = np.random.randint(0, 256, size=(1, 299, 299, 3), dtype=np.uint8)
5image = tf.cast(image, tf.float32)
6processed = tf.keras.applications.inception_resnet_v2.preprocess_input(image)
7
8print(tf.reduce_min(processed).numpy(), tf.reduce_max(processed).numpy())

If you skip preprocessing or use the wrong model's preprocessing function, accuracy can collapse even when the rest of the pipeline is correct.

Build a Transfer-Learning Classifier

A standard pattern is to freeze the backbone first, then add a small classifier head.

python
1import tensorflow as tf
2
3base_model = tf.keras.applications.InceptionResNetV2(
4    include_top=False,
5    weights="imagenet",
6    input_shape=(299, 299, 3),
7    pooling="avg",
8)
9base_model.trainable = False
10
11inputs = tf.keras.Input(shape=(299, 299, 3))
12x = tf.keras.applications.inception_resnet_v2.preprocess_input(inputs)
13x = base_model(x, training=False)
14x = tf.keras.layers.Dense(128, activation="relu")(x)
15x = tf.keras.layers.Dropout(0.2)(x)
16outputs = tf.keras.layers.Dense(3, activation="softmax")(x)
17
18model = tf.keras.Model(inputs, outputs)
19model.compile(
20    optimizer="adam",
21    loss="sparse_categorical_crossentropy",
22    metrics=["accuracy"],
23)
24
25model.summary()

This gives you a strong classifier baseline without immediately updating the large backbone weights.

Fine-Tuning Safely

Once the new classifier head is stable, you can unfreeze part of the backbone and continue training with a smaller learning rate.

python
1base_model.trainable = True
2
3for layer in base_model.layers[:-50]:
4    layer.trainable = False
5
6model.compile(
7    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),
8    loss="sparse_categorical_crossentropy",
9    metrics=["accuracy"],
10)

The key idea is to fine-tune gradually. Unfreezing the whole network immediately with a large learning rate can erase useful pre-trained weights.

Use It as a Feature Extractor

Sometimes you do not want a classifier at all. You just want embeddings for downstream work such as clustering, retrieval, or similarity search.

python
features = base_model(processed, training=False)
print(features.shape)

With pooling="avg", the output is already a compact feature vector that is easy to store or pass into another model.

Dataset Pipeline Notes

For real training data, resize every image to 299 x 299 and keep the channel count at 3. A small tf.data pipeline can handle this cleanly.

python
1import tensorflow as tf
2
3
4def preprocess(image, label):
5    image = tf.image.resize(image, (299, 299))
6    image = tf.cast(image, tf.float32)
7    image = tf.keras.applications.inception_resnet_v2.preprocess_input(image)
8    return image, label

That keeps the model input contract consistent from the first training batch onward.

Common Pitfalls

  • Using the wrong input size instead of 299 x 299.
  • Forgetting model-specific preprocessing before inference or training.
  • Fine-tuning the whole backbone immediately with an aggressive learning rate.
  • Using include_top=True while also expecting to change the classifier output size.
  • Judging the model by one quick experiment without freezing and fine-tuning in stages.

Summary

  • Load InceptionResNetV2 from tf.keras.applications with the correct input shape.
  • Use the matching preprocessing function every time.
  • Start with the backbone frozen and add a lightweight task-specific head.
  • Fine-tune only after the head stabilizes, and use a small learning rate.
  • The same pre-trained model can serve as either a classifier backbone or a general feature extractor.

Course illustration
Course illustration

All Rights Reserved.