使用tf.image
对图片进行数据增强
读入图片
from PIL import Image
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
img = np.array(Image.open('test.jpg').resize((473, 473)), dtype=np.float32)/255.
- 随机左右翻转
augimg = tf.image.random_flip_left_right(img)
plt.imshow(augimg)
- 随机上下翻转
augimg = tf.image.random_flip_up_down(img)
plt.imshow(augimg)
- 随机亮度
augimg = tf.image.random_brightness(img, max_delta=0.5)
plt.imshow(augimg)
- 随机对比度
augimg = tf.image.random_contrast(img, lower=0, upper=3)
plt.imshow(augimg)
- 随机颜色抖动
augimg = tf.image.random_hue(img, 0.2)
plt.imshow(augimg)
- 随机饱和度
augimg = tf.image.random_saturation(img, 0, 3)
plt.imshow(augimg)