-
Notifications
You must be signed in to change notification settings - Fork 0
/
monte_carlo_metric.py
359 lines (257 loc) · 13.2 KB
/
monte_carlo_metric.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
import sys
import os
import random
import math
import pickle
import numpy as np
import tensorflow as tf
from ROAR_pipeline import LoadPixelListFromPath
from retest_pipeline import SaveImage
#seed setting
np.random.seed(42)
tf.set_random_seed(1234)
random.seed(1234)
# INITIALISE FRAMEWORK
###UPDATE FRAMEWORK PATH
framework_path = "/media/harborned/ShutUpN/repos/dais/interpretability_framework"
# framework_path = "/Users/harborned/Documents/repos/dais/interpretability_framework"
sys.path.append(framework_path)
from DaisFrameworkTool import DaisFrameworkTool
explanation_pixel_lists_dict = {
"pixel_lists":{
"LIME":{
"test":{
"CIFAR-10":"trainable_deletion_CIFAR-10_LIME_1557966687.pkl"
# "CIFAR-10":"TEST_CIFAR-10_LIME_1553397515_SMALL_200.pkl"
},
"train":{
"CIFAR-10":"TRAIN_testROAR_CIFAR-10_LIME_1553461654.pkl"
}
},
"Shap":{
"test":{
"CIFAR-10":"trainable_deletion_CIFAR-10_Shap_1557965400.pkl"
},
"train":{
"CIFAR-10":"TRAIN_testROAR_CIFAR-10_Shap_1553686507.pkl"
}
},
"random":{
"test":{
"CIFAR-10":"trainable_deletion_CIFAR-10_random_1557967006.pkl"
},
"train":{
"CIFAR-10":"TRAIN_testROAR_CIFAR-10_random_1553734921.pkl"
}
}
}
}
def GetClassImageIndexs(class_names,y_vals):
class_images = {}
for class_name_i, class_name in enumerate(class_names):
class_images[class_name] = [i for i in range(len(test_y)) if np.argmax(test_y[i]) == class_name_i]
return class_images
def GetCorrectPredictionIndexs(predictions,truth_indexs):
return [i for i in range(len(predictions)) if predictions[i] == np.argmax(truth_indexs[i]) ]
def NormalisePixelList(image_pixel_list):
p_min = min([p[2] for p in image_pixel_list])
positive_list = []
total = 0
for p in image_pixel_list:
positive_list.append([p[0],p[1],p[2]+abs(p_min)])
total += (p[2]+abs(p_min))
distribution_list = []
for p in positive_list:
distribution_list.append([p[0],p[1],p[2]/float(total)])
return distribution_list
def find_interval(x, partition):
""" find_interval -> i
partition is a sequence of numerical values
x is a numerical value
The return value "i" will be the index for which applies
partition[i] < x < partition[i+1], if such an index exists.
-1 otherwise
"""
for i in range(0, len(partition)):
if x < partition[i]:
return i-1
return -1
def weighted_choice(sequence,weights):
"""
weighted_choice selects a random element of
the sequence according to the list of weights
"""
x = np.random.random()
cum_weights = [0] + list(np.cumsum(weights))
index = find_interval(x, cum_weights)
return sequence[index]
def weighted_sample(population, weights, k):
"""
This function draws a random sample of length k
from the sequence 'population' according to the
list of weights
"""
sample = set()
population = list(population)
weights = list(weights)
while (len(sample) < k and len(population)>0):
choice = weighted_choice(population, weights)
sample.add(tuple(choice))
index = population.index(choice)
weights.pop(index)
population.remove(choice)
new_sum = sum(weights)
weights = [ x / new_sum for x in weights]
return list(sample), population, weights
def SampleFromPixelList(normalised_pixel_list,num_pixels_to_select,weights=None,importance_power=1):
if weights is None:
weights = [p[2]**importance_power for p in normalised_pixel_list]
weights_total = sum([p for p in weights])
weights = [p/weights_total for p in weights]
# sample, new_population, new_weights = weighted_sample(normalised_pixel_list, weights, num_pixels_to_select)
pixel_list_indexs = list(range(len(weights)))
if(len(pixel_list_indexs) != len(weights)):
print("size missmatch")
if(len(pixel_list_indexs) != 0):
sample_indexs = np.random.choice(pixel_list_indexs,size=min(len(weights),num_pixels_to_select),replace=False, p=weights)
else:
sample_indexs = []
sample = []
new_population = []
new_weights = []
sample_indexs_set = set(sample_indexs)
for i in range(len(weights)):
if(i in sample_indexs_set):
sample.append(normalised_pixel_list[i])
else:
if(weights[i] > 0):
new_population.append(normalised_pixel_list[i])
new_weights.append(weights[i])
return sample, new_population, new_weights
def DeteriorateImage(working_image,sampled_pixels,dataset_mean):
for px in sampled_pixels:
working_image[px[0]][px[1]] = [dataset_mean[0],dataset_mean[1],dataset_mean[2]]
return working_image
def DeteriorateImageWithRandomColour(working_image,sampled_pixels,dataset_mean):
for px in sampled_pixels:
working_image[px[0]][px[1]] = [random.random(),random.random(),random.random()]
return working_image
if __name__ == "__main__":
dataset_name = "CIFAR-10"
model_name = "vgg16" #M
test_or_train_data = "test"
normalise_data = True
results_dir = "monte_carlo_results"
deterioration_rate = 0.05
num_deterioration_steps = 20
num_trials = 100
exponent = 3
correct_predictions_only = True
experiment_id = "random_colour_expo_"+str(exponent)+"_100_monte_carlo_metric_"+dataset_name+"_"+model_name+"_"+test_or_train_data
if(correct_predictions_only):
experiment_id += "_correct_only"
explanation_names = ["LIME","Shap","random"] #E
load_from_pixel_list_path_dict={}
for explanation_name in explanation_names:
load_from_pixel_list_path_dict[explanation_name] = os.path.join("pixel_lists",explanation_pixel_lists_dict["pixel_lists"][explanation_name][test_or_train_data][dataset_name])
model_train_params ={
"learning_rate": 0.001
,"batch_size":128
,"num_train_steps":150
,"experiment_id":experiment_id
,"dropout":0.5
}
# deteriorate_function = DeteriorateImage
deteriorate_function = DeteriorateImageWithRandomColour
save_deteriorated_images_below_image_index = 3
save_deteriorated_images_below_trial_index = 2
framework_tool = DaisFrameworkTool(explicit_framework_base_path=framework_path)
##DATASET
dataset_json, dataset_tool = framework_tool.LoadFrameworkDataset(dataset_name, load_split_if_available = True)
label_names = [label["label"] for label in dataset_json["labels"]] # gets all labels in dataset. To use a subset of labels, build a list manually
#load train data
source = "test"
test_x, test_y = dataset_tool.GetBatch(batch_size = -1,even_examples=True, y_labels_to_use=label_names, split_batch = True,split_one_hot = True, batch_source = source, shuffle=False)
print("num test examples: "+str(len(test_x)))
print("calculating dataset mean")
dataset_mean = dataset_tool.GetMean()
print(dataset_mean)
if(normalise_data):
denormalise_function = dataset_tool.CreateDestandardizeFuntion()
dataset_class_names = dataset_tool.one_hot_list
class_images_indexs = GetClassImageIndexs(dataset_class_names,test_y)
#INSTANTIATE MODEL
model_save_path_suffix = ""
model_instance = framework_tool.InstantiateModelFromName(model_name,model_save_path_suffix,dataset_json,additional_args =model_train_params)
#LOAD MODEL
model_load_path = model_instance.model_dir
model_instance.LoadModel(model_load_path) #M (loaded trained model)
working_x = np.copy(test_x)
x_prediction_probs, x_predictions = model_instance.Predict(dataset_tool.StandardizeImages(working_x), return_prediction_scores = True)
correct_prediction_indexs = set(GetCorrectPredictionIndexs(x_predictions,test_y))
#RESIZE IMAGES IF NEEDED
#Images may need resizing for model. If that's the case, the framework would have done this automatically during training and explanation generations.
#we need to do this manually before deteriation step, the framework model class can do this for us.
working_x = model_instance.CheckInputArrayAndResize(working_x,model_instance.min_height,model_instance.min_width)
heading_output_string = ",".join(["explanation_name","class_name","img_i","trial_i","original_prediction_strength"]+["{:.2f}".format(i) for i in list(np.arange(0,deterioration_rate*(num_deterioration_steps+1),deterioration_rate))])+"\n"
for explanation_name in explanation_names[:]:
print("Starting Explanation: "+explanation_name)
#Load Importance Pickle
print("Loading Pixel List")
pixel_list = LoadPixelListFromPath(load_from_pixel_list_path_dict[explanation_name])
# explanation_deterioration_results = {}
for class_name_i,class_name in enumerate(dataset_class_names):
print("Beginning Class: "+class_name)
#TODO: remove after testing
if(class_name!= "horse"):
continue
results_output_file_name = experiment_id + "_" +explanation_name +"_"+ class_name +".csv"
results_output_file_path = os.path.join(results_dir,results_output_file_name)
with open(results_output_file_path,"w") as f:
f.write(heading_output_string)
# explanation_deterioration_results[class_name] = []
#Get Class Image Indexs
class_image_indexs = class_images_indexs[class_name]
#Filter to correct predictions only
if(correct_predictions_only):
class_image_indexs = list( correct_prediction_indexs.intersection(set(class_image_indexs)) )
#TODO:remove after testing
image_index_limit = 50
for img_i in class_image_indexs:
if(img_i > image_index_limit):
break
print("Beginning Img Index: "+str(img_i))
# image_results = {"img_i":img_i,"results":[]}
image_pixel_list = pixel_list[img_i]
original_prediction_index = x_predictions[img_i]
original_prediction_strength = x_prediction_probs[img_i][original_prediction_index]
#Normalise Importance Array
normalised_pixel_list = NormalisePixelList(image_pixel_list)
pixel_list_length = len(normalised_pixel_list)
for trial_i in range(num_trials):
trial_vector = [original_prediction_strength,original_prediction_strength/float(original_prediction_strength)]
working_image = np.copy(working_x[img_i])
remaining_pixels = normalised_pixel_list
remaining_weights = None
num_pixels_per_step = int(math.ceil(pixel_list_length * deterioration_rate))
for current_deterioration_step_i in range(num_deterioration_steps):
current_deterioration_proportion = (current_deterioration_step_i+1)*deterioration_rate
# print("Beginning Deterioration Proportion: "+str(current_deterioration_proportion))
sampled_pixels, remaining_pixels, remaining_weights = SampleFromPixelList(remaining_pixels,num_pixels_per_step,weights=remaining_weights,importance_power=exponent)
# print("Detiorating Image")
working_image = deteriorate_function(working_image,sampled_pixels,dataset_mean)
if(img_i < save_deteriorated_images_below_image_index and trial_i < save_deteriorated_images_below_trial_index):
save_output_path = os.path.join("sample_deteriorated_images", experiment_id + "_" +explanation_name+"_"+ str(img_i) + "_" + str(current_deterioration_proportion) + "_" + str(trial_i) +".jpg")
SaveImage(working_image,save_output_path)
det_x_prediction_probs, det_x_predictions = model_instance.Predict(dataset_tool.StandardizeImages(np.array([working_image])), return_prediction_scores = True)
#Store deterioration
trial_vector.append(det_x_prediction_probs[0][original_prediction_index]/float(original_prediction_strength))
output_values = [explanation_name, class_name, str(img_i), str(trial_i)] + [str(v) for v in trial_vector]
oputput_string = ",".join(output_values) +"\n"
with open(results_output_file_path,"a") as f:
f.write(oputput_string)
# #output image vectors
# print("Outputing Vector Dict")
# output_vector_dict_path = os.path.join("carlo_vector_dicts",experiment_id+"_"+explanation_name+".pkl")
# with open(output_vector_dict_path,"wb") as f:
# pickle.dump(explanation_deterioration_results,f)