【tensorflow-v2.0】如何查看模型的输入输出流的属性

 操作过程:

1. 查看mobilenet的variables

loaded = tf.saved_model.load('mobilenet')
print('MobileNet has {} trainable variables: {},...'.format(
       len(loaded.trainable_variables),
       ', '.join([v.name for v in loaded.trainable_variables[:5]])))
trainable_variable_ids = {id(v) for v in loaded.trainable_variables}
non_trainable_variables = [v for v in loaded.variables if id(v) not in trainable_variable_ids]
print('MobileNet also has {} non-trainable variables: {}, ...'.format(
       len(non_trainable_variables),
       ', '.join([v.name for v in non_trainable_variables[:3]])))

输出:输出trainable_variables的后5个variables,non_trainable_variables的后3个variables.

MobileNet has 83 trainable variables: conv1/kernel:0, conv1_bn/gamma:0, conv1_bn/beta:0, conv_dw_1/depthwise_kernel:0, conv_dw_1_bn/gamma:0,...
MobileNet also has 54 non-trainable variables: conv1_bn/moving_mean:0, conv1_bn/moving_variance:0, conv_dw_1_bn/moving_mean:0, ...

但是这种方法输出model/detector模型的variables却出错;

Traceback (most recent call last):
  File "inspect_saved_model.py", line 59, in <module>
    len(facebox_model.trainable_variables),
AttributeError: '_UserObject' object has no attribute 'trainable_variables'

原因还没找出来,有知道的可以私信博主哈~

2. 使用命令行查看模型的signatures

usage: saved_model_cli show [-h] --dir DIR [--all]
[--tag_set TAG_SET] [--signature_def SIGNATURE_DEF_KEY]

例如

saved_model_cli show --dir mobilenet/ --all
or saved_model_cli show
--dir model/detector/ --tag_set serve --signature_def serving_default

输出

(tf_test) ~/workspace/test_code/github_test/faceboxes-tensorflow$ saved_model_cli show --dir model/detector --all

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['images'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1, -1, -1)
        name: serving_default_images:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['boxes'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 100, 4)
        name: StatefulPartitionedCall:0
    outputs['num_boxes'] tensor_info:
        dtype: DT_INT32
        shape: (-1)
        name: StatefulPartitionedCall:1
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 100)
        name: StatefulPartitionedCall:2
  Method name is: tensorflow/serving/predict

这个是model/detector模型的输出;

 参考

1. tensorflow1.x;

2. tf_saved_model;