-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
158 lines (123 loc) · 4.13 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
from PIL import Image
from os.path import join
import imageio
from torch import nn
from torch.nn.modules.linear import Linear
from torch.utils.data import Dataset
from torchvision import transforms
from tqdm import tqdm
import numpy as np
import torch
import torch.nn.functional as F
import torchvision.models as models
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.fc = nn.Sequential(
nn.Linear(784, 100),
nn.ReLU(),
nn.Linear(100, 50),
nn.ReLU(),
nn.Linear(50, 10),
nn.Sigmoid()
)
def forward(self, x):
x = torch.flatten(x,start_dim = 1)
z = self.fc(x)
return z
class FMNIST_CNN(nn.Module):
def __init__(self,
in_channels=1,
input_shape=[28,28],
classes = 10,
drop_rate=0.2,
):
super(FMNIST_CNN,self).__init__()
self.in_channels = in_channels
self.input_shape = input_shape
self.classes = classes
self.drop_rate = drop_rate
# Convolutonal Layer 1
self.convlayer1 = nn.Sequential(
nn.Conv2d(in_channels,32, kernel_size = 5, padding = 0),
nn.PReLU(),
nn.BatchNorm2d(32),
nn.MaxPool2d(kernel_size=2, stride=2),
)
# Convolutonal Layer 2
self.convlayer2 = nn.Sequential(
nn.Conv2d(32,32, kernel_size = 5,padding = 0),
nn.PReLU(),
nn.BatchNorm2d(32),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout(self.drop_rate, inplace=True)
)
# Dense Layer 1
self.denselayer = nn.Sequential(
nn.Linear(32*4*4, self.classes),
)
self.denselayer.name = 'fc'
def forward(self,x): #x = [batch,time,freq]
x = self.convlayer1(x)
x = self.convlayer2(x)
x = torch.flatten(x,start_dim=1)
p = self.denselayer(x)
return p
class CNN_CIFAR_dropout(torch.nn.Module):
"""Model Used by the paper introducing FedAvg"""
def __init__(self):
super(CNN_CIFAR_dropout, self).__init__()
self.conv1 = nn.Conv2d(
in_channels=3, out_channels=32, kernel_size=(3, 3)
)
self.conv2 = nn.Conv2d(
in_channels=32, out_channels=64, kernel_size=(3, 3)
)
self.conv3 = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=(3, 3)
)
self.fc1 = nn.Linear(4 * 4 * 64, 64)
self.fc2 = nn.Linear(64, 10)
self.dropout = nn.Dropout(p=0.2)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = self.dropout(x)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = self.dropout(x)
x = self.conv3(x)
x = self.dropout(x)
x = x.view(-1, 4 * 4 * 64)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
class CNN_CIFAR100_dropout(torch.nn.Module):
"""Model Used by the paper introducing FedAvg"""
def __init__(self):
super(CNN_CIFAR100_dropout, self).__init__()
self.conv1 = nn.Conv2d(
in_channels=3, out_channels=32, kernel_size=(3, 3)
)
self.conv2 = nn.Conv2d(
in_channels=32, out_channels=64, kernel_size=(3, 3)
)
self.conv3 = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=(3, 3)
)
self.fc1 = nn.Linear(4 * 4 * 64, 256)
self.fc2 = nn.Linear(256, 100)
self.dropout = nn.Dropout(p=0.2)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = self.dropout(x)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = self.dropout(x)
x = self.conv3(x)
x = self.dropout(x)
x = x.view(-1, 4 * 4 * 64)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x