优图yolo-v2 loss解析(tensorflow)
生活随笔
收集整理的這篇文章主要介紹了
优图yolo-v2 loss解析(tensorflow)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
最近在做yolo檢測模型的8bit落地工作,因需求手擼了優圖的yolov2的loss,做個記錄方便日后查看。
本篇只介紹核心的loss部分,其余詳細的代碼放到github上面去了,詳見https://github.com/XhtZz/yolo-v2-loss
loss詳解
def yolo_loss( target,output,coord_mask, object_detections, object_no_detections_gt_anch, gt_coord, gt_conf,object_scale=5.0,no_object_scale=1.0, coordinates_scale=3.0):#共有5個anchor,16為網路輸入到輸出的下采樣倍率,由于最后的iou計算以輸出feature map為基準#所以除以16,五個anchor等長寬分別為1,3,5,7,10anchors = tf.constant([[16, 16], [48, 48], [80, 80], [112,112], [160,160]], tf.float32) / tf.constant(16.0)anchors_num = tf.shape(anchors)[0]output_shape = tf.shape(output) #(n,c,h,w)batch_size = output_shape[0]nH = output_shape[2]nW = output_shape[3]#reshape output as [batch,anchor,x-y-w-h-conf,w*h]output = tf.reshape(output,[batch_size, anchors_num, -1, nW*nH])#將output的x、y歸一化到0-1coord_xy = tf.sigmoid(output[:, :, :2]) # x,ycoord_wh = output[:, :, 2:4] # w,hpre_coord = tf.concat([coord_xy,coord_wh], 2)#將置信度歸一化到0-1pre_conf = tf.sigmoid(output[:, :, 4])# Create prediction boxeslin_x = tf.reshape(tf.tile(tf.reshape(tf.linspace(0.0, tf.cast(nW - 1,tf.float32), nW),[1,nW]),[nH,1]),[nW*nH])lin_y = tf.reshape(tf.tile(tf.reshape(tf.linspace(0.0, tf.cast(nH - 1,tf.float32), nH),[nH,1]),[1,nW]),[nW*nH])#將歸一化后的x、y(相當于bias)分別加上對應所在網格的x、y整數值pred_boxes_x = tf.reshape(pre_coord[:, :, 0] + lin_x,[-1,1])pred_boxes_y = tf.reshape(pre_coord[:, :, 1] + lin_y,[-1,1])#解碼過程 對應于后面的編碼過程anchor_w = tf.reshape(anchors[:, 0],[anchors_num,1])anchor_h = tf.reshape(anchors[:, 1],[anchors_num,1])pred_boxes_w = tf.reshape(tf.exp(pre_coord[:, :, 2]) * anchor_w,[-1,1])pred_boxes_h = tf.reshape(tf.exp(pre_coord[:, :, 3]) * anchor_h,[-1,1])#x,y,w,h#將predict boxes reshape成anchors*x*y行4列的tensor(4列指x,y,w,h)pred_boxes = tf.concat([pred_boxes_x,pred_boxes_y,pred_boxes_w,pred_boxes_h],1)#根據predict box及groundtruth box計算iou獲得一定沒有物體所在的網格(每個網格點代表一個box)object_no_detections_gt_pre = build_targets_masks(pred_boxes, target, batch_size, anchors_num, nH, nW, thresh = 0.6, reduction = 16.0)#object_no_detections_gt_anch為傳入參數,由gt所在的best anchor位置的x,y確定,乘以#object_no_detections_gt_pre后將object_no_detections_gt_anch為0的位置強制置零,即gt#的位置no_object一定為0object_no_detections = object_no_detections_gt_pre * object_no_detections_gt_anch# coord#coord_mask為2-gt_w*gt_h/w*h,為小框加大系數,使小框更容易檢測到,坐標相關的loss都要乘coord_mask = tf.tile(coord_mask,[1,1,2,1])pre_coord_center, gt_coord_center = pre_coord[:, :, :2], gt_coord[:, :, :2]pre_coord_wh, gt_coord_wh = pre_coord[:, :, 2:], gt_coord[:, :, 2:]# Compute losses#中心點loss使用二值交叉熵loss,寬高loss采用smooth l1 lossloss_coord_center = 2.0 * 1.0 * coordinates_scale * tf.reduce_sum(coord_mask * tf.keras.backend.binary_crossentropy(gt_coord_center, pre_coord_center))loss_coord_wh = 2.0 * 1.5 * coordinates_scale * tf.reduce_sum(coord_mask * tf.losses.huber_loss(gt_coord_wh, pre_coord_wh, reduction=tf.losses.Reduction.NONE))loss_coord = loss_coord_center + loss_coord_wh#正負樣本置信度均采用二值交叉熵lossloss_conf_pos = 1.0 * object_scale * tf.reduce_sum(object_detections * tf.keras.backend.binary_crossentropy(gt_conf, pre_conf))loss_conf_neg = 1.0 * no_object_scale * tf.reduce_sum(object_no_detections * tf.keras.backend.binary_crossentropy(gt_conf, pre_conf))loss_conf = loss_conf_pos + loss_conf_negloss_tot = (loss_coord + loss_conf) / tf.cast(batch_size,tf.float32)return loss_totloss設計邏輯圖
loss的整個設計我用圖畫出來了,好好理解一下應該不難,從左向右從上到下對應上面代碼好好看一下吧。。。。
總結
以上是生活随笔為你收集整理的优图yolo-v2 loss解析(tensorflow)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模型占用GPU显存计算
- 下一篇: QNNPACK高性能前向内核库全面剖析—