-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodel_mut_operators.py
401 lines (330 loc) · 18.1 KB
/
model_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
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
import tensorflow as tf
import numpy as np
import keras
import math, random
import utils
class ModelMutationOperatorsUtils():
def __init__(self):
self.utils = utils.GeneralUtils()
self.LD_mut_candidates = ['Dense']
self.LAm_mut_candidates = ['Dense']
def GF_on_list(self, lst, mutation_ratio, prob_distribution, STD, lower_bound, upper_bound, lam):
copy_lst = lst.copy()
number_of_data = len(copy_lst)
permutation = self.utils.generate_permutation(number_of_data, mutation_ratio)
if prob_distribution == 'normal':
copy_lst[permutation] += np.random.normal(scale=STD, size=len(permutation))
elif prob_distribution == 'uniform':
copy_lst[permutation] += np.random.uniform(low=lower_bound, high=upper_bound, size=len(permutation))
elif prob_distribution == 'exponential':
assert lam is not 0
scale = 1 / lam
copy_lst[permutation] += np.random.exponential(scale=sclae, size=len(permutation))
else:
pass
return copy_lst
def WS_on_Dense_list(self, lst, output_index):
copy_lst = lst.copy()
grabbed_lst = copy_lst[:, output_index]
shuffle_grabbed_lst = self.utils.shuffle(grabbed_lst)
copy_lst[:, output_index] = shuffle_grabbed_lst
return copy_lst
def WS_on_Conv2D_list(self, lst, output_channel_index):
copy_lst = lst.copy()
filter_width, filter_height, num_of_input_channels, num_of_output_channels = copy_lst.shape
copy_lst = np.reshape(copy_lst, (filter_width * filter_height * num_of_input_channels, num_of_output_channels))
grabbled_lst = copy_lst[:, output_channel_index]
shuffle_grabbed_lst = self.utils.shuffle(grabbled_lst)
copy_lst[:, output_channel_index] = shuffle_grabbed_lst
copy_lst = np.reshape(copy_lst, (filter_width, filter_height, num_of_input_channels, num_of_output_channels))
return copy_lst
def NS_on_Dense_list(self, lst, mutation_ratio):
first_copy_lst = lst.copy()
second_copy_lst = lst.copy()
input_dim, output_dim = first_copy_lst.shape
permutation = self.utils.generate_permutation(input_dim, mutation_ratio)
shuffled_permutation = self.utils.shuffle(permutation)
for index in range(len(permutation)):
second_copy_lst[shuffled_permutation[index], :] = first_copy_lst[permutation[index], :]
return second_copy_lst
def NS_on_Conv2D_list(self, lst, mutation_ratio):
first_copy_lst = lst.copy()
second_copy_lst = lst.copy()
filter_width, filter_height, num_of_input_channels, num_of_output_channels = first_copy_lst.shape
permutation = self.utils.generate_permutation(num_of_input_channels, mutation_ratio)
shuffled_permutation = self.utils.shuffle(permutation)
for index in range(len(permutation)):
second_copy_lst[:, :, shuffled_permutation[index], :] = first_copy_lst[:, :, permutation[index], :]
return second_copy_lst
def LD_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.LD_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 LAm_model_scan(self, model):
index_of_suitable_spots = []
layers = [l for l in model.layers]
for index, layer in enumerate(layers):
layer_type_name = type(layer).__name__
is_in_candidates = layer_type_name in self.LAm_mut_candidates
has_same_input_output_shape = layer.input.shape.as_list() == layer.output.shape.as_list()
should_be_added = is_in_candidates and has_same_input_output_shape
if should_be_added:
index_of_suitable_spots.append(index)
return index_of_suitable_spots
def AFRm_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 ModelMutationOperators():
def __init__(self):
self.utils = utils.GeneralUtils()
self.model_utils = utils.ModelUtils()
self.check = utils.ExaminationalUtils()
self.MMO_utils = ModelMutationOperatorsUtils()
def GF_mut(self, model, mutation_ratio, prob_distribution='normal', STD=0.1, lower_bound=None, upper_bound=None, lam=None, mutated_layer_indices=None):
self.check.mutation_ratio_range_check(mutation_ratio)
valid_prob_distribution_types = ['normal', 'uniform']
assert prob_distribution in valid_prob_distribution_types, 'The probability distribution type ' + prob_distribution + ' is not implemented in GF mutation operator'
if prob_distribution == 'uniform' and ((lower_bound is None) or (upper_bound is None)):
raise ValueError('In uniform distribution, users are required to specify the lower bound and upper bound of noises')
if prob_distribution == 'exponential' and (lam is None):
raise ValueError('In exponential distribution, users are required to specify the lambda value')
GF_model = self.model_utils.model_copy(model, 'GF')
layers = [l for l in GF_model.layers]
num_of_layers = len(layers)
self.check.valid_indices_of_mutated_layers_check(num_of_layers, mutated_layer_indices)
layers_should_be_mutated = self.model_utils.get_booleans_of_layers_should_be_mutated(num_of_layers, mutated_layer_indices)
for index, layer in enumerate(layers):
weights = layer.get_weights()
new_weights = []
if not (len(weights) == 0) and layers_should_be_mutated[index]:
for val in weights:
val_shape = val.shape
flat_val = val.flatten()
GF_flat_val = self.MMO_utils.GF_on_list(flat_val, mutation_ratio, prob_distribution, STD, lower_bound, upper_bound, lam)
GF_val = GF_flat_val.reshape(val_shape)
new_weights.append(GF_val)
layer.set_weights(new_weights)
return GF_model
def WS_mut(self, model, mutation_ratio, mutated_layer_indices=None):
self.check.mutation_ratio_range_check(mutation_ratio)
WS_model = self.model_utils.model_copy(model, 'WS')
layers = [l for l in WS_model.layers]
num_of_layers = len(layers)
self.check.valid_indices_of_mutated_layers_check(num_of_layers, mutated_layer_indices)
layers_should_be_mutated = self.model_utils.get_booleans_of_layers_should_be_mutated(num_of_layers, mutated_layer_indices)
for index, layer in enumerate(layers):
weights = layer.get_weights()
layer_name = type(layer).__name__
new_weights = []
if not (len(weights) == 0):
for val in weights:
val_shape = val.shape
if (len(val.shape) is not 1) and layers_should_be_mutated[index]:
if layer_name == 'Conv2D':
filter_width, filter_height, num_of_input_channels, num_of_output_channels = val_shape
permutation = self.utils.generate_permutation(num_of_output_channels, mutation_ratio)
for output_channel_index in permutation:
val = self.MMO_utils.WS_on_Conv2D_list(val, output_channel_index)
elif layer_name == 'Dense':
input_dim, output_dim = val_shape
permutation = self.utils.generate_permutation(output_dim, mutation_ratio)
for output_dim_index in permutation:
val = self.MMO_utils.WS_on_Dense_list(val, output_dim_index)
else:
pass
new_weights.append(val)
layer.set_weights(new_weights)
return WS_model
def NEB_mut(self, model, mutation_ratio, mutated_layer_indices=None):
self.check.mutation_ratio_range_check(mutation_ratio)
NEB_model = self.model_utils.model_copy(model, 'NEB')
layers = [l for l in NEB_model.layers]
num_of_layers = len(layers)
self.check.valid_indices_of_mutated_layers_check(num_of_layers, mutated_layer_indices)
layers_should_be_mutated = self.model_utils.get_booleans_of_layers_should_be_mutated(num_of_layers, mutated_layer_indices)
for index, layer in enumerate(layers):
weights = layer.get_weights()
layer_name = type(layer).__name__
new_weights = []
if not (len(weights) == 0):
for val in weights:
val_shape = val.shape
if (len(val.shape) is not 1) and layers_should_be_mutated[index]:
if layer_name == 'Conv2D':
filter_width, filter_height, num_of_input_channels, num_of_output_channels = val_shape
permutation = self.utils.generate_permutation(num_of_input_channels, mutation_ratio)
for input_channel_index in permutation:
val[:, :, input_channel_index, :] = 0
elif layer_name == 'Dense':
input_dim, output_dim = val_shape
permutation = self.utils.generate_permutation(input_dim, mutation_ratio)
for input_index in permutation:
val[input_index] = 0
else:
pass
new_weights.append(val)
layer.set_weights(new_weights)
return NEB_model
def NAI_mut(self, model, mutation_ratio, mutated_layer_indices=None):
self.check.mutation_ratio_range_check(mutation_ratio)
NAI_model = self.model_utils.model_copy(model, 'NAI')
layers = [l for l in NAI_model.layers]
num_of_layers = len(layers)
self.check.valid_indices_of_mutated_layers_check(num_of_layers, mutated_layer_indices)
layers_should_be_mutated = self.model_utils.get_booleans_of_layers_should_be_mutated(num_of_layers, mutated_layer_indices)
for index, layer in enumerate(layers):
weights = layer.get_weights()
layer_name = type(layer).__name__
new_weights = []
if not (len(weights) == 0):
for val in weights:
val_shape = val.shape
if (len(val.shape) is not 1) and layers_should_be_mutated[index]:
if layer_name == 'Conv2D':
filter_width, filter_height, num_of_input_channels, num_of_output_channels = val_shape
permutation = self.utils.generate_permutation(num_of_output_channels, mutation_ratio)
for output_channel_index in permutation:
val[:, :, :, output_channel_index] *= -1
elif layer_name == 'Dense':
input_dim, output_dim = val_shape
permutation = self.utils.generate_permutation(output_dim, mutation_ratio)
for output_dim_index in permutation:
val[:, output_dim_index] *= -1
else:
pass
new_weights.append(val)
layer.set_weights(new_weights)
return NAI_model
def NS_mut(self, model, mutation_ratio, mutated_layer_indices=None):
self.check.mutation_ratio_range_check(mutation_ratio)
NS_model = self.model_utils.model_copy(model, 'NS')
layers = [l for l in NS_model.layers]
num_of_layers = len(layers)
self.check.valid_indices_of_mutated_layers_check(num_of_layers, mutated_layer_indices)
layers_should_be_mutated = self.model_utils.get_booleans_of_layers_should_be_mutated(num_of_layers, mutated_layer_indices)
for index, layer in enumerate(layers):
weights = layer.get_weights()
layer_name = type(layer).__name__
new_weights = []
if not (len(weights) == 0):
for val in weights:
val_shape = val.shape
if (len(val.shape) is not 1) and layers_should_be_mutated[index]:
if layer_name == 'Conv2D':
val = self.MMO_utils.NS_on_Conv2D_list(val, mutation_ratio)
elif layer_name == 'Dense':
val = self.MMO_utils.NS_on_Dense_list(val, mutation_ratio)
else:
pass
new_weights.append(val)
layer.set_weights(new_weights)
return NS_model
def LD_mut(self, model, mutated_layer_indices=None):
LD_model = self.model_utils.model_copy(model, 'LD')
# Randomly select from suitable layers instead of the first one
index_of_suitable_layers = self.MMO_utils.LD_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('LD will only remove the layer with the same input and output')
print('')
return LD_model
layers = [l for l in LD_model.layers]
new_model = keras.models.Sequential()
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 LD 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 new_model
def LAm_mut(self, model, mutated_layer_indices=None):
LAm_model = self.model_utils.model_copy(model, 'LAm')
copied_LAm_model = self.model_utils.model_copy(model, 'insert')
# Randomly select from suitable spots instead of the first one
index_of_suitable_spots = self.MMO_utils.LAm_model_scan(model)
number_of_suitable_spots = len(index_of_suitable_spots)
if number_of_suitable_spots == 0:
print('No layers be added')
print('LAm will only add the layer with the same input and output')
print('There is no suitable spot for the input model')
print('')
return LAm_model
new_model = keras.models.Sequential()
layers = [l for l in LAm_model.layers]
copy_layers = [l for l in copied_LAm_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 LRm mutation operator', random_picked_spot_index)
for index, layer in enumerate(layers):
if index == random_picked_spot_index:
new_model.add(layer)
copy_layer = copy_layers[index]
new_model.add(copy_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)
copy_layer = copy_layers[index]
new_model.add(copy_layer)
continue
new_model.add(layer)
return new_model
def AFRm_mut(self, model, mutated_layer_indices=None):
AFRm_model = self.model_utils.model_copy(model, 'AFRm')
# Randomly select from suitable layers instead of the first one
index_of_suitable_layers = self.MMO_utils.AFRm_model_scan(model)
number_of_suitable_layers = len(index_of_suitable_layers)
if number_of_suitable_layers == 0:
print('No activation be removed')
print('Except the output layer, there is no activation function can be removed')
print('')
return AFRm_model
new_model = keras.models.Sequential()
layers = [l for l in AFRm_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 AFRm 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 new_model