繁体   English   中英

深度学习模型不支持将字符串转换为浮点数

[英]cast string to float is not supported in Deep learning model

我正在尝试通过 Streamlit 部署 ML 模型,这是代码

import cv2
import numpy as np
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2,preprocess_input as mobilenet_v2_preprocess_input
from streamlit_option_menu import option_menu


tb_model = tf.keras.models.load_model(r"C:\Users\zahir\Desktop\Heart_Disease_prediction\Saved_model/tb_mdl.h5")
#img_model = tf.keras.models.load_model(r"C:\Users\zahir\Desktop\Heart_Disease_prediction\Saved_model/img_mdl.h5")

# Sidbar for Navigation

with st.sidebar:
    selected = option_menu('Coronary Artery Disease Prediction System',
                           
                           ['Predit by Filling Up Form',
                            'Predict Using Images'],
                           
                           icons = ['activity','heart'],
                           menu_icon="award", 
                           
                           default_index = 0)

#Page for Tabular Data
if (selected == 'Predit by Filling Up Form'):
    
    # page title
    st.title('Heart Disease Prediction Using Deep Learning')
    
    col1, col2, col3 = st.columns(3)
    
    with col1:
        age = st.text_input('Age')

    with col2:
        sex = st.text_input('Sex')
        
    with col3:
        cp = st.text_input('Chest Pain types')
        
    with col1:
        trestbps = st.text_input('Resting Blood Pressure')
        
    with col2:
        chol = st.text_input('Serum Cholestoral in mg/dl')
        
    with col3:
        fbs = st.text_input('Fasting Blood Sugar > 120 mg/dl')
        
    with col1:
        restecg = st.text_input('Resting Electrocardiographic results')
        
    with col2:
        thalach = st.text_input('Maximum Heart Rate achieved')
        
    with col3:
        exang = st.text_input('Exercise Induced Angina')
        
    with col1:
        oldpeak = st.text_input('ST depression induced by exercise')
        
    with col2:
        slope = st.text_input('Slope of the peak exercise ST segment')
        
    with col3:
        ca = st.text_input('Major vessels colored by flourosopy')
        
    with col1:
        thal = st.text_input('thal: 0 = normal; 1 = fixed defect; 2 = reversable defect')
        
        
     
     
    # code for Prediction
    heart_diagnosis = ''
    
    # creating a button for Prediction
    
    if st.button('Heart Disease Test Result'):
        inputs = (age, sex, cp, trestbps, chol, fbs, restecg,thalach,exang,oldpeak,slope,ca,thal)
        npArray = np.asarray(inputs)
        inReshaped = npArray.reshape(1,-1)
        heart_prediction = tb_model.predict(inReshaped)                          
        
        if (heart_prediction[0] == 1):
          heart_diagnosis = 'The person is having heart disease'
        else:
          heart_diagnosis = 'The person does not have any heart disease'
        
    st.success(heart_diagnosis)

我收到此错误

    Cast string to float is not supported [[node sequential/Cast (defined at Users\zahir\Desktop\TensorFlow-Streamlit-main\streamlit_host.py:87) ]] [Op:__inference_predict_function_8085] Function call stack: predict_function
Traceback:
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 554, in _run_script
    exec(code, module.__dict__)
File "C:\Users\zahir\Desktop\TensorFlow-Streamlit-main\streamlit_host.py", line 87, in <module>
    heart_prediction = tb_model.predict(inReshaped)
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
    return method(self, *args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1599, in predict
    tmp_batch_outputs = predict_function(iterator)
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
    result = self._call(*args, **kwds)
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\tensorflow\python\eager\def_function.py", line 846, in _call
    return self._concrete_stateful_fn._filtered_call(canon_args, canon_kwds)  # pylint: disable=protected-access
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\tensorflow\python\eager\function.py", line 1848, in _filtered_call
    cancellation_manager=cancellation_manager)
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\tensorflow\python\eager\function.py", line 1924, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\tensorflow\python\eager\function.py", line 550, in call
    ctx=ctx)
File "C:\ProgramData\Anaconda3\envs\MachineLearning\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)

我犯了一个基本错误,在从用户那里获取输入时,我正在转换所有输入的 Numpy 数组。 但是,不支持将字符串转换为浮点数的错误。 基本上,python 默认情况下接受字符串输入,我们必须手动将字符串转换为整数或浮点数。 幸运的是,Numpy 有一个内置函数可以将字符串转换为浮点数,所以,我在下面修改了一小段代码:

npArray = np.asarray(inputs).astype('float32')

这行代码修复了我的错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM