【问题标题】:how to do model prediction using python in matlab如何在matlab中使用python进行模型预测
【发布时间】:2021-07-20 23:32:55
【问题描述】:

这是 Python 代码,它在 Python 中运行良好:

import keras
from keras.models import load_model
from keras.preprocessing import image
import json
import numpy as np

filepath = 'model2.h5'
test_img = 'falling14145853.png'
model = keras.models.load_model(filepath)

img = image.load_img(test_img, target_size=(150, 150))

x = image.img_to_array(img)
x = np.expand_dims(x, axis = 0)

images = np.vstack([x])
classes = model.predict(images, batch_size = 10)

_mapping = ["falling", "sitting", "standing"]
a = np.concatenate(classes, axis=None).astype(int).tolist()
idx = np.argmax(a[0])
prediction = _mapping[idx]
response_json = {"Prediction" : prediction}
data_in_json = json.dumps(response_json)
with open("prediction.json", "w") as out:
    out.write(data_in_json)

这是在 Matlab 中:

system('test_model.py'); %doing prediction

这是 Matlab 输出,我尝试运行 python 以使用 CV 进行捕获,并且在 Matlab 中运行良好,但是当我尝试运行包含 keras 的 python 代码时,它显示此错误:

Traceback(最近一次调用最后一次): 文件“E:\angie\test_model.py”,第 1 行,在 导入 keras ImportError: 没有名为 keras 的模块

【问题讨论】:

  • Matlab 支持 Python 2.7、3.7 和 3.8。你的 Python 代码与那个版本不同吗?您可能正在使用一种具有 keras 的解释器和一种没有(在 Matlab 中)的解释器。
  • 我使用的是 python 3.8,我已经使用 pyenv 在 matlab 中进行了检查,它显示相同

标签: python matlab keras prediction


【解决方案1】:

通过添加'python. 于是代码就变成了

system('python test_model.py');

就这样。

【讨论】:

    猜你喜欢
    • 2016-03-08
    • 2018-10-15
    • 2020-04-11
    • 2017-12-19
    • 2021-07-01
    • 2017-08-10
    • 2020-08-24
    • 2019-08-05
    • 2018-11-11
    相关资源
    最近更新 更多