Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

103 changes: 82 additions & 21 deletions Grad_CAM_Prior.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import time
import shutil
import tensorflow.keras
from tensorflow.keras.applications import inception_v3 as inc_net
from tensorflow.keras.preprocessing import image
Expand All @@ -13,6 +15,13 @@
from lime import calculate_posteriors
print('Notebook run using keras:', tensorflow.keras.__version__)

def mkdir(path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
else:
shutil.rmtree(path)
os.mkdir(path)

############################################################
# use heatmap from Grad-CAM as prior knowledge for BayLime #
Expand All @@ -31,49 +40,101 @@ def transform_img_fn(path_list):
out.append(x)
return np.vstack(out)

mkdir('evaluation_output')
fname = 'evaluation_output'

images = transform_img_fn([os.path.join('data','5.jpg')])
images = transform_img_fn([os.path.join('data','penguin.jpeg')])

# I'm dividing by 2 and adding 0.5 because of
# how this Inception represents images

plt.imshow(images[0] / 2 + 0.5)
plt.show()
deletion = evaluation.CausalMetric(inet_model,'del')
insertion = evaluation.CausalMetric(inet_model,'ins')


preds = inet_model.predict(images)
pred_label = decode_predictions(preds)[0]
# for x in decode_predictions(preds)[0]:
# print(x)


time1 = time.time()

explainer = lime_image.LimeImageExplainer(feature_selection='none')#kernel_width=0.1

explanation = explainer.explain_instance(images[0], inet_model.predict,
top_labels=1, hide_color=0, batch_size=15,
num_samples=100,model_regressor='Bay_info_prior')
num_samples=200, model_regressor='BayesianRidge_inf_prior_fit_alpha')

#'non_Bay' 'Bay_non_info_prior' 'Bay_info_prior','BayesianRidge_inf_prior_fit_alpha'

time2 = time.time()

temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], num_features=10, hide_rest=False)
fig, ax = plt.subplots(figsize=(6,6))
ax.imshow(mark_boundaries(temp / 2 + 0.5, mask))
ax.set_ylabel(pred_label[0][1],fontsize=20)
fig.savefig(fname+'/Lime_exp.png',bbox_inches='tight')


h1_del = deletion.single_run(images[0], explanation.local_exp[explanation.top_labels[0]], explanation.segments, explanation.top_labels[0], pred_label, 'Lime',fname)
h1_ins = insertion.single_run(images[0], explanation.local_exp[explanation.top_labels[0]], explanation.segments, explanation.top_labels[0], pred_label, 'Lime',fname)


time3 = time.time()

# extract the prior knowledge from grad-cam
prior_knowledge = Grad_CAM.extrat_prior(images,inet_model,explanation)
prior_knowledge = Grad_CAM.extrat_prior(images[0],inet_model,explanation,fname,pred_label[0][1])
prior_exp = np.flip(np.argsort(abs(np.array(prior_knowledge))))
seg = explanation.segments

time4 = time.time()

h2_del = deletion.single_run(images[0], prior_exp, seg, explanation.top_labels[0], pred_label, 'Grad_CAM',fname)
h2_ins = insertion.single_run(images[0], prior_exp, seg, explanation.top_labels[0], pred_label, 'Grad_CAM',fname)

# update the explanation with prior
alpha_var=1
lambda_var=2048

explanation=calculate_posteriors.get_posterior(explanation,prior_knowledge,
hyper_para_alpha=alpha_var,
hyper_para_lambda=lambda_var,
time5 = time.time()

# update the explanation with prior
alpha_init=1
lambda_init=1
with open('./posterior_configure.csv') as csv_file:
csv_reader=csv.reader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 1:
alpha_init=float(row[0])
lambda_init=float(row[1])
line_count=line_count+1


explanation = calculate_posteriors.get_posterior(explanation,prior_knowledge,
hyper_para_alpha=alpha_init,
hyper_para_lambda=lambda_init,
label=explanation.top_labels[0])

deletion = evaluation.CausalMetric(inet_model,'del')
insertion = evaluation.CausalMetric(inet_model,'ins')
h = deletion.single_run(images[0], explanation, pred_label)
h = insertion.single_run(images[0], explanation, pred_label)
time6 = time.time()

temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], num_features=10, hide_rest=False)
fig, ax = plt.subplots(figsize=(6,6))
ax.imshow(mark_boundaries(temp / 2 + 0.5, mask))
ax.set_ylabel(pred_label[0][1],fontsize=20)
fig.savefig(fname+'/BayLime_exp.png',bbox_inches='tight')

