Fast-RCNN代码解读(1)

来源:互联网 发布:sql商务系统开发培训 编辑:程序博客网 时间:2024/06/15 16:55

Fast-RCNN代码解读(1)

这篇博文主要根据博主微薄的代码基础作一些简单的Fast-RCNN源码的解读,基本上就是一些代码阅读记录,写出来跟大家一起分享。这次主要记录一下测试的过程,和训练类似,Fast-RCNN中的测试过程主要由test_net.py,test.py及其他一些功能性python文件构成(比如,bbox_transform.py),其中test_net.py是测试的主要入口,功能是解析输入参数,包含了”__main__”,调用test.py中的test_net()函数,实现了整个测试过程。


Fast-RCNN测试代码解读

root/tools/test_net.py

root/lib/fast_rcnn/test.py

  • def im_detect(net, im, boxes=None)

    • 该函数实现了检测的功能,其中我比较关注的是如何在测试过程中将检测到的proposal变换到target_bbox。由于在Fast-RCNN中,bounding-box回归的过程实际上是回归了离target_bbox最近的box与其的变换(dx, dy, dw, dh),因此在测试过程中,需要将检测到的box通过回归得到的变换,得到最终的bbox。
  • box_deltas

    if cfg.TEST.BBOX_REG:    # cfg.TEST.BBOX_REG = {bool}True        # Apply bounding-box regression deltas        box_deltas = blobs_out['bbox_pred']        pred_boxes = bbox_transform_inv(boxes, box_deltas)        pred_boxes = clip_boxes(pred_boxes, im.shape)    else:        # Simply repeat the boxes, once for each class        pred_boxes = np.tile(boxes, (1, scores.shape[1]))

root/lib/fast_rcnn/bbox_transform.py

  • def bbox_transform_inv(boxes, deltas)
    • 该函数将selective search得到的proposal通过与测试过程中输出的变换deltas计算,得到最终的bbox。
    • 代码非常简单,如下所示:
def bbox_transform_inv(boxes, deltas):    if boxes.shape[0] == 0:        return np.zeros((0, deltas.shape[1]), dtype=deltas.dtype)    boxes = boxes.astype(deltas.dtype, copy=False)    widths = boxes[:, 2] - boxes[:, 0] + 1.0    heights = boxes[:, 3] - boxes[:, 1] + 1.0    ctr_x = boxes[:, 0] + 0.5 * widths    ctr_y = boxes[:, 1] + 0.5 * heights    dx = deltas[:, 0::4]    # start from 0 and jump by 4, [0, 4, 8, ...]    dy = deltas[:, 1::4]    dw = deltas[:, 2::4]    dh = deltas[:, 3::4]    pred_ctr_x = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis]    pred_ctr_y = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis]    pred_w = np.exp(dw) * widths[:, np.newaxis]    pred_h = np.exp(dh) * heights[:, np.newaxis]    pred_boxes = np.zeros(deltas.shape, dtype=deltas.dtype)    # x1    pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * pred_w    # y1    pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * pred_h    # x2    pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * pred_w    # y2    pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * pred_h    return pred_boxes
0 0
原创粉丝点击