首页 > 解决方案 > 未在从张量流中的 dataset.map 调用的函数中获取张量的值

问题描述

我正在尝试从 map 函数调用的函数中获取张量的值。但我收到了这个错误。

tensorflow.python.framework.errors_impl.InvalidArgumentError:您必须使用 dtype 字符串 [[{{node args_0}}]] 为占位符张量“args_0”提供一个值

我的代码如下

在我的主要功能中:

# created instance of Preprocess class
preprocess = Preprocessor(is_train, TOTAL_CLASSES, OUTPUT_SHAPE)

# all the tfrecords which is present in ./tfrecords folder we will gate
dataset = tf.data.Dataset.list_files("./tfrecords")

# we will comprises it into one
dataset = tf.data.TFRecordDataset(dataset)

# Here I am mapped __call__ method which is present in Preprocess class
dataset = dataset.map(preprocess, num_parallel_calls=tf.data.experimental.AUTOTUNE)

现在在预处理类调用方法中我收到错误这是我的预处理类。在调用函数中我收到错误。

class Preprocessor(object):
    def __init__(self, is_train, num_classes, output_shape=(416, 416)):
        self.is_train = is_train
        self.num_classes = num_classes
        self.output_shape = output_shape

    def __call__(self, example):
        features = self.parse_tfexample(example)
        encoded = features['image/encoded']
        image = tf.io.decode_jpeg(encoded)
        image = tf.cast(image, tf.float32)

        sess = tf.Session()

        # at this line I am getting error
        print(sess.run(image))

我该如何解决这个问题。如果我评论 print(sess.run(image)) 这一行,所有代码都可以正常工作。但我想要映射函数中的张量值。

标签: pythontensorflowdeep-learningtfrecordtf.data.dataset

解决方案


推荐阅读