tf.contrib.slim
TensorFlow 2.0
migration guide
deep learning
code upgrade

Upgrading tf.contrib.slim manually to tf 2.0

Master System Design with Codemia

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

Introduction

Migrating from tf.contrib.slim to TensorFlow 2.x is mostly a shift to tf.keras layers, eager execution patterns, and modern checkpoint APIs. There is no drop-in replacement for all Slim helpers, so manual migration requires mapping each concept to a TensorFlow 2 equivalent. A structured migration pass reduces regressions and keeps model behavior traceable.

Replace Slim Layers with Keras Layers

Most Slim layer calls map directly to Keras layer classes.

python
1# TensorFlow 1.x slim style
2# net = slim.conv2d(inputs, 64, [3, 3], stride=1, scope='conv1')
3
4import tensorflow as tf
5
6conv = tf.keras.layers.Conv2D(
7    filters=64,
8    kernel_size=(3, 3),
9    strides=(1, 1),
10    padding="same",
11    activation="relu",
12    name="conv1"
13)
14
15net = conv(inputs)

Do this systematically for conv2d, max_pool2d, dropout, and fully connected layers.

Replace arg_scope with Reusable Layer Factories

Slim arg_scope centralized default arguments. In TensorFlow 2, create helper functions or custom blocks to keep defaults in one place.

python
1import tensorflow as tf
2
3def conv_block(filters, name):
4    return tf.keras.Sequential([
5        tf.keras.layers.Conv2D(filters, 3, padding="same", use_bias=False),
6        tf.keras.layers.BatchNormalization(),
7        tf.keras.layers.ReLU(),
8    ], name=name)

This preserves readability without relying on deprecated global argument scopes.

Migrate Training Loops to model.fit or GradientTape

Slim training scripts often used graph sessions and manual update ops. TensorFlow 2 supports either high-level fit or custom GradientTape loops.

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Input(shape=(224, 224, 3)),
3    tf.keras.layers.Conv2D(32, 3, activation="relu"),
4    tf.keras.layers.GlobalAveragePooling2D(),
5    tf.keras.layers.Dense(10)
6])
7
8model.compile(
9    optimizer=tf.keras.optimizers.Adam(1e-3),
10    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
11    metrics=["accuracy"]
12)
13
14model.fit(train_ds, validation_data=val_ds, epochs=5)

This is simpler than session-based feed dictionaries and update op management.

Convert Checkpoints Carefully

Variable names usually differ between Slim and Keras layers, so direct restore may fail unless mapping is explicit.

python
ckpt = tf.train.Checkpoint(model=model)
ckpt.restore("/path/to/tf2_checkpoint").expect_partial()

For legacy checkpoints, write one conversion script that loads TensorFlow 1 variables and assigns weights layer by layer in TensorFlow 2 models, then saves a clean TensorFlow 2 checkpoint.

Replace Slim Metrics and Summaries

Slim metric ops are replaced by Keras metrics and TensorBoard callbacks.

python
tensorboard_cb = tf.keras.callbacks.TensorBoard(log_dir="logs/run1")
model.fit(train_ds, validation_data=val_ds, callbacks=[tensorboard_cb], epochs=5)

For custom metrics, subclass tf.keras.metrics.Metric and integrate in compile.

Incremental Migration Strategy

A reliable plan is:

  1. Freeze model architecture and reference outputs in TensorFlow 1.
  2. Port layers and forward pass to TensorFlow 2.
  3. Validate output parity on a fixed batch.
  4. Migrate training loop and metrics.
  5. Convert checkpoints and run short regression training.

This staged approach isolates errors and avoids all-at-once rewrites.

Migrate Input Pipelines Alongside Model Code

Slim-era code often relied on queue runners and graph-based input readers. In TensorFlow 2, use tf.data pipelines with explicit map, batch, cache, and prefetch stages.

python
1import tensorflow as tf
2
3def parse_example(path, label):
4    image = tf.io.read_file(path)
5    image = tf.image.decode_jpeg(image, channels=3)
6    image = tf.image.resize(image, [224, 224])
7    image = tf.cast(image, tf.float32) / 255.0
8    return image, label
9
10paths = tf.constant(["img1.jpg", "img2.jpg"])
11labels = tf.constant([0, 1])
12
13ds = tf.data.Dataset.from_tensor_slices((paths, labels))
14ds = ds.map(parse_example, num_parallel_calls=tf.data.AUTOTUNE)
15ds = ds.batch(32).prefetch(tf.data.AUTOTUNE)

Keeping pipeline migration explicit avoids hidden bottlenecks and mismatched preprocessing between training and evaluation.

Common Pitfalls

  • Expecting tf.contrib.slim APIs to exist under compatibility namespaces forever.
  • Migrating layers without validating numeric parity on fixed inputs.
  • Ignoring checkpoint variable-name differences during restore.
  • Replacing graph code and training logic simultaneously without tests.
  • Forgetting to migrate summary and metric logging infrastructure.

Summary

  • Slim migration to TensorFlow 2 is a concept-by-concept mapping exercise.
  • Use Keras layers and reusable helper blocks instead of arg_scope.
  • Move training to fit or GradientTape based on flexibility needs.
  • Treat checkpoint conversion as a dedicated, tested step.
  • Validate parity incrementally to keep migration risk manageable.

Course illustration
Course illustration

All Rights Reserved.