步驟
- Step1:定義 @tf.function
- Step2:匯出 Save Model 自訂模組
- Step3:怎麼呼叫 TensorFlow Serving 的 RESTful API
Step1: 定義 @tf.function
class MLP(tf.keras.Model):
...
@tf.function(input_signature=[tf.TensorSpec([None, 28, 28, 1], tf.float32)])
def call(self, inputs):
...
Step2:匯出 Save Model 自訂模組
model = MLP()
tf.saved_model.save(model, "MLP/1", signatures={"call": model.call})
Step3:怎麼呼叫 TensorFlow Serving 的 RESTful API
import json
import requests
from zh.model.utils import MNISTLoader
data_loader = MNISTLoader()
data = json.dumps({
"signature_name": "call",
"instances": data_loader.test_data[0:10].tolist()
})
headers = {"content-type": "application/json"}
json_response = requests.post(
'http://localhost:8501/v1/models/MLP:predict',
data=data, headers=headers)