File size: 2,231 Bytes
dd78229 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# taken from https://raw.githubusercontent.com/janghyuncho/PiCIE/1d7b034f57e98670b0d6a244b2eea11fa0dde73e/modules/fpn.py
import torch
import torch.nn as nn
import torch.nn.functional as F
from . import backbone_picie as backbone
class PanopticFPN(nn.Module):
def __init__(self, arch, pretrain, n_cls):
super(PanopticFPN, self).__init__()
self.n_cls = n_cls
self.backbone = backbone.__dict__[arch](pretrained=pretrain)
self.decoder = FPNDecoder(arch, n_cls)
def forward(self, x, encoder_features=False, decoder_features=False):
feats = self.backbone(x)
if decoder_features:
dec, outs = self.decoder(feats, get_features=decoder_features)
else:
outs = self.decoder(feats)
if encoder_features:
if decoder_features:
return feats['res5'], dec, outs
else:
return feats['res5'], outs
else:
return outs
class FPNDecoder(nn.Module):
def __init__(self, arch, n_cls):
super(FPNDecoder, self).__init__()
self.n_cls = n_cls
if arch == 'resnet18':
mfactor = 1
out_dim = 128
else:
mfactor = 4
out_dim = 256
self.layer4 = nn.Conv2d(512 * mfactor // 8, out_dim, kernel_size=1, stride=1, padding=0)
self.layer3 = nn.Conv2d(512 * mfactor // 4, out_dim, kernel_size=1, stride=1, padding=0)
self.layer2 = nn.Conv2d(512 * mfactor // 2, out_dim, kernel_size=1, stride=1, padding=0)
self.layer1 = nn.Conv2d(512 * mfactor, out_dim, kernel_size=1, stride=1, padding=0)
self.pred = nn.Conv2d(out_dim, self.n_cls, 1, 1)
def forward(self, x, get_features=False):
o1 = self.layer1(x['res5'])
o2 = self.upsample_add(o1, self.layer2(x['res4']))
o3 = self.upsample_add(o2, self.layer3(x['res3']))
o4 = self.upsample_add(o3, self.layer4(x['res2']))
pred = self.pred(o4)
if get_features:
return o4, pred
else:
return pred
def upsample_add(self, x, y):
_, _, H, W = y.size()
return F.interpolate(x, size=(H, W), mode='bilinear', align_corners=False) + y
|