首页 > 解决方案 > 使用 keras 模型检查输入时出错

问题描述

我是训练模型,将模型保存在磁盘上,使用模型时出错。

Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (150, 150, 3)

火车型号:

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3))

使用型号:

model = load_model(os.path.join('models', 'myModel.h5'))
model.predict(img) # img - OpenCV image

标签: kerasmodelpython-3.6

解决方案


用这个:

import numpy as np
model.predict(img[np.newaxis, :, :, :])

推荐阅读