<jupyter_start><jupyter_text>This notebook is used to test the TensorFlow Serving API* Use this code to deploy yolov3 model in `/tmp/yolov3` to a RESTful API with Docker `docker run -p 8501:8501 --mount type=bind,source=/tmp/yolov3/,target=/models/yolov3 -e MODEL_NAME=yolov3 -t tensorflow/serving &`* The yolov3 SaveModel is created from this folder.* THe RESTful API is `"http://localhost:8501/v1/models/yolov3:predict"`* Use the same code to deploy an MNIST model `docker run -p 8501:8501 --mount type=bind,source=/tmp/mnist,target=/models/mnist -e MODEL_NAME=mnist -t tensorflow/serving &`* The RESTful API is `"http://localhost:8501/v1/models/mnist:predict"`* Check the metadata with `"http://localhost:8501/v1/models/mnist/metadata"`  Test MNIST<jupyter_code>from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import requests
import tensorflow as tf
import json
mnist = input_data.read_data_sets("mnist_data/", one_hot=True)
print(mnist.train.images.shape)
image_data_mnist = mnist.train.images[0]
mnist_api = "http://localhost:8501/v1/models/mnist:predict"
image_data_mnist_list = image_data_mnist[np.newaxis, :].tolist()
headers = {"Content-type": "application/json"}
r = requests.post(mnist_api, headers=headers, 
                  data=json.dumps({"signature_name": "predict",
                                   "instances": image_data_mnist_list})).json()
print(r)<jupyter_output><empty_output><jupyter_text>Test Yolov3<jupyter_code># the following session tests whether the session successfully load the variables
import cv2
from matplotlib import pyplot as plt
import numpy as np
import core.utils as utils
img_size = 608
num_channels = 3
image_path = "./docs/images/sample_computer.jpg"
original_image = cv2.imread(image_path)
original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
image_data = utils.image_preporcess(np.copy(original_image), [img_size, img_size])
print(image_data.shape)
plt.imshow(image_data)
plt.show()
yolov3_api = "http://localhost:8501/v1/models/yolov3:predict"
image_data_yolo_list = image_data[np.newaxis, :].tolist()
headers = {"Content-type": "application/json"}
r = requests.post(yolov3_api, headers=headers, 
                  data=json.dumps({"signature_name": "predict",
                                   "instances": image_data_yolo_list})).json()
output = np.array(r['predictions'])
print(output.shape)
output = np.reshape(output, (-1, 85))
output.shape
original_image_size = original_image.shape[:2]
bboxes = utils.postprocess_boxes(output, original_image_size, img_size, 0.3)
bboxes = utils.nms(bboxes, 0.45, method='nms')
image = utils.draw_bbox(original_image, bboxes)
plt.imshow(image)
plt.show()<jupyter_output><empty_output><jupyter_text>Test decode()<jupyter_code>output_size = 13
y = tf.tile(tf.range(output_size, dtype=tf.int32)[:, tf.newaxis], [1, output_size])

# is it possible to direcly create a (13, 13) tensor?
y
tf.range(13)
x = tf.tile(tf.range(output_size, dtype=tf.int32)[tf.newaxis, :], [output_size, 1])
x
xy_grid = tf.concat([x[:, :, tf.newaxis], y[:, :, tf.newaxis]], axis=-1)
xy_grid
batch_size = -1
anchor_per_scale = 3
xy_grid = tf.tile(xy_grid[tf.newaxis, :, :, tf.newaxis, :], [batch_size, 1, 1, anchor_per_scale, 1])
xy_grid
xy_grid = tf.cast(xy_grid, tf.float32)
xy_grid<jupyter_output><empty_output><jupyter_text>why go through all these steps to create tensor? Is it that hard?<jupyter_code>tf.range(13)
with tf.Session() as sess:
    tmp1 = sess.run(tf.range(13))
    tmp2 = sess.run(x)
tmp1
tmp2
import core.utils as utils
from core.config import cfg
anchors = utils.get_anchors(cfg.YOLO.ANCHORS)
anchors
anchors[0]
len(anchors[0])
print(cfg.YOLO.ANCHOR_PER_SCALE)
print(cfg.YOLO.IOU_LOSS_THRESH)<jupyter_output>3
0.5
{'fraction_non_alphanumeric': 0.1332263242375602, 'fraction_numerical': 0.02835741037988229, 'mean_word_length': 5.402397260273973, 'pattern_counts': {'":': 6, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '625818', 'license': 'MIT', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1377, 'n_words': 289, 'path': 'test_tf_serving_api.ipynb', 'repo_name': 'moon412/tensorflow-yolov3', 'size': '3738'}