-
Notifications
You must be signed in to change notification settings - Fork 28
/
models.py
148 lines (116 loc) · 4.18 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
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
class LinearLayer(nn.Module):
def __init__(self, input_dimension, num_classes, bias=True):
super(LinearLayer, self).__init__()
self.input_dimension = input_dimension
self.num_classes = num_classes
self.fc = nn.Linear(input_dimension, num_classes, bias=bias)
def forward(self, x):
return self.fc(x)
class FemnistCNN(nn.Module):
"""
Implements a model with two convolutional layers followed by pooling, and a final dense layer with 2048 units.
Same architecture used for FEMNIST in "LEAF: A Benchmark for Federated Settings"__
We use `zero`-padding instead of `same`-padding used in
https://github.com/TalwalkarLab/leaf/blob/master/models/femnist/cnn.py.
"""
def __init__(self, num_classes):
super(FemnistCNN, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 5)
self.fc1 = nn.Linear(64 * 4 * 4, 2048)
self.output = nn.Linear(2048, num_classes)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 64 * 4 * 4)
x = F.relu(self.fc1(x))
x = self.output(x)
return x
class CIFAR10CNN(nn.Module):
def __init__(self, num_classes):
super(CIFAR10CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 5)
self.fc1 = nn.Linear(64 * 5 * 5, 2048)
self.output = nn.Linear(2048, num_classes)
def forward(self, x):
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 = self.output(x)
return x
class NextCharacterLSTM(nn.Module):
def __init__(self, input_size, embed_size, hidden_size, output_size, n_layers):
super(NextCharacterLSTM, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.embed_size = embed_size
self.output_size = output_size
self.n_layers = n_layers
self.encoder = nn.Embedding(input_size, embed_size)
self.rnn =\
nn.LSTM(
input_size=embed_size,
hidden_size=hidden_size,
num_layers=n_layers,
batch_first=True
)
self.decoder = nn.Linear(hidden_size, output_size)
def forward(self, input_):
encoded = self.encoder(input_)
output, _ = self.rnn(encoded)
output = self.decoder(output)
output = output.permute(0, 2, 1) # change dimension to (B, C, T)
return output
def get_vgg11(n_classes):
"""
creates VGG11 model with `n_classes` outputs
:param n_classes:
:return: nn.Module
"""
model = models.vgg11(pretrained=True)
model.classifier[6] = nn.Linear(model.classifier[6].in_features, n_classes)
return model
def get_squeezenet(n_classes):
"""
creates SqueezeNet model with `n_classes` outputs
:param n_classes:
:return: nn.Module
"""
model = models.squeezenet1_0(pretrained=True)
model.classifier[1] = nn.Conv2d(512, n_classes, kernel_size=(1, 1), stride=(1, 1))
model.num_classes = n_classes
return model
def get_mobilenet(n_classes):
"""
creates MobileNet model with `n_classes` outputs
:param n_classes:
:return: nn.Module
"""
model = models.mobilenet_v2(pretrained=True)
model.classifier[1] = nn.Linear(model.classifier[1].in_features, n_classes)
return model
def get_resnet18(n_classes):
"""
creates Resnet model with `n_classes` outputs
:param n_classes:
:return: nn.Module
"""
model = models.resnet18(pretrained=True)
model.fc = nn.Linear(model.fc.in_features, n_classes)
return model
def get_resnet34(n_classes):
"""
creates Resnet34 model with `n_classes` outputs
:param n_classes:
:return: nn.Module
"""
model = models.resnet34(pretrained=True)
model.fc = nn.Linear(model.fc.in_features, n_classes)
return model