augment_hsv作为yolov5的默认数据增量方式之一,试图在hsv颜色空间上对图片进行变换,达到数据增量的效果。那么什么是hsv颜色空间呢?在日常生活中我们描述一件物品的颜色的时候通常会说"它是什么颜色(H)?","颜色深不深(S)?","亮不亮(V)?",这其实就是在表述hsv颜色空间,可见他相比rgb颜色空间更符合人们对颜色的认知。对于hsv颜色空间网上有大量的资料(如:https://zhuanlan.zhihu.com/p/67930839),非本文的重点,这里只是就yolov5的代码和默认配置来直观地了解一下augment_hsv的增量效果。
import yaml
import matplotlib.pyplot as plt
#color = sns.color_palette()
import cv2
import numpy as np
# 图形出现在Notebook里而不是新窗口
%matplotlib inline
下面我直接导入yolov5中超参数配置文件,不做任何修改。
# # Hyperparameters for COCO training from scratch
# # python train.py --batch 40 --cfg yolov5m.yaml --weights '' --data coco.yaml --img 640 --epochs 300
# # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
# lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
# lrf: 0.2 # final OneCycleLR learning rate (lr0 * lrf)
# momentum: 0.937 # SGD momentum/Adam beta1
# weight_decay: 0.0005 # optimizer weight decay 5e-4
# warmup_epochs: 3.0 # warmup epochs (fractions ok)
# warmup_momentum: 0.8 # warmup initial momentum
# warmup_bias_lr: 0.1 # warmup initial bias lr
# box: 0.05 # box loss gain
# cls: 0.5 # cls loss gain
# cls_pw: 1.0 # cls BCELoss positive_weight
# obj: 1.0 # obj loss gain (scale with pixels)
# obj_pw: 1.0 # obj BCELoss positive_weight
# iou_t: 0.20 # IoU training threshold
# anchor_t: 4.0 # anchor-multiple threshold
# # anchors: 3 # anchors per output layer (0 to ignore)
# fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
# hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
# hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
# hsv_v: 0.4 # image HSV-Value augmentation (fraction)
# degrees: 0.0 # image rotation (+/- deg)
# translate: 0.1 # image translation (+/- fraction)
# scale: 0.5 # image scale (+/- gain)
# shear: 0.0 # image shear (+/- deg)
# perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
# flipud: 0.0 # image flip up-down (probability)
# fliplr: 0.5 # image flip left-right (probability)
# mosaic: 1.0 # image mosaic (probability)
# mixup: 0.0 # image mixup (probability)
hyp_file="/home/zw/zuosi/projects/yolov5-v4.0/yolov5/data/hyp.scratch.yaml"
with open(hyp_file) as fp:
hyp = yaml.load(fp)
对augment_hsv函数我略做改动,让它能够返回调整之后的图片,而不直接覆盖输入图片,为的是后面多运行几次看看效果。
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
dtype = img.dtype # uint8
x = np.arange(0, 256, dtype=np.int16)
lut_hue = ((x * r[0]) % 180).astype(dtype)
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
img_hsv = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)
return img_hsv
选一张bdd100k的图片运行多次看一下增量效果,并和原图对比一下。
path="../diamond_20210119/images/train/20e2659a-2e3fb6de.jpg"
img = cv2.imread(path)
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
img_hsv = augment_hsv(img,hyp['hsv_h'],hyp['hsv_s'],hyp['hsv_v'])
img_rgb = cv2.cvtColor(img_hsv,cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
img_hsv = augment_hsv(img,hyp['hsv_h'],hyp['hsv_s'],hyp['hsv_v'])
img_rgb = cv2.cvtColor(img_hsv,cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
img_hsv = augment_hsv(img,hyp['hsv_h'],hyp['hsv_s'],hyp['hsv_v'])
img_rgb = cv2.cvtColor(img_hsv,cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)