python - 将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)
返回的输出似乎与上述方法不匹配,即使它们执行相同的功能。我似乎陷入了僵局。如果我在上面犯了任何明显的错误,谁能指出?
解决方案
发现问题 - 与空间尺度相乘后的 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 !!
希望这对某人有帮助!
推荐阅读
- python - 计算由两列过滤的平均数据框
- java - 如何配置java调用批处理,自动确认“你想让这个编对这台计算机进行修改......”?
- java - UDP 套接字在 reply.getData 上需要帮助
- python - 如何将 csv 文件上传到 Dash 以创建网络图?
- android - 接近传感器在其 LifeCycleOwner 为 onDestroy 时保持注册状态
- angular - 我创建了一个表单,在获取数据后将其存储在数组中,并希望在表单下方的表格中显示
- javascript - 在chart.js中更改图例框的颜色和x网格线的颜色
- c# - Equivalent AES encryption code in c# .net core
- epplus - 如何在新的 EPPlus Excel 包中实现 SetBorder 和 VAlign HAlign
- python - 在 django 中重新加载虚拟环境