-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelUtil.py
218 lines (189 loc) · 8.66 KB
/
modelUtil.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import torch
from torchvision.models import alexnet, resnet18
from torch.nn.functional import relu, softmax
from torch.nn.utils import spectral_norm
from torch import nn, tanh
import copy
def weights_init_uniform(m):
if isinstance(m, nn.Conv2d):
nn.init.kaiming_uniform_(m.weight.data, nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias.data, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight.data, 1)
nn.init.constant_(m.bias.data, 0)
elif isinstance(m, nn.Linear):
nn.init.kaiming_uniform_(m.weight.data)
nn.init.constant_(m.bias.data, 0)
def agg_weights(weights):
with torch.no_grad():
weights_avg = copy.deepcopy(weights[0])
for k in weights_avg.keys():
for i in range(1, len(weights)):
weights_avg[k] += weights[i][k]
weights_avg[k] = torch.div(weights_avg[k], len(weights))
return weights_avg
def blend_image(images, trigger, a):
return images*(1-a)+trigger*a, images*(1+a)-trigger*a
def evaluate_global(users, test_dataloders):
testing_corrects = 0
testing_sum = 0
for index in range(len(users)):
corrects, sum = users[index].evaluate(test_dataloders[index])
testing_corrects += corrects
testing_sum += sum
print(f"Acc: {testing_corrects / testing_sum}")
class SingleModel_pretrained(torch.nn.Module):
def __init__(self, backbone='resnet', n_classes=10):
super().__init__()
if backbone == 'alexnet':
self.model = alexnet(pretrained=True)
n_ftrs = self.model.classifier[-1].out_features
self.fc = torch.nn.Linear(n_ftrs, n_classes)
self.fc.apply(weights_init_uniform)
elif backbone == 'resnet':
self.model = resnet18(pretrained=True)
n_ftrs = self.model.fc.in_features
self.model.fc = torch.nn.Sequential()
self.fc = torch.nn.Linear(n_ftrs, n_classes)
elif backbone == 'cnn_cifar':
self.model = nn.Sequential(torch.nn.Conv2d(3, 6, (5,)),
torch.nn.MaxPool2d(2, 2),
torch.nn.Conv2d(6, 16, (5,)),
torch.nn.Flatten(),
torch.nn.Linear(16 * 5 * 5, 100)
)
self.fc = torch.nn.Linear(100, n_classes)
elif backbone == 'cnn_bioid':
self.model = nn.Sequential(torch.nn.Conv2d(1, 6, 5),
torch.nn.MaxPool2d(2, 2),
torch.nn.Conv2d(6, 16, 5),
torch.nn.Flatten(),
torch.nn.Linear(16*122*122, 100),
)
self.fc = torch.nn.Linear(100, n_classes)
self.model.apply(weights_init_uniform)
self.fc.apply(weights_init_uniform)
elif backbone == 'cnn_mnist':
self.model = nn.Sequential(torch.nn.Conv2d(1, 6, 5),
torch.nn.MaxPool2d(2, 2),
torch.nn.Conv2d(6, 16, 5),
torch.nn.Flatten(),
torch.nn.Linear(8*16*8, 100),
# torch.nn.ReLU()
)
self.fc = torch.nn.Linear(100, n_classes)
self.model.apply(weights_init_uniform)
self.fc.apply(weights_init_uniform)
def forward(self, x):
embedding = self.model(x)
logits = self.fc(embedding)
return logits, softmax(logits, dim=1)
class DualModel_pretrained(torch.nn.Module):
def __init__(self, backbone='resnet', n_classes=10):
super().__init__()
if backbone == 'alexnet':
self.model = alexnet(pretrained=True)
self.model.classifier[-1] = nn.Linear(4096, 256)
self.fc = torch.nn.Linear(2 * 256, n_classes)
# n_ftrs = self.model.classifier[-1].out_features
# self.fc = torch.nn.Linear(2*n_ftrs, n_classes)
self.fc.apply(weights_init_uniform)
elif backbone == 'resnet':
self.model = resnet18(pretrained=True)
n_ftrs = self.model.fc.in_features
self.model.fc = torch.nn.Sequential()
self.fc = torch.nn.Linear(2*n_ftrs, n_classes)
# self.model.apply(weights_init_uniform)
self.fc.apply(weights_init_uniform)
def forward(self, x_1, x_2):
embedding_1 = self.model(x_1)
# print(embedding_1.shape)
embedding_2 = self.model(x_2)
logits = self.fc(torch.cat((embedding_1, embedding_2), 1))
return logits, softmax(logits, dim=1)
class DualModel_CNN(torch.nn.Module):
def __init__(self, n_classes=10):
super().__init__()
self.conv1 = torch.nn.Conv2d(3, 6, 5)
self.pool = torch.nn.MaxPool2d(2, 2)
self.conv2 = torch.nn.Conv2d(6, 16, 5)
self.fc1 = torch.nn.Linear(16 * 5 * 5, 100)
self.fc2 = torch.nn.Linear(200, n_classes)
def forward(self, x_1, x_2):
embedding_1 = self.get_embedding(x_1)
embedding_2 = self.get_embedding(x_2)
logits = self.fc2(torch.cat((embedding_1, embedding_2), 1))
return logits, softmax(logits)
def get_embedding(self, x):
x = self.pool(relu(self.conv1(x)))
x = self.pool(relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
return relu(self.fc1(x))
class TriggerHyper(nn.Module):
def __init__(self, n_nodes, embedding_dim, out_dim=64, ngf=16, hidden_dim=100, n_hidden=3):
super().__init__()
self.embedding = nn.Embedding(num_embeddings=n_nodes, embedding_dim=embedding_dim)
self.ngf = ngf
layers = [nn.Linear(embedding_dim, hidden_dim)]
for _ in range(n_hidden):
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Linear(hidden_dim, hidden_dim))
self.mlp = nn.Sequential(*layers)
self.netG = nn.Linear(hidden_dim, out_dim * out_dim * 3)
self.mlp.apply(weights_init_uniform)
self.netG.apply(weights_init_uniform)
def forward(self, idx):
emd = self.embedding(idx)
features = self.mlp(emd)
trigger = tanh(self.netG(features).view(3, 64, 64))
return trigger
class Generator(nn.Module):
def __init__(self, nz):
super(Generator, self).__init__()
ngf=64
self.fc = nn.Linear(nz, 100)
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d(100, ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(True),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(True),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d( ngf, 3, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 64 x 64
)
self.main.apply(weights_init_uniform)
self.fc.apply(weights_init_uniform)
def forward(self, input):
features = self.fc(input)
return self.main(features.view(-1, 100, 1, 1))
class TriggerHyperDis(nn.Module):
def __init__(self, embedding_dim, data_shape, hidden_dim=100, n_hidden=1):
super().__init__()
layers = [spectral_norm(nn.Linear(embedding_dim, hidden_dim))]
for _ in range(n_hidden):
layers.append(nn.ReLU(inplace=True))
layers.append(spectral_norm(nn.Linear(hidden_dim, hidden_dim)))
self.mlp = nn.Sequential(*layers)
self.mlp.apply(weights_init_uniform)
# self.netG = spectral_norm(nn.Linear(hidden_dim, data_shape[0] * data_shape[1] * data_shape[2]))
self.netG = Generator(nz = hidden_dim)
self.data_shape = data_shape
self.netG.apply(weights_init_uniform)
def forward(self, embedding):
features = self.mlp(embedding)
trigger = self.netG(features).view(*self.data_shape)
return trigger