),python-3.x,tensorflow,tensorflow2.0,tensorflow-datasets"/>

首页 > 解决方案 > 如何打印数据管道过程中形成的张量的数据?(tf.data.Dataset.map -)

问题描述

我尝试用tensorflow2查看数据管道的过程

我的代码正在运行,但我无法在此管道步骤中打印一些值。(尤其是里面.map(read_image)

如何在 read_image 函数中打印值?(使用 .map() 方法调用)

def read_image(image_paths, label_map_paths):
    # firstly I want to print => image_paths values 
    # print(type(image_paths)) -> <class 'tensorflow.python.framework.ops.Tensor'>

    img_raw = tf.io.read_file(image_paths)
    # print(img_raw) ?
    # print(type(img_raw)) -> <class 'tensorflow.python.framework.ops.Tensor'>


    image = tf.image.decode_jpeg(img_raw)
    #print(type(image)) -> <class 'tensorflow.python.framework.ops.Tensor'>
    #print(image) ?
    


我可以使用下面的代码打印 training_ds 值,但无法在.map(read_image)函数内部打印

def get_training_dataset(training_image_paths, training_label_map_paths):
    training_ds = tf.data.Dataset.from_tensor_slices((image_paths,label_map_paths))
    for z in training_ds.take(3):
        print(z)

    training_ds = training_ds.map(read_image)
    for x in training_ds.take(1):
        print(x)
output 1 
(<tf.Tensor: shape=(), dtype=string, numpy=b'dataset1/images_prepped_train/0016E5_06330.png'>, <tf.Tensor: shape=(), dtype=string, numpy=b'dataset1/annotations_prepped_train/0016E5_06330.png'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'dataset1/images_prepped_train/0016E5_06360.png'>, 
output2 :
(<tf.Tensor: shape=(360, 480, 3), dtype=uint8, numpy=
array([[[16, 16, 16],
        [16, 16, 16],
        [12, 12, 12],
        ...,
        [15, 19, 20],
        [17, 18, 20],
        [17, 18, 22]],

       [[16, 16, 16],
        [14, 14, 14],
        [14, 14, 14],
        ...,
        [15, 19, 20],
        [18, 19, 21],
        [19, 20, 22]],

       [[14, 14, 14],
        [14, 14, 14],
        [15, 15, 15],
        ...,
        [15, 19, 20],
        [17, 18, 20],
        [16, 17, 20]],

       ...,

       [[16, 17, 19],
        [16, 17, 19],
        [16, 17, 19],
        ...,
        [30, 40, 42],
        [26, 37, 37],
        [21, 33, 38]],

       [[16, 17, 19],
        [16, 17, 19],
        [16, 17, 19],
        ...,
        [27, 37, 40],
        [24, 36, 39],
        [21, 33, 38]],

       [[16, 17, 19],
        [15, 16, 18],
        [15, 16, 18],
        ...,
        [22, 34, 38],
        [23, 35, 38],
        [22, 32, 38]]], dtype=uint8)>, <tf.Tensor: shape=(360, 480, 1), dtype=uint8, numpy=
array([[[ 1],
        [ 1],
        [ 1],
        ...,
        [ 1],
        [ 1],
        [ 1]],

       [[ 1],
        [ 1],
        [ 1],
        ...,
        [ 1],
        [ 1],
        [ 1]],

       [[ 1],
        [ 1],
        [ 1],
        ...,
        [ 1],
        [ 1],
        [ 1]],

       ...,

       [[ 4],
        [ 4],
        [ 4],
        ...,
        [11],
        [11],
        [11]],

       [[ 4],
        [ 4],
        [ 4],
        ...,
        [11],
        [11],
        [11]],

       [[ 4],
        [ 4],
        [ 4],
        ...,
        [11],
        [11],
        [11]]], dtype=uint8)>)


training_image_paths = [dataset1/images_prepped_train/0016E5_07740.png,
                        dataset1/images_prepped_train/0016E5_07710.png
                        dataset1/images_prepped_train/0016E5_07790.png]
training_label_map_paths = [dataset1/images_prepped_train/0016E5_08460.png,
                            dataset1/images_prepped_train/0016E5_08490.png,
                            dataset1/images_prepped_train/0016E5_08520.png]
training_dataset = get_training_dataset(training_image_paths, training_label_map_paths)

标签: python-3.xtensorflowtensorflow2.0tensorflow-datasets

解决方案


tf.print使用此代码打印两者。

import tensorflow as tf

def read_image(image_paths,label_paths):

    tf.print(image_paths)
    img_raw = tf.io.read_file(image_paths)
    image = tf.image.decode_jpeg(img_raw)
    print_data(image)
    return image

def print_data(image):
    tf.print(image)

image_paths = tf.constant(['/Users/my/Documents/Dataset/jpg/image_00001.jpg'])
label_paths = tf.constant([0])

training_ds = tf.data.Dataset.from_tensor_slices((image_paths,label_paths))

training_ds = training_ds.map(read_image)
for i in training_ds:
    pass

推荐阅读