首页 > 解决方案 > 将pytorch中的roi池转换为nn层

问题描述

我有一个使用 ROI 池的 mlmodel,我正在使用它(改编自此处)(非 NN 层版本)

def forward(self, features, rois):
        batch_size, num_channels, data_height, data_width = features.size()
        num_rois = rois.size()[0]
        outputs = Variable(torch.zeros(num_rois, num_channels, self.pooled_height, self.pooled_width)).cuda()

        for roi_ind, roi in enumerate(rois):
            batch_ind = int(roi[0].data[0])
            roi_start_w, roi_start_h, roi_end_w, roi_end_h = np.round(
                roi[1:].data.cpu().numpy() * self.spatial_scale).astype(int)
            roi_width = max(roi_end_w - roi_start_w + 1, 1)
            roi_height = max(roi_end_h - roi_start_h + 1, 1)
            bin_size_w = float(roi_width) / float(self.pooled_width)
            bin_size_h = float(roi_height) / float(self.pooled_height)

            for ph in range(self.pooled_height):
                hstart = int(np.floor(ph * bin_size_h))
                hend = int(np.ceil((ph + 1) * bin_size_h))
                hstart = min(data_height, max(0, hstart + roi_start_h))
                hend = min(data_height, max(0, hend + roi_start_h))
                for pw in range(self.pooled_width):
                    wstart = int(np.floor(pw * bin_size_w))
                    wend = int(np.ceil((pw + 1) * bin_size_w))
                    wstart = min(data_width, max(0, wstart + roi_start_w))
                    wend = min(data_width, max(0, wend + roi_start_w))

                    is_empty = (hend <= hstart) or(wend <= wstart)
                    if is_empty:
                        outputs[roi_ind, :, ph, pw] = 0
                    else:
                        data = features[batch_ind]
                        outputs[roi_ind, :, ph, pw] = torch.max(
                            torch.max(data[:, hstart:hend, wstart:wend], 1)[0], 2)[0].view(-1)

        return outputs

我想将 pytorch 模型转换为 caffe,因此我需要将上面的转换为我使用下面的 NN 层(改编自此处

def forward(self, input, rois):
    output = []
    rois = rois.data.float()
    num_rois = rois.size(0)

    rois[:,1:].mul_(self.spatial_scale)
    rois = rois.long()
    for i in range(num_rois):
        roi = rois[i]
        im_idx = roi[0]
        im = input.narrow(0, im_idx, 1)[..., roi[2]:(roi[4]+1), roi[1]:(roi[3]+1)]
        op = nn.functional.adaptive_max_pool2d(input = im, output_size = self.size) 
        output.append(op)

    return torch.cat(tuple(output), dim=0)

返回的输出似乎与上述方法不匹配,即使它们执行相同的功能。我似乎陷入了僵局。如果我在上面犯了任何明显的错误,谁能指出?

标签: pythonmachine-learningneural-networkpytorch

解决方案


发现问题 - 与空间尺度相乘后的 rois 被四舍五入,必须在调用 long 之前调用 round 函数,像这样

rois = rois.data.float()
num_rois = rois.size(0)

rois[:,1:].mul_(self.spatial_scale)
rois = rois.round().long() ## Check this here !!

希望这对某人有帮助!


推荐阅读