【程序】数据预处理和数据增强:预处理以保证图⽚不失真;
数据增强来扩充数据集规模
预处理:
图⽚直接resize失真,可以使⽤padding的⽅式避免。
from PIL import Image
def letterbox_image(image, size):
# 对图⽚进⾏resize,使图⽚不失真。在空缺的地⽅进⾏padding
中秋夜iw, ih = image.size
w, h = size
scale = min(w/iw, h/ih)
nw = int(iw*scale)
nh = int(ih*scale)
image = size((nw,nh), Image.BICUBIC)
new_image = w('RGB', size, (128,128,128))
new_image.paste(image, ((w-nw)//2, (h-nh)//2))
return new_image
img = Image.open("2007_000039.jpg")
new_image = letterbox_image(img,[416,416])
new_image.show()
数据增强:
1. 在数据集内进⾏数据增强:
ImageDataGenerator(featurewi_center=Fal,
samplewi_center=Fal,
featurewi_std_normalization=Fal,
samplewi_std_normalization=Fal,
zca_whitening=Fal,
助教心得体会zca_epsilon=1e-06,
rotation_range=0,
width_shift_range=0.0,
height_shift_range=0.0,
brightness_range=None,
shear_range=0.0,
zoom_range=0.0,
channel_shift_range=0.0,
fill_mode='nearest',
cval=0.0,
horizontal_flip=Fal,
vertical_flip=Fal,
rescale=None,
preprocessing_function=None,
data_format=None,
validation_split=0.0,
dtype=None)
常⽤的⽅法:
datagen = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.2,
zoom_range=0.1,
horizontal_flip=Fal,
brightness_range=[0.1, 2],
fill_mode='nearest')
实际使⽤时程序:
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img import os
datagen = ImageDataGenerator(
rotation_range=10,
微波炉煮汤圆width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.2,
zoom_range=0.1,
horizontal_flip=Fal,
brightness_range=[0.1, 2],
fill_mode='nearest')
trains = os.listdir("./train/")
for index,train in enumerate(trains):
img = load_img("./train/" + train)
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
i = 0
for batch in datagen.flow(x, batch_size=1,
save_to_dir='./train_out', save_prefix=str(index), save_format='jpg'):
i += 1
if i > 20:
break
2. 在读取图⽚的时候进⾏数据增强
import os
import numpy as np
from PIL import Image
from PIL import ImageEnhance
def Enhance_Brightness(image):
# 变亮,增强因⼦为0.0将产⽣⿊⾊图像,为1.0将保持原始图像。
# 亮度增强
抗疫征文enh_bri = ImageEnhance.Brightness(image)
brightness = np.random.uniform(0.6,1.6)
image_brightened = hance(brightness)
return image_brightened
def Enhance_Color(image):
# ⾊度,增强因⼦为1.0是原始图像
# ⾊度增强
enh_col = ImageEnhance.Color(image)
color = np.random.uniform(0.4,2.6)
image_colored = hance(color)
南阳旅游景点
return image_colored
def Enhance_contrasted(image):
# 对⽐度,增强因⼦为1.0是原始图⽚
# 对⽐度增强
enh_con = ImageEnhance.Contrast(image)
contrast = np.random.uniform(0.6,1.6)
image_contrasted = hance(contrast)
return image_contrasted
def Enhance_sharped(image):
# 锐度,增强因⼦为1.0是原始图⽚
# 锐度增强
enh_sha = ImageEnhance.Sharpness(image)
sharpness = np.random.uniform(0.4,4)
image_sharped = hance(sharpness)
公务员报名系统return image_sharped
def Add_pepper_salt(image):
# 增加椒盐噪声
img = np.array(image)
rows,cols,_=img.shape
random_int = np.random.randint(500,1000)
for _ in range(random_int):
x=np.random.randint(0,rows)
y=np.random.randint(0,cols)
if np.random.randint(0,2):
img[x,y,:]=255
el:
img[x,y,:]=0
img = Image.fromarray(img)
return img
def Enhance(image_path, change_bri=1, change_color=1, change_contras=1, change_sha=1, add_noi=1): #读取图⽚
image = Image.open(image_path)
if change_bri==1:
image = Enhance_Brightness(image)
if change_color==1:拿堂
image = Enhance_Color(image)
if change_contras==1:
image = Enhance_contrasted(image)
if change_sha==1:
image = Enhance_sharped(image)
吉他英雄if add_noi==1:
image = Add_pepper_salt(image)
image.save("0.jpg")
image.save("0.jpg")
Enhance("xxx.jpg")
3. ⽬标检测中的数据增强
要考虑到图⽚扭曲后框的位置,框的位置要跟着图⽚的位置进⾏改变。
from PIL import Image, ImageDraw
import numpy as np
lors import rgb_to_hsv, hsv_to_rgb
def rand(a=0, b=1):
return np.random.rand()*(b-a) + a
def get_random_data(annotation_line, input_shape, random=True, max_boxes=20, jitter=.3, hue=.1, sat=1.5, val=1.5, proc_img=True): '''random preprocessing for real-time data augmentation'''
line = annotation_line.split()
image = Image.open(line[0])
iw, ih = image.size
h, w = input_shape
box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])
# resize image
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)
el:
nw = int(scale*w)
nh = int(nw/new_ar)
image = size((nw,nh), Image.BICUBIC)
# place image
dx = int(rand(0, w-nw))
dy = int(rand(0, h-nh))
new_image = w('RGB', (w,h), (128,128,128))
new_image.paste(image, (dx, dy))
image = new_image
# flip image or not
flip = rand()<.5
if flip: image = anspo(Image.FLIP_LEFT_RIGHT)
# distort image
hue = rand(-hue, hue)
sat = rand(1, sat) if rand()<.5 el 1/rand(1, sat)
val = rand(1, val) if rand()<.5 el 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) # numpy array, 0 to 1
# correct boxes
box_data = np.zeros((max_boxes,5))
if len(box)>0:
np.random.shuffle(box)
box[:, [0,2]] = box[:, [0,2]]*nw/iw + dx
box[:, [1,3]] = box[:, [1,3]]*nh/ih + dy
if flip: box[:, [0,2]] = w - box[:, [2,0]]
box[:, 0:2][box[:, 0:2]<0] = 0
box[:, 2][box[:, 2]>w] = w
box[:, 3][box[:, 3]>h] = h
box_w = box[:, 2] - box[:, 0]
box_h = box[:, 3] - box[:, 1]
box = box[np.logical_and(box_w>1, box_h>1)] # discard invalid box
if len(box)>max_boxes: box = box[:max_boxes]
box_data[:len(box)] = box
return image_data, box_data
if __name__ == "__main__":
line = r"F:\Collection\yolo_Collection\keras-yolo3-master\VOCdevkit/VOC2007/JPEGImages/00001.jpg 738,279,815,414,0" image_data, box_data = get_random_data(line,[416,416])
left, top, right, bottom = box_data[0][0:4]
img = Image.fromarray((image_data*255).astype(np.uint8))
draw = ImageDraw.Draw(img)
img.show()