深度学习模型不支持强制转换字符串为float



我正试图通过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:UserszahirDesktopHeart_Disease_predictionSaved_model/tb_mdl.h5")
#img_model = tf.keras.models.load_model(r"C:UserszahirDesktopHeart_Disease_predictionSaved_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 UserszahirDesktopTensorFlow-Streamlit-mainstreamlit_host.py:87) ]] [Op:__inference_predict_function_8085] Function call stack: predict_function
Traceback:
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagesstreamlitscriptrunnerscript_runner.py", line 554, in _run_script
exec(code, module.__dict__)
File "C:UserszahirDesktopTensorFlow-Streamlit-mainstreamlit_host.py", line 87, in <module>
heart_prediction = tb_model.predict(inReshaped)
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagestensorflowpythonkerasenginetraining.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagestensorflowpythonkerasenginetraining.py", line 1599, in predict
tmp_batch_outputs = predict_function(iterator)
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagestensorflowpythoneagerdef_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagestensorflowpythoneagerdef_function.py", line 846, in _call
return self._concrete_stateful_fn._filtered_call(canon_args, canon_kwds)  # pylint: disable=protected-access
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagestensorflowpythoneagerfunction.py", line 1848, in _filtered_call
cancellation_manager=cancellation_manager)
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagestensorflowpythoneagerfunction.py", line 1924, in _call_flat
ctx, args, cancellation_manager=cancellation_manager))
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagestensorflowpythoneagerfunction.py", line 550, in call
ctx=ctx)
File "C:ProgramDataAnaconda3envsMachineLearninglibsite-packagestensorflowpythoneagerexecute.py", line 60, in quick_execute
inputs, attrs, num_outputs)

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

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

这行代码修复了我的错误

相关内容

  • 没有找到相关文章

最新更新