【目标检测算法实现系列】Keras实现Faster R-CNN算法(三)

本文是Keras实现Faster R-CNN目标检测算法系列的第三篇,详细介绍了从RPN网络到ROIPooling层的实现过程,包括应用回归预测调整anchor位置、非极大值抑制选择有效anchor,以及构造精分类和精回归的训练数据的步骤。后续将进行模型训练和预测。
摘要由CSDN通过智能技术生成

【目标检测算法实现系列】Keras实现Faster R-CNN算法(一)

【目标检测算法实现系列】Keras实现Faster R-CNN算法(二)

在此之前,我们主要实现了相关数据的解析,预处理等准备工作,以及对应Faster RCNN的相关网络模块搭建。接下来我们接着实现其他部分。

一、从RPN网络到ROIPooling层

在上一篇中,我们实现了一个自定义的ROIPooling层,这次我们看下如何建立RPN与ROIpool层之间的联系。下面,我们看下如何代码实现,通过RPN网络的输出,来指定对应ROIPing层的输入。

def rpn_to_roi(rpn_cls_layer, rpn_regr_layer, C, use_regr=True, max_boxes=300,overlap_thresh=0.9):
    '''
    建立rpn网络与roi pooling层的连接
    通过rpn网络的输出,找出对应的roi
    :param rpn_cls_layer:  rpn网络的分类输出
    :param rpn_regr_layer:  rpn网络的回归输出
    :param C:
    :param dim_ordering:
    :param use_regr:
    :param max_boxes:
    :param overlap_thresh:
    :return:
    '''
    regr_layer = rpn_regr_layer / C.std_scaling

    anchor_sizes = C.anchor_box_scales
    anchor_ratios = C.anchor_box_ratios

    assert rpn_cls_layer.shape[0] == 1
    (rows, cols) = rpn_cls_layer.shape[1:3]

    curr_layer = 0
    # A.shape = (4个在feature_map上的对应位置信息(左上角和右下角坐标), feature_map_height, feature_map_wigth, k(9))
    A = np.zeros((4, rpn_cls_layer.shape[1], rpn_cls_layer.shape[2], rpn_cls_layer.shape[3]))
    for anchor_size in anchor_sizes:
        for anchor_ratio in anchor_ratios:

            anchor_x = (anchor_size * anchor_ratio[0])/C.rpn_stride   #对应anchor在feature map上的宽度
            anchor_y = (anchor_size * anchor_ratio[1])/C.rpn_stride   #对应anchor在feature map上的高度
            # if dim_ordering == 'th':
            #     regr = regr_layer[0, 4 * curr_layer:4 * curr_layer + 4, :, :]
            # else:
            #     regr = regr_layer[0, :, :, 4 * curr_layer:4 * curr_layer + 4]  #当前anchor对应回归值
            #     regr = np.transpose(regr, (2, 0, 1))
            regr = regr_layer[0, :, :, 4 * curr_layer:4 * curr_layer + 4]  # 当前anchor对应回归值
            X, Y = np.meshgrid(np.arange(cols), np.arange(rows))

            A[0, :, :, curr_layer] = X - anchor_x/2   #左上点横坐标
            A[1, :, :, curr_layer] = Y - anchor_y/2   #左上纵横坐标
            A[2, :, :, curr_layer] = anchor_x   #暂时存储anchor 宽度
            A[3, :, :, curr_layer] = anchor_y   #暂时存储anchor 高度

            if use_regr:
                #通过rpn网络的回归层的预测值,来调整anchor位置
                A[:, :, :, curr_layer] = apply_regr_np(A[:, :, :, curr_layer], regr)

            A[2, :, :, curr_layer] = np.maximum(1, A[2, :, :, curr_layer])
            A[3, :, :, curr_layer] = np.maximum(1, A[3, :, :, curr_layer])
            A[2, :, :, curr_layer] += A[0, :, :, curr_layer]  #右下角横坐标
            A[3, :, :, curr_layer] += A[1, :, :, curr_layer]  #右下角纵坐标

            #确保anchor不超过feature map尺寸
            A[0, :, :, curr_layer] = np.maximum(0, A[0, :, :, curr_layer])
      
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Keras实现Faster R-CNN旋转目标检测算法可以按以下步骤进行: 1. 数据预处理:将训练数据转换为网络需要的格式,包括图片大小的调整、数据增强等等。 2. 构建模型:搭建Faster R-CNN网络模型,包括特征提取层、RPN层、ROI Pooling层、分类和回归层等。 3. 编译模型:设置模型的优化器、损失函数等参数。 4. 训练模型:对构建好的模型进行训练,并保存训练好的权重。 5. 模型评估:使用测试数据对训练好的模型进行评估,计算模型的精度、召回率等指标。 以下是一个基于Keras实现Faster R-CNN旋转目标检测算法的示例代码: ``` # 数据预处理 # TODO: 数据预处理代码 # 构建模型 input_shape = (None, None, 3) img_input = Input(shape=input_shape) shared_layers = nn.nn_base(img_input, trainable=True) # RPN网络 num_anchors = len(config.RPN_ANCHOR_RATIOS) * len(config.ANGLE_BINS) rpn = nn.rpn(shared_layers, num_anchors) # ROI Pooling层 roi_input = Input(shape=(config.TRAIN_ROIS_PER_IMAGE, 5)) roi_pooling = PyramidROIAlign([config.POOL_SIZE, config.POOL_SIZE], name="roi_align")([shared_layers, roi_input]) # 分类和回归层 x = TimeDistributed(Flatten(name='flatten'))(roi_pooling) x = TimeDistributed(Dense(4096, activation='relu', name='fc1'))(x) x = TimeDistributed(Dropout(0.5))(x) x = TimeDistributed(Dense(4096, activation='relu', name='fc2'))(x) x = TimeDistributed(Dropout(0.5))(x) # 分类和回归输出 cls_output = TimeDistributed(Dense(config.NUM_CLASSES, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(config.NUM_CLASSES))(x) angle_output = TimeDistributed(Dense(num_anchors * config.NUM_ANGLES, activation='linear', kernel_initializer='zero'), name='dense_angle_{}'.format(num_anchors * config.NUM_ANGLES))(x) bbox_output = TimeDistributed(Dense(num_anchors * 4, activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(4))(x) # 编译模型 model = Model([img_input, roi_input], [cls_output, angle_output, bbox_output]) model.compile(optimizer=Adam(lr=config.LEARNING_RATE), loss=[losses.class_loss(), losses.angle_loss(), losses.rpn_regress_loss(config.NUM_ANCHORS)]) # 训练模型 # TODO: 训练模型代码 # 模型评估 # TODO: 模型评估代码 ``` 需要注意的是,在实现旋转目标检测时,需要对RoI Pooling和NMS等部分进行修改,以支持旋转矩形的处理。具体实现可以参考上述项目中的代码和论文《R2CNN: Rotational Region CNN for Orientation Robust Scene Text Detection》。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值