-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodel_m1.py
102 lines (87 loc) · 3.27 KB
/
model_m1.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
import torch
import torch.nn as nn
import torch.nn.functional as F
def adaptive_concat_pool2d(x, sz=(1, 1)):
out1 = F.adaptive_avg_pool2d(x, sz).view(x.size(0), -1)
out2 = F.adaptive_max_pool2d(x, sz).view(x.size(0), -1)
return torch.cat([out1, out2], 1)
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, pool=True):
super().__init__()
padding = kernel_size // 2
self.pool = pool
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=1, padding=padding),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
self.conv2 = nn.Sequential(
nn.Conv2d(out_channels + in_channels, out_channels, kernel_size=kernel_size, stride=1, padding=padding),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
self._init_weights()
def _init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.zeros_(m.bias)
def forward(self, x): # x.shape = [batch_size, in_channels, a, b]
x1 = self.conv1(x)
x = self.conv2(torch.cat([x, x1], 1))
if (self.pool): x = F.avg_pool2d(x, 2)
return x # x.shape = [batch_size, out_channels, a//2, b//2]
class Classifier_M3(nn.Module):
def __init__(self, num_classes=6):
super().__init__()
self.conv1 = ConvBlock(1, 64)
self.conv2 = ConvBlock(64, 128)
self.conv3 = ConvBlock(128, 256)
self.conv4 = ConvBlock(256, 512)
self.conv5 = ConvBlock(512, 1024, pool=False)
self.fc = nn.Sequential(
nn.BatchNorm1d(3840),
nn.Linear(3840, 256),
nn.PReLU(),
nn.BatchNorm1d(256),
nn.Linear(256, num_classes),
)
def forward(self, x): # batch_size, 3, a, b
x1 = self.conv1(x)
x2 = self.conv2(x1)
x3 = self.conv3(x2)
x4 = self.conv4(x3)
x5 = self.conv5(x4)
# pyramid pooling
x = torch.cat([adaptive_concat_pool2d(x2), adaptive_concat_pool2d(x3),
adaptive_concat_pool2d(x4), adaptive_concat_pool2d(x5)], 1)
x = self.fc(x)
return x
class Classifier_M2(nn.Module):
def __init__(self, num_classes=6):
super().__init__()
self.conv1 = ConvBlock(1, 64)
self.conv2 = ConvBlock(64, 128)
self.conv3 = ConvBlock(128, 256)
self.conv4 = ConvBlock(256, 512, pool=False)
self.fc = nn.Sequential(
nn.BatchNorm1d(1792),
nn.Linear(1792, 256),
nn.PReLU(),
nn.BatchNorm1d(256),
nn.Linear(256, num_classes),
)
def forward(self, x): # batch_size, 3, a, b
x1 = self.conv1(x)
x2 = self.conv2(x1)
x3 = self.conv3(x2)
x4 = self.conv4(x3)
# pyramid pooling
x = torch.cat([adaptive_concat_pool2d(x2), adaptive_concat_pool2d(x3),
adaptive_concat_pool2d(x4)], 1)
x = self.fc(x)
return x