File models/model_context_aware_network.py added (mode: 100644) (index 0000000..23c2326) |
|
1 |
|
import torch.nn as nn |
|
2 |
|
import torch |
|
3 |
|
from torch.nn import functional as F |
|
4 |
|
from torchvision import models |
|
5 |
|
|
|
6 |
|
|
|
7 |
|
class ContextualModule(nn.Module): |
|
8 |
|
def __init__(self, features, out_features=512, sizes=(1, 2, 3, 6)): |
|
9 |
|
super(ContextualModule, self).__init__() |
|
10 |
|
self.scales = [] |
|
11 |
|
self.scales = nn.ModuleList([self._make_scale(features, size) for size in sizes]) |
|
12 |
|
self.bottleneck = nn.Conv2d(features * 2, out_features, kernel_size=1) |
|
13 |
|
self.relu = nn.ReLU() |
|
14 |
|
self.weight_net = nn.Conv2d(features,features,kernel_size=1) |
|
15 |
|
|
|
16 |
|
def __make_weight(self,feature,scale_feature): |
|
17 |
|
weight_feature = feature - scale_feature |
|
18 |
|
return F.sigmoid(self.weight_net(weight_feature)) |
|
19 |
|
|
|
20 |
|
def _make_scale(self, features, size): |
|
21 |
|
prior = nn.AdaptiveAvgPool2d(output_size=(size, size)) |
|
22 |
|
conv = nn.Conv2d(features, features, kernel_size=1, bias=False) |
|
23 |
|
return nn.Sequential(prior, conv) |
|
24 |
|
|
|
25 |
|
def forward(self, feats): |
|
26 |
|
h, w = feats.size(2), feats.size(3) |
|
27 |
|
multi_scales = [F.upsample(input=stage(feats), size=(h, w), mode='bilinear') for stage in self.scales] |
|
28 |
|
weights = [self.__make_weight(feats,scale_feature) for scale_feature in multi_scales] |
|
29 |
|
overall_features = [(multi_scales[0]*weights[0]+multi_scales[1]*weights[1]+multi_scales[2]*weights[2]+multi_scales[3]*weights[3])/(weights[0]+weights[1]+weights[2]+weights[3])]+ [feats] |
|
30 |
|
bottle = self.bottleneck(torch.cat(overall_features, 1)) |
|
31 |
|
return self.relu(bottle) |
|
32 |
|
|
|
33 |
|
|
|
34 |
|
class CANNet(nn.Module): |
|
35 |
|
def __init__(self, load_weights=False): |
|
36 |
|
super(CANNet, self).__init__() |
|
37 |
|
self.seen = 0 |
|
38 |
|
self.context = ContextualModule(512, 512) |
|
39 |
|
self.frontend_feat = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512] |
|
40 |
|
self.backend_feat = [512, 512, 512,256,128,64] |
|
41 |
|
self.frontend = make_layers(self.frontend_feat) |
|
42 |
|
self.backend = make_layers(self.backend_feat,in_channels = 512,batch_norm=True, dilation = True) |
|
43 |
|
self.output_layer = nn.Conv2d(64, 1, kernel_size=1) |
|
44 |
|
if not load_weights: |
|
45 |
|
mod = models.vgg16(pretrained = True) |
|
46 |
|
self._initialize_weights() |
|
47 |
|
for i in range(len(self.frontend.state_dict().items())): |
|
48 |
|
list(self.frontend.state_dict().items())[i][1].data[:] = list(mod.state_dict().items())[i][1].data[:] |
|
49 |
|
|
|
50 |
|
def forward(self,x): |
|
51 |
|
x = self.frontend(x) |
|
52 |
|
x = self.context(x) |
|
53 |
|
x = self.backend(x) |
|
54 |
|
x = self.output_layer(x) |
|
55 |
|
return x |
|
56 |
|
|
|
57 |
|
def _initialize_weights(self): |
|
58 |
|
for m in self.modules(): |
|
59 |
|
if isinstance(m, nn.Conv2d): |
|
60 |
|
nn.init.normal_(m.weight, std=0.01) |
|
61 |
|
if m.bias is not None: |
|
62 |
|
nn.init.constant_(m.bias, 0) |
|
63 |
|
elif isinstance(m, nn.BatchNorm2d): |
|
64 |
|
nn.init.constant_(m.weight, 1) |
|
65 |
|
nn.init.constant_(m.bias, 0) |
|
66 |
|
|
|
67 |
|
|
|
68 |
|
def make_layers(cfg, in_channels=3, batch_norm=False, dilation=False): |
|
69 |
|
if dilation: |
|
70 |
|
d_rate = 2 |
|
71 |
|
else: |
|
72 |
|
d_rate = 1 |
|
73 |
|
layers = [] |
|
74 |
|
for v in cfg: |
|
75 |
|
if v == 'M': |
|
76 |
|
layers += [nn.MaxPool2d(kernel_size=2, stride=2)] |
|
77 |
|
else: |
|
78 |
|
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=d_rate,dilation = d_rate) |
|
79 |
|
if batch_norm: |
|
80 |
|
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] |
|
81 |
|
else: |
|
82 |
|
layers += [conv2d, nn.ReLU(inplace=True)] |
|
83 |
|
in_channels = v |
|
84 |
|
return nn.Sequential(*layers) |