-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
167 lines (141 loc) · 4.77 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
import torch.nn as nn
import os
import torch
class LeNet(nn.Module):
def __init__(self, args):
super(LeNet, self).__init__()
in_channels = 3 if args.is_rgb_data else 1
self.conv1 = nn.Sequential(
# (1, 28, 28) => (6, 28, 28)
nn.Conv2d(in_channels=in_channels,
out_channels=6,
kernel_size=5,
padding=2),
nn.ReLU(),
# (6, 28, 28) => (6, 14, 14)
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.conv2 = nn.Sequential(
# (6, 14, 14) => (16, 10, 10)
nn.Conv2d(in_channels=6,
out_channels=16,
kernel_size=5),
nn.ReLU(),
# (16, 10, 10) => (16, 5, 5)
nn.MaxPool2d(2, 2)
)
# A flatten layer here: (16, 5, 5) => (16*5*5)
self.fc1 = nn.Sequential(
# (16*5*5) => (120)
nn.Linear(16 * 5 * 5, 120),
nn.ReLU()
)
self.fc2 = nn.Sequential(
# (120, 84)
nn.Linear(120, 84),
nn.ReLU()
)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size()[0], -1)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
class LeNetWithReg(nn.Module):
def __init__(self, args):
super(LeNetWithReg, self).__init__()
self.args = args
in_channels = 3 if args.is_rgb_data else 1
self.conv1 = nn.Sequential(
# (1, 28, 28) => (6, 28, 28)
nn.Conv2d(in_channels=in_channels,
out_channels=6,
kernel_size=5,
padding=2),
nn.ReLU(),
# (6, 28, 28) => (6, 14, 14)
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.conv2 = nn.Sequential(
# (6, 14, 14) => (16, 10, 10)
nn.Conv2d(in_channels=6,
out_channels=16,
kernel_size=5),
nn.ReLU(),
# (16, 10, 10) => (16, 5, 5)
nn.MaxPool2d(2, 2)
)
# A flatten layer here: (16, 5, 5) => (16*5*5)
self.fc1 = nn.Sequential(
# (16*5*5) => (120)
nn.Linear(16 * 5 * 5, 120),
nn.ReLU()
)
if args.use_dropout:
self.fc2 = nn.Sequential(
# (120, 84)
nn.Linear(120, 84),
nn.ReLU(),
nn.Dropout()
)
else:
self.fc2 = nn.Sequential(
# (120, 84)
nn.Linear(120, 84),
nn.ReLU(),
)
self.fc3 = nn.Linear(84, 10)
self.cnn_parameters = set(self.parameters())
# CNN ends here. Following are regularization layers
if args.reg_layers == 1:
if args.method == 1 or args.method == 5:
if args.reg_object == 0:
self.fc_reg = nn.Sequential(
nn.Linear(10, 1, bias=not args.method == 5)
)
elif args.reg_object == 1:
self.fc_reg = nn.Sequential(
nn.Linear(84, 1, bias=not args.method == 5)
)
elif args.reg_layers == 2:
if args.method == 1 or args.method == 5:
if args.reg_object == 0:
self.fc_reg = nn.Sequential(
nn.Linear(10, 10, bias=not args.method == 5),
nn.Linear(10, 1, bias=not args.method == 5)
)
elif args.reg_object == 1:
self.fc_reg = nn.Sequential(
nn.Linear(84, 50, bias=not args.method == 5),
nn.Linear(50, 1, bias=not args.method == 5)
)
self.reg_parameters = set(self.parameters()) - self.cnn_parameters
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size()[0], -1)
x = self.fc1(x)
h = self.fc2(x)
logit = self.fc3(h)
if self.args.reg_object == 0:
reg_obj = logit
elif self.args.reg_object == 1:
reg_obj = h
if self.args.method == 1 or self.args.method == 5:
reg = self.fc_reg(reg_obj)
else:
reg = None
return logit, reg, reg_obj
def save_model(state_dict: dict, args, new=False):
if new:
torch.save(state_dict, args.new_save_path)
else:
torch.save(state_dict, args.save_path)
def load_model(args):
save_path = args.save_path
if not os.path.exists(save_path):
return None
return torch.load(save_path)