加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
retinanet.py 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
import torch
import torch.nn as nn
from fpn import FPN50
from torch.autograd import Variable
class RetinaNet(nn.Module):
num_anchors = 9
def __init__(self, num_classes=20):
super(RetinaNet, self).__init__()
self.fpn = FPN50()
self.num_classes = num_classes
self.loc_head = self._make_head(self.num_anchors*4)
self.cls_head = self._make_head(self.num_anchors*self.num_classes)
def forward(self, x):
fms = self.fpn(x)
loc_preds = []
cls_preds = []
for fm in fms:
loc_pred = self.loc_head(fm)
cls_pred = self.cls_head(fm)
loc_pred = loc_pred.permute(0,2,3,1).contiguous().view(x.size(0),-1,4) # [N, 9*4,H,W] -> [N,H,W, 9*4] -> [N,H*W*9, 4]
cls_pred = cls_pred.permute(0,2,3,1).contiguous().view(x.size(0),-1,self.num_classes) # [N,9*20,H,W] -> [N,H,W,9*20] -> [N,H*W*9,20]
loc_preds.append(loc_pred)
cls_preds.append(cls_pred)
return torch.cat(loc_preds,1), torch.cat(cls_preds,1)
def _make_head(self, out_planes):
layers = []
for _ in range(4):
layers.append(nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1))
layers.append(nn.ReLU(True))
layers.append(nn.Conv2d(256, out_planes, kernel_size=3, stride=1, padding=1))
return nn.Sequential(*layers)
def freeze_bn(self):
'''Freeze BatchNorm layers.'''
for layer in self.modules():
if isinstance(layer, nn.BatchNorm2d):
layer.eval()
def test():
net = RetinaNet()
loc_preds, cls_preds = net(Variable(torch.randn(2,3,224,224)))
print(loc_preds.size())
print(cls_preds.size())
loc_grads = Variable(torch.randn(loc_preds.size()))
cls_grads = Variable(torch.randn(cls_preds.size()))
loc_preds.backward(loc_grads)
cls_preds.backward(cls_grads)
# test()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化