深度学习模型中不支持将字符串变成浮动

发布于 2025-02-11 07:45:17 字数 5060 浏览 4 评论 0原文

代码

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)

我正在尝试通过简化部署ML模型,这是我遇到此错误的

    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)

I am trying to deployee the ML model through Streamlit, here is the code

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)

I am getting this error

    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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2025-02-18 07:45:17

我犯了一个基本错误,在从用户那里获取输入时,我正在转换所有输入numpy数组。但是,获取错误的错误,不支持将字符串投入到浮动。基本上,Python默认情况下会在字符串中输入输入,我们必须将字符串施放到整数或浮动。幸运的是,numpy具有一个内置功能,可以将字符串转换为float,因此,我在下面修改了一些代码:

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

这条代码已修复我的错误

I was committing a basic mistake, while taking inputs from the user, I was converting all the inputs Numpy Array. But, getting an error that 'cast string to float is not supported. Basically, python by default takes inputs in string and we have to cast string to integer or float manually. Fortunately, Numpy have a built-in function to convert string to float, so, I amend a little piece of code below:

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

this line of code fixed my error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文