-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_select.py
340 lines (250 loc) · 12.6 KB
/
random_select.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
#! /usr/bin/python3
from snapshot import SnapshotCallbackBuilder
from sklearn.metrics import balanced_accuracy_score, f1_score
from fitness import *
from diversity_utils import *
import tensorflow as tf
import os.path
import random
import numpy as np
import json
import time
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
import uuid
identifier = str(uuid.uuid4())
import argparse
parser = argparse.ArgumentParser(description='Train model with fault params')
parser.add_argument('--dataset', type=str, choices=['mnist', 'cifar10', 'gtsrb', 'pneumonia'], default='cifar10')
parser.add_argument('--fault_amount_min', type=int, choices=[10, 30, 50], default=10)
parser.add_argument('--fault_amount_max', type=int, choices=[10, 30, 50], default=50)
parser.add_argument('--fault_type', type=str, choices=['label_err', 'remove', 'repeat'], default="label_err")
parser.add_argument('--natural', action='store_true')
parser.add_argument('--acc_metric', type=str, choices=['accuracy', 'balanced_accuracy', 'f1'], default='balanced_accuracy')
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--epochs', type=int, default=1)
parser.add_argument('--partition', type=float, default=63)
parser.add_argument('--snapshots', type=int, default=3)
parser.add_argument('--alpha_zero', type=float, default=0.01)
args = parser.parse_args()
def arch(dataset, model_name, final_fault, symmetric, num_epochs, batch_size):
(x_train, y_train), (x_test, y_test) = load_training_data(dataset, final_fault, symmetric)
model = get_trained_model(dataset, model_name, x_train, y_train, num_epochs, batch_size)
scores = model.evaluate(x_test,
y_test,
batch_size=batch_size,
verbose=0)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
print("\n\nTraining finished\n\n")
div_op = "arch"
write_preds_file(model, x_test, div_op, dataset, model_name, final_fault, symmetric, "0")
def data(dataset, model_name, final_fault, symmetric, num_epochs, batch_size, partition, identifier):
(x_train, y_train), (x_test, y_test) = load_training_data(dataset, final_fault, symmetric, True, partition)
model = get_trained_model(dataset, model_name, x_train, y_train, num_epochs, batch_size)
scores = model.evaluate(x_test,
y_test,
batch_size=batch_size,
verbose=0)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
print("\n\nTraining finished\n\n")
div_op = "data"
write_preds_file(model, x_test, div_op, dataset, model_name, final_fault, symmetric, identifier)
def snapshot(dataset, model_name, final_fault, symmetric, num_epochs, batch_size, num_snapshots, alpha_zero):
(x_train, y_train), (x_test, y_test) = load_training_data(dataset, final_fault, symmetric)
M = num_snapshots # number of snapshots
T = num_epochs # number of epochs
# initial learning rate is alpha_zero
model_prefix = 'Model_'
snapshot = SnapshotCallbackBuilder(T, M, x_test, dataset, model_name, final_fault, symmetric, alpha_zero)
input_shape = x_train.shape[1:]
model = get_model_by_name(model_name, input_shape)
model.compile(loss='sparse_categorical_crossentropy', optimizer='Adam', metrics=['acc'])
model.fit(x_train, y_train, callbacks=snapshot.get_callbacks(model_prefix=model_prefix), epochs=num_epochs, batch_size=batch_size)
def random_select(encoded_arr, total_models, ens_size, num_div_ops):
total_arr_len = total_models * (1 + (num_div_ops - 1))
encoded_arr_seed = [0] * total_arr_len
ens_size = 3
total_pos_comb = total_models * (1 + (num_div_ops - 1) * ens_size)
random_idx_list = random.sample(range(total_pos_comb), ens_size)
random_idx_list.sort()
for idx in random_idx_list:
if idx < total_models:
encoded_arr[idx] = 1
if idx >= total_models:
new_idx = (idx - total_models) // ens_size + total_models
encoded_arr[new_idx] += 1
print("Sum random select arr: ", sum(encoded_arr))
print("Random select arr: ", encoded_arr)
return encoded_arr
def config_exists(dataset, model_name, final_fault, symmetric, div_op, identifier=""):
if final_fault == "golden":
fault_type = golden
else:
fault_type = final_fault.split('-')[0]
if div_op == "arch":
identifier = "0"
if symmetric:
filepath = "./injection/" + dataset + "/" + fault_type + "/" + div_op + "-" + model_name + "-" + final_fault + "-" + identifier
else:
filepath = "./injection/" + dataset + "/" + fault_type + "/" + div_op + "-" + model_name + "-" + final_fault + "-asymmetric-" + identifier
return os.path.exists(filepath)
def get_pred_filename(dataset, model_name, final_fault, symmetric, div_op, identifier=""):
if final_fault == "golden":
fault_type = golden
else:
fault_type = final_fault.split('-')[0]
if div_op == "arch":
identifier = "0"
if symmetric:
filepath = "./injection/" + dataset + "/" + fault_type + "/" + div_op + "-" + model_name + "-" + final_fault + "-" + identifier
else:
filepath = "./injection/" + dataset + "/" + fault_type + "/" + div_op + "-" + model_name + "-" + final_fault + "-asymmetric-" + identifier
return filepath
def is_valid_encoding(encoded_arr, total_models, ens_size, num_div_ops):
return (len(encoded_arr) == total_models * num_div_ops and
sum(encoded_arr) == ens_size)
def decode(encoded_arr, dataset, final_fault, symmetric, num_epochs, batch_size, partition, alpha_zero):
model_list = ["ConvNet", "DeconvNet", "MobileNet", "ResNet18", "ResNet50", "VGG11", "VGG16"]
ens_size = 3
N = int(len(encoded_arr)/ens_size)
pred_filenames = []
for idx, config in enumerate(encoded_arr):
if idx < N:
if config != 0:
model_name = model_list[idx]
div_op = "arch"
if not config_exists(dataset, model_name, final_fault, symmetric, div_op):
print("Training arch: ", model_name)
arch(dataset, model_name, final_fault, symmetric, num_epochs, batch_size)
pred_filenames.append(get_pred_filename(dataset, model_name, final_fault, symmetric, div_op))
elif idx < 2*N:
if config != 0:
model_name = model_list[idx-N]
div_op = "data"
for j in range(config):
identifier = str(j)
if not config_exists(dataset, model_name, final_fault, symmetric, div_op, identifier):
print("Training data div: ", model_name)
data(dataset, model_name, final_fault, symmetric, num_epochs, batch_size, partition, identifier)
pred_filenames.append(get_pred_filename(dataset, model_name, final_fault, symmetric, div_op, identifier))
elif idx < 3*N:
if config != 0:
model_name = model_list[idx-2*N]
div_op = "snapshot"
if not config_exists(dataset, model_name, final_fault, symmetric, div_op, str(ens_size-1)):
print("Training snapshots: ", model_name)
snapshot(dataset, model_name, final_fault, symmetric, num_epochs, batch_size, ens_size, alpha_zero)
for j in range(config):
identifier = str(j)
pred_filenames.append(get_pred_filename(dataset, model_name, final_fault, symmetric, div_op, identifier))
return pred_filenames
def read_ground_golden_labels(dataset):
groundtruth_filename = "./groundtruth/" + dataset
with open(groundtruth_filename, "r") as f:
ground_labels = json.load(f)
golden_filename = "./golden/unified-" + dataset + "-golden.txt"
with open(golden_filename, "r") as f:
golden_labels = json.load(f)
return ground_labels, golden_labels
def eval_ensemble(encoded_arr, dataset, ground_labels, golden_labels, final_fault, symmetric, num_epochs, batch_size, partition, alpha_zero, acc_metric):
encoded_str = ''.join(map(str, encoded_arr))
ens_size = sum(encoded_arr)
if final_fault == "golden":
fault_type = golden
fault_amt = "0"
else:
fault_type = final_fault.split('-')[0]
fault_amt = final_fault.split('-')[1]
if symmetric:
ens_fprefix = "./injection/" + dataset + "/" + fault_type + "/" + final_fault + "-" + encoded_str
else:
ens_fprefix = "./injection/" + dataset + "/" + fault_type + "/" + final_fault + "-asymmetric-" + encoded_str
ens_fname_pred = ens_fprefix + "-pred.csv"
ens_fname_corr = ens_fprefix + "-correct.csv"
if not os.path.exists(ens_fname_pred) or not os.path.exists(ens_fname_corr):
pred_filenames = decode(encoded_arr, dataset, final_fault, symmetric, num_epochs, batch_size, partition, alpha_zero)
write_ensemble_decision(encoded_arr, pred_filenames, dataset, final_fault, symmetric)
#Directly compute using ens_fname
curr_df = pd.read_csv(ens_fname_pred)
df = curr_df.copy()
curr_df["ground"] = ground_labels
# Calculate accuracy
corr = curr_df[curr_df["ens"]==curr_df["ground"]].shape[0]
total = curr_df.shape[0]
if acc_metric == "balanced_accuracy":
accuracy = balanced_accuracy_score(curr_df["ground"], curr_df["ens"]) # Balanced Accuracy
elif acc_metric == "f1":
accuracy = f1_score(curr_df["ground"], curr_df["ens"], average="binary") # Macro F1
else:
accuracy = corr / total # Regular Accuracy
if fault_amt != "0":
curr_df = curr_df.iloc[golden_labels]
incorr = curr_df[curr_df["ens"]!=curr_df["ground"]].shape[0]
total = curr_df.shape[0]
ad = incorr / total
else:
ad = 0
resilience = ad
gdf = pd.read_csv(ens_fname_corr)
disagreement_measure = get_disagreement_measure(gdf)
shannon_entropy = get_avg_eh(df, ens_size)
diversity = 0.5 * disagreement_measure + 0.5 * shannon_entropy
accuracy = str(round(accuracy, 2))
resilience = str(round(resilience, 2))
diversity = str(round(diversity, 2))
return accuracy, resilience, diversity
def print_best_ensemble(encoded_str, ens_stat_list, dataset, fault_type, elapsed_time, symmetric):
elapsed_time = str(int(elapsed_time))
if symmetric:
desemble_logfilename = "./output/" + dataset + "_" + fault_type + "_random_select"
else:
desemble_logfilename = "./output/" + dataset + "_" + fault_type + "_asymmetric_random_select"
with open(desemble_logfilename, "w") as f:
for ens_stats in ens_stat_list:
fault_amount = ens_stats[0]
accuracy = ens_stats[1]
resilience = ens_stats[2]
diversity = ens_stats[3]
entry = f'{encoded_str}, {fault_type}, {fault_amount}, {accuracy}, {resilience}, {diversity}, {elapsed_time}'
f.write(entry)
f.write("\n")
def get_fault_amt_range(fault_amt_lower, fault_amt_higher):
all_fault_range = [10, 30, 50]
return [str(fault_amt) for fault_amt in all_fault_range if fault_amt_lower <= fault_amt <= fault_amt_higher]
def main():
dataset = args.dataset
fault_type = args.fault_type
symmetric = not args.natural
fault_amount_min = args.fault_amount_min
fault_amount_max = args.fault_amount_max
acc_metric = args.acc_metric
partition = args.partition
num_epochs = args.epochs
batch_size = args.batch_size
alpha_zero = args.alpha_zero
N = total_models = 7
num_div_ops = 3
ens_size = 3
fault_amt_arr = get_fault_amt_range(fault_amount_min, fault_amount_max)
ground_labels, golden_labels = read_ground_golden_labels(dataset)
zeros = [0] * N
encoded_arr = []
encoded_arr.extend(zeros)
encoded_arr.extend(zeros)
encoded_arr.extend(zeros)
start = time.time()
encoded_arr = random_select(encoded_arr, total_models, ens_size, num_div_ops)
encoded_str = ''.join(map(str, encoded_arr))
ens_stat_list = []
for fault_amt in fault_amt_arr:
final_fault = fault_type + "-" + str(fault_amt)
accuracy, resilience, diversity = eval_ensemble(encoded_arr, dataset, ground_labels, golden_labels, final_fault, symmetric, num_epochs, batch_size, partition, alpha_zero, acc_metric)
ens_stat_list.append([fault_amt, accuracy, resilience, diversity])
end = time.time()
elapsed_time = end - start
print_best_ensemble(encoded_str, ens_stat_list, dataset, fault_type, elapsed_time, symmetric)
if __name__ == "__main__":
main()