Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'saved_model_cli.py' bug fix! #15611

Closed
huaxz1986 opened this issue Dec 24, 2017 · 7 comments
Closed

'saved_model_cli.py' bug fix! #15611

huaxz1986 opened this issue Dec 24, 2017 · 7 comments
Assignees
Labels

Comments

@huaxz1986
Copy link

In file python/tools/saved_model_cli.py at function def _print_tensor_info(tensor_info):
The first line should be:

print(' dtype: ' + {value:key for (key,value) in types_pb2.DataType.items()}[tensor_info.dtype])

Not be : print(' dtype: ' + types_pb2.DataType.keyss()[tensor_info.dtype])

because tensor_info.dtype is an Integer which is the value of types(not the index of type values).

@qmick
Copy link
Contributor

qmick commented Dec 24, 2017

It's not necessary to do that, types_pb2.DataType is not python dict. The order of EnumTypeWrapper.keys() is not arbitrary.

See the definition of EnumTypeWrapper.keys():

  def keys(self):
    """Return a list of the string names in the enum.
    These are returned in the order they were defined in the .proto file.
    """

    return [value_descriptor.name
            for value_descriptor in self._enum_type.values]

and self._enum_type.values is something like:

  values=[
    _descriptor.EnumValueDescriptor(
      name='DT_INVALID', index=0, number=0,
      options=None,
      type=None),
    _descriptor.EnumValueDescriptor(
      name='DT_FLOAT', index=1, number=1,
      options=None,
      type=None),
    _descriptor.EnumValueDescriptor(
      name='DT_DOUBLE', index=2, number=2,
      options=None,
      type=None),
      ...
  ]

@huaxz1986
Copy link
Author

When I run tfdbg:

graph = tf.get_default_graph()
with  graph.as_default():
    v1 = tf.get_variable('v1', initializer=[1, 2, 3])
    v2 = tf.square(v1, name='v2')
    sess = tf.Session()
    sess = tf_dbg.LocalCLIDebugWrapperSession(sess)
    with sess.as_default():
      sess.run(tf.global_variables_initializer())
      _v2 = sess.run(v2)
      print(_v2)

The type of v1 is DT_INT_REF,and saved_model_cli.py throws exception.

@qmick
Copy link
Contributor

qmick commented Dec 26, 2017

Yes, my mistake, for REF type, the value is not the same as its index. Can you add a minimum repuducible code and its error log to better describe this problem?

@huaxz1986
Copy link
Author

This is a simple example to show the usage of saved_model_cli

export_dir = 'out'
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
graph = tf.get_default_graph()
with graph.as_default():
  v1 = tf.get_variable('v1',shape=[3],
                  initializer=tf.zeros_initializer)
  v2 = tf.square(v1,name='v2')
  with tf.Session(graph=graph) as sess:
    sess.run(v1.initializer)
    builder.add_meta_graph_and_variables(sess,['xxx'])
    predict_signature_def = tf.saved_model.\
    	signature_def_utils.build_signature_def(\
             inputs={
        		 'input_x': \
                  tf.saved_model.utils.build_tensor_info(v1),
             }, outputs={'pred_y':\
				  tf.saved_model.utils.build_tensor_info(v2)},\
             method_name=tf.saved_model.\
 			 signature_constants.PREDICT_OUTPUTS)
    builder.add_meta_graph([tf.saved_model.\
             tag_constants.TRAINING,
             tf.saved_model.tag_constants.SERVING],\
             signature_def_map={'predict_graph': predict_signature_def})
    builder.save(as_text=True)

Then run saved_model_cli in shell

saved_model_cli show --dir out/ --tag_set serve,train --signature_def predict_graph

The result is :

The given SavedModel SignatureDef contains the following input(s):
inputs['input_x'] tensor_info:
tensor type: 101
    dtype: DT_FLOAT_REF
    shape: (3)
    name: v1:0
The given SavedModel SignatureDef contains the following output(s):
outputs['pred_y'] tensor_info:
tensor type: 1
    dtype: DT_FLOAT
    shape: (3)
    name: v2:0
Method name is: outputs

I change tensorflow/python/tools/saved_model_cli._print_tensor_info() like this:

def _print_tensor_info(tensor_info):
  """Prints details of the given tensor_info.

  Args:
    tensor_info: TensorInfo object to be printed.
  """
#####  this is what I do ####
  print('tensor type:',tensor_info.dtype)
  print('    dtype: ' + {value:key for (key,value) in types_pb2.DataType.items()}[tensor_info.dtype])
##########
  # Display shape as tuple.
  if tensor_info.tensor_shape.unknown_rank:
    shape = 'unknown_rank'
  else:
    dims = [str(dim.size) for dim in tensor_info.tensor_shape.dim]
    shape = ', '.join(dims)
    shape = '(' + shape + ')'
  print('    shape: ' + shape)
  print('    name: ' + tensor_info.name)

By the way, I use python3.6+ubuntu 16.04+ tensorflow 1.4

@qmick
Copy link
Contributor

qmick commented Dec 27, 2017

Thanks for the supplement. I am not sure who is responsible for this module, friendly ping @yifeif @drpngx

@yifeif yifeif self-assigned this Dec 27, 2017
@drpngx
Copy link
Contributor

drpngx commented Dec 27, 2017

/CC @sukritiramesh

@yifeif
Copy link
Contributor

yifeif commented Dec 27, 2017

Thanks @huaxz1986! Will fix this internally!

@yifeif yifeif added the type:bug Bug label Dec 27, 2017
@drpngx drpngx closed this as completed in 2e2715b Dec 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants