我目前正在开展一个项目,涉及训练回归模型,保存它,然后加载它以使用该模型进行进一步的预测。但是我有一个问题。每次我对图像进行 model.predict 时,它都会给出相同的预测。我不完全确定问题是什么,也许是在训练阶段,或者我只是做错了什么。
我正在关注本教程 https://www.pyimagesearch.com/2019/01/28/keras-regression-and-cnns/
所有的文件都在这个里面github 仓库 https://github.com/tomercahal/statispic_ML
以下是代码中的一些内容:
(这部分是训练模型并保存)
model = create_cnn(400, 400, 3, regress=True)
opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="mean_absolute_percentage_error", optimizer=opt)
model.fit(X, Y, epochs=70, batch_size=8)
model.save("D:/statispic2/final-statispic_model.hdf5")
下一个代码部分是加载模型并进行预测。
model = load_model("D:/statispic2/statispic_model.hdf5") # Loading the model
prediction = model.predict(images_ready_for_prediction) #images ready for prediction include a numpy array
#that is loaded with the images just like I loaded them for the training stage.
print(prediction_list)
尝试后,这是模型的输出预测:
[[0.05169942] # I gave it 5 images as parameters
[0.05169942]
[0.05169942]
[0.05169942]
[0.05169942]]
如果有任何不清楚的地方,或者您想查看更多代码,请告诉我。