神经网络学习小记录27——数据预处理

古城微笑少年丶 2023-05-29 14:26 61阅读 0赞

神经网络学习小记录27——数据预处理

  • 学习前言
  • 处理长宽不同的图片
  • 数据增强
    • 1、在数据集内进行数据增强
    • 2、在读取图片的时候数据增强
    • 3、目标检测中的数据增强

学习前言

进行训练的话,如果直接用原图进行训练,也是可以的(就如我们最喜欢Mnist手写体),但是大部分图片长和宽不一样,直接resize的话容易出问题。
除去resize的问题外,有些时候数据不足该怎么办呢,当然要用到数据增强啦。
这篇文章就是记录我最近收集的一些数据预处理的方式。
在这里插入图片描述

处理长宽不同的图片

对于很多分类、目标检测算法,输入的图片长宽是一样的,如224,224、416,416等。
在这里插入图片描述
直接resize的话,图片就会失真。
在这里插入图片描述
但是我们可以采用如下的代码,使其用padding的方式不失真。

  1. from PIL import Image
  2. def letterbox_image(image, size):
  3. # 对图片进行resize,使图片不失真。在空缺的地方进行padding
  4. iw, ih = image.size
  5. w, h = size
  6. scale = min(w/iw, h/ih)
  7. nw = int(iw*scale)
  8. nh = int(ih*scale)
  9. image = image.resize((nw,nh), Image.BICUBIC)
  10. new_image = Image.new('RGB', size, (128,128,128))
  11. new_image.paste(image, ((w-nw)//2, (h-nh)//2))
  12. return new_image
  13. img = Image.open("2007_000039.jpg")
  14. new_image = letterbox_image(img,[416,416])
  15. new_image.show()

得到图片为:
在这里插入图片描述

数据增强

1、在数据集内进行数据增强

这个的意思就是可以直接增加图片的方式进行数据增强。
其主要用到的函数是:

  1. ImageDataGenerator(featurewise_center=False,
  2. samplewise_center=False,
  3. featurewise_std_normalization=False,
  4. samplewise_std_normalization=False,
  5. zca_whitening=False,
  6. zca_epsilon=1e-06,
  7. rotation_range=0,
  8. width_shift_range=0.0,
  9. height_shift_range=0.0,
  10. brightness_range=None,
  11. shear_range=0.0,
  12. zoom_range=0.0,
  13. channel_shift_range=0.0,
  14. fill_mode='nearest',
  15. cval=0.0,
  16. horizontal_flip=False,
  17. vertical_flip=False,
  18. rescale=None,
  19. preprocessing_function=None,
  20. data_format=None,
  21. validation_split=0.0,
  22. dtype=None)

对于我而言,常用的方法如下:

  1. datagen = ImageDataGenerator(
  2. rotation_range=10,
  3. width_shift_range=0.1,
  4. height_shift_range=0.1,
  5. shear_range=0.2,
  6. zoom_range=0.1,
  7. horizontal_flip=False,
  8. brightness_range=[0.1, 2],
  9. fill_mode='nearest')

其中,参数的意义为:
1、rotation_range:旋转范围
2、width_shift_range:水平平移范围
3、height_shift_range:垂直平移范围
4、shear_range:float, 透视变换的范围
5、zoom_range:缩放范围
6、horizontal_flip:水平反转
7、brightness_range:图像随机亮度增强,给定一个含两个float值的list,亮度值取自上下限值间
8、fill_mode:‘constant’,‘nearest’,‘reflect’或‘wrap’之一,当进行变换时超出边界的点将根据本参数给定的方法进行处理。

实际使用时可以利用如下函数生成图像:

  1. from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
  2. import os
  3. datagen = ImageDataGenerator(
  4. rotation_range=10,
  5. width_shift_range=0.1,
  6. height_shift_range=0.1,
  7. shear_range=0.2,
  8. zoom_range=0.1,
  9. horizontal_flip=False,
  10. brightness_range=[0.1, 2],
  11. fill_mode='nearest')
  12. trains = os.listdir("./train/")
  13. for index,train in enumerate(trains):
  14. img = load_img("./train/" + train)
  15. x = img_to_array(img)
  16. x = x.reshape((1,) + x.shape)
  17. i = 0
  18. for batch in datagen.flow(x, batch_size=1,
  19. save_to_dir='./train_out', save_prefix=str(index), save_format='jpg'):
  20. i += 1
  21. if i > 20:
  22. break

生成效果为:
在这里插入图片描述

2、在读取图片的时候数据增强

ImageDataGenerator是一个非常nice的增强方式,不过如果不想生成太多的图片,然后想要直接在读图的时候处理,也是可以的。
我们用到PIL中的ImageEnhance库。
1、亮度增强ImageEnhance.Brightness(image)
2、色度增强ImageEnhance.Color(image)
3、对比度增强ImageEnhance.Contrast(image)
4、锐度增强ImageEnhance.Sharpness(image)

在如下的函数中,可以通过改变Ehance函数中的参数实现不同的增强方式。

  1. import os
  2. import numpy as np
  3. from PIL import Image
  4. from PIL import ImageEnhance
  5. def Enhance_Brightness(image):
  6. # 变亮,增强因子为0.0将产生黑色图像,为1.0将保持原始图像。
  7. # 亮度增强
  8. enh_bri = ImageEnhance.Brightness(image)
  9. brightness = np.random.uniform(0.6,1.6)
  10. image_brightened = enh_bri.enhance(brightness)
  11. return image_brightened
  12. def Enhance_Color(image):
  13. # 色度,增强因子为1.0是原始图像
  14. # 色度增强
  15. enh_col = ImageEnhance.Color(image)
  16. color = np.random.uniform(0.4,2.6)
  17. image_colored = enh_col.enhance(color)
  18. return image_colored
  19. def Enhance_contrasted(image):
  20. # 对比度,增强因子为1.0是原始图片
  21. # 对比度增强
  22. enh_con = ImageEnhance.Contrast(image)
  23. contrast = np.random.uniform(0.6,1.6)
  24. image_contrasted = enh_con.enhance(contrast)
  25. return image_contrasted
  26. def Enhance_sharped(image):
  27. # 锐度,增强因子为1.0是原始图片
  28. # 锐度增强
  29. enh_sha = ImageEnhance.Sharpness(image)
  30. sharpness = np.random.uniform(0.4,4)
  31. image_sharped = enh_sha.enhance(sharpness)
  32. return image_sharped
  33. def Add_pepper_salt(image):
  34. # 增加椒盐噪声
  35. img = np.array(image)
  36. rows,cols,_=img.shape
  37. random_int = np.random.randint(500,1000)
  38. for _ in range(random_int):
  39. x=np.random.randint(0,rows)
  40. y=np.random.randint(0,cols)
  41. if np.random.randint(0,2):
  42. img[x,y,:]=255
  43. else:
  44. img[x,y,:]=0
  45. img = Image.fromarray(img)
  46. return img
  47. def Enhance(image_path, change_bri=1, change_color=1, change_contras=1, change_sha=1, add_noise=1):
  48. #读取图片
  49. image = Image.open(image_path)
  50. if change_bri==1:
  51. image = Enhance_Brightness(image)
  52. if change_color==1:
  53. image = Enhance_Color(image)
  54. if change_contras==1:
  55. image = Enhance_contrasted(image)
  56. if change_sha==1:
  57. image = Enhance_sharped(image)
  58. if add_noise==1:
  59. image = Add_pepper_salt(image)
  60. image.save("0.jpg")
  61. Enhance("2007_000039.jpg")

原图:
在这里插入图片描述
效果如下:
在这里插入图片描述

3、目标检测中的数据增强

在目标检测中如果要增强数据,并不是直接增强图片就好了,还要考虑到图片扭曲后框的位置。
也就是框的位置要跟着图片的位置进行改变。
原图:
在这里插入图片描述
增强后:
在这里插入图片描述

  1. from PIL import Image, ImageDraw
  2. import numpy as np
  3. from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
  4. def rand(a=0, b=1):
  5. return np.random.rand()*(b-a) + a
  6. 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):
  7. '''random preprocessing for real-time data augmentation'''
  8. line = annotation_line.split()
  9. image = Image.open(line[0])
  10. iw, ih = image.size
  11. h, w = input_shape
  12. box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])
  13. # resize image
  14. new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)
  15. scale = rand(.7, 1.3)
  16. if new_ar < 1:
  17. nh = int(scale*h)
  18. nw = int(nh*new_ar)
  19. else:
  20. nw = int(scale*w)
  21. nh = int(nw/new_ar)
  22. image = image.resize((nw,nh), Image.BICUBIC)
  23. # place image
  24. dx = int(rand(0, w-nw))
  25. dy = int(rand(0, h-nh))
  26. new_image = Image.new('RGB', (w,h), (128,128,128))
  27. new_image.paste(image, (dx, dy))
  28. image = new_image
  29. # flip image or not
  30. flip = rand()<.5
  31. if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)
  32. # distort image
  33. hue = rand(-hue, hue)
  34. sat = rand(1, sat) if rand()<.5 else 1/rand(1, sat)
  35. val = rand(1, val) if rand()<.5 else 1/rand(1, val)
  36. x = rgb_to_hsv(np.array(image)/255.)
  37. x[..., 0] += hue
  38. x[..., 0][x[..., 0]>1] -= 1
  39. x[..., 0][x[..., 0]<0] += 1
  40. x[..., 1] *= sat
  41. x[..., 2] *= val
  42. x[x>1] = 1
  43. x[x<0] = 0
  44. image_data = hsv_to_rgb(x) # numpy array, 0 to 1
  45. # correct boxes
  46. box_data = np.zeros((max_boxes,5))
  47. if len(box)>0:
  48. np.random.shuffle(box)
  49. box[:, [0,2]] = box[:, [0,2]]*nw/iw + dx
  50. box[:, [1,3]] = box[:, [1,3]]*nh/ih + dy
  51. if flip: box[:, [0,2]] = w - box[:, [2,0]]
  52. box[:, 0:2][box[:, 0:2]<0] = 0
  53. box[:, 2][box[:, 2]>w] = w
  54. box[:, 3][box[:, 3]>h] = h
  55. box_w = box[:, 2] - box[:, 0]
  56. box_h = box[:, 3] - box[:, 1]
  57. box = box[np.logical_and(box_w>1, box_h>1)] # discard invalid box
  58. if len(box)>max_boxes: box = box[:max_boxes]
  59. box_data[:len(box)] = box
  60. return image_data, box_data
  61. if __name__ == "__main__":
  62. line = r"F:\Collection\yolo_Collection\keras-yolo3-master\VOCdevkit/VOC2007/JPEGImages/00001.jpg 738,279,815,414,0"
  63. image_data, box_data = get_random_data(line,[416,416])
  64. left, top, right, bottom = box_data[0][0:4]
  65. img = Image.fromarray((image_data*255).astype(np.uint8))
  66. draw = ImageDraw.Draw(img)
  67. draw.rectangle([left, top, right, bottom])
  68. img.show()

发表评论

表情:
评论列表 (有 0 条评论,61人围观)

还没有评论,来说两句吧...

相关阅读