Tensorflow
machine learning
model deployment
AI application
deep learning

Tensorflow How to use a trained model in a application?

Master System Design with Codemia

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

Introduction

Using a trained TensorFlow model in an application usually means running inference, not retraining. The important part is not just loading the saved model file, but reproducing the same input preprocessing and output interpretation that the model expected during training.

Core Sections

Save the model in a deployable format

For TensorFlow 2 and Keras-based projects, the standard deployment-friendly format is SavedModel or the newer Keras save format. A simple example:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(8, activation="relu"),
5    tf.keras.layers.Dense(2, activation="softmax"),
6])
7
8model.build((None, 4))
9model.save("saved_model_dir")

That exported artifact contains the network structure and weights. In a real application, this is what you package with the service or bundle into a deployment pipeline.

Load the model for inference

The application side normally loads the model once during startup and reuses it for predictions.

python
1import tensorflow as tf
2import numpy as np
3
4model = tf.keras.models.load_model("saved_model_dir")
5
6sample = np.array([[0.2, 0.4, 0.1, 0.9]], dtype="float32")
7prediction = model.predict(sample, verbose=0)
8print(prediction)

Loading the model per request is usually the wrong design because it adds unnecessary overhead and latency.

Keep preprocessing identical to training

This is where many application integrations fail. The model was trained on data with a specific shape, dtype, scaling rule, tokenization rule, or image normalization. If the application feeds raw inputs in a different format, the model may load successfully and still produce bad predictions.

For example, an image model might expect normalized pixel values:

python
1import numpy as np
2
3def preprocess_image(raw_pixels):
4    image = np.asarray(raw_pixels, dtype="float32")
5    image = image / 255.0
6    return np.expand_dims(image, axis=0)

A text model might expect token IDs instead of raw strings. The trained model is only one piece of the serving pipeline; the preprocessing contract matters just as much.

Interpret the output correctly

Model outputs are often logits, probabilities, embeddings, or regression values. The application must know how to turn them into usable answers.

python
1import numpy as np
2
3prediction = model.predict(sample, verbose=0)
4predicted_class = int(np.argmax(prediction, axis=1)[0])
5print(predicted_class)

If the model outputs logits, you may need softmax. If it is a binary sigmoid model, you may need thresholding. If it is a regression model, there may be no class conversion at all.

Wrap inference behind one application function

A clean application integration usually hides the TensorFlow details behind a small prediction function or service object.

python
1import numpy as np
2import tensorflow as tf
3
4class Predictor:
5    def __init__(self, model_path):
6        self.model = tf.keras.models.load_model(model_path)
7
8    def predict_class(self, features):
9        x = np.asarray([features], dtype="float32")
10        scores = self.model.predict(x, verbose=0)
11        return int(np.argmax(scores, axis=1)[0])
12
13
14predictor = Predictor("saved_model_dir")
15print(predictor.predict_class([0.2, 0.4, 0.1, 0.9]))

That structure is much easier to test and reuse than sprinkling load_model and predict calls across multiple routes or UI actions.

Deployment format depends on the target

How you package the model depends on the application environment:

  • Python backend: load_model inside the service
  • mobile app: often TensorFlow Lite instead of full TensorFlow
  • browser: TensorFlow.js conversion
  • high-throughput serving: TensorFlow Serving or another model-serving layer

The inference logic is conceptually the same, but the runtime changes depending on latency, size, and platform requirements.

Common Pitfalls

  • Loading the model successfully but forgetting to replicate the training-time preprocessing.
  • Re-loading the model for every request instead of keeping one long-lived inference object.
  • Misinterpreting logits, probabilities, or regression outputs on the application side.
  • Packaging only the model file and forgetting associated label maps, tokenizers, or normalization rules.
  • Deploying full TensorFlow into an environment that really needs TensorFlow Lite, TensorFlow.js, or a serving system.

Summary

  • Using a trained TensorFlow model in an application is mostly an inference and integration problem.
  • Save the trained model in a format the target runtime can load.
  • Load the model once and reuse it instead of repeatedly opening it.
  • Keep preprocessing and output interpretation identical to the training pipeline.
  • Choose the serving runtime based on the application target: backend, mobile, browser, or dedicated serving infrastructure.

Course illustration
Course illustration

All Rights Reserved.