-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnets.py
57 lines (51 loc) · 1.93 KB
/
nets.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
import torch.nn as nn
from natsort import natsorted
import torch.nn.functional as F
import torch
class Residual_Block(nn.Module):
def __init__(self, inc, outc, s=1):
super(Residual_Block, self).__init__()
self.para = 0.01
self.model = nn.Sequential(
nn.Conv2d(inc, outc, kernel_size=3, stride=s, padding=1),
nn.BatchNorm2d(outc),
nn.LeakyReLU(self.para),
nn.Conv2d(outc, outc, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(outc),
nn.LeakyReLU(self.para)
)
self.downsample = nn.Conv2d(inc, outc, kernel_size=1, stride=s, padding=0)
self.relu = nn.LeakyReLU(self.para)
def forward(self, x):
t = self.model(x)
if t.size(1) != x.size(1):
x = self.downsample(x)
out = self.relu(x + t)
return out
class Residual(nn.Module):
def __init__(self, inc, num_classes, dt):
"""
inc: the num_channels of images
dt: dictionary for the intermediate layers. details: {num_channels : num_iterations}
"""
super(Residual, self).__init__()
self.keys = natsorted(dt.keys())
self.fc = nn.Linear(self.keys[-1], num_classes)
layers = []
layers += [nn.Conv2d(inc, self.keys[0], kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(self.keys[0]),
nn.LeakyReLU()]
for i in range(len(dt)):
k = self.keys[i]
for _ in range(dt[k]):
layers += [Residual_Block(k, k)]
if i+1<len(dt):
k_next = self.keys[i+1]
layers += [Residual_Block(k, k_next, s=2)]
self.model = nn.Sequential(*layers)
def forward(self, x):
x = self.model(x)
x = F.adaptive_avg_pool2d(x, (1, 1))
x = x.view(x.size(0), -1)
x = self.fc(x)
return x