[关闭]
@wanghuijiao 2021-01-28T14:18:27.000000Z 字数 3118 阅读 2869

数据增强:目标检测中数据不均衡可以使用的数据增强方法

实验报告


0. 选择标准

1. 什么是数据增强?有哪些数据增强方法?

2. 代码实现?决定尝试哪几种?实验设计、预判、结论?

2.1 Pytorch的torchvision.tranforms模块

2.2 Python的imgaug包

  1. pip install imgaug
  2. # 出现Shapely错误时,用这个
  3. pip install imgaug upgrade no-deps

2.3 albumentations 数据增强工具

  1. # pip 安装
  2. pip install albumentations
  3. # conda 安装,需先安装imgaug
  4. conda install -c conda-forge imgaug
  5. conda install albumentations -c albumentations
  1. # 1-导入库
  2. import albumentations as A
  3. import cv2
  4. # 2-定义增强Pipeline
  5. # format='pascal_voc','albumantations','coco','yolo';
  6. transform = A.Compose([
  7. A.RandomCrop(width=450, height=450),
  8. A.HorizontalFlip(p=0.5),
  9. A.RandomBrightnessContrast(p=0.2),
  10. ],bbox_param=A.BboxParams(format='yolo'))
  11. # 3-读图片和Bbox
  12. image = cv2.imread("/path/to/image.jpg")
  13. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  14. bboxes = [
  15. [23, 74, 295, 388, 'dog', 'animal'],
  16. [377, 294, 252, 161, 'cat', 'animal'],
  17. [333, 421, 49, 49, 'sports ball', 'item'],
  18. ]
  19. # 4-获取增强结果
  20. transformed = transform(image=image, bboxes=bboxes)
  21. transformed_image = transformed['image']
  22. transformed_bboxes = transformed['bboxes']
  23. # 展示一下高阶用法
  24. # 2-
  25. # min_area, 当增强后bbox面积小于min_area,该bbox会被丢弃,返回的bbox增强列表不包含该bbox;
  26. # min_visibility,(0-1),当增强后bbox面积占增强前bbox面积比例小于该值,那么该bbox同样被丢弃。
  27. transform = A.Compose([
  28. A.RandomCrop(width=450, height=450),
  29. A.HorizontalFlip(p=0.5),
  30. A.RandomBrightnessContrast(p=0.2),
  31. ], bbox_params=A.BboxParams(format='coco', label_fields=['class_labels', 'class_categories']))
  32. # 3-
  33. image = cv2.imread("/path/to/image.jpg")
  34. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  35. bboxes = [
  36. [23, 74, 295, 388],
  37. [377, 294, 252, 161],
  38. [333, 421, 49, 49],
  39. ]
  40. class_labels = ['cat', 'dog', 'parrot']
  41. class_categories = ['animal', 'animal', 'item']
  42. # 4-
  43. transformed = transform(image=image, bboxes=bboxes, class_labels=class_labels, class_categories=class_categories)
  44. transformed_image = transformed['image']
  45. transformed_bboxes = transformed['bboxes']
  46. transformed_class_labels = transformed['class_labels']
  47. transformed_class_categories = transformed['class_categories']

拓展:Yolov4中的数据增强策略

1. Random Earsing 随机擦除

2. Cutout

3. GridMask

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注