简体   繁体   English

使用keras加载模型时如何解决错误

[英]How to solve error while loading model with keras

I have a flask app, and i am using tensorflow and keras to load the model that i am going to use.Below is a code snippet我有一个烧瓶应用程序,我正在使用 tensorflow 和 keras 来加载我将要使用的模型。下面是一个代码片段

from tensorflow.keras.models import Model , load_model
from keras.preprocessing import image

# Flask utils
from flask import Flask, redirect, url_for, request, render_template
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer

app = Flask(__name__)


Model= load_model('models/model_pro1.h5') 

so i then deploy the flask app on heroku and it builds successfully, but when i check the logs i see that i get an error image for the heroku logs I have tried installing both tensorflow and keras with out a particular version in the requirements.txt file but not getting it solved.所以我然后在heroku上部署flask应用程序并且它成功构建,但是当我检查日志时我看到我得到了heroku日志的错误图像我已经尝试安装tensorflow和keras而没有在requirements.txt中的特定版本文件,但没有得到解决。 I want to know what i am not doing right, and thanks in advance.我想知道我做错了什么,并提前致谢。 I am using code from this github repo github repo我正在使用这个 github repo github repo 中的代码

Beginning with Python 3, all strings are unicode by default.从 Python 3 开始,所有字符串默认都是 unicode。 So i think your dev environment, where you built and trained the models is running python2 while the heroku env has python3.所以我认为你的开发环境,你构建和训练模型的地方运行的是 python2,而 heroku env 运行的是 python3。

I'll give an instance of saving a model and then loading it我将给出一个保存模型然后加载它的实例

import tensorflow as tf
from tensorflow import keras
model.save(./abc.h5  # saves in h5 format)
model.save(./defg)#defg is a folder having pb file 

To load the saved model,要加载保存的模型,

new_model = tf.keras.models.load_model('./abc.h5') #for loading h5 model

or或者

new_model = tf.keras.models.load_model('defg/) # for loading pb model

I think your syntax of load_model is the mistake you committed我认为你的 load_model 语法是你犯的错误

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

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