tianyapiaozi
tianyapiaozi

Reputation: 2008

How to prepare warmup request file for tensorflow serving?

Current version of tensorflow-serving try to load warmup request from assets.extra/tf_serving_warmup_requests file.

2018-08-16 16:05:28.513085: I tensorflow_serving/servables/tensorflow/saved_model_warmup.cc:83] No warmup data file found at /tmp/faster_rcnn_inception_v2_coco_2018_01_28_string_input_version-export/1/assets.extra/tf_serving_warmup_requests

I wonder if tensorflow provides common api to export request to the location or not? Or should we write request to the location manually?

Upvotes: 4

Views: 6161

Answers (3)

Xin Liang
Xin Liang

Reputation: 21

We refered to the official doc

Specially, we used Classification instead of Prediction, so we altered that code to be

log = prediction_log_pb2.PredictionLog(
            classify_log=prediction_log_pb2.ClassifyLog(request=<request>))

Upvotes: 2

gogasca
gogasca

Reputation: 10058

This is a complete example of an object detection system using a ResNet model. The prediction consist of an image.

import tensorflow as tf
import requests
import base64

from tensorflow.python.framework import tensor_util
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_log_pb2


IMAGE_URL = 'https://tensorflow.org/images/blogs/serving/cat.jpg'
NUM_RECORDS = 100


def get_image_bytes():
    image_content = requests.get(IMAGE_URL, stream=True)
    image_content.raise_for_status()
    return image_content.content


def main():
    """Generate TFRecords for warming up."""

    with tf.io.TFRecordWriter("tf_serving_warmup_requests") as writer:
        image_bytes = get_image_bytes()
        predict_request = predict_pb2.PredictRequest()
        predict_request.model_spec.name = 'resnet'
        predict_request.model_spec.signature_name = 'serving_default'
        predict_request.inputs['image_bytes'].CopyFrom(
            tensor_util.make_tensor_proto([image_bytes], tf.string))        
        log = prediction_log_pb2.PredictionLog(
            predict_log=prediction_log_pb2.PredictLog(request=predict_request))
        for r in range(NUM_RECORDS):
            writer.write(log.SerializeToString())    

if __name__ == "__main__":
    main()

This script will create a file called “tf_serving_warmup_requests”

I moved this file to /your_model_location/resnet/1538687457/assets.extra/ and then restart my docker image to pickup the new changes.

Upvotes: 1

Christis
Christis

Reputation: 96

At this point there is no common API for exporting the warmup data into the assets.extra. It's relatively simple to write a script (similar to below):

import tensorflow as tf
from tensorflow_serving.apis import model_pb2
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_log_pb2

def main():
    with tf.python_io.TFRecordWriter("tf_serving_warmup_requests") as writer:
        request = predict_pb2.PredictRequest(
            model_spec=model_pb2.ModelSpec(name="<add here>"),
            inputs={"examples": tf.make_tensor_proto([<add here>])}
        )
    log = prediction_log_pb2.PredictionLog(
        predict_log=prediction_log_pb2.PredictLog(request=request))
    writer.write(log.SerializeToString())

if __name__ == "__main__":
    main()

Upvotes: 8

Related Questions