numpy tensorflow2如何打印Tensor值

bf1o4zei  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(136)

我尝试打印从我的自定义tfds数据集加载的Tensor的实际值。我不知道该如何操作。我使用的是Tensorflow2,因此不再鼓励使用该会话。我尝试使用. numpy()tf.print. tf.正在执行.急切地()但是什么都不起作用。它要么只打印Tensor对象,向我展示形状,要么在. numpy的情况下()它抛出标题中的错误。我需要Tensor的值,我需要把它带回numpy以便调试代码。
下面是我创建数据集的方法:

class dt(tfds.core.GeneratorBasedBuilder):
    ''' Dataset builder'''

    # DOuble check
    VERSION = tfds.core.Version('1.0.0')
    RELEASE_NOTES = {
      '1.0.0': 'Initial release.',
    }

    def _info(self) ->tfds.core.DatasetInfo:
        '''Dataset metadata'''

        return tfds.core.DatasetInfo(
            builder=self,
            features=tfds.features.FeaturesDict({
                "id": tf.int64,
                "image": tfds.features.Image(shape=(IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), encoding_format='png'),
                "mask": tfds.features.Image(shape=(IMG_HEIGHT, IMG_WIDTH, 1), encoding_format='png'),
                "label": tfds.features.ClassLabel(names=CLASSES),
            }),
        supervised_keys=('image', 'mask')
        )

    def _split_generators(self, dl_manager: tfds.download.DownloadManager):
        '''Splitgenerator for train and test splits'''

        path = DATASETS_ROOT
        return {
            "train": self._generate_examples(
                images_path=os.path.join(path, "train/rgb"),
                masks_path=os.path.join(path, "train/masks")
                ),
            "test": self._generate_examples(
                images_path=os.path.join(path, "test/rgb"),
                masks_path=os.path.join(path, "test/masks")
                )
        }

    def _generate_examples(self, images_path, masks_path):
        '''Generator of examples for each split'''
        
        for i, (image, mask) in enumerate(zip(glob.glob(images_path + "/*.png"), glob.glob(masks_path + "/*.png"))):
            yield i, {
                "id": i,
                "image": image,
                "mask": mask,
                "label": CLASSES[3],
            }

这就是我如何提取numpy数组

def custom_load_X_Y(training=True):

    if training:
        dt, dt_info = tfds.load("dt", split="train", shuffle_files=True, as_supervised=True, with_info=True)

        print(f'EAGERLY {tf.executing_eagerly()}')
        print(f'MOde type {type(dt)}')
        tf.print(f"aaaaa {dt.numpy()} aaaaaa")

控制台输出:
Console output

vltsax25

vltsax251#

您可以使用www.example.com_examples()打印图像数据集tfds.show_examples()

import tensorflow_datasets as tfds
ds, ds_info = tfds.load('cifar10', split='train', with_info=True)
fig = tfds.show_examples(ds, ds_info)

输出:

更多详情请参考gist。谢谢。

相关问题