python - Printing all tf.Tensors of a model in a loop -
i have list of tensors in model had float32
type:
import os import os.path import tensorflow tf tensorflow.python.platform import gfile import numpy numpy.set_printoptions(threshold=numpy.nan) tf.session() sess: model_filename = 'my_pb_file.pb' gfile.fastgfile(model_filename, 'rb') f: graph_def = tf.graphdef() graph_def.parsefromstring(f.read()) _= tf.import_graph_def(graph_def,name='') pprint import pprint pprint([out op in tf.get_default_graph().get_operations() if op.type != 'placeholder' out in op.values() if out.dtype == tf.float32])
which gives me list:
<tf.tensor 'mobilenetv1/mobilenetv1/conv2d_1_pointwise/batchnorm/batchnorm/add:0' shape=(16,) dtype=float32>, <tf.tensor 'mobilenetv1/mobilenetv1/conv2d_1_pointwise/batchnorm/batchnorm/rsqrt:0' shape=(16,) dtype=float32>, <tf.tensor 'mobilenetv1/mobilenetv1/conv2d_1_pointwise/batchnorm/batchnorm/mul:0' shape=(16,) dtype=float32>, <tf.tensor 'mobilenetv1/mobilenetv1/conv2d_1_pointwise/batchnorm/batchnorm/mul_1:0' shape=(?, 64, 64, 16) dtype=float32>, <tf.tensor 'mobilenetv1/mobilenetv1/conv2d_1_pointwise/batchnorm/batchnorm/mul_2:0' shape=(16,) dtype=float32>, <tf.tensor 'mobilenetv1/mobilenetv1/conv2d_1_pointwise/batchnorm/batchnorm/sub:0' shape=(16,) dtype=float32>, <tf.tensor 'mobilenetv1/mobilenetv1/conv2d_1_pointwise/batchnorm/batchnorm/add_1:0' shape=(?, 64, 64, 16) dtype=float32>, ...
at point can use sess.run('name')
see value:
>>> sess.run('mobilenetv1/mobilenetv1/conv2d_1_pointwise/batchnorm/batchnorm/mul:0') array([ 0.51656026, 29.6620369 , 0.48722425, 7.73186255, -9.51173401, 0.60846734, 0.21111809, 0.23865609, 23.85105324, 1.04134226, 28.59620476, 35.79195023, 0.34110394, 0.5557093 , 10.39805031, 10.99952412], dtype=float32)
however, print tf.tensor
values in loop. how can that?
apparently, needs dictionary defined:
sess.run('mobilenetv1/mobilenetv1/conv2d_1_pointwise/relu6:0')
e.g.:
invalidargumenterror (see above traceback): must feed value placeholder tensor 'input' dtype float , shape [?,128,128,3] [[node: input = placeholder[dtype=dt_float, shape=[?,128,128,3], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
when request value of tensor, tensorflow computes value in graph tensor values not retained across different calls sess.run
(that's variables for). seems operations necessary compute value of tensor requested need input 1 of input placeholders (named input
in error statement), must feed value placeholder through feed dictionary in sess.run
.
following comment, consider example:
import tensorflow tf = tf.constant(4) b = tf.constant(3) c = tf.placeholder(tf.int32, [], 'c') d = + b e = + c
requesting tensor d
works fine:
with tf.session() sess: print(sess.run(d)) # prints 7
however, requesting e
throws same error report:
with tf.session() sess: print(sess.run(e))
which prints
invalidargumenterror (see above traceback): must feed value placeholder tensor 'c' dtype int32 [[node: c = placeholder[dtype=dt_int32, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
this happens because in order evaluate e
must compute a + c
, if don't feed value c
, not possible. so, e.g., works:
with tf.session() sess: print(sess.run(e, feed_dict={c: 1})) # prints 5
evaluating d
worked fine because computation path necessary evaluate d
not involve placeholder. so, fix problem, should feed value placeholder called 'input'
in call sess.run('mobilenetv1/mobilenetv1/conv2d_1_pointwise/relu6:0')
.
Comments
Post a Comment