學習Faster R-CNN代碼roi_pooling(二)


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.csdn.net/weixin_43872578/article/details/86628515

在這里插入圖片描述

functions/roi_pool.py

 1 import torch
 2 from torch.autograd import Function
 3 from .._ext import roi_pooling
 4 import pdb
 5 
 6 # 重寫函數實現RoI層的正向傳播和反向傳播 modules中的roi_pool實現層的封裝
 7 
 8 class RoIPoolFunction(Function):
 9     def __init__(ctx, pooled_height, pooled_width, spatial_scale):
10         #ctx is a context object that can be used to stash information for backward computation
11         #上下文對象,可用於存儲信息以進行反向計算
12         ctx.pooled_width = pooled_width
13         ctx.pooled_height = pooled_height
14         ctx.spatial_scale = spatial_scale
15         ctx.feature_size = None
16 
17     def forward(ctx, features, rois): 
18         ctx.feature_size = features.size()          
19         batch_size, num_channels, data_height, data_width = ctx.feature_size
20         num_rois = rois.size(0)
21         output = features.new(num_rois, num_channels, ctx.pooled_height, ctx.pooled_width).zero_()
22         ctx.argmax = features.new(num_rois, num_channels, ctx.pooled_height, ctx.pooled_width).zero_().int()
23         ctx.rois = rois
24         if not features.is_cuda:
25             _features = features.permute(0, 2, 3, 1)
26             roi_pooling.roi_pooling_forward(ctx.pooled_height, ctx.pooled_width, ctx.spatial_scale,
27                                             _features, rois, output)
28         else:
29             roi_pooling.roi_pooling_forward_cuda(ctx.pooled_height, ctx.pooled_width, ctx.spatial_scale,
30                                                  features, rois, output, ctx.argmax)
31 
32         return output
33 
34     def backward(ctx, grad_output):
35         assert(ctx.feature_size is not None and grad_output.is_cuda)
36         batch_size, num_channels, data_height, data_width = ctx.feature_size
37         grad_input = grad_output.new(batch_size, num_channels, data_height, data_width).zero_()
38 
39         roi_pooling.roi_pooling_backward_cuda(ctx.pooled_height, ctx.pooled_width, ctx.spatial_scale,
40                                               grad_output, ctx.rois, grad_input, ctx.argmax)
41 
42         return grad_input, None

modules/roi_pool.py

 1 from torch.nn.modules.module import Module
 2 from ..functions.roi_pool import RoIPoolFunction
 3 
 4 # 對roi_pooling層的封裝,就是ROI Pooling Layer了
 5 
 6 class _RoIPooling(Module):
 7     def __init__(self, pooled_height, pooled_width, spatial_scale):
 8         super(_RoIPooling, self).__init__()
 9 
10         self.pooled_width = int(pooled_width)
11         self.pooled_height = int(pooled_height)
12         self.spatial_scale = float(spatial_scale)
13 
14     def forward(self, features, rois):
15         return RoIPoolFunction(self.pooled_height, self.pooled_width, self.spatial_scale)(features, rois)
16         # 直接調用了functions中的函數,此時已經實現了foward,backward操作

剩下的src,_ext文件的代碼就可以自己讀讀了,就是用c,cuda對roi_pooling實現了foward和backward,目的就是為了讓python可以調用。

【未完,待更新…】

ref:https://www.jianshu.com/p/d674e16ce896

https://blog.csdn.net/weixin_43872578/article/details/86616801


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM