【问题标题】:Tensorflow dataset with multiple inputs and target具有多个输入和目标的 TensorFlow 数据集
【发布时间】:2021-08-10 18:26:07
【问题描述】:

我正在尝试使用 ArcFace 层实现模型: https://github.com/4uiiurz1/keras-arcface

为此,我创建了一个像这样的 tf.data.dataset:

images= tf.data.Dataset.from_tensor_slices(train.A_image.to_numpy())
target = tf.keras.utils.to_categorical(
    train.Label.to_numpy(), num_classes=n_class, dtype='float32'
)
target = tf.data.Dataset.from_tensor_slices(target)

images= images.map(transform_img)

dataset = tf.data.Dataset.zip((images, target, target))

当我打电话给model.fit(dataset)

我收到以下错误:

ValueError: Layer model expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=<unknown> dtype=float32>]

但这应该可以根据:

tf.data with multiple inputs / outputs in Keras

有人能指出我的愚蠢吗?

谢谢!

编辑: 这解决了一些问题:

#reads in filepaths to images from dataframe train
images = tf.data.Dataset.from_tensor_slices(train.image.to_numpy())
#converts labels to one hot encoding vector
target = tf.keras.utils.to_categorical(train.Label.to_numpy(), num_classes=n_class, dtype='float32')
#reads in the image and resizes it
images= images.map(transform_img)
input_1 = tf.data.Dataset.zip((anchors, target))
dataset = tf.data.Dataset.zip((input_1, target))

我认为这是我们正在尝试的。但我得到目标的形状错误,它是 (n_class, 1) 而不仅仅是 (n_class,)

即fit 方法抛出此错误

ValueError: Shapes (n_class, 1) and (n_class, n_class) are incompatible

还有这个警告

input expected is (None, n_class) but received an input of (n_class, 1)

【问题讨论】:

    标签: python tensorflow tensorflow-datasets tf.data.dataset


    【解决方案1】:

    我已经根据 arcface 对解决方案进行了更改,你想要的是代码,我已经设法训练它

    第一个来自张量切片作为原始输入,我使用 mnist 进行测试

    def map_data(inputs, outputs):
        image = tf.cast(inputs['image_input'], tf.float32)
        image = image / 255.
        image = tf.expand_dims(image, axis=2)
        
        labels = tf.one_hot(outputs, 10)
        
        return {'image_input': image, 'label_input': labels}, labels
    
    dataset = tf.data.Dataset.from_tensor_slices(({
        'image_input': x_train, 'label_input': y_train
    }, y_train))
    dataset = dataset.map(map_data)
    dataset = dataset.batch(2)
    

    这是我尝试使用张量切片中的法线的第二种类型,然后我将其转换为多输入,因为两个法线标签都用于输入和输出

    def map_data(images, annot_labels):
        image = tf.cast(images, tf.float32)
        image = image / 255.
        image = tf.expand_dims(image, axis=2) # convert to 0 - 1 range
        
        labels = tf.one_hot(annot_labels, 10)
        
        return {'image_input': image, 'label_input': labels}, labels
    
    dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
    dataset = dataset.map(map_data)
    dataset = dataset.batch(2)
    

    【讨论】:

    • 嗨 Edwin,很高兴再次见到你,我也尝试了这个(实际上部分是由于我们昨天讨论的结果,但是对于不同的模型)但是当我这样做时,它说它需要两个输入但是只收到 1 个张量
    • 嗯,我明白了,等一下,我试试看
    • 嗨 Olli,我已经用你尝试实现的 arcface 层运行了这个,这个 1 根据我的测试工作
    • 顺便说一句,我正在使用 mnist,我不确定它是否适合您的数据集,因此请进行相应的更改
    • 这很好用,我非常感谢你,我今天大部分时间都在努力解决这个问题。谢谢!!如果你在苏黎世,我会请你喝啤酒
    【解决方案2】:

    我认为你应该这样做:

    target = tf.keras.utils.to_categorical(train.Label.to_numpy(), num_classes=n_class, dtype='float32')
        
    images_target = tf.data.Dataset.from_tensor_slices((train.A_image.to_numpy(), target))
    
    images_target = images_target.map(lambda x, y: (transform_img(x), y))
        
    
    target = tf.data.Dataset.from_tensor_slices(target)
        
    dataset = tf.data.Dataset.zip((images_target, target))
    

    【讨论】:

    • 非常感谢。我也找到了这个解决方案,压缩前者,然后压缩后者,至少几乎相同。但是后来我从第一个拉链中得到目标形状的错误,它最后增加了一个额外的尺寸
    • 我也刚刚尝试过使用 lambda 的方式(略有不同)得到以下错误: TypeError: () missing 1 required positional argument: 'y' 它可能是目标的结果是一个热编码? IE。超过 1 个输入?
    • 也许lambda x: (transform_img(x[0]), x[1]) 有效。哦,也许是 OE 的问题,不确定。
    • 你能用你正在尝试的东西编辑你的问题吗?通过 cmets 很难阅读。
    • 那之后你要添加批量大小吗?类似dataset = dataset.batch(32),只是为了排除一些选项。
    猜你喜欢
    • 2018-11-16
    • 2020-12-07
    • 2018-08-17
    • 2021-08-20
    • 2020-06-04
    • 1970-01-01
    • 2020-05-10
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多