Onnx 推理在 streamlit 框架内使用 numpy float32 数据类型引发错误

Onnx inference throws an error with numpy float32 datatype inside the streamlit framework

在一个数据科学网络应用程序项目中,我设计了一个应用程序来预测植物病害的类型。它包含 onnx 模型。独立预测 运行s 没有错误。但是在 streamlit 代码中,它引发了一个错误:

UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U23'), dtype('float32')) -> None**

有人遇到过这种情况吗?

这是 link 到项目文件:https://drive.google.com/drive/folders/1TVn9zRaJsoMUPz_6puaWk11LFkpvIyyl?usp=sharing(到 运行 项目,在安装依赖项后,运行 streamlit run webapp_plant_dis_clas.py,暂时只有西红柿的模型存在。因此只有 select 它在网络应用程序中进行预测。)

这与streamlit无关。

使用以下方法解决您的问题。

prediction = pred_out(img, model_selector, plant_model_dictionary)[0]

这是为了获得最大索引。

print(prediction.argmax(axis=0))  # 6

输出预测

[2.8063682e-14 3.1059124e-05 5.7825161e-11 8.3977110e-09 2.5989549e-13
 1.2324781e-04 9.9980527e-01 7.5968347e-17 4.0455303e-05 3.2742336e-09]

Streamlit 运行

代码

if conf is True: #predict button is pressed, you need a way of identifying is it pressed or not
    # prediction - pred_out(img, model_selector, plant_model_dictionary)[0]
    prediction = pred_out(img, model_selector, plant_model_dictionary)[0]
    st.write(f'prediction: {prediction}')
    # max_ind = np.where(prediction==max)[0][0]
    # print(max_ind)
    st.write(f'prediction max index: {prediction.argmax(axis=0)}')