右侧有目录,节约时间直达
前提:
先确认一下你的数据增强方法是否如下:
def get_random_data(image, input_shape, random=True, jitter=.5, hue=.1, sat=1.5, val=1.5, proc_img=True)
如果你想看每个图像处理环节之后,图像变成什么样了,或者用在自己的论文中等等,可在原基础上加上两行代码
plt.imshow(image)
plt.show()
色域扭曲环节之后查看图片的写法:
img = np.array(image_data, dtype=np.float64)
img2 = Image.fromarray(np.uint8(img))
plt.imshow(img2)
plt.show()
示例:
# 对图像进行缩放并且进行长和宽的扭曲
new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(1 - jitter, 1 + jitter)
scale = rand(.7, 1.3)
if new_ar < 1:
nh = int(scale * h)
nw = int(nh * new_ar)
else:
nw = int(scale * w)
nh = int(nw / new_ar)
image = image.resize((nw, nh), Image.BICUBIC)
plt.imshow(image)
plt.show()
同理,剩下的这几个环节一样,直接加就行:
# place image—将图像多余的部分加上灰条
# flip image or not-翻转图像
如果你也有色域扭曲这个环节,就不能直接加了,因为生成的是RGB数据,不是一张图片了。这时候写法就变了,我的色域扭曲环节代码如下:
# distort image-色域扭曲
hue = rand(-hue, hue)
sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
x = rgb_to_hsv(np.array(image) / 255.)
x[..., 0] += hue
x[..., 0][x[..., 0] > 1] -= 1
x[..., 0][x[..., 0] < 0] += 1
x[..., 1] *= sat
x[..., 2] *= val
x[x > 1] = 1
x[x < 0] = 0
image_data = hsv_to_rgb(x) * 255 # numpy array, 0 to 1
在后面加的代码如下:
img = np.array(image_data, dtype=np.float64)
img2 = Image.fromarray(np.uint8(img))
plt.imshow(img2)
plt.show()