1

我正在尝试使用 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>]

但这应该根据:

Keras 中具有多个输入/输出的 tf.data

有人能指出我的愚蠢吗?

谢谢!

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

#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)
4

2 回答 2

3

我已经根据弧面对解决方案进行了更改,您想要的是代码,我已经设法对其进行了训练

第一个来自张量切片作为原始输入,我使用 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)
于 2021-05-21T15:39:40.210 回答
1

我认为你应该这样做:

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))
于 2021-05-21T15:02:38.893 回答