-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils_models.py
67 lines (58 loc) · 2.66 KB
/
utils_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
from utils_libs import *
class client_model(nn.Module):
def __init__(self, name, args=''):
super(client_model, self).__init__()
self.name = name
self.proto = False
if self.name == 'cifar10':
self.n_cls = args if isinstance(args, int) else 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)
if self.name == 'cifar100':
self.n_cls = args if isinstance(args, int) else 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)
if self.name == 'miniImageNet':
self.n_cls = args if isinstance(args, int) else 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.conv3 = 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*7*7, 400)
self.fc2 = nn.Linear(400, 100)
self.fc3 = nn.Linear(100, self.n_cls)
def forward(self, x):
if 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_)
if 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_)
if self.name == 'miniImageNet':
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
x = x.view(-1, 64*7*7)
x = F.relu(self.fc1(x))
x_= F.relu(self.fc2(x))
x = self.fc3(x_)
if self.proto:
return x_
else:
return x