h3_del = deletion.single_run(images[0], explanation.local_exp[explanation.top_labels[0]], explanation.segments, explanation.top_labels[0], pred_label, 'BayLime',fname)
h3_ins = insertion.single_run(images[0], explanation.local_exp[explanation.top_labels[0]], explanation.segments, explanation.top_labels[0], pred_label, 'BayLime',fname)


temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=5, hide_rest=False)


plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
plt.show()
print('-----------------------------')
print('Lime deletion: ', h1_del)
print('Lime insertion: ', h1_ins)
print('Grad-CAM deletion: ', h2_del)
print('Grad-CAM insertion: ', h2_ins)
print('Baylime deletion: ', h3_del)
print('Baylime insertion: ', h3_ins)
print('-----------------------------')
print("grad_CAM:", time4 - time3, "s")
print("Lime :", time2 - time1, "s")
print("BayLime :", time6 - time5 + time2 - time1 + time4 - time3, "s")

print(explanation.as_list(explanation.top_labels[0]))
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ BayLIME is a Bayesian modification of LIME that provides a principled mechanism
from sklearn import linear_model
print(linear_model.__file__)
```
3. Download the necessary dataset for ImageNet and GTSRB model, unzip the files and move to the data folder.
```
ImageNet (original images): http://image-net.org/download-images
GTSRB (.h5 file): https://drive.google.com/file/d/1MjgsnH3bOYG_PvdvqmoamoCPmySQazRJ/view?usp=sharing
```
(Tested with Python version 3.7.3, scikit-learn version 0.22.1, Tensorflow version 2.0.0)

## Repository Structure

* **experiments** contains the experiments for the draft paper, in which you may find both the code (in Python jupyter-notebook) and the original data generated (stored as HTML and .csv files).
Expand All @@ -30,3 +34,26 @@ Now when calling the explainer.explain_instance() API of BayLime, we have four o
4. model_regressor='BayesianRidge_inf_prior_fit_alpha' uses the modified BayesianRidge regressor and reads the hyperparameters lambda from configuration files and fit alpha from sampling data.

Please refer to the tutorials (e.g., BayLIME_tutorial_images.ipynb) for details.

## Embed Prior from GradCAM
To get the explanation for specified image (e.g. king penguin) in data folder, firstly modify the image path in Line 46 of Grad_CAM_Prior.py, then type
```
python Grad_CAM_Prior.py
```
You will get the explanation results along with Deletion and Insertion AUC figures from GradCAM, LIME and BayLIME under the created *evaluation_output* folder.
To get statistical fidelity evaluation on ImageNet dataset, firstly make sure the validation dataset from ImageNet called ILSVRC2012_img_val is already downloaded and moved to the data folder, then type
```
python del_ins_exp.py
```
You will the explanation result for each image from ImageNet and a record file for recording the runtime output in the created *evaluation_output* folder. Be cautious the evaluation_output folder will be reset every time running the program, so take a copy if you want to save the running results.

## Embed Prior from Neural Cleanse
In backdoor_exp.py, we provide the explanation for backdoor input from BadNet and TrojanAttack models. To get the IoU and AMD evaluation for Prior, LIME and BayLIME, type the command
```
python backdoor_exp.py
```
You will get the print out of IoU and AMD scores for each backdoor attacked images. The interpretation of IoU and AMD scores can be referred to the paper.




Binary file added backdoor/.DS_Store
Binary file not shown.
Binary file added backdoor/GTSRB.h5
Binary file not shown.
Loading