-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributed_keras.py
283 lines (252 loc) · 13.4 KB
/
distributed_keras.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
#!/usr/bin/env python -W ignore::DeprecationWarning
from __future__ import print_function
import itertools
import keras
from keras.datasets import mnist, cifar10
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import numpy as np
import math
import csv
USE_CIFAR = False
N_ITERATIONS = 15
ACC_THRESHOLD = 0.6
class distributed_training:
def __init__(self, n_segments, run_centralized=False):
self.n_classes = 10
self.n_iterations = N_ITERATIONS
self.batch_size = 32
self.n_segments = n_segments
self.n_epochs = 1
self.get_data()
self.distribute_data()
self.aggregate_model = self.define_segment_models()
self.train_model_aggregate(run_centralized)
def get_data(self):
if USE_CIFAR:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
self.num_train_examples, self.num_test_examples = x_train.shape[0], x_test.shape[0]
self.img_rows, self.img_cols, self.num_channels = x_train.shape[2], x_train.shape[3], x_train.shape[1]
x_train = x_train.reshape(self.num_train_examples, self.num_channels, self.img_rows, self.img_cols)
x_test = x_test.reshape(self.num_test_examples, self.num_channels, self.img_rows, self.img_cols)
else:
(x_train, y_train), (x_test, y_test) = mnist.load_data()
self.num_train_examples, self.num_test_examples = x_train.shape[0], x_test.shape[0]
self.img_rows, self.img_cols, self.num_channels = x_train.shape[1], x_train.shape[2], 1
x_train = x_train.reshape(self.num_train_examples, self.img_rows, self.img_cols, self.num_channels)
x_test = x_test.reshape(self.num_test_examples, self.img_rows, self.img_cols, self.num_channels)
self.y_train = y_train.reshape(self.num_train_examples, 1)
self.y_test = y_test.reshape(self.num_test_examples, 1)
self.x_train = x_train.astype('float32')
self.x_test = x_test.astype('float32')
self.x_train /= 255.0
self.x_test /= 255.0
# Convert the y vectors to categorical format for crossentropy prediction
self.y_train = keras.utils.to_categorical(self.y_train, self.n_classes)
self.y_test = keras.utils.to_categorical(self.y_test, self.n_classes)
def distribute_data(self):
self.segment_batches = []
# self.x_train, self.y_train = shuffle(self.x_train, self.y_train)
data_per_segment = int(math.floor(self.x_train.shape[0] / self.n_segments))
for i in range(self.n_segments):
self.segment_batches.append((self.x_train[data_per_segment * i:data_per_segment * i + data_per_segment],
self.y_train[data_per_segment * i:data_per_segment * i + data_per_segment]))
print([(s[0].shape, s[1].shape) for s in self.segment_batches])
def get_new_model(self):
model = Sequential()
if USE_CIFAR:
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(self.num_channels, self.img_rows, self.img_cols,)))
else:
model.add(Conv2D(3, kernel_size=(3, 3),
activation='sigmoid',
input_shape=(self.img_rows, self.img_cols, self.num_channels,)))
# model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
# model.add(Dense(128, activation='relu'))
# model.add(Dropout(0.2))
model.add(Dense(self.n_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=Adam(),
metrics=['categorical_accuracy'])
return model
def define_segment_models(self):
self.segment_models = []
common_model = self.get_new_model()
for i in range(self.n_segments):
cloned_model = self.get_new_model()
cloned_model.set_weights(common_model.get_weights())
cloned_model.compile(loss='categorical_crossentropy',
optimizer=Adam(),
metrics=['categorical_accuracy'])
self.segment_models.append(cloned_model)
return common_model
def aggregate_from_segments(self):
# Compile aggregate model
# self.aggregate_model.compile(loss='categorical_crossentropy',
# optimizer=Adam(),
# metrics=['accuracy'])
try:
avg_weights = sum(
[np.array(s.get_weights()) for s in self.segment_models]) / self.n_segments
self.aggregate_model.set_weights(avg_weights)
except Exception as e:
print(str(e))
for s in self.segment_models:
print('------------------------------------------------------')
print([i.shape for i in s.get_weights()])
print('------------------------------------------------------')
raise
def clone_to_segments(self):
for model in self.segment_models:
model.set_weights(self.aggregate_model.get_weights())
def train_model_aggregate(self, run_centralized=False):
initial_model_score = self.aggregate_model.evaluate(self.x_train, self.y_train, verbose=0)[1]
self.agg_model_scores = [initial_model_score]
self.segment_models_scores = [[initial_model_score] * self.n_segments]
# Training and evaluation for a single model with all training data
if run_centralized:
self.centralized_model_scores = [initial_model_score]
self.centralized_model = self.get_new_model()
self.centralized_model.set_weights(self.aggregate_model.get_weights())
# self.centralized_model.warm_start = True
self.centralized_model.compile(loss='categorical_crossentropy',
optimizer=Adam(),
metrics=['categorical_accuracy'])
for i in range(self.n_iterations):
self.centralized_model.fit(self.x_train, self.y_train,
batch_size=self.batch_size,
epochs=1,
verbose=0)
centralized_score = self.centralized_model.evaluate(self.x_train,
self.y_train,
verbose=0)[1]
print("Iteration {}: centralized score = {}".format(i, centralized_score))
self.centralized_model_scores.append(centralized_score)
print('------------------- Nu of segments = {} ----------------------'.
format(self.n_segments))
# Training and evaluation loop for aggregating models
# for i in itertools.count():
# for i in range(self.n_iterations):
if False:
self.segment_models_scores.append(list(itertools.repeat(0, self.n_segments)))
for seg_index, model_seg in enumerate(self.segment_models):
(x_train_seg, y_train_seg) = self.segment_batches[seg_index]
model_seg.fit(x_train_seg, y_train_seg,
batch_size=self.batch_size,
verbose=0, epochs=1)
model_score = model_seg.evaluate(self.x_train, self.y_train, verbose=0)[1]
print("\t Segment model {} score = {}".format(seg_index, model_score))
self.segment_models_scores[i+1][seg_index] = model_score
self.aggregate_from_segments()
self.clone_to_segments()
agg_score = self.aggregate_model.evaluate(self.x_train, self.y_train, verbose=0)[1]
self.agg_model_scores.append(agg_score)
print("Iteration {}/{}, aggregate model score = {}".
format(i+1, self.n_iterations, agg_score))
# if agg_score >= ACC_THRESHOLD or (i+1) == self.n_iterations:
# break
print('---------------------------------------------------------------')
def run_experiment():
for n_segments in range(2, 3, 2): # range(start, stop, step)
model = distributed_training(n_segments, run_centralized=True)
if hasattr(model, 'centralized_model_scores'):
with open('mnist_keras_scores_seg_1.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for val in model.centralized_model_scores:
csvwriter.writerow([val])
with open('mnist_keras_scores_seg_{}.csv'.format(n_segments), 'w') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for seg_scores, agg_score in zip(model.segment_models_scores, model.agg_model_scores):
csvwriter.writerow(seg_scores + [agg_score])
def compute_cutoff():
data_label = 'CIFAR' # 'MNIST'
suptitle = '{} data classification'.format(data_label)
all_cutoffs = []
segment_counts = [1] + range(2, 11, 2)
for n_segments in segment_counts:
filename = '{}_keras_scores_seg_{}.csv'.format(data_label.lower(), n_segments)
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for n_iter, line in enumerate(csvreader):
if float(line[-1]) >= ACC_THRESHOLD:
all_cutoffs.append(n_iter + 1)
break
else:
all_cutoffs.append(N_ITERATIONS)
# plot comparison between single-node and multi-node convergencee
fig = plt.figure()
fig.suptitle(suptitle, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('Time to reach {} accuracy'.format(ACC_THRESHOLD))
ax.set_xlabel('Number of segments')
ax.set_ylabel('Number of iterations')
linear_scale_line = np.array(segment_counts) * all_cutoffs[0]
plt.plot(segment_counts, all_cutoffs, 'g-', marker='+', label="Actual convergence times")
plt.plot(segment_counts, linear_scale_line, 'b-', alpha=0.5, label="Break-even wrt to centralized")
ax.legend(loc='lower right')
plt.savefig('{}_keras_accuracy_cutoffs.png'.format(data_label.lower()))
def plot_model_scores():
for data_label in ('CIFAR', 'MNIST'):
suptitle = '{} data classification'.format(data_label)
x_axis = range(N_ITERATIONS + 1)
segment_counts = [1] + range(2, 11, 2) # first one should always be 1
all_single_models = []
for n_segments in segment_counts:
segment_models_scores = []
single_model_scores = []
with open('{}_keras_scores_seg_{}.csv'.format(data_label.lower(), n_segments), 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for line in csvreader:
segment_models_scores.append([float(i) for i in line[:-1]])
if n_segments > 1:
single_model_scores.append(float(line[-1]))
else:
single_model_scores.append(float(line[0]))
all_single_models.append(single_model_scores)
if n_segments > 1:
fig = plt.figure()
fig.suptitle(suptitle, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('Number of segments = {}'.format(n_segments))
ax.set_xlabel('Number of iterations')
ax.set_ylabel('Accuracy')
plt.ylim((0, 1))
segment_models_scores = np.transpose(np.array(segment_models_scores))
for scores in segment_models_scores[1:]:
plt.plot(x_axis, scores, 'k-', alpha=0.2)
plt.plot(x_axis, single_model_scores, 'b-', label='Aggregated model')
plt.plot(x_axis, all_single_models[0], 'g-', label='Centralized model')
ax.legend(loc='lower right')
plt.savefig('{}_keras_scores_seg_{}.png'.format(data_label.lower(), n_segments))
# plot comparison between single-node and multi-node convergencee
fig = plt.figure()
fig.suptitle(suptitle, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('Centralized vs Distributed')
ax.set_xlabel('Number of iterations')
ax.set_ylabel('Accuracy')
plt.ylim((0, 1))
for n_segments, each_agg_model in zip(segment_counts, all_single_models):
if n_segments > 1:
plt.plot(x_axis, each_agg_model, label='{} segments'.format(n_segments))
else:
plt.plot(x_axis, each_agg_model, 'g-', marker='+', label='Centralized model')
ax.legend(loc='lower right')
plt.savefig('{}_keras_scores_comparison.png'.format(data_label.lower()))
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# run_experiment()
# plot_model_scores()
compute_cutoff()