我使用以下Flask应用程序作为API,我将图像发布到keras模型中,以将其分类为两个图像之一。我发布的图像(通过curl)是64px x 64px的图像,模型使用了相同大小的图像进行训练。每次通过curl将图像发送到API时,都会收到调整大小错误:
from flask import Flask, jsonify, request
import numpy as np
import PIL
from PIL import Image
from keras.models import load_model
import keras
import tensorflow as tf
app = Flask(__name__)
def auc(y_true, y_pred):
auc = tf.metrics.auc(y_true, y_pred)[1]
keras.backend.get_session().run(tf.local_variables_initializer())
return auc
model = load_model('image_model_2.h5',custom_objects={'auc': auc})
@app.route('/predict', methods=["POST"])
def predict_image():
# Preprocess the image so that it matches the training input
image = request.files['file']
image = Image.open(image)
image = np.asarray(image.resize((64,64)))
image = image.reshape(1,1,64,64)
# Use the loaded model to generate a prediction.
pred = model.predict(image)
# Prepare and send the response.
digit = np.argmax(pred)
prediction = {'digit':int(digit)}
return jsonify(prediction)
if __name__ == "__main__":
app.run()
这是curl命令:
curl -F 'file=@small_cat.png' 127.0.0.1:5000/predict
这是错误:
ValueError: cannot reshape array of size 12288 into shape (1,1,64,64)