为什么streamlit抛出“ValueError:在配置文件中找不到模型”?

2024-10-06 16:45:24 发布

您现在位置:Python中文网/ 问答频道 /正文

`ValueError: No model found in config file.

Traceback:
File "c:\users\makje\anaconda3\envs\tensorflow\lib\site-packages\streamlit\script_runner.py", line 338, in _run_script
exec(code, module.__dict__)

File "C:\Users\makje\Documents\CODE\Python\A.I.(intern)\major.py", line 8, in <module>
model = tf.keras.models.load_model("C:\\Users\\makje\\Documents\\CODE\\Python\\A.I(intern)\\malay_model.hdf5")

File "c:\users\makje\anaconda3\envs\tensorflow\lib\site- packages\tensorflow\python\keras\saving\save.py", line 182, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)

File "c:\users\makje\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 175, in load_model_from_hdf5
raise ValueError('No model found in config file.')`

这些是在得出任何结论之前需要知道的事情

  1. 我在jupyter笔记本中创建了一个模型
  2. 它100%工作(准确预测)
  3. 模型对象保存在本地“C”驱动器中
  4. 我在jupyter笔记本中创建了一个Streamlight web应用程序

给定下面的Streamlight代码\

%%writefile dvc.py
import streamlit as st
import tensorflow as tf
import numpy as np
import streamlit as st 
from PIL import Image
import requests
from io import BytesIO

st.set_option('deprecation.showfileUploaderEncoding', False)
st.title("Dogs and Cats Classification")
st.text("Provide an Image for image classification")

@st.cache(allow_output_mutation=True)
def load_model():
  model = tf.keras.models.load_model('C:\\Users\\makje\\Documents\\CODE\\Python\\A.I(intern)\\model.h5')
  return model

with st.spinner('Loading Model Into Memory....'):
  model = load_model()

def scale(image):
  image = tf.cast(image, tf.float32)
  image /= 255.0

  return tf.image.resize(image,[128,128])

def decode_img(image):
  img = tf.image.decode_jpeg(image, channels=3)
  img = scale(img)
  return np.expand_dims(img, axis=0)

path = st.text_input('Enter Image URL to classify...','https://www.thesprucepets.com/thmb/fkocatcypSHCKeezdfPdGXOi3FU=/1333x1000/smart/filters:no_upscale()/top-friendliest-dog-breeds-4691511_hero-5c6a918dcf56409c888d78b0fac82d18.jpg')

if path is not None:
  content = requests.get(path).content

  st.write("Predicted Class")
  with st.spinner('Classifying....'):
    label = np.argmax(model.predict(decode_img(content)),axis=1)
    st.write(classes[label[0]])
    
 st.write("")
 image = Image.open(BytesIO(contenttent))
 st.image(image, caption='Classifying Dog/Cat Image', use_column_width=True)

Tags: inpyimageimportimgmodeltftensorflow