【发布时间】:2020-07-19 20:07:59
【问题描述】:
我目前正在使用 Flask 来结合后端和前端表单图像分类。我还使用 .h5 文件来预测输出。输出不同,完全错误。输出应该是预测概率。代码如下:
def upload():
if request.method == 'POST':
# Get the file from post request
f = request.files['file']
# Save the file to ./uploads
basepath = os.path.dirname(__file__)
file_path = os.path.join(
basepath, 'uploads', secure_filename(f.filename))
f.save(file_path)
MODEL_ARCHITECTURE = 'model_adam_01.json'
MODEL_WEIGHTS = 'model_50_eopchs_adam_01.h5'
json_file = open(MODEL_ARCHITECTURE)
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights(MODEL_WEIGHTS)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
prediction = model_predict(file_path, model)
print("I think that is ")
print(prediction)
# print('I think that is {}.'.format(predicted_class.lower()))
return str(prediction)
下面是model_predict函数,我在其中传递了图像路径和模型
def model_predict(img_path, model):
'''
Args:
-- img_path : an URL path where a given image is stored.
-- model : a given Keras CNN model.
'''
IMG = image.load_img(img_path)
print(type(IMG))
IMG_ = np.asarray(IMG)
print(type(IMG_))
print(IMG_.shape)
IMG_ = prepare(IMG_)
print(IMG_.shape)
#print(model)
prediction = model.predict(IMG_)
print(prediction.shape)
return str(prediction)
以下是我得到的输出:
I think that is
[[0.]]
为什么会出现这个问题?我正在使用 keras 2.3.1 和 tesorflow 1.15.2
【问题讨论】:
标签: python tensorflow machine-learning image-processing keras