图像增强就是对图像的简单变形,解决因拍照角度不同,引起的图片变形。
image_gen_train.fit(x_train)这里的fit需要输入一个思维的数据,所以要对x_train进行reshape,把60000张28行28列的数据,转换为60000张28行28列的单通道数据(即为1),单通道为灰度值。
import tensorflow as tf
from matplotlib import pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0],28,28,1)
image_gen_train = ImageDataGenerator(
rescale = 1. / 255,
rotation_range = 45,
width_shift_range = .15,
height_shift_range = .15,
horizontal_flip = False,
zoom_range = 0.5
)
image_gen_train.fit(x_train)
print("x_train",x_train.shape)
x_train_subset1 = np.squeeze(x_train[:12])
print("x_train_subset1",x_train_subset1.shape)
print("x_train",x_train.shape)
x_train_subset2 = x_train[:12]
print("x_train_subset2",x_train_subset2.shape)
fig = plt.figure(figsize=(20,2))
plt.set_cmap('gray')
for i in range(0,len(x_train_subset1)):
ax = fig.add_subplot(1,12,i + 1)
ax.imshow(x_train_subset1[i])
fig.suptitle('Subset of Original Traaining Images',fontsize = 20)
plt.show()
fig = plt.figure(figsize = (20,2))
for x_batch in image_gen_train.flow(x_train_subset2,batch_size=12,shuffle=False):
for i in range(0,12):
ax = fig.add_subplot(1,12,i + 1)
ax.imshow(np.squeeze(x_batch[i]))
fig.suptitle('Augmented Image',fontsize = 20)
plt.show()
break;