【问题标题】:How can I inspect the contents of my tensor using TensorFlow’s eager execution?如何使用 TensorFlow 的 Eager Execution 检查我的张量的内容?
【发布时间】:2019-05-15 10:15:02
【问题描述】:

我使用 TensorFlow 1.12 并使用 Eager Execution,并且我有以下(不完整的)函数,我想在其中检查一些中间张量:

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

但是,print(mask) 仅返回 Tensor("mask:0", shape=(?, 1000, 1200), dtype=uint8),而我想查看实际值。这应该是可能的,如TensorFlow’s eager execution guide 所示。我也试过tf.print(mask, output_stream=sys.stdout),但只打印了一个空行。 mask.dtype 是 uint8,所以我猜它应该包含整数,因为它具有 a 形状。我还觉得奇怪的是mask.device 是空字符串。它应该存储在某些设备上,对吧?

如何打印mask 张量的内容?

【问题讨论】:

    标签: python tensorflow tensor eager-execution


    【解决方案1】:

    如果启用了急切执行,那么您应该可以调用

    mask.numpy() 
    

    返回该张量中值的 numpy 数组。

    我的印象是print 在启用急切执行时也应该打印内容,但这可能取决于张量的大小。

    无论哪种方式,只需通过调用来检查您是否启用了急切执行:

    tf.enable_eager_execution()
    

    【讨论】:

    • 感谢您的回复!不幸的是,这会引发AttributeError: 'Tensor' object has no attribute 'numpy'。 type(mask) 是 <class tensorflow.python.framework.ops.Tensor'>。我正在急切地执行,但函数中的代码可能突然不再急切地运行了?顺便说一句,该函数是从急切执行的代码中调用的。
    • 我还觉得奇怪的是 .numpy() 函数被推荐的频率更高,但我在 API 文档中找不到它。
    • 我已经明白了:我在调用 tf.Dataset.map() 时使用了我的函数,并且该代码不再急切地执行了。
    • @EmielBoss 啊,是的,这是有道理的。 Dataset.map() 内部更棘手,因为(正如您所说的)它并不急于求成。假设这是开发/调试过程的一部分,也许最好的做法是从一个 map 操作中返回 mask 以允许对其进行检查,但我怀疑您已经想到了:-/
    猜你喜欢
    • 2019-02-13
    • 2020-01-10
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2018-10-02
    • 2023-03-13
    相关资源
    最近更新 更多