neuleaf / tensorflow_serving_demo

TF serving for deeplab v3+, using Flask
0 stars 1 forks source link

你好,我已经训练好了模型,该如何用您的这个程序应用呢 #1

Open betterhalfwzm opened 6 years ago

betterhalfwzm commented 6 years ago

你好,我已经训练好了模型,该如何用您的这个程序应用呢?

neuleaf commented 6 years ago

@betterhalfwzm 参考一下https://blog.csdn.net/qq_14975217/article/details/80985127

如果不需要做成web服务,这个博客应该就够了

betterhalfwzm commented 6 years ago

@neuleaf '_INPUT_NAME','_OUTPUT_NAME'要根据自己的网络设置,这个该怎么找到呢,您用的deeplab v3是哪个源码?能给个链接吗,我现在训练的是v3+,想要转成pb,一直不成功

neuleaf commented 6 years ago

是v3+,tensorflow/models 的源码。如果你也是v3+那直接用我博客里的代码就行。

betterhalfwzm commented 6 years ago

@neuleaf 好的谢啦

betterhalfwzm commented 6 years ago

@neuleaf 您好,我在运行测试代码的时候遇到了错误,grpc.framework.interfaces.face.face.ExpirationError: ExpirationError(code=StatusCode.DEADLINE_EXCEEDED, details="Deadline Exceeded"),这个是什么意思呢 已解决,时间设置长点就好了

betterhalfwzm commented 6 years ago

@neuleaf 您的导出pb的代码和源程序自带的export_model.py有区别吗?我用他自带的有问题

betterhalfwzm commented 6 years ago

@neuleaf 我用您的导出pb的代码生成pb,然后输出结果图像为同一颜色紫色,您遇到过这个问题吗

neuleaf commented 6 years ago

1,检查你得到的图像矩阵的数据格式,uint8? 2,matplotlib 显示图像可以设置显示方式

betterhalfwzm commented 6 years ago

@neuleaf 您好能加一下qq吗?有问题向您请教 qq 790406941

betterhalfwzm commented 6 years ago

@neuleaf 我把output打印出来发现所有的值都是0,是不是转pb的时候出问题了

betterhalfwzm commented 6 years ago

@neuleaf 能够指导一下吗?还是没有结果谢了

neuleaf commented 6 years ago

@betterhalfwzm 把你的代码贴过来,看一下

betterhalfwzm commented 6 years ago

@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出错

betterhalfwzm commented 6 years ago

This is a placeholder for a Google-internal import.

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()

betterhalfwzm commented 6 years ago

@neuleaf 实在找不出哪出问题了,output输出的矩阵结果都是0.

neuleaf commented 6 years ago

@betterhalfwzm 'predict_image' 'image' 'seg',这几个名字的定义与你转pb时一样吗。也贴出来看看

betterhalfwzm commented 6 years ago

@neuleaf 都是一样的,要不会报错。能加一下qq吗?

betterhalfwzm commented 6 years ago

@neuleaf 我现在不在实验室,我明天把程序都贴上您看一下

betterhalfwzm commented 6 years ago

@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].')

For xception_65, use atrous_rates = [12, 24, 36] if output_stride = 8, or

rates = [6, 12, 18] if output_stride = 16. For mobilenet_v2, use None. Note

one could use different atrous_rates/output_stride during training/evaluation.

flags.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.')

Change to [0.5, 0.75, 1.0, 1.25, 1.5, 1.75] for multi-scale inference.

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_preprocess takes 4-D image tensor as input.

input_image = tf.placeholder(tf.uint8, [1, None, None, 3], name='ImageTensor') original_image_size = tf.shape(input_image)[1:3]

Squeeze the dimension in axis=0 since preprocess_image_and_label assumes

image to be 3-D.

image = 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]

Expand the dimension in axis=0, since the following operations assume the

image to be 4-D.

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()

测试的程序

This is a placeholder for a Google-internal import.

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'))

im = cv2.imread('/home/tjpu/2007_000027.jpg')

cv2.imshow('test', im)

cv2.waitKey(0)

im = cv2.resize(im, (513, 513), interpolation=cv2.INTER_CUBIC)

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

print im.astype(dtype=np.float32)

mask=np.reshape(output, (513, 513))

cv2.imwrite('1.jpg', mask)

im = cv2.imread('1.jpg')

cv2.imshow('test win', mask)

cv2.waitKey(0)

print mask

misc.imsave(mask)

img = plt.imread(mask)

plt.imshow(mask) plt.show()

betterhalfwzm commented 6 years ago

@neuleaf 您看一下,有什么问题

betterhalfwzm commented 6 years ago

@neuleaf 您能把您转好的pb传给我吗,我试一下,看有没有输出结果,十分感谢,在做毕设,感激不尽 qq 790406941