美文网首页
faster_rcnn_pytorch中的roi_pooling

faster_rcnn_pytorch中的roi_pooling

作者: 悟空宝宝真帅 | 来源:发表于2018-06-05 19:09 被阅读0次

源码来自于https://github.com/longcw/faster_rcnn_pytorch
这次只看了roi_pooling文件夹下的代码,其他的需要编译的文件跟这个类似。

roi_pooling目录

roi_pooling目录

-src文件夹下是c和cuda版本的源码,其中roi_pooling的操作的foward是c和cuda版本都有的,而backward仅写了cuda版本的代码。
-functions文件夹下的roi_pool.py是继承了torch.autograd.Function类,实现RoI层的foward和backward函数。
-modules文件夹下的roi_pool.py是继承了torch.nn.Modules类,实现了对RoI层的封装,此时RoI层就跟ReLU层一样的使用了。
-_ext文件夹下还有个roi_pooling文件夹,这个文件夹是存储src中c,cuda编译过后的文件的,编译过后就可以被funcitons中的roi_pool.py调用了。

functions/roi_pool.py

# -*- coding:utf8 -*-
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.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_() #new是torch.tensor的方法
        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)  # permute = transform 也是torch.tensor的方法
            roi_pooling.roi_pooling_forward(ctx.pooled_height, ctx.pooled_width, ctx.spatial_scale,
                                            _features, rois, output) #调用_ext下的编译好的cpu版本函数
        else:
            roi_pooling.roi_pooling_forward_cuda(ctx.pooled_height, ctx.pooled_width, ctx.spatial_scale,
                                                 features, rois, output, ctx.argmax) #调用_ext下的编译好的gpu版本函数

        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) #这个地方就只有gpu版本的了

        return grad_input, None


modules\roi_pool.py

# -*- coding:utf8 -*-

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可以调用。

相关文章

  • faster_rcnn_pytorch中的roi_pooling

    源码来自于https://github.com/longcw/faster_rcnn_pytorch这次只看了ro...

  • Faster RCNN源码解读(2)-roi_pooling

    本系列的代码来自:https://github.com/jwyang/faster-rcnn.pytorch大家可...

  • 日语中的「中」的读法

    今天来讲讲日语中接尾词「中」的读音。 在日语里,「中」可用作接尾词接在名词之后,作接尾词使用时它有两种读音:「ちゅ...

  • 【乱世中的天堂】中

    清晨,我在一阵一阵富有节奏感的声音中起床了,八点多了,我向旁边撇了一眼,看到我的妻子早已起床,伴着熟悉的剁菜声中我...

  • 儿子中中的成长

    儿子中中一米八的个子,只要得空,就假装手拍篮球,做着各种球场上躲人、过人、上篮的动作,他爱篮球。他笑起来若隐若现单...

  • R中的对象(中)

    我们对R中的数据类型对象有了一定的了解,接下来就是对数据对象的简单操作。在excel里我们可以通过鼠标来实现,在R...

  • 《论语》中的成语(中)

    1.见贤思齐 【解释】:贤:德才兼备的人;齐:相等。见到德才兼备的人就想赶上他。 【出自】:《论语·里仁》:“见贤...

  • 记忆中的北京(中)

    故宫作为曾经握有最大权利,地位最尊贵的皇帝的行宫,其规模宏大,其建造复杂是我们无法想象的。 随着导游的介绍和讲解,...

  • 电影中的与现实中的

    电影中我们经常看到一些我们所希望看到的结局一样 但现实生活中跟剧中的差距如此之大 我总是幻想可以像电影中一样 有一...

  • 幻想中的美好,现实中的……

    阴雨️绵绵,像我现在的心情一样,总是想象着自己是个十全十美的人,只要是自己想做的事情都可以做到。长大后才知道...

网友评论

      本文标题:faster_rcnn_pytorch中的roi_pooling

      本文链接:https://www.haomeiwen.com/subject/ygumsftx.html