-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
122 lines (101 loc) · 4.13 KB
/
modules.py
File metadata and controls
122 lines (101 loc) · 4.13 KB
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
import torch
from torch import nn
class ConvModule(nn.Module):
#feature extraction with shared weights
def __init__(self, channels=16, dropout_rate=0, bn=False):
super(ConvModule, self).__init__()
in_out = [(1, channels // 2), (channels // 2, channels), (channels, channels)]
self.conv = nn.ModuleList(
[nn.Conv2d(in_channels=in_c, out_channels=out_c, kernel_size=(3, 3)) for in_c, out_c in in_out])
self.bn = nn.ModuleList([nn.BatchNorm2d(out_c) for in_c, out_c in in_out])
self.dropout = nn.Dropout(dropout_rate)
self.maxpool = nn.MaxPool2d(kernel_size=(2, 2), padding=1)
self.relu = nn.ReLU()
self.use_bn = bn
def forward(self, x):
N = x.shape[0]
x = x.reshape(-1, 1, 14, 14)
for conv, bn in zip(self.conv, self.bn):
x = conv(x)
x = self.relu(x)
if self.use_bn:
x = bn(x)
x = self.maxpool(x)
x = self.dropout(x)
x = x.reshape(2 * N, -1)
return x
class ConvModuleSep(nn.Module):
# feature extraction with separate weights
def __init__(self, channels=16, dropout_rate=0, bn=False):
super(ConvModuleSep, self).__init__()
in_out = [(2, channels), (channels, channels * 2), (channels * 2, channels * 2)]
self.conv = nn.ModuleList(
[nn.Conv2d(in_channels=in_c, out_channels=out_c, kernel_size=(3, 3), groups=2) for in_c, out_c in in_out])
self.bn = nn.ModuleList([nn.BatchNorm2d(out_c) for in_c, out_c in in_out])
self.dropout = nn.Dropout(dropout_rate)
self.maxpool = nn.MaxPool2d(kernel_size=(2, 2), padding=1)
self.relu = nn.ReLU()
self.use_bn = bn
def forward(self, x):
N = x.shape[0] // 2
for conv, bn in zip(self.conv, self.bn):
x = conv(x)
x = self.relu(x)
if self.use_bn:
x = bn(x)
x = self.maxpool(x)
x = self.dropout(x)
x = x.reshape(2 * N, -1)
return x
class TargetPrediction(nn.Module):
# dense target prediction with shared weights
def __init__(self, channels, final_features):
super(TargetPrediction, self).__init__()
self.fc1 = nn.Linear(in_features=channels, out_features=final_features)
self.fc2 = nn.Linear(in_features=final_features * 2, out_features=1)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def forward(self, x):
N = x.shape[0] // 2
x = x.reshape(2 * N, -1)
x = self.fc1(x)
x = self.relu(x)
x = x.reshape(N, -1)
x = self.fc2(x)
x = self.sigmoid(x)
return x
class TargetPredictionSep(nn.Module):
# dense target prediction with separate weights
def __init__(self, channels, final_features):
super(TargetPredictionSep, self).__init__()
self.fc1_1 = nn.Linear(in_features=channels, out_features=final_features)
self.fc1_2 = nn.Linear(in_features=channels, out_features=final_features)
self.fc2 = nn.Linear(in_features=final_features * 2, out_features=1)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def forward(self, x):
N = x.shape[0] // 2
x = x.reshape(N, 2, -1)
x_1 = self.fc1_1(x[:, 0, :])
x_2 = self.fc1_2(x[:, 1, :])
x = torch.stack([x_1, x_2], dim=1).reshape(N, -1)
x = self.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return x
class ClassPrediction(nn.Module):
# class prediction for auxillary loss
def __init__(self, channels=16, hidden_features=16):
super(ClassPrediction, self).__init__()
self.fc_class_1 = nn.Linear(in_features=channels, out_features=hidden_features)
self.fc_class_2 = nn.Linear(in_features=hidden_features, out_features=10)
self.relu = nn.ReLU()
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
N = x.shape[0] // 2
classes = x.reshape(2 * N, -1)
classes = self.fc_class_1(classes)
classes = self.relu(classes)
classes = self.fc_class_2(classes)
classes = self.softmax(classes)
return classes