-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet1.py
148 lines (135 loc) · 5.78 KB
/
net1.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import torch.nn as nn
import torch
import torch.nn.functional as F
from torchvision import models
class Conv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, bn=False, dropout = False, activation = 'leakyrelu'):
super(Conv2d, self).__init__()
padding = int((kernel_size - 1) / 2)
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=padding)
self.bn = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0, affine=True) if bn else None
self.dropout = nn.Dropout(p=0.5) if dropout else None
if activation == 'leakyrelu':
self.activation = nn.LeakyReLU(negative_slope = 0.2, inplace=True)
elif activation == 'relu':
self.activation = nn.ReLU(inplace=True)
elif activation == 'tanh':
self.activation = nn.Tanh()
else:
raise ValueError('Not a valid activation, received {}'.format(activation))
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.dropout is not None:
x = self.dropout(x)
x = self.activation(x)
return x
class Deconv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, bn=False, dropout = False, activation = 'leakyrelu'):
super(Deconv2d, self).__init__()
padding = int((kernel_size - 1) / 2)
self.conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding=padding)
self.bn = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0, affine=True) if bn else None
self.dropout = nn.Dropout(p=0.5) if dropout else None
if activation == 'leakyrelu':
self.activation = nn.LeakyReLU(negative_slope = 0.2, inplace=True)
elif activation == 'relu':
self.activation = nn.ReLU(inplace=True)
elif activation == 'tanh':
self.activation = nn.Tanh()
else:
raise ValueError('Not a valid activation, received {}'.format(activation))
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.dropout is not None:
x = self.dropout(x)
x = self.activation(x)
return x
class ResBranch(nn.Module):
def __init__(self):
super(ResBranch, self).__init__()
self.backend_feat = [512,'A',256,'A',128,64]
self.backend = make_layers(self.backend_feat, in_channels=512, dilation=True)
self.output_layer = nn.Conv2d(64, 1, kernel_size=1)
self._initialize_weights()
def forward(self,x1, x2):
x1 = F.adaptive_avg_pool2d(x1, [64, 64])
x2 = F.adaptive_avg_pool2d(x2, [64, 64])
x = self.backend(x1 - x2)
x = self.output_layer(x)
out = torch.mean(x, dim=(2,3))
return out
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight, std=0.01)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
class CSRNet(nn.Module):
def __init__(self, load_weights=False, add_mode=False, res_mode=False):
super(CSRNet, self).__init__()
self.seen = 0
self.frontend_feat = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512]
self.backend_feat = [512,256,128,64]
self.frontend = make_layers(self.frontend_feat)
self.backend = make_layers(self.backend_feat,in_channels = 512,dilation = True)
self.output_layer = nn.Conv2d(64, 1, kernel_size=1)
self.add_mode = add_mode
self.res_mode = res_mode
if not self.add_mode:
self.upsample = nn.Upsample(scale_factor=8, mode='bilinear')
self._initialize_weights()
if not load_weights:
mod = models.vgg16(pretrained = True)
fs = self.frontend.state_dict()
ms = mod.state_dict()
for key in fs:
fs[key] = ms['features.'+key]
self.frontend.load_state_dict(fs)
else:
print("Don't pre-train on ImageNet")
def forward(self,x):
x = self.frontend(x)
inner = x
x = self.backend(x)
x = self.output_layer(x)
if not self.add_mode:
x = self.upsample(x)
out = torch.sum(x, dim=(2,3))
if self.res_mode:
return out, inner
return out, x
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight, std=0.01)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def make_layers(cfg, in_channels = 3,batch_norm=False,dilation = False):
if dilation:
d_rate = 2
else:
d_rate = 1
layers = []
for v in cfg:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
elif v == 'A':
layers += [nn.AvgPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=d_rate,dilation = d_rate)
if batch_norm:
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
else:
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
return nn.Sequential(*layers)