您可以在此处的 TensorFlow 网站https://www.tensorflow.org/tutorials/images/data_augmentation 上找到使用 TF 2.x 的数据增强教程。
您还可以使用 ImageDataGenerator 库在 Tf 2.x 中执行数据扩充。
tf.keras.preprocessing.image.ImageDataGenerator
imagedatagenerator的示例代码sn-p
import tensorflow as tf
image = tf.keras.preprocessing.image.load_img('flower.jpeg')
image_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
#convert image to array
im_array = tf.keras.preprocessing.image.img_to_array(image)
img = im_array.reshape((1,) + im_array.shape)
#Generate the images
count = 0
for batch in image_datagen.flow(img, batch_size=1, save_to_dir ='image_gen' , save_prefix='flower', save_format='jpeg'):
count +=1
if count==5:
break
#Input image
import matplotlib.pylab as plt
image = plt.imread('flower.jpeg')
plt.imshow(image)
plt.show()
#After augmentation
import matplotlib.pylab as plt
image = plt.imread('image_gen/flower_0_1167.jpeg')
plt.imshow(image)
plt.show()