Python 基于JSON中base64输入的tf.estimator模型导出预测
我想为模型预测构建一个rest api我的模型接受json文件中的base64图像字符串,正如我在google cloud ML上测试的那样。它不接受来自android应用程序的预测请求,所以我尝试使用rest api。从python脚本来看,我的预测给了我太多的错误,只是在没有api的情况下进行本地测试 这是我的出口型号代码Python 基于JSON中base64输入的tf.estimator模型导出预测,python,tensorflow,machine-learning,keras,Python,Tensorflow,Machine Learning,Keras,我想为模型预测构建一个rest api我的模型接受json文件中的base64图像字符串,正如我在google cloud ML上测试的那样。它不接受来自android应用程序的预测请求,所以我尝试使用rest api。从python脚本来看,我的预测给了我太多的错误,只是在没有api的情况下进行本地测试 这是我的出口型号代码 import os import shutil import tensorflow as tf from tensorflow import keras HEIGH
import os
import shutil
import tensorflow as tf
from tensorflow import keras
HEIGHT = 48
WIDTH = 48
CHANNELS = 1
version = 'v1'
h5_model_path = os.path.join('model_4layer_2_2_pool yes.h5')
tf_model_path = os.path.join('D:/university/working/trying/Facial-
Expression-Recognition-master/tryMore')
estimator = keras.estimator.model_to_estimator(
keras_model_path=h5_model_path,
model_dir=tf_model_path)
def image_preprocessing(image):
image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(image, [HEIGHT, WIDTH],
align_corners=False)
image = tf.squeeze(image, axis=[0])
image = tf.cast(image, dtype=tf.uint8)
return image
#
以64字节为基数的图像
我的预测代码如下所示
import tensorflow as tf
import pickle
import os
import imgJSON
dir_path = os.path.dirname(__file__)
exported_path= os.path.join(dir_path, "models/json_b64/v1/1539157474")
model_input=imgJSON.img_bytes
global data
def main():
with tf.Session() as sess:
Model=tf.saved_model.loader.load(sess,
[tf.saved_model.tag_constants.SERVING], exported_path)
predictor= tf.contrib.predictor.from_saved_model(exported_path)
import json
with open("D:/university/working/trying/Facial-Expression-Recognition-
master/tryMore/test_json_b64.json") as f:
data = json.loads(f.read())
print(data["image_bytes"])
output_dict= predictor({"image_bytes":[model_input]})
print(" prediction is " , output_dict['scores'])
if __name__ == "__main__":
main()
我的错误消息如下:
InvalidArgumentError (see above for traceback): Expected image (JPEG, PNG, or GIF), got unknown format starting with '/9j/4AAQSkZJRgAB'
[[Node: map/while/DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=1, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](map/while/TensorArrayReadV3)]]
如何预测将base64字符串传递到相应的导出模型 嘿,你明白了吗?谢谢
InvalidArgumentError (see above for traceback): Expected image (JPEG, PNG, or GIF), got unknown format starting with '/9j/4AAQSkZJRgAB'
[[Node: map/while/DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=1, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](map/while/TensorArrayReadV3)]]