1. 資料增強
基於影象的深度學習演算法,通常需要資料增強,比較常規的就是翻轉,旋轉,影象裁剪
在目標檢測中,對進行變換,還會涉及到框的變化,尤其時對影象進行resize成相同大小時,需要對框進行相應的縮放
1.1讀取影象&讀取框(目標位置)
image = image.open(line[0])
box = np.array([np.array(list(map(int, box.split(',')))) for box in line[1:]]) #(n, 5)1.2影象resize&框的縮放
image = image.resize((nw, nh), image.bicubic) #按一定比例縮放# 複製在(416, 416)的灰色背景圖上,從而實現影象大小一致
new_image = image.new('rgb', (w, h), (128,128,128))
new_image.paste(image, (dx, dy))
image = new_image
box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx1.3影象翻轉&框的翻轉box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
if flip: image = image.transpose(image.flip_left_right)
if flip: box[:, [0, 2]
box[:, 0:2][box[:, 0:2] < 0] = 01.5 影象色域變換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
x = cv2.cvtcolor(np.array(image, np.float32)/255, cv2.color_rgb2hsv)x[..., 0] += hue*360
x[..., 0][x[..., 0]>1] -= 1
x[..., 0][x[..., 0]<1] += 1
x[..., 1] *= sat
x[..., 2] *= val
x[x[:,:,0] > 360, 0] = 360
x[:, :, 1:][x[:, :, 1:]>1] = 1
x[x<0] = 0
image_data = cv2.cvtcolor(x, cv2.color_hsv2rgb)2. 真實框編碼
真實框需要轉化成與網路模型的輸出維度一致的矩陣,這樣才能計算損失
以乙個batch_size 為例,網路模型的輸出為[(b, 13, 13, 3*(num_classes+5)), (b, 26, 26, 3*(num_classes+5)), (b, 52, 52, 3*(num_classes+5))]
所以需要將真實框編碼為[(b, 13, 13, 3,(num_classes+5)), (b, 26, 26, 3,(num_classes+5)), (b, 52, 52, 3,(num_classes+5))] 形式,
y_true = [np.zeros((m, grid_shape[l][0], grid_shape[l][1], len(anchor_mask[l]), 5+num_classes), dtype='float32') for l in range(num_layers)]以(13,13)為例,有三個anchor框與真實框對應,所以維度中存在乙個3, 哪個anchor框與真實框的iou大,就將對應的位置的對應channel設定為存在真實框,對應位置,框的中心點落在其grid_cell內
for b in range(m):wh = boxes_wh[b, valid_mask[b]]
if len(wh) == 0:
continue
# (n, 1, 2) n代表一張圖的框數, 2代表(w,h)
wh = np.expand_dims(wh, -2)
box_maxes = wh / 2.
box_mins = -box_maxes
#(n, 9, 2)
intersect_mins = np.maximum(box_mins, anchor_mins) # 逐位比較(broadcast)
intersect_maxes = np.minimum(box_maxes, anchor_maxes)
intersect_wh = np.maximum(intersect_maxes - intersect_mins, 0.)
#(n, 9)
intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1]
#(n, 1)
box_area = wh[..., 0] * wh[..., 1]
#(1, 9)
anchor_area = anchors[..., 0] * anchors[..., 1]
#(n, 9)
iou = intersect_area / (box_area + anchor_area - intersect_area)
#(n,)
best_anchor = np.argmax(iou, axis=-1)
for n, index in enumerate(best_anchor):
for l in range(num_layers):
if index in anchor_mask[l]:
i = np.floor(true_boxes[b, n, 0] * grid_shape[l][1]).astype('int32')
# print("i=", i)
j = np.floor(true_boxes[b, n, 1] * grid_shape[l][1]).astype('int32')
# print("j=", j)
k = anchor_mask[l].index(index)
c = true_boxes[b, n, 4].astype('int32')
# 座標
y_true[l][b, j, i, k, 0:4] = true_boxes[b, n, 0:4]
# 置信度
y_true[l][b, j, i, k, 4] = 1
# 類別
y_true[l][b, j, i, k, 5 + c] = 1
3. 生成迭代器
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes, mosaic=false):n =len(annotation_lines)
i = 0
flag =true
while
true:
image_data =
box_data =
for b in
range(batch_size):
if i == 0
: np.random.shuffle(annotation_lines)
ifmosaic:
if flag and (i+4) image, box = get_random_data_with_mosaic(annotation_lines[i:i+4
], input_shape)
i = (i+1) %n
else
: image, box =get_random_data(annotation_lines[i], input_shape)
i = (i+1) %n
flag = bool(1 -flag)
else
: image, box =get_random_data(annotation_lines[i], input_shape)
i = (i+1) %n
image_data =np.array(image_data)
box_data =np.array(box_data)
y_true =preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
yield [image_data, *y_true], np.zeros(batch_size)
在yolov4中用了乙個特殊的影象增強trick,mosaic,
將四張拼接成一張,這樣可以產生大量樣本,通過拼接,使影象的正樣本數量增多,可以減小樣本不平衡造成的影響
目標檢測資料書籤
目標檢測進化史 object detection and classification using r cnns faster rcnn 理論 1 faster rcnn詳解 物體檢測 faster r cnn 一 載入訓練資料 物體檢測 faster r cnn 二 基於resnet的faster...
目標檢測 資料增強
一 目標檢測 yolo v4的related work部分精簡的介紹了目標檢測部分。目標檢測大概分為one stage和two stage部分。其中two stage部分主要包括r cnn系列。one stage則包括anchor based和anchor free兩類方法。其中anchor bas...
目標檢測網路 YOLO V4(一)
最近,yolo v4的開源掀起了一波熱潮,根據給出的資料yolo v4實現了速度和精度的雙向突破,吊打了一系列的目標檢測網路。因此,考慮後續感測器融合的目標跟蹤研究的需要,研究yolo v4是不可或缺的部分,並且先學習github中的開源demo,再研究該網路的原理。1.基於linux下執行,首先執...