我无法使用 Keras CNN (VGGNet) 模型执行预测。它是一个多类分类,以 96x96x3 的图像张量作为输入,产生大小为 114(类)的概率向量。它被 Google ML Engine 接受为有效模型,并且预测输入 image.json 格式正确(与张量有一行),但调用 gcloud ml-engine predict 会出现以下错误:
"error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.INVALID_ARGUMENT, details=\"您必须为占位符张量 'Placeholder_1' 提供一个值,其 dtype 为 float 和 shape [?,114]\n\t [[节点:Placeholder_1 = Placeholderdtype=DT_FLOAT, shape=[?,114], _device=\"/job:localhost/replica:0/task:0/device:CPU:0\"]]\")"
我的预测输入image.json
包含
{"x": [ [ [ [ 1.0, 1.0, 1.0 ], ..., [ 1.0, 1.0, 1.0 ] ] ] ]}
生成 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])}
)
我在这里想念什么?有没有更简单的工作方式?该模型也可作为 .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")
感谢您的帮助!