如何将imgaug扩充应用于使用dataset_from_directory创建的tensorflow数据集



我正在尝试使用imgaug将一些数据扩充应用于我现有的trainDataset。

数据集是使用dataset_from_directory创建的,如下所示。

trainDataset = tf.keras.utils.image_dataset_from_directory(
directory,
labels='inferred',
label_mode='int',
class_names=classNames,
color_mode='rgb',
batch_size=64,
image_size=(224, 224),
shuffle=True,
seed=seed,
validation_split=0.15,
subset='training',
interpolation='bilinear',
follow_links=False,
crop_to_aspect_ratio=False
)

我试图应用于数据集的imgaug如下所示

augmenter = iaa.Sequential([
iaa.Fliplr(0.5),
iaa.Affine(rotate=(-10, 10)),
iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}),
iaa.Crop(percent=(0, 0.1)),
iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
iaa.Multiply((0.8, 1.2), per_channel=0.2),
iaa.AddToHueAndSaturation((-20, 20))
])

我一辈子都不知道如何将其实际应用于我的数据集。我试过使用map,但它不起作用,因为增强器需要一个numpy数组?如有任何帮助,我们将不胜感激:)

这是我第一次发帖,所以如果我遗漏了任何重要的,我很抱歉

更新1:

我现在已经更改了我的代码,这样在应用任何增强之前数据集就不会被拆分,并围绕一些推荐给我的东西进行了更改。

dataset = tf.keras.utils.image_dataset_from_directory(
directory,
labels='inferred',
label_mode='int',
class_names=classNames,
color_mode='rgb',
batch_size=64,
image_size=(224, 224),
shuffle=True,
seed=seed,
interpolation='bilinear',
follow_links=False,
crop_to_aspect_ratio=False
)
augmenter = iaa.Sequential([
iaa.Fliplr(0.5),
iaa.Affine(rotate=(-10, 10)),
iaa.Affine(scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}),
iaa.Crop(percent=(0, 0.1)),
iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
iaa.Multiply((0.8, 1.2), per_channel=0.2),
iaa.AddToHueAndSaturation((-20, 20))
])
def augment(image, label):
image = tfds.as_numpy(image)
image = augmenter.augment_image(image)
image = tf.convert_to_tensor(image, dtype=tf.float32)
return image, label
augDataset = dataset.map(augment)

我现在得到以下错误,不知道它意味着什么。

必须为占位符张量"args_0"提供一个值,该值具有dtype float和shape[?,224224,3][{{node args_0}}]

感谢您的帮助:)

您似乎正在使用较旧的TensorFlow版本代码以及一些不推荐使用的API增强。

可以使用图像预处理或图像数据增强层进行增强。使用image_dataset_from_directory获取数据集后,可以在模型内部定义这些层,如下所示:

classifier = tf.keras.Sequential([
##data augmention layers    
layers.Resizing(IMG_SIZE, IMG_SIZE),
layers.Rescaling(1./255),
layers.RandomFlip("horizontal"),
layers.RandomZoom(0.1),

#model building layers
layers.Conv2D(32, (3, 3), input_shape = (IMG_SIZE, IMG_SIZE, 3), activation = 'relu'),
layers.MaxPooling2D(pool_size = (2, 2)),
layers.Conv2D(32, (3, 3), activation = 'relu'),
layers.MaxPooling2D(pool_size = (2, 2)),
layers.Flatten(),
layers.Dense(units = 128, activation = 'relu'),
layers.Dense(units = 1, activation = 'sigmoid')
])

请参考这个要点。

最新更新