-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
163 lines (144 loc) · 5.2 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
import torch
from torch import nn
from torchvision.models import resnet18
def create_model(dataset_name, model_name):
"""
Input: Dataset name: can be 'femnist' or 'cifar'
Input: Model name: can be 'resnet18', 'CNNRes4M3M', 'CNNRes3M2M' or 'CNN500k'
The number correspond roughly to the parameteters - in CNNRes4M3M, for cifar it has 4 million parameters,
for femnist it has 3 million parameters
"""
if dataset_name=="femnist":
num_channels=1
image_size=28
num_classes=62
elif dataset_name=="cifar":
num_channels=3
image_size=32
num_classes=10
else:
return None
if model_name=="resnet18":
model = resnet18(num_classes=num_classes)
model.conv1 = nn.Conv2d(num_channels, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
return model
model_dict = {"CNNRes4M3M": CNNRes4M3M,
"CNNRes3M2M": CNNRes3M2M,
"CNN500k": CNN500k,
}
if model_name not in model_dict:
return None
torch.manual_seed(47)
return model_dict[model_name](*[num_channels, image_size, num_classes])
class CNNRes4M3M(nn.Module):
def __init__(self, num_channels, image_size, num_classes):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(num_channels, 32, kernel_size=3, padding=1),
nn.LeakyReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.MaxPool2d(2, 2),
)
self.res1 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
)
self.conv2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.MaxPool2d(2, 2),
)
self.res2 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
)
self.pool = nn.MaxPool2d(2, 2)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(256 * int(image_size/8) * int(image_size/8), 512),
nn.LeakyReLU(),
nn.Dropout(0.5),
nn.Linear(512, num_classes)
)
def forward(self, x):
x = self.conv1(x)
x = self.res1(x) + x
x = self.conv2(x)
x = self.res2(x) + x
x = self.pool(x)
x = self.classifier(x)
return x
class CNNRes3M2M(nn.Module):
def __init__(self, num_channels, image_size, num_classes):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(num_channels, 32, kernel_size=3, padding=1),
nn.LeakyReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.MaxPool2d(2, 2),
)
self.res1 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
)
self.conv2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
nn.MaxPool2d(2, 2),
)
self.res2 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(),
)
self.pool = nn.MaxPool2d(2, 2)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(256 * int(image_size/8) * int(image_size/8), 512),
nn.LeakyReLU(),
nn.Dropout(0.5),
nn.Linear(512, num_classes)
)
def forward(self, x):
x = self.conv1(x)
x = self.res1(x) + x
x = self.conv2(x)
x = self.res2(x) + x
x = self.pool(x)
x = self.classifier(x)
return x
class CNN500k(nn.Module):
def __init__(self, num_channels, image_size, num_classes):
super().__init__()
self.layer_stack = nn.Sequential(
nn.Conv2d(num_channels, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Flatten(),
nn.Linear(32 * int(image_size/8) * int(image_size/8), 512),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, num_classes)
)
def forward(self, x):
return self.layer_stack(x)