本文主要是介绍easy-Fpn源码解读(五):rpn,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- easy-Fpn源码解读(五):rpn
- region_proposal_network.py代码解析
easy-Fpn源码解读(五):rpn
region_proposal_network.py代码解析
from typing import Tuple, Listimport numpy as np
import torch
from torch import nn, Tensor
from torch.nn import functional as Ffrom bbox import BBox
from nms.nms import NMSclass RegionProposalNetwork(nn.Module):def __init__(self, num_features_out: int, anchor_ratios: List[Tuple[int, int]], anchor_scales: List[int], pre_nms_top_n: int, post_nms_top_n: int):super().__init__()self._features = nn.Sequential(nn.Conv2d(in_channels=num_features_out, out_channels=512, kernel_size=3, padding=1),nn.ReLU())# 通道数从256维升至512维self._anchor_ratios = anchor_ratiosself._anchor_scales = anchor_scalesnum_anchor_ratios = len(self._anchor_ratios)num_anchor_scales = len(self._anchor_scales)num_anchors = num_anchor_ratios * num_anchor_scalesself._pre_nms_top_n = pre_nms_top_nself._post_nms_top_n = post_nms_top_nself._objectness = nn.Conv2d(in_channels=512, out_channels=num_anchors * 2, kernel_size=1)self._transformer = nn.Conv2d(in_channels=512, out_channels=num_anchors * 4, kernel_size=1)# 因为是1*1的kernel,所以不会改变特征图的size,# 对一个特征图像上的每一个点来说,会生成num*anchors个anchor,而每个anchor都有两个前景/背景概率# 和4个坐标位置,因此对于self._objectness来说两个通道对应一个anchor,而对于self._transformer来说# 4个通道对应一个anchor。这4个通道是一个anchor的四个位置的修正偏移量。# 所以在forward的里,才会将通道数换至最后一维,再展开。这样每一行就会对应一个anchordef forward(self, features: Tensor, image_width: int, image_height: int) -> Tuple[Tensor, Tensor]:features = self._features(features)objectnesses = self._objectness(features) # anchor前景/背景修正transformers = self._transformer(features)
这篇关于easy-Fpn源码解读(五):rpn的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!