簡體   English   中英

在 Heroku 上部署 Streamlit Web 應用程序時出現 OSError

[英]OSError when deploying streamlit web application on Heroku

我使用 streamlit 構建了一個 Web 應用程序並嘗試將它部署在 heroku 上。 我收到這個錯誤說

OSError:SavedModel 文件不存在於:covid_model.hdf5/{saved_model.pbtxt|saved_model.pb}

該模型以及所有其他文件都在我的 github 存儲庫中,並且我已在 heroku 中啟用了自動部署。 webapp 在本地主機上運行良好。

我嘗試更改模型的路徑,在開頭包含一個“/”,但錯誤仍然存​​在。

這是我的github 存儲庫,其中發布了所有代碼、模型和數據。

編輯:按照 James 的建議,從我用來運行 webapp 的主文件中添加代碼:

model = tf.keras.models.load_model('covid_model.hdf5')
def import_and_predict(image_data, model):
    
        size = (160,160)    
        image = ImageOps.fit(image_data, size, Image.ANTIALIAS)
        image = image.convert('RGB')
        image = np.asarray(image)
        image = (image.astype(np.float32) / 255.0)

        img_reshape = image[np.newaxis,...]

        prediction = model.predict(img_reshape)
        
        return prediction

file = st.file_uploader("Please upload an image file", type=["jpg", "png"])
if file is None:
    st.text("You haven't uploaded an image file")
else:
    image = Image.open(file)
    st.image(image, use_column_width=True)
    prediction = import_and_predict(image, model)
    if np.argmax(prediction) == 0:
            st.write("Coronavirus Positive!")
    elif np.argmax(prediction) == 1:
            st.write("Coronavirus Negative!")

    st.text("Probability (0: Corona +, 1: Corona -)")
    st.write(prediction)

我只需將以下代碼從此 app.py 文件移動到我編寫用於構建模型covid_model.hdf5的代碼的 .py 文件即可解決此錯誤

model = tf.keras.models.load_model('covid_model.hdf5')

def import_and_predict(image_data, model):
    size = (160,160)    
    image = ImageOps.fit(image_data, size, Image.ANTIALIAS)
    image = image.convert('RGB')
    image = np.asarray(image)
    image = (image.astype(np.float32) / 255.0)

    img_reshape = image[np.newaxis,...]

    prediction = model.predict(img_reshape)
    
    return prediction

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM