学习Faster R-CNN代码roi_pooling(二)

本文深入解析RoI Pooling层的工作原理,介绍其在目标检测任务中的关键作用。通过分析源码,详细解释了RoI Pooling层的前向和后向传播过程,以及如何在PyTorch框架中实现这一层。文章还提供了详细的代码解读,帮助读者理解各变量的含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

roi_pooling理解起来比较简单,所以我就先看了一下这部分的代码。

roi_pooling目录下
在这里插入图片描述
在这里插入图片描述
-src文件夹下是c和cuda版本的源码。

-functions文件夹下的roi_pool.py是继承了torch.autograd.Function类,实现RoI层的foward和backward函数。class RoIPoolFunction(Function)。

-modules文件夹下的roi_pool.py是继承了torch.nn.Modules类,实现了对RoI层的封装,此时RoI层就跟ReLU层一样的使用了。class _RoIPooling(Module)。

-_ext文件夹下还有个roi_pooling文件夹,这个文件夹是存储src中c,cuda编译过后的文件的,编译过后就可以被funcitons中的roi_pool.py调用了。

具体代码:
如下图所示:为roi_pooling/src/roi_pooling.c文件中的一段,可以根据该代码了解到各变量包含哪些内容。如果只看py代码,有些地方真不知道包含哪些信息,看起来很费劲,仔细看了下C代码,解决了自己的疑惑。

rois[0] num_rois即roi的个数;
rois[1] size_rois即大小;
features[0] batch_size,批大小;
features[1] data_height,高;
features[2] data_width,宽;
features[3] num_channels,通道数。

细致一点的理解看我的另一篇博客:https://blog.youkuaiyun.com/weixin_43872578/article/details/86628515
在这里插入图片描述

functions/roi_pool.py

import torch
from torch.autograd import Function
from .._ext import roi_pooling
import pdb

# 重写函数实现RoI层的正向传播和反向传播 modules中的roi_pool实现层的封装

class RoIPoolFunction(Function):
    def __init__(ctx, pooled_height, pooled_width, spatial_scale):
	    #ctx is a context object that can be used to stash information for backward computation
		#上下文对象,可用于存储信息以进行反向计算
        ctx.pooled_width = pooled_width
        ctx.pooled_height = pooled_height
        ctx.spatial_scale = spatial_scale
        ctx.feature_size = None

    def forward(ctx, features, rois): 
        ctx.feature_size = features.size()          
        batch_size, num_channels, data_height, data_width = ctx.feature_size
        num_rois = rois.size(0)
        output = features.new(num_rois, num_channels, ctx.pooled_height, ctx.pooled_width).zero_()
        ctx.argmax = features.new(num_rois, num_channels, ctx.pooled_height, ctx.pooled_width).zero_().int()
        ctx.rois = rois
        if not features.is_cuda:
            _features = features.permute(0, 2, 3, 1)
            roi_pooling.roi_pooling_forward(ctx.pooled_height, ctx.pooled_width, ctx.spatial_scale,
                                            _features, rois, output)
        else:
            roi_pooling.roi_pooling_forward_cuda(ctx.pooled_height, ctx.pooled_width, ctx.spatial_scale,
                                                 features, rois, output, ctx.argmax)

        return output

    def backward(ctx, grad_output):
        assert(ctx.feature_size is not None and grad_output.is_cuda)
        batch_size, num_channels, data_height, data_width = ctx.feature_size
        grad_input = grad_output.new(batch_size, num_channels, data_height, data_width).zero_()

        roi_pooling.roi_pooling_backward_cuda(ctx.pooled_height, ctx.pooled_width, ctx.spatial_scale,
                                              grad_output, ctx.rois, grad_input, ctx.argmax)

        return grad_input, None

modules/roi_pool.py

from torch.nn.modules.module import Module
from ..functions.roi_pool import RoIPoolFunction

# 对roi_pooling层的封装,就是ROI Pooling Layer了

class _RoIPooling(Module):
    def __init__(self, pooled_height, pooled_width, spatial_scale):
        super(_RoIPooling, self).__init__()

        self.pooled_width = int(pooled_width)
        self.pooled_height = int(pooled_height)
        self.spatial_scale = float(spatial_scale)

    def forward(self, features, rois):
        return RoIPoolFunction(self.pooled_height, self.pooled_width, self.spatial_scale)(features, rois)
        # 直接调用了functions中的函数,此时已经实现了foward,backward操作

剩下的src,_ext文件的代码就可以自己读读了,就是用c,cuda对roi_pooling实现了foward和backward,目的就是为了让python可以调用。

【未完,待更新…】

reference:

  1. https://www.jianshu.com/p/d674e16ce896
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值