Open betterhalfwzm opened 6 years ago
@betterhalfwzm 参考一下https://blog.csdn.net/qq_14975217/article/details/80985127
如果不需要做成web服务,这个博客应该就够了
@neuleaf '_INPUT_NAME','_OUTPUT_NAME'要根据自己的网络设置,这个该怎么找到呢,您用的deeplab v3是哪个源码?能给个链接吗,我现在训练的是v3+,想要转成pb,一直不成功
是v3+,tensorflow/models 的源码。如果你也是v3+那直接用我博客里的代码就行。
@neuleaf 好的谢啦
@neuleaf 您好,我在运行测试代码的时候遇到了错误,grpc.framework.interfaces.face.face.ExpirationError: ExpirationError(code=StatusCode.DEADLINE_EXCEEDED, details="Deadline Exceeded"),这个是什么意思呢 已解决,时间设置长点就好了
@neuleaf 您的导出pb的代码和源程序自带的export_model.py有区别吗?我用他自带的有问题
@neuleaf 我用您的导出pb的代码生成pb,然后输出结果图像为同一颜色紫色,您遇到过这个问题吗
1,检查你得到的图像矩阵的数据格式,uint8? 2,matplotlib 显示图像可以设置显示方式
@neuleaf 您好能加一下qq吗?有问题向您请教 qq 790406941
@neuleaf 我把output打印出来发现所有的值都是0,是不是转pb的时候出问题了
@neuleaf 能够指导一下吗?还是没有结果谢了
@betterhalfwzm 把你的代码贴过来,看一下
@neuleaf 我就改了路径,output_stride=16, tf_example['x'] = tf.reshape(tf_example['x'], (1, 720, 1280, 3))改成了 tf_example['x'] = tf.reshape(tf_example['x'], (1, 513, 513, 3)) 要不然转pb出错
from grpc.beta import implementations import numpy as np from PIL import Image import tensorflow as tf import matplotlib.pyplot as plt
from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_service_pb2
server="localhost:8500" host, port = server.split(':')
im=np.array(Image.open('/home/tjpu/1.jpg'))
channel = implementations.insecure_channel(host, int(port)) stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest() request.model_spec.name = "deeplab" request.model_spec.signature_name = 'predict_image'
request.inputs["image"].CopyFrom( tf.contrib.util.make_tensor_proto(im.astype(dtype=np.float32), shape=[1, 513, 513, 3]))
response = stub.Predict(request, 10.0)
output = np.array(response.outputs['seg'].int64_val)
mask=np.reshape(output, (513, 513)) plt.imshow(mask) plt.show()
@neuleaf 实在找不出哪出问题了,output输出的矩阵结果都是0.
@betterhalfwzm 'predict_image' 'image' 'seg',这几个名字的定义与你转pb时一样吗。也贴出来看看
@neuleaf 都是一样的,要不会报错。能加一下qq吗?
@neuleaf 我现在不在实验室,我明天把程序都贴上您看一下
@neuleaf 转pb的程序 import os import tensorflow as tf
from tensorflow.python.tools import freeze_graph from deeplab import input_preprocess from deeplab import common from deeplab import model
slim = tf.contrib.slim flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('checkpoint_path', '/home/tjpu/models/research/deeplab/datasets/pascal_voc_seg/exp/train_on_train_set/train', 'Checkpoint path')
flags.DEFINE_string('export_path', '/home/tjpu/models/research/deeplab/datasets/pascal_voc_seg/exp/train_on_train_set/train','Path to output Tensorflow frozen graph.') flags.DEFINE_integer('model_version', 1, 'Models version number.')
flags.DEFINE_integer('num_classes', 21, 'Number of classes.')
flags.DEFINE_multi_integer('crop_size', [513, 513], 'Crop size [height, width].')
xception_65
, use atrous_rates = [12, 24, 36] if output_stride = 8, ormobilenet_v2
, use None. Noteflags.DEFINE_multi_integer('atrous_rates', [6, 12, 18], 'Atrous rates for atrous spatial pyramid pooling.')
flags.DEFINE_integer('output_stride', 16, 'The ratio of input to output spatial resolution.')
flags.DEFINE_multi_float('inference_scales', [1.0], 'The scales to resize images for inference.')
flags.DEFINE_bool('add_flipped_images', False, 'Add flipped images during inference or not.')
def _create_input_tensors(): """Creates and prepares input tensors for DeepLab model. This method creates a 4-D uint8 image tensor 'ImageTensor' with shape [1, None, None, 3]. The actual input tensor name to use during inference is 'ImageTensor:0'. Returns: image: Preprocessed 4-D float32 tensor with shape [1, crop_height, crop_width, 3]. original_image_size: Original image shape tensor [height, width]. resized_image_size: Resized image shape tensor [height, width]. """
input_image = tf.placeholder(tf.uint8, [1, None, None, 3], name='ImageTensor') original_image_size = tf.shape(input_image)[1:3]
preprocess_image_and_label
assumesimage = tf.squeeze(input_image, axis=0) resizedimage, image, = input_preprocess.preprocess_image_and_label( image, label=None, crop_height=FLAGS.crop_size[0], crop_width=FLAGS.crop_size[1], min_resize_value=FLAGS.min_resize_value, max_resize_value=FLAGS.max_resize_value, resize_factor=FLAGS.resize_factor, is_training=False, model_variant=FLAGS.model_variant) resized_image_size = tf.shape(resized_image)[:2]
image = tf.expand_dims(image, 0)
return image, original_image_size, resized_image_size
def main(unused_argv): tf.logging.set_verbosity(tf.logging.INFO) tf.logging.info('Prepare to export model to: %s', FLAGS.export_path)
with tf.Session(graph=tf.Graph()) as sess: model_options = common.ModelOptions( outputs_to_num_classes={common.OUTPUT_TYPE: FLAGS.num_classes}, crop_size=FLAGS.crop_size, atrous_rates=FLAGS.atrous_rates, output_stride=FLAGS.output_stride)
# placeholder for receiving the serialized input image
serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
feature_configs = {'x': tf.FixedLenFeature(shape=[], dtype=tf.float32), }
tf_example = tf.parse_example(serialized_tf_example, feature_configs)
# reshape the input image to its original dimension
tf_example['x'] = tf.reshape(tf_example['x'], (1, 513, 513, 3))
input_tensor = tf.identity(tf_example['x'], name='x') # use tf.identity() to assign name
if tuple(FLAGS.inference_scales) == (1.0,):
tf.logging.info('Exported model performs single-scale inference.')
predictions = model.predict_labels(
input_tensor,
model_options=model_options,
image_pyramid=FLAGS.image_pyramid)
else:
tf.logging.info('Exported model performs multi-scale inference.')
predictions = model.predict_labels_multi_scale(
input_tensor,
model_options=model_options,
eval_scales=FLAGS.inference_scales,
add_flipped_images=FLAGS.add_flipped_images)
semantic_predictions = predictions[common.OUTPUT_TYPE]
#restore model from checkpoints
saver = tf.train.Saver()
module_file=tf.train.latest_checkpoint(FLAGS.checkpoint_path)
saver.restore(sess, module_file)
#builder
builder = tf.saved_model.builder.SavedModelBuilder(os.path.join(FLAGS.export_path, str(FLAGS.model_version)))
tensor_info_x = tf.saved_model.utils.build_tensor_info(input_tensor)
tensor_info_y = tf.saved_model.utils.build_tensor_info(semantic_predictions)
signature_def_map={
"predict_image":tf.saved_model.signature_def_utils.build_signature_def(
inputs={"image": tensor_info_x},
outputs={"SemanticPredictions": tensor_info_y},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)
}
builder.add_meta_graph_and_variables(sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map=signature_def_map)
builder.save()
if name == 'main': flags.mark_flag_as_required('checkpoint_path') flags.mark_flag_as_required('export_path') tf.app.run()
测试的程序
from grpc.beta import implementations import numpy as np from PIL import Image import tensorflow as tf import matplotlib.pyplot as plt from scipy import misc
import cv2
from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_service_pb2 np.set_printoptions(threshold='nan') server="localhost:8500" host, port = server.split(':')
im=np.array(Image.open('/home/tjpu/106.png'))
channel = implementations.insecure_channel(host, int(port)) stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest() request.model_spec.name = "deeplab" request.model_spec.signature_name = 'predict_image'
request.inputs["image"].CopyFrom( tf.contrib.util.make_tensor_proto(im.astype(dtype=np.float32), shape=[1, 513, 513, 3]))
response = stub.Predict(request, 50.0) print request
output = np.array(response.outputs['SemanticPredictions'].int64_val) print output
mask=np.reshape(output, (513, 513))
plt.imshow(mask) plt.show()
@neuleaf 您看一下,有什么问题
@neuleaf 您能把您转好的pb传给我吗,我试一下,看有没有输出结果,十分感谢,在做毕设,感激不尽 qq 790406941
你好,我已经训练好了模型,该如何用您的这个程序应用呢?