1

I followed "Tensorflow for poets" in 2017 and retrained my own collection of images and created "retrained_graph.pb" and "retrained_labels.txt"
Today I need to run this model on Tensorflow Serving. There are two options to accomplish this:

  1. Upgrade the old model to save it as under the "saved_model" format and use it on Tensorflow Serving - I found some SO postings to acccomplish it (this or that).

  2. Use the latest tensorflow Hub with Keras (https://www.tensorflow.org/tutorials/images/hub_with_keras)

I am looking for the best option among these, or a new one.

santhosh
  • 69
  • 1
  • 12

1 Answers1

0

In my opinion, either using Tensorflow Hub or using the Pre-Trained Models inside tf.keras.applications is preferable because, in either cases, there won't be many code changes required to Save the Model, to make it compatible for Tensorflow Serving.

The code for reusing the Pre-Trained Model, MobileNet which is present inside tf.keras.applications is shown below:

#Import MobileNet V2 with pre-trained weights AND exclude fully connected layers
IMG_SIZE = 224

from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import Model


IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

# Create the base model from the pre-trained model MobileNet V2
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')

# Add Global Average Pooling Layer
x = base_model.output
x = GlobalAveragePooling2D()(x)

# Add a Output Layer
my_mobilenetv2_output = Dense(5, activation='softmax')(x)

# Combine whole Neural Network
my_mobilenetv2_model = Model(inputs=base_model.input, outputs=my_mobilenetv2_output)

You can Save the Model using the Code given below:

version = 1
MODEL_DIR = 'Image_Classification_Model'
export_path = os.path.join(MODEL_DIR, str(version))

tf.keras.models.save_model(model = model, filepath = export_path)