-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv_network.py
89 lines (67 loc) · 3.42 KB
/
conv_network.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
import torch
import torch.nn as nn
__all__ = ['ConvNet']
# VGG model class
class ConvNet(nn.Module):
def __init__(self, base_dim=64, num_classes=2, kernel_size=3, dilation=1):
super().__init__()
padding = (kernel_size + 2 * dilation - 1) // 2
self.layer_1 = nn.Sequential(
ConvBN(1, base_dim, kernel_size=kernel_size, padding=padding, dilation=dilation),
ConvBN(base_dim, base_dim, kernel_size=kernel_size, padding=padding, dilation=dilation),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer_2 = nn.Sequential(
ConvBN(base_dim, base_dim * 2, kernel_size=kernel_size, padding=padding, dilation=dilation),
ConvBN(base_dim * 2, base_dim * 2, kernel_size=kernel_size, padding=padding, dilation=dilation),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer_3 = nn.Sequential(
ConvBN(base_dim * 2, base_dim * 4, kernel_size=kernel_size, padding=padding, dilation=dilation),
ConvBN(base_dim * 4, base_dim * 4, kernel_size=kernel_size, padding=padding, dilation=dilation),
ConvBN(base_dim * 4, base_dim * 4, kernel_size=kernel_size, padding=padding, dilation=dilation),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer_4 = nn.Sequential(
ConvBN(base_dim * 4, base_dim * 8, kernel_size=kernel_size, padding=padding, dilation=dilation),
ConvBN(base_dim * 8, base_dim * 8, kernel_size=kernel_size, padding=padding, dilation=dilation),
ConvBN(base_dim * 8, base_dim * 8, kernel_size=kernel_size, padding=padding, dilation=dilation),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer_5 = nn.Sequential(
ConvBN(base_dim * 8, base_dim * 8, kernel_size=kernel_size, padding=padding, dilation=dilation),
ConvBN(base_dim * 8, base_dim * 8, kernel_size=kernel_size, padding=padding, dilation=dilation),
ConvBN(base_dim * 8, base_dim * 8, kernel_size=kernel_size, padding=padding, dilation=dilation),
nn.Conv2d(base_dim * 8, base_dim * 16, kernel_size=3, padding=1),
nn.ReLU(inplace=True))
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc_layer = nn.Linear(base_dim * 16, num_classes)
# Initialization
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.orthogonal_(m.weight)
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)
elif isinstance(m, nn.Linear):
nn.init.orthogonal_(m.weight)
nn.init.constant_(m.bias, 0)
def forward(self, x):
batch_size = x.size(0)
out = self.layer_1(x)
out = self.layer_2(out)
out = self.layer_3(out)
out = self.layer_4(out)
out = self.layer_5(out)
out = self.avg_pool(out).view(batch_size, -1)
out = self.fc_layer(out)
return out
class ConvBN(nn.Module):
def __init__(self, in_dim, out_dim, **kwargs):
super().__init__()
self.layer = nn.Sequential(
nn.Conv2d(in_dim, out_dim, bias=False, **kwargs),
nn.BatchNorm2d(out_dim),
nn.ReLU(inplace=True))
def forward(self, x):
return self.layer(x)
if __name__ == '__main__':
model = ConvNet()