-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsource_mut_operators.py
278 lines (224 loc) · 12.8 KB
/
source_mut_operators.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
import tensorflow as tf
import numpy as np
import keras
import random
import math
import utils
class SourceMutationOperatorsUtils():
def __init__(self):
self.LR_mut_candidates = ['Dense', 'BatchNormalization']
self.LA_mut_candidates = [keras.layers.ReLU(), keras.layers.BatchNormalization()]
def LA_get_random_layer(self):
num_of_LA_candidates = len(self.LA_mut_candidates)
random_index = random.randint(0, num_of_LA_candidates - 1)
return self.LA_mut_candidates[random_index]
def LR_model_scan(self, model):
index_of_suitable_layers = []
layers = [l for l in model.layers]
for index, layer in enumerate(layers):
layer_name = type(layer).__name__
is_in_candidates = layer_name in self.LR_mut_candidates
has_same_input_output_shape = layer.input.shape.as_list() == layer.output.shape.as_list()
should_be_removed = is_in_candidates and has_same_input_output_shape
if index == 0 or index == (len(model.layers) - 1):
continue
if should_be_removed:
index_of_suitable_layers.append(index)
return index_of_suitable_layers
def LAs_model_scan(self, model):
index_of_suitable_spots = []
layers = [l for l in model.layers]
for index, layer in enumerate(layers):
if index == (len(model.layers) - 1):
continue
index_of_suitable_spots.append(index)
return index_of_suitable_spots
def AFRs_model_scan(self, model):
index_of_suitable_spots = []
layers = [l for l in model.layers]
for index, layer in enumerate(layers):
if index == (len(model.layers) - 1):
continue
try:
if layer.activation is not None:
index_of_suitable_spots.append(index)
except:
pass
return index_of_suitable_spots
class SourceMutationOperators():
def __init__(self):
self.utils = utils.GeneralUtils()
self.check = utils.ExaminationalUtils()
self.model_utils = utils.ModelUtils()
self.SMO_utils = SourceMutationOperatorsUtils()
def DR_mut(self, train_dataset, model, mutation_ratio):
deep_copied_model = self.model_utils.model_copy(model, 'DR')
train_datas, train_labels = train_dataset
self.check.mutation_ratio_range_check(mutation_ratio)
self.check.training_dataset_consistent_length_check(train_datas, train_labels)
# shuffle the original train data
shuffled_train_datas, shuffled_train_labels = self.utils.shuffle_in_uni(train_datas, train_labels)
# select a portion of data and reproduce
number_of_train_data = len(train_datas)
number_of_duplicate = math.floor(number_of_train_data * mutation_ratio)
repeated_train_datas = shuffled_train_datas[:number_of_duplicate]
repeated_train_labels = shuffled_train_labels[:number_of_duplicate]
repeated_train_datas = np.append(train_datas, repeated_train_datas, axis=0)
repeated_train_labels = np.append(train_labels, repeated_train_labels, axis=0)
return (repeated_train_datas, repeated_train_labels), deep_copied_model
def LE_mut(self, train_dataset, model, label_lower_bound, label_upper_bound, mutation_ratio):
deep_copied_model = self.model_utils.model_copy(model, 'LE')
train_datas, train_labels = train_dataset
LE_train_datas, LE_train_labels = train_datas.copy(), train_labels.copy()
self.check.mutation_ratio_range_check(mutation_ratio)
self.check.training_dataset_consistent_length_check(LE_train_datas, LE_train_labels)
number_of_train_data = len(LE_train_datas)
number_of_error_labels = math.floor(number_of_train_data * mutation_ratio)
permutation = np.random.permutation(number_of_train_data)
permutation = permutation[:number_of_error_labels]
for old_index, new_index in enumerate(permutation):
while True:
val = random.randint(label_lower_bound, label_upper_bound)
num_of_classes = label_upper_bound - label_lower_bound + 1
val = keras.utils.np_utils.to_categorical(val, num_of_classes)
if np.array_equal(LE_train_labels[new_index], val):
continue
else:
LE_train_labels[new_index] = val
break
return (LE_train_datas, LE_train_labels), deep_copied_model
def DM_mut(self, train_dataset, model, mutation_ratio):
deep_copied_model = self.model_utils.model_copy(model, 'DM')
train_datas, train_labels = train_dataset
DM_train_datas, DM_train_labels = train_datas.copy(), train_labels.copy()
self.check.mutation_ratio_range_check(mutation_ratio)
self.check.training_dataset_consistent_length_check(DM_train_datas, DM_train_labels)
number_of_train_data = len(DM_train_datas)
number_of_error_labels = math.floor(number_of_train_data * mutation_ratio)
permutation = np.random.permutation(number_of_train_data)
permutation = permutation[:number_of_error_labels]
DM_train_datas = np.delete(DM_train_datas, permutation, 0)
DM_train_labels = np.delete(DM_train_labels, permutation, 0)
return (DM_train_datas, DM_train_labels), deep_copied_model
def DF_mut(self, train_dataset, model, mutation_ratio):
deep_copied_model = self.model_utils.model_copy(model, 'DF')
train_datas, train_labels = train_dataset
DF_train_datas, DF_train_labels = train_datas.copy(), train_labels.copy()
self.check.mutation_ratio_range_check(mutation_ratio)
self.check.training_dataset_consistent_length_check(DF_train_datas, DF_train_labels)
number_of_train_data = len(DF_train_datas)
number_of_shuffled_datas = math.floor(number_of_train_data * mutation_ratio)
permutation = np.random.permutation(number_of_train_data)
permutation = permutation[:number_of_shuffled_datas]
DF_train_datas, DF_train_labels = self.utils.shuffle_in_uni_with_permutation(DF_train_datas, DF_train_labels, permutation)
return (DF_train_datas, DF_train_labels), deep_copied_model
def NP_mut(self, train_dataset, model, mutation_ratio, STD=0.1):
deep_copied_model = self.model_utils.model_copy(model, 'NP')
train_datas, train_labels = train_dataset
NP_train_datas, NP_train_labels = train_datas.copy(), train_labels.copy()
self.check.mutation_ratio_range_check(mutation_ratio)
self.check.training_dataset_consistent_length_check(NP_train_datas, NP_train_labels)
number_of_train_data = len(NP_train_datas)
number_of_noise_perturbs = math.floor(number_of_train_data * mutation_ratio)
permutation = np.random.permutation(number_of_train_data)
permutation = permutation[:number_of_noise_perturbs]
random_train_datas = np.random.standard_normal(NP_train_datas.shape) * STD
for old_index, new_index in enumerate(permutation):
NP_train_datas[new_index] += random_train_datas[new_index]
return (NP_train_datas, NP_train_labels), deep_copied_model
def LR_mut(self, train_dataset, model, mutated_layer_indices=None):
# Copying and some assertions
deep_copied_model = self.model_utils.model_copy(model, 'LR')
train_datas, train_labels = train_dataset
copied_train_datas, copied_train_labels = train_datas.copy(), train_labels.copy()
self.check.training_dataset_consistent_length_check(copied_train_datas, copied_train_labels)
# Randomly select from suitable layers instead of the first one
index_of_suitable_layers = self.SMO_utils.LR_model_scan(model)
number_of_suitable_layers = len(index_of_suitable_layers)
if number_of_suitable_layers == 0:
print('None of layers be removed')
print('LR will only remove the layer with the same input and output')
print('However, there is no suitable layer for the input model')
return (copied_train_datas, copied_train_labels), deep_copied_model
new_model = keras.models.Sequential()
layers = [l for l in deep_copied_model.layers]
if mutated_layer_indices == None:
random_picked_layer_index = index_of_suitable_layers[random.randint(0, number_of_suitable_layers-1)]
print('Selected layer by LR mutation operator', random_picked_layer_index)
for index, layer in enumerate(layers):
if index == random_picked_layer_index:
continue
new_model.add(layer)
else:
self.check.in_suitable_indices_check(index_of_suitable_layers, mutated_layer_indices)
for index, layer in enumerate(layers):
if index in mutated_layer_indices:
continue
new_model.add(layer)
return (copied_train_datas, copied_train_labels), new_model
def LAs_mut(self, train_dataset, model, mutated_layer_indices=None):
# Copying and some assertions
deep_copied_model = self.model_utils.model_copy(model, 'LAs')
train_datas, train_labels = train_dataset
copied_train_datas, copied_train_labels = train_datas.copy(), train_labels.copy()
self.check.training_dataset_consistent_length_check(copied_train_datas, copied_train_labels)
# Randomly select from suitable spots instead of the first one
index_of_suitable_spots = self.SMO_utils.LAs_model_scan(model)
number_of_suitable_spots = len(index_of_suitable_spots)
if number_of_suitable_spots == 0:
print('No layers be added')
print('There is no suitable spot for the input model')
return (copied_train_datas, copied_train_labels), deep_copied_model
new_model = keras.models.Sequential()
layers = [l for l in deep_copied_model.layers]
if mutated_layer_indices == None:
random_picked_spot_index = index_of_suitable_spots[random.randint(0, number_of_suitable_spots-1)]
print('Selected layer by LAs mutation operator', random_picked_spot_index)
for index, layer in enumerate(layers):
if index == random_picked_spot_index:
new_model.add(layer)
new_model.add(self.SMO_utils.LA_get_random_layer())
continue
new_model.add(layer)
else:
self.check.in_suitable_indices_check(index_of_suitable_spots, mutated_layer_indices)
for index, layer in enumerate(layers):
if index in mutated_layer_indices:
new_model.add(layer)
new_model.add(self.SMO_utils.LA_get_random_layer())
continue
new_model.add(layer)
return (copied_train_datas, copied_train_labels), new_model
def AFRs_mut(self, train_dataset, model, mutated_layer_indices=None):
# Copying and some assertions
deep_copied_model = self.model_utils.model_copy(model, 'AFRs')
train_datas, train_labels = train_dataset
copied_train_datas, copied_train_labels = train_datas.copy(), train_labels.copy()
self.check.training_dataset_consistent_length_check(copied_train_datas, copied_train_labels)
# Randomly select from suitable layers instead of the first one
index_of_suitable_layers = self.SMO_utils.AFRs_model_scan(model)
number_of_suitable_layers = len(index_of_suitable_layers)
if number_of_suitable_layers == 0:
print('None activation of layers be removed')
print('There is no suitable layer for the input model')
return (copied_train_datas, copied_train_labels), deep_copied_model
new_model = keras.models.Sequential()
layers = [l for l in deep_copied_model.layers]
if mutated_layer_indices == None:
random_picked_layer_index = index_of_suitable_layers[random.randint(0, number_of_suitable_layers-1)]
print('Seleced layer by AFRs mutation operator', random_picked_layer_index)
for index, layer in enumerate(layers):
if index == random_picked_layer_index:
layer.activation = lambda x: x
new_model.add(layer)
continue
new_model.add(layer)
else:
self.check.in_suitable_indices_check(index_of_suitable_layers, mutated_layer_indices)
for index, layer in enumerate(layers):
if index in mutated_layer_indices:
layer.activation = lambda x: x
new_model.add(layer)
continue
new_model.add(layer)
return (copied_train_datas, copied_train_labels), new_model