forked from xiaoweih/DLV
/
DLV.py
executable file
·448 lines (364 loc) · 20.1 KB
/
DLV.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python
"""
main file
author: Xiaowei Huang
"""
import sys
sys.path.append('networks')
sys.path.append('safety_check')
sys.path.append('configuration')
sys.path.append('basics')
sys.path.append('MCTS')
import time
import numpy as np
import copy
import random
import matplotlib.pyplot as plt
import matplotlib as mpl
from loadData import loadData
from regionSynth import regionSynth, initialiseRegion
from precisionSynth import precisionSynth
from safety_analysis import safety_analysis
from configuration import *
from basics import *
from networkBasics import *
from searchTree import searchTree
from searchMCTS import searchMCTS
from mcts_twoPlayer import mcts_twoPlayer
from initialiseSiftKeypoints import GMM
from dataCollection import dataCollection
from inputManipulation import applyManipulation,assignManipulationSimple
from keras import backend as K
def main():
model = loadData()
dc = dataCollection()
# handle a set of inputs starting from an index
succNum = 0
for whichIndex in range(startIndexOfImage,startIndexOfImage + dataProcessingBatchNum):
print "\n\nprocessing input of index %s in the dataset: " %(str(whichIndex))
succ = handleOne(model,dc,whichIndex)
if succ == True: succNum += 1
dc.addSuccPercent(succNum/float(dataProcessingBatchNum))
dc.provideDetails()
dc.summarise()
dc.close()
###########################################################################
#
# safety checking
# starting from the a specified hidden layer
#
############################################################################
## how many branches to expand
numOfPointsAfterEachFeature = 1
mcts_mode = "sift_twoPlayer"
#mcts_mode = "singlePlayer"
def handleOne(model,dc,startIndexOfImage):
# get an image to interpolate
global np
image = NN.getImage(model,startIndexOfImage)
print("the shape of the input is "+ str(image.shape))
if dataset == "twoDcurve": image = np.array([3.58747339,1.11101673])
dc.initialiseIndex(startIndexOfImage)
originalImage = copy.deepcopy(image)
if checkingMode == "stepwise":
k = startLayer
elif checkingMode == "specificLayer":
k = maxLayer
while k <= maxLayer:
layerType = getLayerType(model, k)
re = False
start_time = time.time()
# only these layers need to be checked
if layerType in ["Convolution2D","Conv2D", "Dense", "InputLayer"] and k >= 0 :
dc.initialiseLayer(k)
st = searchTree(image,k)
st.addImages(model,[image])
print("\n================================================================")
print "\nstart checking the safety of layer "+str(k)
(originalClass,originalConfident) = NN.predictWithImage(model,image)
origClassStr = dataBasics.LABELS(int(originalClass))
path0="%s/%s_original_as_%s_with_confidence_%s.png"%(directory_pic_string,startIndexOfImage,origClassStr,originalConfident)
dataBasics.save(-1,originalImage, path0)
# for every layer
f = 0
while f < numOfFeatures :
f += 1
print("\n================================================================")
print("Round %s of layer %s for image %s"%(f,k,startIndexOfImage))
index = st.getOneUnexplored()
imageIndex = copy.deepcopy(index)
# for every image
# start from the first hidden layer
t = 0
re = False
while True and index != (-1,-1):
# pick the first element of the queue
print "(1) get a manipulated input ..."
(image0,span,numSpan,numDimsToMani,_) = st.getInfo(index)
print "current layer: %s."%(t)
print "current index: %s."%(str(index))
path2 = directory_pic_string+"/temp.png"
print "current operated image is saved into %s"%(path2)
dataBasics.save(index[0],image0,path2)
print "(2) synthesise region from %s..."%(span.keys())
# ne: next region, i.e., e_{k+1}
#print "manipulated: %s"%(st.manipulated[t])
(nextSpan,nextNumSpan,numDimsToMani) = regionSynth(model,dataset,image0,st.manipulated[t],t,span,numSpan,numDimsToMani)
st.addManipulated(t,nextSpan.keys())
(nextSpan,nextNumSpan,npre) = precisionSynth(model,image0,t,span,numSpan,nextSpan,nextNumSpan)
print "dimensions to be considered: %s"%(nextSpan)
print "spans for the dimensions: %s"%(nextNumSpan)
if t == k:
# only after reaching the k layer, it is counted as a pass
print "(3) safety analysis ..."
# wk for the set of counterexamples
# rk for the set of images that need to be considered in the next precision
# rs remembers how many input images have been processed in the last round
# nextSpan and nextNumSpan are revised by considering the precision npre
(nextSpan,nextNumSpan,rs,wk,rk) = safety_analysis(model,dataset,t,startIndexOfImage,st,index,nextSpan,nextNumSpan,npre)
if len(rk) > 0:
rk = (zip (*rk))[0]
print "(4) add new images ..."
random.seed(time.time())
if len(rk) > numOfPointsAfterEachFeature:
rk = random.sample(rk, numOfPointsAfterEachFeature)
diffs = diffImage(image0,rk[0])
print("the dimensions of the images that are changed in the this round: %s"%diffs)
if len(diffs) == 0:
st.clearManipulated(k)
return
st.addImages(model,rk)
st.removeProcessed(imageIndex)
(re,percent,eudist,l1dist,l0dist) = reportInfo(image,wk)
print "euclidean distance %s"%(euclideanDistance(image,rk[0]))
print "L1 distance %s"%(l1Distance(image,rk[0]))
print "L0 distance %s"%(l0Distance(image,rk[0]))
print "manipulated percentage distance %s\n"%(diffPercent(image,rk[0]))
break
else:
st.removeProcessed(imageIndex)
break
else:
print "(3) add new intermediate node ..."
index = st.addIntermediateNode(image0,nextSpan,nextNumSpan,npre,numDimsToMani,index)
re = False
t += 1
if re == True:
dc.addManipulationPercentage(percent)
print "euclidean distance %s"%(eudist)
print "L1 distance %s"%(l1dist)
print "L0 distance %s"%(l0dist)
print "manipulated percentage distance %s\n"%(percent)
dc.addEuclideanDistance(eudist)
dc.addl1Distance(l1dist)
dc.addl0Distance(l0dist)
(ocl,ocf) = NN.predictWithImage(model,wk[0])
dc.addConfidence(ocf)
break
if f == numOfFeatures:
print "(6) no adversarial example is found in this layer within the distance restriction."
st.destructor()
elif layerType in ["Input"] and k < 0 and mcts_mode == "sift_twoPlayer" :
print "directly handling the image ... "
dc.initialiseLayer(k)
(originalClass,originalConfident) = NN.predictWithImage(model,image)
origClassStr = dataBasics.LABELS(int(originalClass))
path0="%s/%s_original_as_%s_with_confidence_%s.png"%(directory_pic_string,startIndexOfImage,origClassStr,originalConfident)
dataBasics.save(-1,originalImage, path0)
# initialise a search tree
st= mcts_twoPlayer(model,model,image,image,-1,"cooperator")
st.initialiseActions()
st.setManipulationType("sift_twoPlayer")
start_time_all = time.time()
runningTime_all = 0
numberOfMoves = 0
while st.terminalNode(st.rootIndex) == False and st.terminatedByControlledSearch(st.rootIndex) == False and runningTime_all <= MCTS_all_maximal_time:
print("the number of moves we have made up to now: %s"%(numberOfMoves))
eudist = st.euclideanDist(st.rootIndex)
l1dist = st.l1Dist(st.rootIndex)
l0dist = st.l0Dist(st.rootIndex)
percent = st.diffPercent(st.rootIndex)
diffs = st.diffImage(st.rootIndex)
print("euclidean distance %s"%(eudist))
print("L1 distance %s"%(l1dist))
print("L0 distance %s"%(l0dist))
print("manipulated percentage distance %s"%(percent))
print("manipulated dimensions %s"%(diffs))
start_time_level = time.time()
runningTime_level = 0
childTerminated = False
while runningTime_level <= MCTS_level_maximal_time:
(leafNode,availableActions) = st.treeTraversal(st.rootIndex)
newNodes = st.initialiseExplorationNode(leafNode,availableActions)
for node in newNodes:
(childTerminated, value) = st.sampling(node,availableActions)
#if childTerminated == True: break
st.backPropagation(node,value)
#if childTerminated == True: break
runningTime_level = time.time() - start_time_level
nprint("best possible one is %s"%(str(st.bestCase)))
bestChild = st.bestChild(st.rootIndex)
#st.collectUselessPixels(st.rootIndex)
st.makeOneMove(bestChild)
image1 = st.applyManipulationToGetImage(st.spans[st.rootIndex],st.numSpans[st.rootIndex])
diffs = st.diffImage(st.rootIndex)
path0="%s/%s_temp_%s.png"%(directory_pic_string,startIndexOfImage,len(diffs))
dataBasics.save(-1,image1,path0)
(newClass,newConfident) = NN.predictWithImage(model,image1)
print("confidence: %s"%(newConfident))
if childTerminated == True: break
# store the current best
(_,bestSpans,bestNumSpans) = st.bestCase
image1 = st.applyManipulationToGetImage(bestSpans,bestNumSpans)
path0="%s/%s_currentBest.png"%(directory_pic_string,startIndexOfImage)
dataBasics.save(-1,image1,path0)
numberOfMoves += 1
runningTime_all = time.time() - start_time_all
(_,bestSpans,bestNumSpans) = st.bestCase
#image1 = applyManipulation(st.image,st.spans[st.rootIndex],st.numSpans[st.rootIndex])
image1 = st.applyManipulationToGetImage(bestSpans,bestNumSpans)
(newClass,newConfident) = NN.predictWithImage(model,image1)
newClassStr = dataBasics.LABELS(int(newClass))
re = newClass != originalClass
if re == True:
path0="%s/%s_%s_%s_modified_into_%s_with_confidence_%s.png"%(directory_pic_string,startIndexOfImage,"sift_twoPlayer", origClassStr,newClassStr,newConfident)
dataBasics.save(-1,image1,path0)
path0="%s/%s_diff.png"%(directory_pic_string,startIndexOfImage)
dataBasics.save(-1,np.subtract(image,image1),path0)
print("\nfound an adversary image within prespecified bounded computational resource. The following is its information: ")
print("difference between images: %s"%(diffImage(image,image1)))
print("number of adversarial examples found: %s"%(st.numAdv))
eudist = euclideanDistance(st.image,image1)
l1dist = l1Distance(st.image,image1)
l0dist = l0Distance(st.image,image1)
percent = diffPercent(st.image,image1)
print("euclidean distance %s"%(eudist))
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"%(newClassStr, newConfident))
dc.addRunningTime(time.time() - start_time_all)
dc.addConfidence(newConfident)
dc.addManipulationPercentage(percent)
dc.addEuclideanDistance(eudist)
dc.addl1Distance(l1dist)
dc.addl0Distance(l0dist)
#path0="%s/%s_original_as_%s_heatmap.png"%(directory_pic_string,startIndexOfImage,origClassStr)
#plt.imshow(GMM(image),interpolation='none')
#plt.savefig(path0)
#path1="%s/%s_%s_%s_modified_into_%s_heatmap.png"%(directory_pic_string,startIndexOfImage,"sift_twoPlayer", origClassStr,newClassStr)
#plt.imshow(GMM(image1),interpolation='none')
#plt.savefig(path1)
else:
print("\nfailed to find an adversary image within prespecified bounded computational resource. ")
elif layerType in ["Input"] and k < 0 and mcts_mode == "singlePlayer" :
print "directly handling the image ... "
dc.initialiseLayer(k)
(originalClass,originalConfident) = NN.predictWithImage(model,image)
origClassStr = dataBasics.LABELS(int(originalClass))
path0="%s/%s_original_as_%s_with_confidence_%s.png"%(directory_pic_string,startIndexOfImage,origClassStr,originalConfident)
dataBasics.save(-1,originalImage, path0)
# initialise a search tree
st = searchMCTS(model,image,k)
st.initialiseActions()
start_time_all = time.time()
runningTime_all = 0
numberOfMoves = 0
while st.terminalNode(st.rootIndex) == False and st.terminatedByControlledSearch(st.rootIndex) == False and runningTime_all <= MCTS_all_maximal_time:
print("the number of moves we have made up to now: %s"%(numberOfMoves))
eudist = st.euclideanDist(st.rootIndex)
l1dist = st.l1Dist(st.rootIndex)
l0dist = st.l0Dist(st.rootIndex)
percent = st.diffPercent(st.rootIndex)
diffs = st.diffImage(st.rootIndex)
print "euclidean distance %s"%(eudist)
print "L1 distance %s"%(l1dist)
print "L0 distance %s"%(l0dist)
print "manipulated percentage distance %s"%(percent)
print "manipulated dimensions %s"%(diffs)
start_time_level = time.time()
runningTime_level = 0
childTerminated = False
while runningTime_level <= MCTS_level_maximal_time:
(leafNode,availableActions) = st.treeTraversal(st.rootIndex)
newNodes = st.initialiseExplorationNode(leafNode,availableActions)
for node in newNodes:
(childTerminated, value) = st.sampling(node,availableActions)
if childTerminated == True: break
st.backPropagation(node,value)
if childTerminated == True: break
runningTime_level = time.time() - start_time_level
print("best possible one is %s"%(st.showBestCase()))
bestChild = st.bestChild(st.rootIndex)
#st.collectUselessPixels(st.rootIndex)
st.makeOneMove(bestChild)
image1 = applyManipulation(st.image,st.spans[st.rootIndex],st.numSpans[st.rootIndex])
diffs = st.diffImage(st.rootIndex)
path0="%s/%s_temp_%s.png"%(directory_pic_string,startIndexOfImage,len(diffs))
dataBasics.save(-1,image1,path0)
(newClass,newConfident) = NN.predictWithImage(model,image1)
print "confidence: %s"%(newConfident)
if childTerminated == True: break
# store the current best
(_,bestSpans,bestNumSpans) = st.bestCase
image1 = applyManipulation(st.image,bestSpans,bestNumSpans)
path0="%s/%s_currentBest.png"%(directory_pic_string,startIndexOfImage)
dataBasics.save(-1,image1,path0)
runningTime_all = time.time() - runningTime_all
numberOfMoves += 1
(_,bestSpans,bestNumSpans) = st.bestCase
#image1 = applyManipulation(st.image,st.spans[st.rootIndex],st.numSpans[st.rootIndex])
image1 = applyManipulation(st.image,bestSpans,bestNumSpans)
(newClass,newConfident) = NN.predictWithImage(model,image1)
newClassStr = dataBasics.LABELS(int(newClass))
re = newClass != originalClass
path0="%s/%s_%s_modified_into_%s_with_confidence_%s.png"%(directory_pic_string,startIndexOfImage,origClassStr,newClassStr,newConfident)
dataBasics.save(-1,image1,path0)
#print np.max(image1), np.min(image1)
print("difference between images: %s"%(diffImage(image,image1)))
#plt.imshow(image1 * 255, cmap=mpl.cm.Greys)
#plt.show()
if re == True:
eudist = euclideanDistance(st.image,image1)
l1dist = l1Distance(st.image,image1)
l0dist = l0Distance(st.image,image1)
percent = diffPercent(st.image,image1)
print "euclidean distance %s"%(eudist)
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"%(newClassStr, newConfident)
dc.addEuclideanDistance(eudist)
dc.addl1Distance(l1dist)
dc.addl0Distance(l0dist)
dc.addManipulationPercentage(percent)
st.destructor()
else:
print("layer %s is of type %s, skipping"%(k,layerType))
#return
runningTime = time.time() - start_time
dc.addRunningTime(runningTime)
if re == True and exitWhen == "foundFirst":
break
k += 1
print("Please refer to the file %s for statistics."%(dc.fileName))
if re == True:
return True
else: return False
def reportInfo(image,wk):
# exit only when we find an adversarial example
if wk == []:
print "(5) no adversarial example is found in this round."
return (False,0,0,0,0)
else:
print "(5) an adversarial example has been found."
image0 = wk[0]
eudist = euclideanDistance(image,image0)
l1dist = l1Distance(image,image0)
l0dist = l0Distance(image,image0)
percent = diffPercent(image,image0)
return (True,percent,eudist,l1dist,l0dist)
if __name__ == "__main__":
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))