-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodels.py
422 lines (319 loc) · 14.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""
Implementation of MNIST and CIFAR models used in IoT paper.
"""
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # set otherwise tf prints gpu info
import tensorflow as tf
import numpy as np
from tensorflow.keras import Model
from tensorflow.keras.layers import ( Dense, Flatten, Conv2D, MaxPool2D,
BatchNormalization, ReLU, Softmax,
Dropout)
class FedAvgModel(Model):
"""
Abstract class for models used in paper. Provides easy methods for getting
and setting model and optimizer weights, and training/testing within FedAvg
code. Children must implement fwd_train and fwd_test.
"""
def __init__(self, optimizer, loss_calc):
"""
Returns a new FedAvgModel. Uses the associated optimizer and loss
function as part of the training/testing methods.
Parameters:
optimizer (tf.keras.optimizers) class callable *not* instance
loss_cals (tf.keras.losses) loss callable *not* instance
"""
super(FedAvgModel, self).__init__()
self.optimizer = optimizer()
self.loss_calc = loss_calc()
def set_weights(self, new_ws):
""" Set model weights to given 2d list of layers. """
for i in range(len(new_ws)):
self.layer_list[i].set_weights(new_ws[i])
def get_weights(self):
""" Returns list of layers of weights. """
return [w.get_weights() for w in self.layer_list]
def get_optim_weights(self):
""" Returns list of arrays of current optimizer values. """
return self.optimizer.get_weights()
def set_optim_weights(self, ws):
""" Set the optimizer values using given 2d list of layers. """
self.optimizer.set_weights(ws)
def fwd_train(self, x):
"""
Perform one forward training pass.
Parameters:
x (array): input data of shape [batch_size, data_shape]
Returns:
array of shape [batch_size, n_outputs]
"""
raise NotImplementedError()
def fwd_test(self, x):
"""
Perform one forward pass during testing.
Parameters:
x (array): input data of shape [batch_size, data_shape]
Returns:
array of shape [batch_size, n_outputs]
"""
raise NotImplementedError()
def train_step(self, x, y):
"""
Perform one training step using the given input data and labels, using
the loss function and optimizer given when created.
Parameters:
x (array): input data of shape [batch_size, sample_shape]
y (array): one-hot labels, shape [batch_size, num_classes]
Returns:
(mean accuracy, mean loss) over data
"""
grads, loss, preds = self.get_grads_loss_preds(x, y)
self.optimizer.apply_gradients(zip(grads, self.trainable_variables))
# calc top-1 accuracy
acc = np.mean(np.argmax(preds.numpy(), axis=0) == np.argmax(y, axis=0))
return acc, loss
def get_grads_loss_preds(self, x, y):
"""
Perform one forward and backward pass using the given data.
Parameters:
x (array): input data of shape [batch_size, sample_shape]
y (array): one-hot labels, shape [batch_size, num_classes]
Returns:
grads (list): list of gradients computed over batch
loss (float): average loss over batch
preds (array): predictions for batch, shape [batch_size, n_classes]
"""
# forward pass
with tf.GradientTape() as tape:
preds = self.fwd_train(x)
loss = self.loss_calc(y, preds)
# backward pass
grads = tape.gradient(loss, self.trainable_variables)
loss = loss.numpy()
return grads, loss, preds
def train(self, x, y, B):
"""
Splits data into batches of size B and performs SGD for 1 epoch.
Parameters:
x (array): samples, shape [total_samples, sample_shape]
y (array): one-hot labels, shape [total_samples, num_classes]
B (int): batch size
"""
b_pr_e = x.shape[0] // B
for b in range(b_pr_e):
self.train_step(x[b*B:(b+1)*B], y[b*B:(b+1)*B])
if b_pr_e * B < x.shape[0]:
self.train_step(x[b_pr_e*B:], y[b_pr_e*B:])
def _test_xy(self, x, y):
"""
Return average loss and top-1 accuracy on one batch.
Parameters:
x (array): samples array of shape [batch_size, sample_shape]
y (array): one-hot labels of shape [batch_size, num_classes]
Returns:
(mean loss, mean accuracy) over data
"""
preds = self.fwd_test(x)
loss = self.loss_calc(y, preds).numpy()
acc = np.mean(np.argmax(preds.numpy(), axis=1) == np.argmax(y, axis=1))
return loss, acc
def test(self, x, y, B):
"""
Compute mean error and accuracy over given data using batch size B
Parameters:
x (array): samples of shape [total_samples, sample_shape]
y (array): one-hot labels of shape [total_samples, num_classes]
B (int): batch size to use during testing
Returns:
(mean error, mean accuracy) over data
"""
b_pr_t = x.shape[0] // B
avg_err = 0.0
avg_acc = 0.0
tot_samples = 0
for b in range(b_pr_t):
loss, acc = self._test_xy(x[b*B:(b+1)*B], y[b*B:(b+1)*B])
avg_err += loss
avg_acc += acc
tot_samples += 1
# run on last few samples if B does not fit exactly into total_samples
if b_pr_t * B < x.shape[0]:
loss, acc = self._test_xy(x[b_pr_t*B:], y[b_pr_t*B:])
avg_err += loss
avg_acc += acc
tot_samples += 1
avg_err /= tot_samples
avg_acc /= tot_samples
return avg_err, avg_acc
class MNIST2NNModel(FedAvgModel):
""" MNIST-2NN model described in Section V A. """
name = 'MNIST2NNModel'
def __init__(self, optimizer, loss_calc, in_size, outputs):
"""
Create a new MNIST-2NN model.
Parameters:
optimizer (tf.keras.optimizers): class callable *not* instance
loss_cals (tf.keras.losses): loss callable *not* instance
in_size (int): number of sample features
outputs (int): number of classes
"""
super(MNIST2NNModel, self).__init__(optimizer, loss_calc)
self.dense1 = Dense(200, activation='relu')
self.dense2 = Dense(200, activation='relu')
self.out = Dense(outputs, activation='softmax')
self.layer_list = [self.dense1, self.dense2, self.out]
# because of TF 2's eager execution I can't find a way of initialising
# the model weights and optimizer values easily without running a single
# training step when the model is created
self.train_step(np.zeros((1,in_size), dtype=np.float32),
np.zeros((1,outputs), dtype=np.float32))
def fwd_train(self, x):
"""
Perform one forward pass during training.
Parameters:
x (array): input data of shape [batch_size, data_shape]
Returns:
array of shape [batch_size, n_outputs]
"""
a = self.dense1(x)
a = self.dense2(a)
return self.out(a)
def fwd_test(self, x):
"""
Perform one forward pass during testing.
Parameters:
x (array): input data of shape [batch_size, data_shape]
Returns:
array of shape [batch_size, n_outputs]
"""
return self.fwd_train(x)
class MNISTCNNModel(FedAvgModel):
""" MNIST-CNN model described in section V A. """
name = 'MNISTCNNModel'
def __init__(self, optimizer, loss_calc, img_size, channels, outputs):
"""
Create a new MNIST-CNN model.
Parameters:
optimizer (tf.keras.optimizers): class callable *not* instance
loss_cals (tf.keras.losses): loss callable *not* instance
img_size (int): height and width of image
channels (int): number of channels in image
outputs (int): number of classes
"""
super(MNISTCNNModel, self).__init__(optimizer, loss_calc)
self.conv1 = Conv2D(32, 5, activation='relu', padding='same')
self.pool1 = MaxPool2D((2, 2), (2, 2))
self.conv2 = Conv2D(64, 5, activation='relu', padding='same')
self.pool2 = MaxPool2D((2, 2), (2, 2))
self.flatten = Flatten()
self.dense1 = Dense(512, activation='relu')
self.out = Dense(outputs, activation='softmax')
self.layer_list = [self.conv1, self.conv2, self.dense1, self.out]
# because of TF 2's eager execution I can't find a way of initialising
# the model weights and optimizer values easily without running a single
# training step when the model is created
self.train_step(np.zeros((1,img_size,img_size,channels), dtype=np.float32),
np.zeros((1,outputs), dtype=np.float32))
def fwd_train(self, x):
"""
Perform one forward pass during training.
Parameters:
x (array): input data of shape [batch_size, data_shape]
Returns:
array of shape [batch_size, n_outputs]
"""
x_2d = tf.reshape(x, [-1, 28, 28, 1])
a = self.conv1(x_2d)
a = self.pool1(a)
a = self.conv2(a)
a = self.pool2(a)
a = self.flatten(a)
a = self.dense1(a)
return self.out(a)
def fwd_test(self, x):
"""
Perform one forward pass during testing.
Parameters:
x (array): input data of shape [batch_size, data_shape]
Returns:
array of shape [batch_size, n_outputs]
"""
return self.fwd_train(x)
class CIFARCNNModel(FedAvgModel):
""" CIFAR-CNN fully-convolutional model as described in section V A. """
name = 'CIFARCNNModel'
def __init__(self, optimizer, loss_calc, img_size, channels, outputs):
"""
Create a new CIFAR-CNN model.
Parameters:
optimizer (tf.keras.optimizers): class callable *not* instance
loss_cals (tf.keras.losses): loss callable *not* instance
img_size (int): height and width of image
channels (int): number of channels in image
outputs (int): number of classes
"""
super(CIFARCNNModel, self).__init__(optimizer, loss_calc)
self.conv1 = Conv2D(32, 3, activation='relu', padding='same')
self.bn1 = BatchNormalization()
self.conv2 = Conv2D(32, 3, activation='relu', padding='same')
self.bn2 = BatchNormalization()
self.drop1 = Dropout(0.2)
self.conv3 = Conv2D(64, 3, activation='relu', padding='same')
self.bn3 = BatchNormalization()
self.conv4 = Conv2D(64, 3, activation='relu', padding='same')
self.bn4 = BatchNormalization()
self.drop2 = Dropout(0.3)
self.conv5 = Conv2D(128, 3, activation='relu', padding='same')
self.bn5 = BatchNormalization()
self.conv6 = Conv2D(128, 3, activation='relu', padding='same')
self.bn6 = BatchNormalization()
self.drop3 = Dropout(0.4)
self.flat = Flatten()
self.out = Dense(outputs, activation='softmax')
self.layer_list = [ self.conv1, self.bn1, self.conv2, self.bn2,
self.conv3, self.bn3, self.conv4, self.bn4,
self.conv5, self.bn5, self.conv6, self.bn6,
self.out]
self.train_step(np.zeros((1,img_size,img_size,channels), dtype=np.float32),
np.zeros((1,outputs), dtype=np.float32))
def _fwd(self, x, train):
"""
Internal method used by fwd_train and fwd_test: model contains BN layers
that need a boolean passed whether training (using batch statistics) or
testing (using running averages).
"""
a = self.conv1(x)
a = self.bn1(a, training=train)
a = self.conv2(a)
a = self.bn2(a, training=train)
a = self.drop1(a)
a = self.conv3(a)
a = self.bn3(a, training=train)
a = self.conv4(a)
a = self.bn4(a, training=train)
a = self.drop2(a)
a = self.conv5(a)
a = self.bn5(a, training=train)
a = self.conv6(a)
a = self.bn6(a, training=train)
a = self.drop3(a)
a = self.flat(a)
return self.out(a)
def fwd_train(self, x):
"""
Perform one forward pass during training.
Parameters:
x (array): input data of shape [batch_size, data_shape]
Returns:
array of shape [batch_size, n_outputs]
"""
return self._fwd(x, True)
def fwd_test(self, x):
"""
Perform one forward pass during testing.
Parameters:
x (array): input data of shape [batch_size, data_shape]
Returns:
array of shape [batch_size, n_outputs]
"""
return self._fwd(x, False)