-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowerbound.py
89 lines (77 loc) · 3.58 KB
/
lowerbound.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Construct a 'lowerbound' function to compute
the lower bound of Player I’s minimum adversary distance
while Player II being cooperative, or Player I's maximum
adversary distance whilst Player II being competitive.
Author: Min Wu
Email: min.wu@cs.ox.ac.uk
"""
from CooperativeAStar import *
from CompetitiveAlphaBeta import *
from NeuralNetwork import *
from DataSet import *
import numpy
def lowerbound(dataset_name, image_index, game_type, eta, tau, wbits, abits, target_class,nameFile):
NN = NeuralNetwork(dataset_name, abits, wbits, 'full-qnn')
NN.train_network_QNN()
print("Dataset is %s." % NN.data_set)
#NN.model.summary()
dataset = DataSet(dataset_name, 'testing')
image = dataset.get_input(image_index)
realLabel=dataset.get_True_Label(image_index)
realIndex=numpy.nonzero(realLabel)
(label, confidence) = NN.predict(image)
label_str = NN.get_label(int(label))
print("Working on input with index %s, whose class predicted is '%s' (real label %s) and the confidence is %s."
% (image_index, label_str, NN.get_label(int(realLabel)), confidence))
print("The second player is being %s." % game_type)
path = "%s_pic/idx_%s_label_[%s]_with_confidence_%s.png" % (
dataset_name, image_index, label_str, confidence)
NN.save_input(image, path)
if not label_str== NN.get_label(int(realLabel)):
print ("NN misclassification without attack")
exit(0)
if game_type == 'cooperative':
tic = time.time()
cooperative = CooperativeAStar(image_index, image, NN, eta, tau, target_class)
cooperative.play_game(image,dataset_name)
if cooperative.ADVERSARY_FOUND is True:
elapsed = time.time() - tic
adversary = cooperative.ADVERSARY
adv_label, adv_confidence = NN.predict(adversary)
adv_label_str = NN.get_label(int(adv_label))
print("\nFound an adversary within pre-specified bounded computational resource. "
"\nThe following is its information: ")
print("difference between images: %s" % (diffImage(image, adversary)))
l2dist = l2Distance(image, adversary)
l1dist = l1Distance(image, adversary)
l0dist = l0Distance(image, adversary)
percent = diffPercent(image, adversary)
print("L2 distance %s" % l2dist)
print("L1 distance %s" % l1dist)
print("L0 distance %s" % l0dist)
print("manipulated percentage distance %s" % percent)
print("class is changed into '%s' with confidence %s\n" % (adv_label_str, adv_confidence))
path = "%s_pic/idx_%s_modified_into_[%s]_with_confidence_%s.png" % (
dataset_name, image_index, adv_label_str, adv_confidence)
NN.save_input(adversary, path)
if eta[0] == 'L0':
dist = l0dist
elif eta[0] == 'L1':
dist = l1dist
elif eta[0] == 'L2':
dist = l2dist
else:
print("Unrecognised distance metric.")
path = "%s_pic/idx_%s_modified_diff_%s=%s_time=%s.png" % (
dataset_name, image_index, eta[0], dist, elapsed)
NN.save_input(np.absolute(image - adversary), path)
else:
print("Adversarial distance exceeds distance bound.")
elif game_type == 'competitive':
competitive = CompetitiveAlphaBeta(image, NN, eta, tau)
competitive.play_game(image)
else:
print("Unrecognised game type. Try 'cooperative' or 'competitive'.")