开发环境
- Ubuntu 18.x
- Docker 18.x
- Tensorflow 1.x
构建模型signature
"""
tf.saver.restore(...)
"""
# 上面省略了模型的加载的代码
#设置导出的路径
export_path_base = "serving"
export_version = '1';
export_path = os.path.join(export_path_base,export_version)
print('Exporting trained model to', export_path)
builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_path)
print('step1 => builder created successfully')
# 构建signature_def_map,参数为模型的输入与输出
tensor_info_input = tf.compat.v1.saved_model.utils.build_tensor_info(self.model.input)
tensor_info_output = tf.compat.v1.saved_model.utils.build_tensor_info(self.model.out)
# 定义模型的类型
prediction_signature = (
tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
inputs={'images': tensor_info_input},
outputs={'result': tensor_info_output},
method_name=tf.saved_model.PREDICT_METHOD_NAME))
print('step2 => prediction_signature created successfully')
builder.add_meta_graph_and_variables(
# tags:SERVING,TRAINING,EVAL,GPU,TPU
self.sess, [tf.saved_model.SERVING],
signature_def_map={'predict_images': prediction_signature,
})
print('step3 => builder successfully add meta graph and variables\nNext is to export model...')
#builder.save(as_text=True)
builder.save()
print('Done exporting!')
导出的文件结构
├── serving //模型文件
│ ├──1 //版本号
│ │ ├── saved_model.pb
│ │ ├── variables
│ │ │ ├── variables
│ │ │ ├── variables.data-00000-of-00001
│ │ │ ├── variables.index
用docker Serving自己的模型
略
使用pyhon的requests调用模型
import requests
import skimage.io as skio
import numpy as np
import matplotlib.pyplot as plt
SERVER_URL = 'http://*.*.*.*:8501/v1/models/super-resolution:predict'
class Serving(object):
def __init__(self):
self.img_size = 48
self.scale = 3
def main(self):
# 图片路径,只能是灰度图像
img = skio.imread('./testImage/Set5/butterfly_GT.bmp', as_gray=True);
img_narray = np.asarray(img)
img_narray = img_narray[0:48,0:48]
# 模型能接受的图片大小只能是[48,48],且格式为[1,48,48,1]
img_narray = img_narray.reshape([1,img_narray.shape[0],img_narray.shape[1], 1])
#构建请求数据,这里的格式很容易错,如果错了,会说:400 Client Error: Bad Request for url:****
predict_request = '{"signature_name": "predict_images", "instances":%s }' % img_narray.tolist()
response = requests.post(SERVER_URL, data=predict_request)
response.raise_for_status()
#获取预测结果
prediction = response.json()['predictions']
prediction_img = np.array(prediction)
plt.imshow(prediction_img.reshape([144, 144]), 'gray')
plt.show()
if __name__ == '__main__':
P_get = requests.get("http://101.37.14.171:8501/v1/models/super-resolution")
print(P_get.content)
s = Serving()
s.main()
补充:使用keras导出.pb格式的模型
keras原本的模型保存格式是h5格式的,
# 导入加载模型的方法load_model和模型编译使用的优化器类Adam
from keras.models import load_model
from keras.optimizers import Adam
import tensorflow as tf
from keras import backend as K
import os
# 加载已经训练好的ResNet50图片分类模型的模型文件
model_filePath = './my_model_resnet.h5'
model = load_model(model_filePath)
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.001),
metrics=['accuracy'])
#模型导出到Tensorflow Serving的代码
export_path_base = "serving"
export_version = '1';
export_path = os.path.join(export_path_base,
export_version)
print('Exporting trained model to', export_path)
builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_path)
# Build the signature_def_map.
tensor_info_input = tf.compat.v1.saved_model.utils.build_tensor_info(model.input)
tensor_info_output = tf.compat.v1.saved_model.utils.build_tensor_info(model.output)
# 定义模型的类型
prediction_signature = (
tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
inputs={'images': tensor_info_input},
outputs={'result': tensor_info_output},
method_name=tf.saved_model.PREDICT_METHOD_NAME))
print('step3 => prediction_signature created successfully')
builder.add_meta_graph_and_variables(
# tags:SERVING,TRAINING,EVAL,GPU,TPU
K.get_session(), [tf.saved_model.SERVING],
signature_def_map={
'predict_images': prediction_signature,
})
print('step4 => builder successfully add meta graph and variables\nNext is to export model...')
#builder.save(as_text=True)
builder.save()
print('Done exporting!')