简体   繁体   English

在Google ML Engine上部署.model .json和.h5的Keras / Tensorflow CNN的最简单方法是什么?

[英]What is the simplest way of deploying a Keras/Tensorflow CNN available as .model .json and .h5 on Google ML Engine?

I have trouble executing predictions using a Keras CNN (VGGNet) model. 我使用Keras CNN(VGGNet)模型执行预测时遇到问题。 It is a multi-class-classification, taking a 96x96x3 image tensor as input, yielding a probability vector of size 114 (classes). 它是一个多类分类,以96x96x3图像张量作为输入,产生一个大小为114(类)的概率向量。 It is accepted by Google ML Engine as a valid model and the prediction input image.json is in the correct format (one line with tensor), but calling the gcloud ml-engine predict gives the following error: 它被Google ML Engine接受为有效模型,预测输入image.json的格式正确(一行有张量),但调用gcloud ml-engine预测会出现以下错误:

"error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\\"You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,114]\\n\\t [[Node: Placeholder_1 = Placeholderdtype=DT_FLOAT, shape=[?,114], _device=\\"/job:localhost/replica:0/task:0/device:CPU:0\\"]]\\")" “错误”:“预测失败:模型执行期间出错:AbortionError(代码= StatusCode.INVALID_ARGUMENT,details = \\”您必须为占位符张量'Placeholder_1'提供dtype float和shape [?,114] \\ n \\ t的值[[Node:Placeholder_1 = Placeholderdtype = DT_FLOAT,shape = [?,114],_ device = \\“/ job:localhost / replica:0 / task:0 / device:CPU:0 \\”]] \\“)”

My prediction input image.json contains 我的预测输入image.json包含

{"x": [ [ [ [ 1.0, 1.0, 1.0 ], ..., [ 1.0, 1.0, 1.0 ] ] ] ]}

and the code generating the save_model.pb file is 生成save_model.pb文件的代码是

def build_graph(x):

  model = load_model("my-model.model")
  labels = pickle.loads(open("labels.pickle", "rb").read())

  # classify the input image
  probabilities = model.predict(x)

  outputs = tf.convert_to_tensor(probabilities)
  saver = tf.train.Saver()

  return outputs, saver

image_path = "testset/testimage.png"
# preprocess the image for classification
image = cv2.imread(image_path)
image = cv2.resize(image, (96, 96))
image = image.astype("float") / 255.0
image = img_to_array(image)
image = np.expand_dims(image, axis=0)

# Do training
with tf.Graph().as_default() as prediction_graph:
  x = image
  outputs = tf.placeholder(tf.float32, shape=[None, 114])
  outputs, saver = build_graph(x)

with tf.Session(graph=prediction_graph) as sess:
  sess.run([tf.local_variables_initializer(), tf.tables_initializer()])
  x = tf.placeholder(tf.float32, shape=[None, 96, 96, 3])
  sess.run(outputs, {x: image})

# export model
export_dir = "export3"
tf.saved_model.simple_save(
    sess,
    export_dir,
    inputs={"x": tf.placeholder(tf.float32, shape=[None, 96, 96, 3])},
    outputs={"y": tf.placeholder(tf.float32, shape=[None, 114])}
)

What am I missing here? 我在这里错过了什么? Is there a simpler working way? 有更简单的工作方式吗? The model is also available as .json and .h5 file generated by 该模型也可用.json和.h5文件生成

# serialize model to JSON
model_json = model.to_json()
with open("my-model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("my-model.h5")

Thanks for helping! 谢谢你的帮助!

Somehow, the expected output shape of [None, 114] is not fulfilled. 不知何故,[None,114]的预期输出形状未得到满足。

I understand that after expand_dims, the shape for image is [1,96,96]. 据我所知,在expand_dims之后,图像的形状是[1,96,96]。 But since I don't know what do you have in your model, I can't know how do you get a probability vector of size 114. 但由于我不知道你的模型中有什么,我不知道你怎么得到一个大小为114的概率向量。

A vague suggestion, considering the prior clarification, is to check if you're using the tf.Variable class in your model and if you aren't changing the shape properly; 考虑到事先的澄清,一个模糊的建议是检查你是否在你的模型中使用tf.Variable类,如果你没有正确改变形状; since tf.Variable restricts your ability to change the shape of the variable once it has been created. 因为tf.Variable限制了你在变量创建后改变形状的能力。

If this isn't the case, provide more details regarding your model. 如果不是这种情况,请提供有关您的型号的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM