Search code examples
pythonnumpystreamlitonnxruntime

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


In one of datascience web app project, I designed an app to predict the type of plant disease. It contains onnx models. The prediction runs without an error standalone. But inside the streamlit code, it raises an error:

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

Did someone come across this kind of a situation?

enter image description here

This is the link to the project files: https://drive.google.com/drive/folders/1TVn9zRaJsoMUPz_6puaWk11LFkpvIyyl?usp=sharing (to run the project, after installing dependancies, run streamlit run webapp_plant_dis_clas.py, for now only model for the tomato exists. so only select it inside the web app to make a prediction.)


Solution

  • This has nothing to do with streamlit.

    Use the following to fix your issue.

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

    and this to get the max index.

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

    Output Prediction

    [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 run

    enter image description here

    Code

    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)}')