如何对一批图像应用相同的增强?
How can I apply the same augmentation to a batch of images?
我有一个视频数据集。由于数据集很小,我正在尝试扩充视频数据。我还没有找到任何关于增强视频的资源,所以我认为有用的是 -
- 从视频中提取所需的帧
- 对提取的帧应用数据增强
现在,假设我从一个视频中提取了 20 帧。为了让我的数据有意义,我将不得不对这 20 帧应用相同的增强。我怎样才能做到这一点?如果工作简单,我也愿意接受其他图书馆。
我猜测对 ImageDataGenerator.flow_from_directory(...)
参数进行一些更改就可以解决问题。这是 Keras 文档中的代码片段。
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",
)
提前致谢!
您可以使用 tf.data.Dataset
,并在批处理操作后应用转换。这将需要一些工作来制作您自己的目录迭代器(类似于 ),但这是它的本质:
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
。
我有一个视频数据集。由于数据集很小,我正在尝试扩充视频数据。我还没有找到任何关于增强视频的资源,所以我认为有用的是 -
- 从视频中提取所需的帧
- 对提取的帧应用数据增强
现在,假设我从一个视频中提取了 20 帧。为了让我的数据有意义,我将不得不对这 20 帧应用相同的增强。我怎样才能做到这一点?如果工作简单,我也愿意接受其他图书馆。
我猜测对 ImageDataGenerator.flow_from_directory(...)
参数进行一些更改就可以解决问题。这是 Keras 文档中的代码片段。
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",
)
提前致谢!
您可以使用 tf.data.Dataset
,并在批处理操作后应用转换。这将需要一些工作来制作您自己的目录迭代器(类似于
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
。