-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
171 lines (120 loc) · 4.79 KB
/
models.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
Author: Can Bakiskan
Date: 2019-09-03
"""
import torch
from torch import nn
import torch.nn.functional as F
from normalized_conv2d import Normalized_Conv2d, Saturation_activation
class Classifier(nn.Module):
def __init__(self, in_channels=1, **kwargs):
super(Classifier, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 32, kernel_size=5)
self.conv2 = nn.Conv2d(32, 64, kernel_size=5)
self.fc1 = nn.Linear(1024, 100)
self.fc2 = nn.Linear(100, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2(x), 2))
x = x.view(-1, 1024)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
class Classifier_no_maxpool(nn.Module):
def __init__(self, in_channels=1, **kwargs):
super(Classifier_no_maxpool, self).__init__()
self.conv1 = nn.Conv2d(in_channels, 16, kernel_size=4)
self.conv2 = nn.Conv2d(16, 32, kernel_size=4)
self.fc1 = nn.Linear(15488, 100)
self.fc2 = nn.Linear(100, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = x.view(-1, 15488)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
class Direct_quantization_model(nn.Module):
def __init__(self, in_channels=1, jump=0.2, bpda_steepness=16, **kwargs):
super(Direct_quantization_model, self).__init__()
# self.frontend = Normalized_Conv2d(
# in_channels=in_channels,
# out_channels=1,
# kernel_size=5,
# jump=jump,
# bpda_steepness=bpda_steepness,
# padding=2,
# )
# self.frontend.weight.data[0, 0, :, :] = 0.0
# self.frontend.weight.data[0, 0, 3, 3] = 1.0
# self.frontend.weight.requires_grad = False
self.jump = nn.Parameter(torch.tensor(jump, dtype=torch.float))
self.bpda_steepness = nn.Parameter(torch.tensor(jump, dtype=torch.float))
self.frontend = Saturation_activation().apply
self.classifier = Classifier(in_channels=1)
def forward(self, x):
x = self.frontend(x, self.jump, self.bpda_steepness)
x = self.classifier(x)
return x
class Polarization_quantization_model(nn.Module):
def __init__(self, in_channels=1, jump=0.2, bpda_steepness=16, **kwargs):
super(Polarization_quantization_model, self).__init__()
self.frontend = Normalized_Conv2d(
in_channels=in_channels,
out_channels=25,
kernel_size=5,
jump=jump,
bpda_steepness=bpda_steepness,
padding=2,
)
nn.init.kaiming_uniform_(self.frontend.weight, nonlinearity="relu")
self.frontend.weight.requires_grad = True
self.classifier = Classifier(in_channels=self.frontend.out_channels)
def forward(self, x):
x = self.frontend(x)
x = self.classifier(x)
return x
def set_bpda_steepness(self, bpda_steepness):
self.frontend.set_bpda_steepness(bpda_steepness)
class Polarization_quantization_model_no_maxpool(nn.Module):
def __init__(self, in_channels=1, jump=0.2, bpda_steepness=16, **kwargs):
super(Polarization_quantization_model_no_maxpool, self).__init__()
self.frontend = Normalized_Conv2d(
in_channels=in_channels,
out_channels=25,
kernel_size=5,
jump=jump,
bpda_steepness=bpda_steepness,
padding=2,
)
nn.init.kaiming_uniform_(self.frontend.weight, nonlinearity="relu")
self.frontend.weight.requires_grad = True
self.classifier = Classifier_no_maxpool(in_channels=self.frontend.out_channels)
def forward(self, x):
x = self.frontend(x)
x = self.classifier(x)
return x
def set_bpda_steepness(self, bpda_steepness):
self.frontend.set_bpda_steepness(bpda_steepness)
class Blackbox_model(nn.Module):
def __init__(self):
super(Blackbox_model, self).__init__()
# self.bn1 = nn.BatchNorm2d(1)
self.conv1 = nn.Conv2d(1, 64, kernel_size=4, stride=2)
self.bn1 = nn.BatchNorm2d(64)
self.conv2 = nn.Conv2d(64, 128, kernel_size=4)
self.dense1 = nn.Linear(in_features=1024, out_features=256)
self.dropout2 = nn.Dropout(0.2)
self.dense2 = nn.Linear(in_features=256, out_features=64)
self.dense3 = nn.Linear(in_features=64, out_features=10)
def forward(self, x):
# x = self.bn1(x)
x = F.relu(self.conv1(x))
x = self.bn1(x)
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(x.size(0), -1) # reshape
x = F.relu(self.dense1(x))
x = self.dropout2(x)
x = F.relu(self.dense2(x))
x = self.dense3(x)
return x