AI开发平台MODELARTS-Step1 在Notebook中拷贝模型包:模型包文件样例
模型包文件样例
模型包文件model.zip中需要用户自己准备模型文件,此处仅是举例示意说明,以一个手写数字识别模型为例。
Model目录下必须要包含推理脚本文件customize_service.py,目的是为开发者提供模型预处理和后处理的逻辑。
推理脚本customize_service.py的具体写法要求可以参考模型推理代码编写说明。
本案例中提供的customize_service.py文件具体内容如下:
import logging import threading import numpy as np import tensorflow as tf from PIL import Image from model_service.tfserving_model_service import TfServingBaseService class mnist_service(TfServingBaseService): def __init__(self, model_name, model_path): self.model_name = model_name self.model_path = model_path self.model = None self.predict = None # 非阻塞方式加载saved_model模型,防止阻塞超时 thread = threading.Thread(target=self.load_model) thread.start() def load_model(self): # load saved_model 格式的模型 self.model = tf.saved_model.load(self.model_path) signature_defs = self.model.signatures.keys() signature = [] # only one signature allowed for signature_def in signature_defs: signature.append(signature_def) if len(signature) == 1: model_signature = signature[0] else: logging.warning("signatures more than one, use serving_default signature from %s", signature) model_signature = tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY self.predict = self.model.signatures[model_signature] def _preprocess(self, data): images = [] for k, v in data.items(): for file_name, file_content in v.items(): image1 = Image.open(file_content) image1 = np.array(image1, dtype=np.float32) image1.resize((28, 28, 1)) images.append(image1) images = tf.convert_to_tensor(images, dtype=tf.dtypes.float32) preprocessed_data = images return preprocessed_data def _inference(self, data): return self.predict(data) def _postprocess(self, data): return { "result": int(data["output"].numpy()[0].argmax()) }