图解 YoloV2

整个训练分两部分

  1. 对 DarkNet19 的预训练
  2. 基于 DarkNet19 进行物体检测训练

原图尺寸:input_shape

标签:原始标签 box 为 (class, xmin, ymin, xmax, ymax) 每个值都是用原图片归一化( [0,1])

yolov2 分类

Darknet19 训练

预处理

论文中提到 random crops, rotation, hue, saturation, exposure shift 等,但实际代码是最常用的预处理方式,四个角剪切 + 中心,翻转,再四个角 + 中心

训练

用 ImageNet 数据集经过 DarkNet19 + avgpool + softmax 之后,得到 1000 分类。

训练过程

  1. 在 224x224x3 以 0.1 的学习速率,weight decay 0.0005 momentum 0.9 训练 160 epoch;
  2. 448x448x3 上以 learning rate 10-3 次方继续训练 10 epoch

yolov2 检测

物体检测训练

图片尺寸 :input_shape = [height, width]

预处理

图片预处理

resize 为 416x416 并除以 255 进行像素级别归一化

标签预处理

  1. boxes 转换为 (x,y, w,h, class) 格式,x, y, w, h 除以图片 input_shape 进行归一化
  2. (x, y, w, h) 乘以 output_shape
  3. 将 box 坐标 resize 到 output_shape 得到 box_output,此时, box_output 的 x, y 中心正好落在 output 的某一个 grid cell
  4. box_output 中的每个元素 box 与所有 anchor 计算 IOU,找到 IoU 最大值。
  5. 根据 IOU 最大值,记录 detectors_mask[i, j, anchor_index] = 1,其中 i = floor(box_output[1]), j = floor(box_output[0]),anchor_index 为最大 IOU 对应的 anchor 索引
  6. 根据 IOU 最大值,记录 true_box 为 (box_output[1] - j, box_ouput[0] - i, log(box_output[2:4] / anchor), box_class),anchor_index 为最大 IOU 对应的 anchor 索引

由于每个 grid cell 对应 B 个 anchor,因此,上面的所有 anchor 指 B 个 anchor

detectors_mask 记录了每一个 grid cell 对应的 box 与哪个 anchor 的 IOU 最大。

true_box 记录了每个 box 相对于 grid cell 的偏移。

基础网络

经过 DarkNet19 之后,得到 output,维度为 output_shape ([batch_size, height, width, num_anchors, 5 + num_classes]),其中最后一维依次为 [x,y,w,h, confidence, class_one_hot]

编码

grid : [1, output_shape[1], output_shape[2], 1, 2] 其中 2 为 output_shape 的索引

对于 grid,比如 output_shape 为的 height 为 4, width 为 3

grid 为 (0, 0), (0, 1), (0,2) (1,0) … (3, 2)

anchor : [1, 1, 1, num_anchor, 2]

pre_yx = (grid + sigmoid(output[…, :2]) )/ output_shape[1:3]
pre_hw = exp(output[…, 2:4]) * anchor/ output_shape[1:3]
pred_box = [pre_yx, pred_hw]
pre_box_confidence = sigmoid(output_confidence)
pre_box_class = sofmax(output_class_one_hot)

此时 pre_* 也记录了相对于每个 grid cell 的偏移

Loss

  1. 将 pre_* 和 true_box 都转为 [ymin, xmin, ymax, xmax] 的格式
  2. 计算 pre_* 与 true_box 的 IoU,IoU 大于 0.6,用 object_detections 记录该 grid 包含对象
no_object_scale = 1
object_scale = 5
no_object_loss = no_object_scale * (1 - object_detections) * (1 - detectors_mask)  * (-pre_confidence)^2
objects_loss = (object_scale * detectors_mask * square(1 - pred_confidence)
confidence_loss = no_object_loss + objects_loss
classification_loss = class_scale * detectors_mask * square(matching_classes - pred_class_prob)
classification_loss = one_hot(matching_classes[..., 4], num_classes)
matching_boxes = matching_true_boxes[..., 0:4]
coordinates_loss = coordinates_scale * detectors_mask * square(matching_boxes - pred_boxes)
total_loss = 0.5 * (sum(confidence_loss) + sum(classification_loss) + sum(coordinates_loss))

训练参数

160 epoch

learning rate 10-3 60(10-4) 90(10-5)

weight decay 0.0005 momentum 0.9

data augment : 与 Yolo 相同

当看到 detection 标签的时候, 反向传播包括 detection 和 classification 的 loss,看到 classification 标签的时候,只计算反向传播 classification 的 loss

验证

yolov2 验证

预处理

参考物体检测训练部分

基础网络

经过 DarkNet19 之后,得到 box, box_confidence, box_class_probs,记录了预测 box 的坐标,box 分数,box所属分类 。维度依次为 ([batch_size, height, width, num_anchors, 4],[batch_size, height, width, num_anchors, 1],[batch_size, height, width, num_anchors, num_classes])))

过滤

  1. box_scores = box_confidence * box_class_probs
  2. box_classes 为 box_scores 最大值对应类 K
  3. box_class_scores 为 box_scores 属于分类 K 的概率值
  4. box_class_scores 大于阈值(0.6) 对应 box,scores, classes 中的元素保留,其余删除。

恢复

box 恢复到输入图片大小

NMS

box, box_class_scores 根据 IoU 0.5 进行 NMS 过滤,最多保留 10 个。

以上,就是 YoloV2 的基本实现。

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值