-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
121 lines (100 loc) · 4.71 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
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
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
class Model(nn.Module):
def __init__(self, name, args=True):
super(Model, self).__init__()
self.name = name
if self.name == "linear":
[self.n_dim, self.n_out] = args
self.fc = nn.Linear(self.n_dim, self.n_out)
elif self.name == "mnist":
self.n_cls = 10
self.fc1 = nn.Linear(1 * 28 * 28, 200)
self.fc2 = nn.Linear(200, 200)
self.fc3 = nn.Linear(200, self.n_cls)
elif self.name == "emnist":
self.n_cls = 10
self.fc1 = nn.Linear(1 * 28 * 28, 100)
self.fc2 = nn.Linear(100, 100)
self.fc3 = nn.Linear(100, self.n_cls)
elif self.name == "cifar10":
self.n_cls = 10
self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=5)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(64 * 5 * 5, 384)
self.fc2 = nn.Linear(384, 192)
self.fc3 = nn.Linear(192, self.n_cls)
elif self.name == "cifar100":
self.n_cls = 100
self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=5)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(64 * 5 * 5, 384)
self.fc2 = nn.Linear(384, 192)
self.fc3 = nn.Linear(192, self.n_cls)
elif self.name == "resnet18":
resnet18 = models.resnet18()
resnet18.fc = nn.Linear(512, 10)
# Change BN to GN
resnet18.bn1 = nn.GroupNorm(num_groups=2, num_channels=64)
resnet18.layer1[0].bn1 = nn.GroupNorm(num_groups=2, num_channels=64)
resnet18.layer1[0].bn2 = nn.GroupNorm(num_groups=2, num_channels=64)
resnet18.layer1[1].bn1 = nn.GroupNorm(num_groups=2, num_channels=64)
resnet18.layer1[1].bn2 = nn.GroupNorm(num_groups=2, num_channels=64)
resnet18.layer2[0].bn1 = nn.GroupNorm(num_groups=2, num_channels=128)
resnet18.layer2[0].bn2 = nn.GroupNorm(num_groups=2, num_channels=128)
resnet18.layer2[0].downsample[1] = nn.GroupNorm(
num_groups=2, num_channels=128
)
resnet18.layer2[1].bn1 = nn.GroupNorm(num_groups=2, num_channels=128)
resnet18.layer2[1].bn2 = nn.GroupNorm(num_groups=2, num_channels=128)
resnet18.layer3[0].bn1 = nn.GroupNorm(num_groups=2, num_channels=256)
resnet18.layer3[0].bn2 = nn.GroupNorm(num_groups=2, num_channels=256)
resnet18.layer3[0].downsample[1] = nn.GroupNorm(
num_groups=2, num_channels=256
)
resnet18.layer3[1].bn1 = nn.GroupNorm(num_groups=2, num_channels=256)
resnet18.layer3[1].bn2 = nn.GroupNorm(num_groups=2, num_channels=256)
resnet18.layer4[0].bn1 = nn.GroupNorm(num_groups=2, num_channels=512)
resnet18.layer4[0].bn2 = nn.GroupNorm(num_groups=2, num_channels=512)
resnet18.layer4[0].downsample[1] = nn.GroupNorm(
num_groups=2, num_channels=512
)
resnet18.layer4[1].bn1 = nn.GroupNorm(num_groups=2, num_channels=512)
resnet18.layer4[1].bn2 = nn.GroupNorm(num_groups=2, num_channels=512)
assert len(dict(resnet18.named_parameters()).keys()) == len(
resnet18.state_dict().keys()
), "More BN layers are there..."
self.model = resnet18
def forward(self, x):
if self.name == "linear":
x = self.fc(x)
elif self.name == "mnist":
x = x.view(-1, 1 * 28 * 28)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
elif self.name == "emnist":
x = x.view(-1, 1 * 28 * 28)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
elif self.name == "cifar10":
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 64 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
elif self.name == "cifar100":
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 64 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
elif self.name == "resnet18":
x = self.model(x)
return x