数据增强:旋转图像和零值

数据挖掘 神经网络 数据增强
2022-02-20 08:25:29

许多人旋转图像以创建更大的神经网络训练集。对于大多数网络,所有输入必须具有相同的大小,因此图像旋转函数必须裁剪新旋转的图像以匹配输入大小。所以,说你有32×32分辨率图像并进行 45 度旋转。一些输出图像看起来像菱形,边角有黑色(零值)。所以,我的问题是:你应该不理会这些零值还是以某种方式改变它们,如果是这样,怎么做?

1个回答

将值保持为零会给您的网络带来一些偏差。鉴于您对大部分数据集都有这种角点效应,您不希望网络识别出使角点变黑的可能性很高。因此,你应该填充它们,你可以扩展边缘,做反射,包裹。您还可以执行一些更复杂的功能,例如平均图像中的几个补丁,然后将它们放置在缺失的区域中。

Keras 有一个非常好的功能,可以为您完成所有这些工作。

import numpy as np
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
%matplotlib inline

# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.astype('float32')

# set up your data generator
datagen = ImageDataGenerator(
    featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=15,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    vertical_flip = False)

# Fit the generator using your data
datagen.fit(X_train.reshape((len(X_train), 28, 28, 1)))

# Black images
image = X_train[5]
plt.imshow(image,  cmap='gray')
plt.show()

plt.figure(figsize=(12,12))
plt.subplot(4, 4, 1)
plt.imshow(image.reshape((28,28)),  cmap='gray')

for j in range(15):
    augmented = datagen.random_transform(image.reshape((28,28,1)))
    plt.subplot(4, 4, j+2)
    plt.imshow(augmented.reshape((28,28)),  cmap='gray')
plt.tight_layout()
plt.show()

# White images
image = -1*X_train[5]
plt.imshow(image,  cmap='gray')
plt.show()

plt.figure(figsize=(12,12))
plt.subplot(4, 4, 1)
plt.imshow(image.reshape((28,28)),  cmap='gray')

for j in range(15):
    augmented = datagen.random_transform(image.reshape((28,28,1)))
    plt.subplot(4, 4, j+2)
    plt.imshow(augmented.reshape((28,28)),  cmap='gray')
plt.tight_layout()
plt.show()