我是ML的新手,试图找到一些基于keras的有趣预测。我跟随AK Nain使用tensorflow + keras解决captcha图像的帖子,一切顺利。当我使用flask时,构建了一个简单的api,可以成功地预测输出。问题是,我想扩展这个api的性能。目前,每次预测需要40毫秒,所以我可以使用这个api实现每秒20-25次预测。我需要使用更多的cpu内核和内存来扩展它,以获得最佳性能。所以在搜索了大约7天后,我接近在Tensorflow服务Docker镜像上运行它。这里出现了问题,我无法得到tensorflow服务的预测特征,因为它期望一个不同的输入,但我没有分析它的实际输入。这里我附加了flask api的工作预测,
img = flask.request.files["image"].read()
# 2. Decode and convert to grayscale
img = tf.io.decode_png(img, channels=1)
# 3. Convert to float32 in [0, 1] range
img = tf.image.convert_image_dtype(img, tf.float32)
# 4. Resize to the desired size
img = tf.image.resize(img, [img_height, img_width])
# 5. Transpose the image because we want the time
# dimension to correspond to the width of the image.
img = tf.transpose(img, perm=[1, 0, 2])
img = tf.expand_dims(img, axis=0)
preds = prediction_model.predict(img)
pred_text = decode_batch_predictions(preds)
# indicate that the request was a success
data["success"] = True
data["text"]=pred_text[0]
这个flask api服务器提供实际输出。我将相同的模型导出到docker映像,并稍微更改此api以工作,
img = flask.request.files["image"].read()
# 2. Decode and convert to grayscale
img = tf.io.decode_png(img, channels=1)
# 3. Convert to float32 in [0, 1] range
img = tf.image.convert_image_dtype(img, tf.float32)
# 4. Resize to the desired size
img = tf.image.resize(img, [img_height, img_width])
# 5. Transpose the image because we want the time
# dimension to correspond to the width of the image.
img = tf.transpose(img, perm=[1, 0, 2])
img = tf.expand_dims(img, axis=0)
payload = json.dumps({"signature_name": "serving_default", "instances":
[{"image":img.numpy().tolist()}]})
r = requests.post(KERAS_REST_API_URL, data=payload,headers=headers).json()
失败,并显示以下错误,
{'error': 'Failed to process element: 0 of 'instances' list. JSON object: {n "image": [n [n [n [n 1.0n ],n [n 1.0n ],n [n 1.0n ],n [n 1.0n ],n [n 1.0n ],n [n 0.03921568766236305n ],n [n 1.0n ],n [n 1.0n ],n
[n 1.0n ],n [n 1.0n ],n [n 1.0n
],n [n 1.0n ],n [n 1.0n ],n [n
1.0n ],n [n 1.0n ],n [n 0.03921568766236305n ],n [n 1.0n ],n [n 1.0n ],n
我的模型元数据是这样的
{"model_spec":{"name":"my_model","signature_name":"","version":"1"},"metadata":
{"signature_def":{"signature_def":{"serving_default":{"inputs":{"label":
{"dtype":"DT_FLOAT","tensor_shape":{"dim":[{"size":"-1","name":""},
{"size":"-1","name":""}],"unknown_rank":false},"name":"serving_default_label:0"},"image":
{"dtype":"DT_FLOAT","tensor_shape":{"dim":[{"size":"-1","name":""},{"size":"100","name":""},
{"size":"36","name":""},
{"size":"1","name":""}],"unknown_rank":false},"name":"serving_default_image:0"}},"outputs":
{"ctc_loss":{"dtype":"DT_FLOAT","tensor_shape":{"dim":[{"size":"-1","name":""},
{"size":"25","name":""},
{"size":"12","name":""}],"unknown_rank":false},"name":"StatefulPartitionedCall:0"}},
"method_name":"tensorflow/serving/predict"},"__saved_model_init_op":{"inputs":{},"outputs":
{"__saved_model_init_op":{"dtype":"DT_INVALID","tensor_shape":{"dim":
[],"unknown_rank":true},"name":"NoOp"}},"method_name":""}}}}}
你能告诉我我错在哪里吗?
因此,使用tensorflow服务,您必须提供保存的模型本身。这就是为什么将管道集成到模型中如此重要的原因。这里有两个地方,你可以了解tensorflow服务架构,以及如何实际服务模型。
为什么架构是这样的?根据我的理解,这是为了执行图本身的所有内容而设计的,所以你不会有python的延迟。