-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
91 lines (76 loc) · 3.43 KB
/
model.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
import torch.nn as nn
import torch
import torch.nn.functional as F
from torch.autograd import Variable
class PMG_V2(nn.Module):
def __init__(self, model, feature_size, classes_num):
super(PMG_V2, self).__init__()
self.features = model
self.max1 = nn.MaxPool2d(kernel_size=56, stride=56)
self.max2 = nn.MaxPool2d(kernel_size=28, stride=28)
self.max3 = nn.MaxPool2d(kernel_size=14, stride=14)
self.num_ftrs = 2048 * 1 * 1
self.conv_block3 = nn.Sequential(
BasicConv(self.num_ftrs, feature_size, kernel_size=1, stride=1, padding=0, relu=True),
BasicConv(feature_size, self.num_ftrs//2, kernel_size=3, stride=1, padding=1, relu=True)
)
self.classifier3 = nn.Sequential(
nn.BatchNorm1d(self.num_ftrs//2),
nn.Linear(self.num_ftrs//2, feature_size),
nn.BatchNorm1d(feature_size),
nn.ReLU(inplace=True),
nn.Linear(feature_size, classes_num),
)
self.conv_block2 = nn.Sequential(
BasicConv(self.num_ftrs//2, feature_size, kernel_size=1, stride=1, padding=0, relu=True),
BasicConv(feature_size, self.num_ftrs//2, kernel_size=3, stride=1, padding=1, relu=True)
)
self.classifier2 = nn.Sequential(
nn.BatchNorm1d(self.num_ftrs//2),
nn.Linear(self.num_ftrs//2, feature_size),
nn.BatchNorm1d(feature_size),
nn.ReLU(inplace=True),
nn.Linear(feature_size, classes_num),
)
self.conv_block1 = nn.Sequential(
BasicConv(self.num_ftrs//4, feature_size, kernel_size=1, stride=1, padding=0, relu=True),
BasicConv(feature_size, self.num_ftrs//2, kernel_size=3, stride=1, padding=1, relu=True)
)
self.classifier1 = nn.Sequential(
nn.BatchNorm1d(self.num_ftrs//2),
nn.Linear(self.num_ftrs//2, feature_size),
nn.BatchNorm1d(feature_size),
nn.ReLU(inplace=True),
nn.Linear(feature_size, classes_num),
)
def forward(self, x, block=[0, 0, 0, 0]):
x1, x2, x3, x4, x5, f1, f2, f3 = self.features(x, block)
xs1 = self.conv_block1(x3)
xs2 = self.conv_block2(x4)
xs3 = self.conv_block3(x5)
xl1 = self.max1(xs1)
xl1 = xl1.view(xl1.size(0), -1)
xc1 = self.classifier1(xl1)
xl2 = self.max2(xs2)
xl2 = xl2.view(xl2.size(0), -1)
xc2 = self.classifier2(xl2)
xl3 = self.max3(xs3)
xl3 = xl3.view(xl3.size(0), -1)
xc3 = self.classifier3(xl3)
return xc1, xc2, xc3, f1, f2, f3
class BasicConv(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True, bn=True, bias=True):
super(BasicConv, self).__init__()
self.out_channels = out_planes
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size,
stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
self.bn = nn.BatchNorm2d(out_planes, eps=1e-5,
momentum=0.01, affine=True) if bn else None
self.relu = nn.ReLU() if relu else None
def forward(self, x):
x = self.conv(x)
if self.bn is not None:
x = self.bn(x)
if self.relu is not None:
x = self.relu(x)
return x