TensorFlow
machine learning
model evaluation
image processing
AI model restoration

Tensorflow restoring a graph and model then running evaluation on a single image

Master System Design with Codemia

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

Introduction

Running inference on one image sounds simple, but the details depend on how the TensorFlow model was saved. The main things to get right are the model format, the input tensor shape, and the preprocessing steps that were used during training.

Identify the Saved Format First

Before writing inference code, confirm whether you have a TensorFlow SavedModel, a Keras model saved with model.save, or an older TensorFlow 1 checkpoint plus graph. The loading API is different for each format, and mixing them usually leads to confusing errors.

For modern TensorFlow 2 projects, a Keras or SavedModel export is the best path. You can usually load the model directly and call it on a batch of one image.

python
1import numpy as np
2import tensorflow as tf
3
4model = tf.keras.models.load_model("saved_classifier")
5
6image = tf.keras.utils.load_img("cat.jpg", target_size=(224, 224))
7array = tf.keras.utils.img_to_array(image)
8array = array / 255.0
9batch = np.expand_dims(array, axis=0)
10
11predictions = model.predict(batch, verbose=0)
12print(predictions.shape)
13print(predictions[0])

Two details matter here. First, the model still expects a batch dimension, even though you only have one image. Second, the pixel scaling must match training. If the original training code used application-specific preprocessing, use that same function at inference time.

Restore and Run a TensorFlow 2 SavedModel

If the model was exported as a general SavedModel, load it with tf.saved_model.load and call the serving signature. This is common when the saved artifact came from a deployment pipeline rather than from plain Keras training code.

python
1import numpy as np
2import tensorflow as tf
3
4saved = tf.saved_model.load("exported_model")
5infer = saved.signatures["serving_default"]
6
7image = tf.keras.utils.load_img("dog.jpg", target_size=(224, 224))
8array = tf.keras.utils.img_to_array(image).astype("float32") / 255.0
9batch = tf.constant(np.expand_dims(array, axis=0))
10
11outputs = infer(batch)
12for name, value in outputs.items():
13    print(name, value.numpy())

This approach is useful when the output is a dictionary of named tensors instead of a single NumPy array. If you are unsure which signature exists, inspect list(saved.signatures) in a shell first.

Restore a Legacy TensorFlow 1 Graph

Older projects often saved a checkpoint and a .meta graph file. In that case, you restore the graph into a TensorFlow 1 session, fetch tensors by name, and then run the session with a feed_dict.

python
1import numpy as np
2from PIL import Image
3import tensorflow.compat.v1 as tf
4
5tf.disable_eager_execution()
6
7def load_image(path):
8    image = Image.open(path).resize((224, 224))
9    array = np.asarray(image, dtype=np.float32) / 255.0
10    return np.expand_dims(array, axis=0)
11
12saver = tf.train.import_meta_graph("model.ckpt.meta")
13
14with tf.Session() as sess:
15    saver.restore(sess, "model.ckpt")
16    graph = tf.get_default_graph()
17
18    input_tensor = graph.get_tensor_by_name("input:0")
19    output_tensor = graph.get_tensor_by_name("predictions:0")
20
21    result = sess.run(output_tensor, feed_dict={
22        input_tensor: load_image("sample.jpg")
23    })
24
25    print(result)

The fragile part is tensor naming. If input:0 or predictions:0 is wrong, list the graph operations and confirm the exported names before guessing.

Keep Preprocessing and Label Mapping Together

A model restore step is only half of inference. You also need the exact preprocessing recipe and the label mapping used during training. If the model expects 224 x 224 RGB images with application-specific normalization, feeding raw pixel values from a different pipeline will give bad predictions even though the code runs.

python
1import json
2import numpy as np
3import tensorflow as tf
4
5model = tf.keras.models.load_model("saved_classifier")
6
7with open("labels.json", "r", encoding="utf-8") as fh:
8    labels = json.load(fh)
9
10image = tf.keras.utils.load_img("cat.jpg", target_size=(224, 224))
11array = tf.keras.applications.mobilenet_v2.preprocess_input(
12    tf.keras.utils.img_to_array(image)
13)
14batch = np.expand_dims(array, axis=0)
15
16scores = model.predict(batch, verbose=0)[0]
17best = int(np.argmax(scores))
18print(labels[str(best)], float(scores[best]))

Treat the preprocessing code, the saved model, and the labels file as one deployable unit. Splitting them across unrelated scripts is a common source of silent inference bugs.

Common Pitfalls

The most common problem is forgetting the batch dimension. A single image usually has shape 224 x 224 x 3, while the model expects 1 x 224 x 224 x 3.

Another problem is using the wrong preprocessing routine. Many pretrained architectures expect channel scaling or normalization rules that are not interchangeable.

TensorFlow 1 models also fail frequently because of missing tensor names or version mismatches. If a legacy model is involved, verify the saved graph structure first instead of guessing the input and output names.

Finally, be careful with color layout. PIL, OpenCV, and custom pipelines may not agree on channel order. If training used RGB and inference accidentally uses BGR, the prediction quality can collapse without any obvious runtime error.

Summary

  • Confirm whether the model is a Keras export, a generic SavedModel, or a legacy TensorFlow 1 checkpoint.
  • Always add a batch dimension before inference, even for one image.
  • Reuse the exact preprocessing pipeline from training.
  • For TensorFlow 1 models, verify tensor names before calling sess.run.
  • Keep label files and preprocessing logic aligned with the saved model artifact.

Course illustration
Course illustration

All Rights Reserved.