CIFAR-10 Keras 图像数据仅针对一张图像的增强效果
CIFAR-10 Keras image data augmentation effect for one image only
我想在一张图片上展示不同数据增强(随机缩放、旋转和平移)的效果。我绘制了 x_train 中的第一张图片,但是第二张图似乎没有任何变化。
我想我用错了datagen.flow,请指教。谢谢
from matplotlib import pyplot as plt
from tensorflow.python.keras.datasets import cifar10
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x1=x_train[0]
print(x1.shape)
plt.imshow(x1)
plt.show()
# set up image augmentation
datagen = ImageDataGenerator(
rotation_range=180, # Randomly rotate by degrees
width_shift_range=0.2, # For translating image vertically
height_shift_range=0.2, # For translating image horizontally
horizontal_flip=True,
rescale=None,
fill_mode='nearest'
)
datagen.fit(x_train)
# see example augmentation images
x_batch = datagen.flow(x_train)
x2=x_batch[0]
print(x2.shape)
x2 的输出形状是 (32, 32, 32, 3) 这就是我无法绘制它的原因。为什么是这样的尺寸,我该怎么办?
datagen.flow()
实际上 returns 来自 x_train
的(增强)批次,它不会影响 x_train
就地。你需要这样做:
x_batch = datagen.flow(x_train)[0]
img = x_batch[0] / 255
plt.imshow(img)
plt.show()
感谢 Djib2011 的建议。我发现它是因为该函数默认会随机播放图像,所以我们可以设置 shuffle = false 来保留索引。
x_batch = datagen.flow(x_train,shuffle=False)[0]
print(x_batch.shape)
x2=x_batch[0]
plt.imshow((x2.astype(np.uint8)))
plt.show()
我想在一张图片上展示不同数据增强(随机缩放、旋转和平移)的效果。我绘制了 x_train 中的第一张图片,但是第二张图似乎没有任何变化。
我想我用错了datagen.flow,请指教。谢谢
from matplotlib import pyplot as plt
from tensorflow.python.keras.datasets import cifar10
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x1=x_train[0]
print(x1.shape)
plt.imshow(x1)
plt.show()
# set up image augmentation
datagen = ImageDataGenerator(
rotation_range=180, # Randomly rotate by degrees
width_shift_range=0.2, # For translating image vertically
height_shift_range=0.2, # For translating image horizontally
horizontal_flip=True,
rescale=None,
fill_mode='nearest'
)
datagen.fit(x_train)
# see example augmentation images
x_batch = datagen.flow(x_train)
x2=x_batch[0]
print(x2.shape)
x2 的输出形状是 (32, 32, 32, 3) 这就是我无法绘制它的原因。为什么是这样的尺寸,我该怎么办?
datagen.flow()
实际上 returns 来自 x_train
的(增强)批次,它不会影响 x_train
就地。你需要这样做:
x_batch = datagen.flow(x_train)[0]
img = x_batch[0] / 255
plt.imshow(img)
plt.show()
感谢 Djib2011 的建议。我发现它是因为该函数默认会随机播放图像,所以我们可以设置 shuffle = false 来保留索引。
x_batch = datagen.flow(x_train,shuffle=False)[0]
print(x_batch.shape)
x2=x_batch[0]
plt.imshow((x2.astype(np.uint8)))
plt.show()