【问题标题】:How can I apply the same augmentation to a batch of images?如何将相同的增强应用于一批图像?
【发布时间】:2021-03-28 22:48:40
【问题描述】:

我有一个视频数据集。由于数据集很小,我正在尝试扩充视频数据。我还没有找到任何关于增强视频的资源,所以我认为可行的是 -

  1. 从视频中提取所需帧
  2. 对提取的帧应用数据增强

现在,假设我从一个视频中提取了 20 帧。为了使我的数据有意义,我必须对这 20 帧应用相同的增强。我怎样才能做到这一点?如果它使工作变得容易,我也对其他库开放。

我猜想对 ImageDataGenerator.flow_from_directory(...) 参数进行一些更改就可以解决问题。这是 Keras 文档中的代码 sn-p。

ImageDataGenerator.flow_from_directory(
    directory,
    target_size=(256, 256),
    color_mode="rgb",
    classes=None,
    class_mode="categorical",
    batch_size=32,
    shuffle=True,
    seed=None,
    save_to_dir=None,
    save_prefix="",
    save_format="png",
    follow_links=False,
    subset=None,
    interpolation="nearest",
)

提前谢谢你!

【问题讨论】:

  • 尝试设置batch_size等于number _of_frames,或者创建自己的custom generator
  • 我不确定对同一视频中的图像应用相同的增强是否是这种情况下的最佳方法。虽然正如您所说,它确实更“有意义”,但我认为具有不同的增强可能会迫使网络真正关注数据序列中的不变量,并最终可能有助于过度拟合。但它可能确实需要更多数据。

标签: python tensorflow image-processing keras data-augmentation


【解决方案1】:

您可以使用tf.data.Dataset,并在批处理操作后应用转换。这将需要一些工作来制作您自己的目录迭代器(类似于this),但这是它的精髓:

import tensorflow as tf
import matplotlib.pyplot as plt
from skimage import data

cats = tf.concat([data.chelsea()[None, ...] for i in range(24)], axis=0)

test = tf.data.Dataset.from_tensor_slices(cats)


def augment(tensor):
    tensor = tf.cast(x=tensor, dtype=tf.float32)
    tensor = tf.divide(x=tensor, y=tf.constant(255.))
    tensor = tf.image.random_hue(image=tensor, max_delta=5e-1)
    tensor = tf.image.random_brightness(image=tensor, max_delta=2e-1)
    return tensor


test = test.batch(8).map(lambda x: augment(x))


fig = plt.figure()
plt.subplots_adjust(wspace=.1, hspace=.2)
images = next(iter(test))
for index, image in enumerate(images):
    ax = plt.subplot(4, 2, index + 1)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.imshow(tf.clip_by_value(image, clip_value_min=0, clip_value_max=1))
plt.show()

不是因为某种原因,这不适用于tf.image.random_flip_left_right。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    相关资源
    最近更新 更多