ROI pooling与ROI align

本文详细介绍了深度学习中用于目标检测的两种关键方法:ROIpooling和ROIalign。ROIpooling最早由Kaiming He提出,解决了深度卷积网络输入图像需要固定大小的问题。而ROIalign则是在Mask R-CNN中提出的改进版,通过避免量化误差提高了特征对齐的准确性。

1、ROI pooling
比较早出现这个是Kaiming He的"Spatial Pyramid Pooling in Deep convolutional Networks for Visual Recognition"。之所以要提出这个东东,主要是因为深度卷积网络的输入图像需要固定的大小,这是因为要满足最后的全连接层输入需要固定长度的需求。
在这里插入图片描述
上图就是作者实现roi pooling的方法。把最后一个卷积层和全连接层之间的池化层改造成spatial pyramid pooling(SPP)层。每个spatial bin采用池化操作(文章中采用最大值池化)。SPP输出的维度是kMkMkMkkk是最后一层卷积层输出的通道数,MMM是bin的个数。这就可以固定任意大小输入图像的输出维数。roi pooling涉及到量化。
Fast rcnn中roi pooling计算方法和spp计算类似。

2、ROI align
roi align是mask r-cnn文章中提出了,在图像分割任务中,roi pooling由于量化的存在,使得特征图中点的坐标和输入图像中点的坐标不能一一对应,因此才提出roi align方法的。
方法也很简单,和roi pooling过程一样,只是没有量化,计算的浮点数的值采用双线性插值进行计算。
在这里插入图片描述

### ROI Pooling ROI Align 的概念 #### ROI Pooling ROI (Region of Interest) Pooling 是一种用于将不同大小的输入转换成固定尺寸输出的技术。该方法通过将候选区域划分为相同数量的小块,然后对每一小块执行最大池化操作来获取固定维度的特征向量。这种方法确保了无论原始候选框的实际大小如何,最终得到的特征映射都具有相同的宽度高度。 然而,在实际应用中发现,由于采用了量化的方式确定子区域边界,这可能会引入一些误差,因为这些边界的坐标通常是浮点数而被强制转成了整数值[^1]。 ```python def roi_pooling(feature_map, rois, pooled_height, pooled_width): """ 实现简单的ROI Pooling算法 参数: feature_map -- 特征图 rois -- 候选区域列表 pooled_height -- 输出的高度 pooled_width -- 输出的宽度 返回: pooled_rois -- 经过pool后的rois """ num_rois = len(rois) channels = feature_map.shape[-1] pooled_rois = np.zeros((num_rois, pooled_height, pooled_width, channels)) for i in range(num_rois): h_start, w_start, h_end, w_end = map(int, rois[i]) # 将roi划分成pooled_height * pooled_width个小格子 bin_size_h = (h_end - h_start) / float(pooled_height) bin_size_w = (w_end - w_start) / float(pooled_width) for ph in range(pooled_height): for pw in range(pooled_width): h1 = int(np.floor(ph * bin_size_h + h_start)) w1 = int(np.floor(pw * bin_size_w + w_start)) h2 = int(np.ceil((ph + 1) * bin_size_h + h_start)) w2 = int(np.ceil((pw + 1) * bin_size_w + w_start)) # 执行max pooling pooled_rois[i, ph, pw, :] = np.max( feature_map[h1:h2, w1:w2], axis=(0, 1)) return pooled_rois ``` #### ROI Align 为了克服上述提到的问题,ROI Align 被设计出来作为改进方案。传统的ROI Pooling不同的是,ROI Align 不会简单地取最接近的位置来进行采样,而是采用双线性插值的方法精确计算出对应位置上的像素值。具体来说,对于每一个bin内的四个最近邻点,根据它们之间的相对距离加权求得目标位置处的真实响应值。这样做的好处是可以更准确地保留物体的空间信息而不受网格化的负面影响。 ```python def bilinear_interpolate(im, y, x): """Bilinear interpolation function.""" height, width = im.shape[:2] if y < 0 or y > height - 1 or x < 0 or x > width - 1: return 0 y_low = int(math.floor(y)) x_low = int(math.floor(x)) y_high = min(height - 1, math.ceil(y)) x_high = min(width - 1, math.ceil(x)) ly = y - y_low lx = x - x_low hy = 1. - ly hx = 1. - lx v1 = im[y_low, x_low] v2 = im[y_low, x_high] v3 = im[y_high, x_low] v4 = im[y_high, x_high] w1 = hy * hx w2 = hy * lx w3 = ly * hx w4 = ly * lx val = w1*v1 + w2*v2 + w3*v3 + w4*v4 return val def roi_align(feature_map, rois, pooled_height=7, pooled_width=7, sampling_ratio=-1): """ 实现简单的ROI Align算法 参数: feature_map -- 输入特征图 rois -- 区域建议框 pooled_height -- 池化后高度,默认为7 pooled_width -- 池化后宽度,默认为7 sampling_ratio -- 抽样的比例因子,默认自适应调整(-1) 返回: aligned_features -- 对齐后的特征矩阵 """ batch_size, _, feat_height, feat_width = feature_map.size() num_rois = rois.size()[0] device = feature_map.device aligned_features = torch.zeros([num_rois, feature_map.size(1), pooled_height, pooled_width]).to(device=device) for n_roi in range(num_rois): start_y, start_x, end_y, end_x = rois[n_roi].tolist() roi_width = max(end_x - start_x, 1.) roi_height = max(end_y - start_y, 1.) bin_size_h = 1. * roi_height / pooled_height bin_size_w = 1. * roi_width / pooled_width sample_num_h = ( sampling_ratio if sampling_ratio > 0 else ceil(bin_size_h)) + 1 sample_num_w = ( sampling_ratio if sampling_ratio > 0 else ceil(bin_size_w)) + 1 for ph in range(pooled_height): for pw in range(pooled_width): # 计算当前bin中心点相对于原图的比例偏移 yc = (start_y + ph * bin_size_h + ((sample_num_h - 1.) / 2.) * (bin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值