Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Saved Model Half Plus Two

From Leeroopedia
Knowledge Sources
Domains Model Serving, Testing, Model Export
Last Updated 2026-02-13 00:00 GMT

Overview

Python script that exports a "half plus two" linear regression model (y = 0.5*x + 2) in multiple SavedModel formats for testing TensorFlow Serving load and execution functionality.

Description

The saved_model_half_plus_two.py script generates SavedModel exports of a simple linear regression model used extensively for testing TensorFlow Serving. The model computes y = a*x + b (where a=0.5, b=2) and optionally y2 = a*x + c (where c=3) and y3 = a*x2 + c.

The script supports two export paths:

TF1 Export (default): Uses tf.saved_model.builder.SavedModelBuilder to create a model with:

  • Variables a (0.5), b (2.0), c (3.0) that exercise variable loading on restore
  • Serialized tf.Example parsing for classification and regression inputs
  • Multiple signature definitions: regress_x_to_y, regress_x_to_y2, regress_x2_to_y3, classify_x_to_y, and the default predict signature
  • Asset file support for testing asset loading
  • MKL-DNN convolution ops when device_type="mkl"
  • Optional TFLite conversion with or without SignatureDefs

TF2 Export: Uses a native HalfPlusTwoModel (tf.Module subclass) with:

  • tf.Variables for a, b, c
  • tf.function-decorated methods for each signature (predict, regress_xy, regress_xy2, regress_x2y3, classify_xy, classify_x2y3)
  • Input signatures using tf.TensorSpec for concrete function tracing
  • SavedModel Asset support

The main function generates seven model variants: standard, TF2, pbtxt (text format), main_op, TFLite, MLMD-enabled, and TFLite with SignatureDef. Helper functions build predict, regression, and classification SignatureDefs, write asset files, and write MLMD UUID files.

Usage

Use this script to generate test models for TensorFlow Serving integration tests. The exported models exercise the full spectrum of serving capabilities including predict, classify, regress, SavedModel loading, asset handling, TFLite serving, and MLMD integration. Run with bazel run saved_model_half_plus_two -- --device=cpu.

Code Reference

Source Location

  • Repository: Tensorflow_Serving
  • File: tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two.py (lines 1-512)

Signature

def _generate_saved_model_for_half_plus_two(
    export_dir, tf2=False, as_text=False, as_tflite=False,
    as_tflite_with_sigdef=False, use_main_op=False,
    include_mlmd=False, device_type="cpu"):
    """Generates SavedModel for half plus two."""

class HalfPlusTwoModel(tf.Module):
    """Native TF2 half-plus-two model."""
    def predict(self, x): ...
    def regress_xy(self, serialized_proto): ...
    def classify_xy(self, serialized_proto): ...

Import

# Standalone script, run via bazel or python
import tensorflow.compat.v1 as tf

I/O Contract

Inputs

Name Type Required Description
--output_dir string No Directory for standard SavedModel (default: /tmp/saved_model_half_plus_two)
--device string No Device type: "cpu", "mkl", or "gpu" (default: "cpu")
--output_dir_tf2 string No Directory for TF2 SavedModel
--output_dir_tflite string No Directory for TFLite model

Outputs

Name Type Description
SavedModel files filesystem Multiple SavedModel exports in various formats at the specified directories
model.tflite filesystem TFLite model file when as_tflite=True
mlmd_uuid filesystem MLMD UUID file in assets.extra when include_mlmd=True

Usage Examples

Generating Test Models

# Generate CPU model
_generate_saved_model_for_half_plus_two(
    "/tmp/saved_model_half_plus_two", device_type="cpu")

# Generate TF2 model
_generate_saved_model_for_half_plus_two(
    "/tmp/saved_model_half_plus_two_tf2", tf2=True)

# Generate TFLite model
_generate_saved_model_for_half_plus_two(
    "/tmp/saved_model_half_plus_two_tflite", as_tflite=True)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment