1

I use TensorFlow 1.12 using eager execution, and I have the following (incomplete) function in which I want to inspect some intermediate tensor:

def parse_example(example_proto, width, height, num_classes):
    features = {
        'image/encoded': tf.FixedLenFeature((), tf.string),
        'image/height': tf.FixedLenFeature((), tf.int64),
        'image/width': tf.FixedLenFeature((), tf.int64),
        'image/filename': tf.FixedLenFeature((), tf.string),
        'image/object/bbox/xmin': tf.VarLenFeature(tf.float32),
        'image/object/bbox/xmax': tf.VarLenFeature(tf.float32),
        'image/object/bbox/ymin': tf.VarLenFeature(tf.float32),
        'image/object/bbox/ymax': tf.VarLenFeature(tf.float32),
        'image/object/class/label': tf.VarLenFeature(tf.int64),
        'image/object/class/text': tf.VarLenFeature(tf.string),
        'image/object/mask': tf.VarLenFeature(tf.string),
        'image/depth': tf.FixedLenFeature((), tf.string)
    }

    parsed_example = tf.parse_single_example(example_proto, features)

    #print(tf.sparse_tensor_to_dense(parsed_example['image/object/mask'], default_value=0))

    # Decode image
    image = tf.image.decode_jpeg(parsed_example['image/encoded'])
    parsed_example['image/encoded'] = image

    # Depth + RGBD
    depth = utilities.decode_depth(parsed_example['image/depth'])
    parsed_example['image/depth'] = depth
    rgbd = tf.concat([tf.image.convert_image_dtype(image, tf.float32), depth], axis=2)
    rgbd = tf.reshape(rgbd, shape=tf.stack([height, width, 4]))
    parsed_example['image/rgbd'] = rgbd

    mask = tf.sparse.to_dense(parsed_example['image/object/mask'], default_value="")
    mask = tf.map_fn(utilities.decode_png_mask, mask, dtype=tf.uint8)
    mask = tf.reshape(mask, shape=tf.stack([-1, height, width]), name='mask')
    print(mask)
    sys.exit()

However, print(mask) merely returns Tensor("mask:0", shape=(?, 1000, 1200), dtype=uint8), while I would like to see the actual values. This should be possible, as demonstrated in TensorFlow’s eager execution guide. I also tried tf.print(mask, output_stream=sys.stdout), but only a blank line is being printed. mask.dtype is uint8, so I guess it should contain integers, given that is has a shape. What I also find strange is that mask.device is the empty string. It should be stored on some device, right?

How can I print the contents of the mask tensor?

1 Answers1

2

If eager execution is enabled then you should be able to call

mask.numpy() 

to return a numpy array of the values in that tensor.

I was under the impression that print should also print the contents when eager execution is enabled but this may depend on the size of the tensor.

Either way, it would be worth just checking that you enabled eager execution by calling:

tf.enable_eager_execution()
Stewart_R
  • 13,764
  • 11
  • 60
  • 106
  • Thanks for your reply! Unfortunately, this throws ```AttributeError: 'Tensor' object has no attribute 'numpy'```. ```type(mask)``` is ``````. I am running in eager execution, but perhaps code within functions suddenly isn't run eagerly anymore? The function is called from eagerly executing code, btw. –  May 15 '19 at 12:03
  • What I also find strange is that the ```.numpy()``` function gets recommended more often, but I can't find it in the API docs. –  May 15 '19 at 12:06
  • 1
    I get it already: I use my function in a call to ```tf.Dataset.map()``` call, and that code isnt being executed eagerly anoymore. –  May 15 '19 at 12:09
  • 1
    @EmielBoss Ah, yes that makes sense. Trickier inside `Dataset.map()` because (as you rightly say) it's not eager. Assuming this is part of a dev/debug process perhaps the best course of action is to return `mask` from one `map` operation to allow for it to be inspected but I suspect you thought of that already :-/ – Stewart_R May 15 '19 at 12:30