我正在测试我的大图像识别模型及其子代码,如下所示。理想情况下,它应该能够加载imagenet模型,然后显示" hello world"但是它会在行graph = K.get_session().graph
中引发错误,这是 RuntimeError:会话图是空的。在调用run()之前向图表添加操作。我已经尝试更改行的位置但是它不起作用。我想使用with.graph as default()
,因为我在主代码中使用它。我正在使用keras 2.09。
import dlib
import requests
import numpy as np
from skimage import io
from skimage.transform import resize
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
from keras import applications
from flask import Flask, jsonify, request, abort, make_response
app = Flask(__name__)
auth_token = 'WyIxYSDFg467YT.A3MmJlODcyODkzOGQzZjk4YzUiXQ.B5e5SgsDcaMgiRqx21Ydf8M'
top_model_weights_ethnicity = 'ethnicity.071217.23-0.28.hdf5'
img_width, img_height = 139, 139
confidence_ethnicity = '0.59'
detector = dlib.get_frontal_face_detector()
graph = K.get_session().graph
def get_pretrained_model():
with graph.as_default():
pretrained_model = applications.InceptionResNetV2(include_top=False, weights='imagenet',
input_shape=(img_width, img_height, 3))
return pretrained_model
with graph.as_default():
pretrained_model = get_pretrained_model()
@app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0')
答案 0 :(得分:0)
该错误是由于我在程序中使用的Keras版本已弃用。我将其升级到了最新版本,即2.2.4,为我解决了这个问题。