TF-Slim

raw JSON →
1.1.0 verified Fri May 01 auth: no python maintenance

TF-Slim is a lightweight library for defining, training, and evaluating complex models in TensorFlow. The current version is 1.1.0, with no recent updates and likely in maintenance mode.

pip install tf-slim
error ModuleNotFoundError: No module named 'tensorflow.contrib.slim'
cause Using old import path from TensorFlow contrib (removed in TF 2.x).
fix
Install tf-slim and import as 'import tf_slim as slim' or 'from tf_slim import slim'.
error ValueError: Variable resnet_v2_50/conv1/weights already exists, disallowed.
cause Reusing the same variable scope without enclosing in a variable scope or using slim.get_unique_variable_scope() properly.
fix
Use tf.variable_scope('model', reuse=tf.AUTO_REUSE) or reuse=True when reusing the model.
error AttributeError: module 'tensorflow' has no attribute 'contrib'
cause TensorFlow 2.x removed the contrib module entirely.
fix
Install tf-slim and use the correct import path: 'import tf_slim as slim'.
breaking TF-Slim was part of TensorFlow contrib, which was removed in TF 2.0. The standalone tf-slim package must be installed separately.
fix Install tf-slim with pip and use 'import tf_slim as slim' instead of 'tensorflow.contrib.slim'.
gotcha TF-Slim's TF 1.x APIs (e.g., slim.layers, slim.losses) are not fully compatible with eager execution. You may need to run inside tf.Graph().as_default() or disable eager execution.
fix Use tf.compat.v1.disable_eager_execution() at the start of your script or wrap code in a graph context.
deprecated Many TF-Slim functions (e.g., slim.learning, slim.evaluation) are deprecated in favor of TensorFlow native APIs like tf.keras or tf.estimator.
fix Consider migrating to tf.keras for model building and tf.train.Checkpoint/tf.function for training.

Load a pretrained ResNet-50 model using TF-Slim's model zoo.

import tensorflow as tf
import tf_slim as slim

inputs = tf.random.normal([4, 224, 224, 3])
nets = tf_slim.nets.resnet_v2
with slim.arg_scope(nets.resnet_arg_scope()):
    logits, _ = nets.resnet_v2_50(inputs, num_classes=1000, is_training=False)
print(logits.shape)