【发布时间】:2021-04-11 01:34:29
【问题描述】:
我正在训练一个 CNN,并通过以下定义通过 Keras 层应用数据增强:
data_augmentation = keras.Sequential([
layers.experimental.preprocessing.RandomRotation(factor=0.4, fill_mode="wrap"),
layers.experimental.preprocessing.RandomTranslation(height_factor=0.2, width_factor=0.2, fill_mode="wrap"),
layers.experimental.preprocessing.RandomFlip("horizontal"),
layers.experimental.preprocessing.RandomContrast(factor=0.2),
layers.experimental.preprocessing.RandomHeight(factor=0.2),
layers.experimental.preprocessing.RandomWidth(factor=0.2)
])
以下是相关代码的 sn-ps:
def process(x, y):
x = DATASETS_DIR + "/" + x + ".jpg"
x = tf.io.read_file(x)
x = tf.image.decode_jpeg(x, channels=3)
x = tf.image.resize(x, [299, 299])
x = layers.experimental.preprocessing.Rescaling(1./255)(x)
return x, y
def process_with_augmentation(x, y):
x, y = process(x, y)
x = data_augmentation(x)
return x, y
train_ds = train_ds.map(process_with_augmentation, num_parallel_calls=tf.data.experimental.AUTOTUNE)
validation_ds = validation_ds.map(process, num_parallel_calls=tf.data.experimental.AUTOTUNE)
如果我在process_with_augmentation() 中注释掉x = data_augmentation(x),则代码可以正常工作。如果我不注释掉,我会收到以下错误:
ValueError: Shape must be rank 4 but is rank 3 for '{{node sequential/random_rotation/transform/ImageProjectiveTransformV3}} = ImageProjectiveTransformV3[dtype=DT_FLOAT, fill_mode="WRAP", interpolation="BILINEAR"](rescaling/add, sequential/random_rotation/rotation_matrix/concat, sequential/random_rotation/transform/strided_slice, sequential/random_rotation/transform/fill_value)' with input shapes: [299,299,3], [299,8], [2], [].
关于如何解决这个问题的任何想法?
【问题讨论】:
标签: tensorflow keras tensorflow-datasets keras-layer data-augmentation