导出模型的完整代码:(我已经训练过它,现在从权重文件加载)
def cnn_layers(inputs):
conv_base= keras.applications.mobilenetv2.MobileNetV2(input_shape=(224,224,3), input_tensor=inputs, include_top=False, weights='imagenet')
for layer in conv_base.layers[:-200]:
layer.trainable = False
last_layer = conv_base.output
x = GlobalAveragePooling2D()(last_layer)
x= keras.layers.GaussianNoise(0.3)(x)
x = Dense(1024,name='fc-1')(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.advanced_activations.LeakyReLU(0.3)(x)
x = Dropout(0.4)(x)
x = Dense(512,name='fc-2')(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.advanced_activations.LeakyReLU(0.3)(x)
x = Dropout(0.3)(x)
out = Dense(10, activation='softmax',name='output_layer')(x)
return out
model_input = layers.Input(shape=(224,224,3))
model_output = cnn_layers(model_input)
test_model = keras.models.Model(inputs=model_input, outputs=model_output)
weight_path = os.path.join(tempfile.gettempdir(), 'saved_wt.h5')
test_model.load_weights(weight_path)
export_path='export'
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import utils
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import build_signature_def, predict_signature_def
from tensorflow.contrib.session_bundle import exporter
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'image': test_model.input},
outputs={'prediction': test_model.output})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={'predict': signature})
builder.save()其输出(dir 1有saved_model.pb和models dir):
python /tensorflow/python/tools/saved_model_cli.py show --dir /1 --all是
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
signature_def['predict']:
The given SavedModel SignatureDef contains the following input(s):
inputs['image'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 224, 224, 3)
name: input_1:0
The given SavedModel SignatureDef contains the following output(s):
outputs['prediction'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 107)
name: output_layer/Softmax:0
Method name is: tensorflow/serving/predict接受字符串:代码是为(224, 224, 3) numpy数组编写的。因此,我对上述代码所做的修改如下:
_bytes传递时,应该将_bytes添加到输入中。所以,predict_signature_def(inputs={'image':......
变到
predict_signature_def(inputs={'image_bytes':.....
type(test_model.input)是:(224, 224, 3)和dtype: DT_FLOAT。所以,signature = predict_signature_def(inputs={'image': test_model.input},.....改为(reference)
temp = tf.placeholder(shape=[None], dtype=tf.string)
signature = predict_signature_def(inputs={'image_bytes': temp},.....
编辑:
使用请求发送的代码是:(如注释中提到的)
encoded_image = None
with open('/1.jpg', "rb") as image_file:
encoded_image = base64.b64encode(image_file.read())
object_for_api = {"signature_name": "predict",
"instances": [
{
"image_bytes":{"b64":encoded_image}
#"b64":encoded_image (or this way since "image" is not needed)
}]
}
p=requests.post(url='http://localhost:8501/v1/models/mnist:predict', json=json.dumps(object_for_api),headers=headers)
print(p)我得到了<Response [400]>错误。我认为我发送的方式没有错误。需要在导出模型的代码中更改一些内容,特别是在
temp = tf.placeholder(shape=[None], dtype=tf.string)。
发布于 2018-07-05 10:10:05
查看您提供的文档,您想要做的是获取图像并将其发送到API中。如果您对图像进行编码,则可以轻松地以文本格式传输图像,base64基本上是标准的。因此,我们要做的是创建一个json对象,在正确的位置将图像作为base64,然后将这个json对象发送到REST。python有一个请求库,这使得将python字典作为JSON发送非常容易。
因此,获取图像,对其进行编码,将其放入字典并使用请求将其发送出去:
import requests
import base64
encoded_image = None
with open("image.png", "rb") as image_file:
encoded_image = base64.b64encode(image_file.read())
object_for_api = {"signature_name": "predict",
"instances": [
{
"image": {"b64": encoded_image}
}]
}
requests.post(url='http://localhost:8501/v1/models/mnist:predict', json=object_for_api)您也可以将numpy数组编码到JSON中,但是API文档似乎并不需要这样做。
发布于 2018-07-16 13:57:33
两个边注:
tf.saved_model.simple_savemodel_to_estimator很方便。saved_model_cli的输出显示输入和输出的外部维度都是None ),但是发送浮点数的JSON数组是相当低效的最后一点,修改代码以完成图像解码服务器端的操作通常更容易,因此您将通过有线发送一个base64编码的JPG或PNG,而不是一个浮动数组。以下是Keras的一个示例(我计划用更简单的代码更新这个答案)。
https://stackoverflow.com/questions/51187140
复制相似问题