Closed wenston2006 closed 5 years ago
import sys import os
import tensorflow as tf
sys.path.append(os.getcwd())
sys.path.append("..")
from cfg import Config from other import resize_im
from lib.networks.factory import get_network from lib.fast_rcnn.config import cfg from lib.fast_rcnn.test import test_ctpn
from keras import backend as K import tensorflow as tf from grpc.beta import implementations from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_service_pb2 tf.app.flags.DEFINE_string('server', 'localhost:9001', 'PredictionService host:port') tf.app.flags.DEFINE_string('ctpn_image', '', 'path to ctpn input image format') FLAGS = tf.app.flags.FLAGS import tensorflow.contrib import h5py
''' load network 输入的名称为'Net_model' 'VGGnet_test'--test 'VGGnet_train'-train '''
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
def load_tf_model(export_version, export_path='prod_models'): cfg.TEST.HAS_RPN = True # Use RPN for proposals sess = [] saver = [] net = []
#在转模型是不能用with sess约束,因with sess会将sess关闭
sess = tf.Session()
net = get_network("VGGnet_test")
# load model
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state('../ckpt/')
reader = tf.train.NewCheckpointReader(ckpt.model_checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
for key in var_to_shape_map:
pass
saver.restore(sess, ckpt.model_checkpoint_path)
print("load vggnet done")
h5FileName = r'../h5model/net_classification.h5'
reader = tf.train.NewCheckpointReader(ckpt.model_checkpoint_path)
f = h5py.File(h5FileName, 'w')
t_g = None
for key in sorted(reader.get_variable_to_shape_map()):
if key.endswith('w') or key.endswith('biases'):
keySplits = key.split(r'/')
keyDict = keySplits[0] + '/' + keySplits[0] + '/' + keySplits[1]
f[keyDict] = reader.get_tensor(key)
return sess, saver, net
def save_model_to_serving(sess, saver, basemodel, export_version, export_path='prod_models'): print("input data:", basemodel.data) print("input im_info:", basemodel.im_info) print("input keep_prob:", basemodel.keep_prob) print("output:", basemodel.get_output('rois')[0])
with sess.as_default():
signature = (tf.saved_model.signature_def_utils.predict_signature_def(inputs={'ctpn_data': basemodel.data,
'ctpn_im_info': basemodel.im_info,
'ctpn_keep_prob': basemodel.keep_prob},
outputs={'ctpn_output': basemodel.get_output('rois')[0]}))
export_path = os.path.join(tf.compat.as_bytes(export_path), tf.compat.as_bytes(str(export_version)))
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
with tf.Session(graph=tf.Graph()) as sess:
builder.add_meta_graph_and_variables(sess, tags=[tf.saved_model.tag_constants.SERVING],
signature_def_map={'ctpn_classification': signature, },
legacy_init_op=legacy_init_op)
with tf.Session(graph=tf.Graph()) as sess:
builder.add_meta_graph(["ctpn-tag"])
builder.save()
def ctpn(img): """ text box detect """ scale, max_scale = Config.SCALE, Config.MAX_SCALE
img, f = resize_im(img, scale=scale, max_scale=max_scale)
scores, boxes = test_ctpn(sess, net, img)
return scores, boxes, img
export_path = "../test_model"
sess, saver, net = load_tf_model("1", export_path)
The above screenshot is the error when I start the service. I also attached the .sh file which I used to start the service as follows.
/home/liusong/serving/./.cache/_bazel_liusong/dcf8fb3b05719fe93eeae3106285b583/execroot/tf_serving/bazel-out/k8-opt/bin/tensorflow_serving/model_servers/tensorflow_model_server_beta_grpc_test.runfiles/tf_serving/tensorflow_serving/model_servers/tensorflow_model_server --port=9001 --model_name="ctpn_image" --model_base_path="/home/liusong/tensorflow_proj/gov-affair-1-1-4-3-3-2/densenet_ocr/ctpn/test_model/"
The structure tree of the ckpt files are as follows:
Please go to Stack Overflow for help and support:
https://stackoverflow.com/questions/tagged/tensorflow-serving
If you open a GitHub issue, it must be a bug, a feature request, or a significant problem with documentation (for small docs fixes please send a PR instead).
Thanks!
did you solve your problem? I met the same problem as yours, best! @wenston2006
I'm facing a similar problem.
I found a possible solution here: https://stackoverflow.com/questions/53284674/tensorflow-serving-with-contrib-operations
thanks
did you solve your problem? I met the same problem as yours, best! @wenston2006
I tried to convert the CTPN model(ckpt format) to tf serving model using SaveModel module. Although I got the files including "saved_model.pb" and the "variables" subfolder, I came across the error as shown in the issue title when I start the service using "tensorflow_model_server". How can I solve this problem? The following attachment is my code and the error screenshot.