这是我用来将keras模型导出为tensorflow服务格式的代码。导出的模型已在tensorflow服务中成功加载(没有任何警告或错误)。但是,当我使用客户端向服务器发出请求时,出现FailedPrecondition错误。
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.FAILED_PRECONDITION
details = "Attempting to use uninitialized value block11_sepconv2_bn/moving_mean
import sys
import os
import tensorflow as tf
from keras import backend as K
from keras.models import Model
from keras.models import load_model
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import utils
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import
build_signature_def, predict_signature_def
from tensorflow.contrib.session_bundle import exporter
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
config = tf.ConfigProto( device_count = {'GPU': 2 , 'CPU': 12} )
sess = tf.Session(config=config)
K.set_session(sess)
K._LEARNING_PHASE = tf.constant(0)
K.set_learning_phase(0)
xception = load_model('models/xception/model.h5')
config = xception.get_config()
weights = xception.get_weights()
new_xception = Model.from_config(config)
new_xception.set_weights(weights)
export_path = 'prod_models/2'
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'images': new_xception.input},
outputs={'scores': new_xception.output})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={'predict':
signature})
builder.save()
答案 0 :(得分:0)
由于无法访问您正在使用的模型文件,我尝试使用以下模型来复制您的问题:
from keras.applications.xception import Xception
new_xception = Xception()
我可以对此模型进行请求,而不会出现问题(python 3.6.4,tf 1.8.0,keras 2.2.0)。您正在使用哪个版本的TF服务?