-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLV_MCTS.py
executable file
·319 lines (248 loc) · 12 KB
/
DLV_MCTS.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
#!/usr/bin/env python
"""
main file
author: Xiaowei Huang
"""
import sys
sys.path.append('networks')
sys.path.append('configuration')
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 configuration import *
from basics import *
from networkBasics import *
from mcts import mcts
from mcts_geo import mcts_geo
from dataCollection import dataCollection
from superPixels import superPixel_slic
from mnist_network import dynamic_build_model
from inputManipulation import applyManipulation,assignManipulationSimple
from re_training import re_training
import theano
import theano.tensor as T
def main():
#re_training()
explaination()
def explaination():
model = loadData()
if whichMode == "train": return
if trainingModel == "autoencoder":
(model,autoencoder) = model
if startLayer == -1: autoencoder = model
else: autoencoder = model
# initialise a dataCollection instance
phase = "firstRound"
dc = dataCollection("%s_%s_%s"%(startIndexOfImage,dataProcessingBatchNum, controlledSearch))
# initialise a re_training instance
reTrain = re_training(model, NN.getImage(model,startIndexOfImage).shape)
#originalScore = reTrain.evaluateWithOriginalModel()
#dc.addComment("original test score: %s\n\n"%originalScore)
# finding adversarial examples from original model
succNum = 0
for whichIndex in range(startIndexOfImage,startIndexOfImage + dataProcessingBatchNum):
print "\n\nprocessing input of index %s in the dataset: " %(str(whichIndex))
succ = handleOne(model,autoencoder,dc,reTrain,phase,whichIndex,firstRound_manipulations[0])
if succ == True: succNum += 1
dc.addSuccPercent(succNum/float(dataProcessingBatchNum))
# output statistics for original model
print("Please refer to the file %s for statistics."%(dc.fileName))
dc.provideDetails()
dc.summarise()
dc.close()
def re_training():
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
for firstRound_manipulation in firstRound_manipulations:
for sndRound_manipulation in sndRound_manipulations:
reTraining_experiment(firstRound_manipulation,sndRound_manipulation)
def reTraining_experiment(firstRound_manipulation,sndRound_manipulation):
model = loadData()
if whichMode == "train": return
if trainingModel == "autoencoder":
(model,autoencoder) = model
if startLayer == -1: autoencoder = model
else: autoencoder = model
# initialise a dataCollection instance
phase = "firstRound"
dc = dataCollection("%s_%s_%s_%s_%s_firstRound"%(startIndexOfImage,dataProcessingBatchNum, controlledSearch, firstRound_manipulation, sndRound_manipulation))
# initialise a re_training instance
reTrain = re_training(model, NN.getImage(model,startIndexOfImage).shape)
originalScore = reTrain.evaluateWithOriginalModel()
dc.addComment("original test score: %s\n\n"%originalScore)
# finding adversarial examples from original model
succNum = 0
for whichIndex in range(startIndexOfImage,startIndexOfImage + dataProcessingBatchNum):
print "\n\nprocessing input of index %s in the dataset: " %(str(whichIndex))
succ = handleOne(model,autoencoder,dc,reTrain,phase,whichIndex,firstRound_manipulation)
if succ == True: succNum += 1
dc.addSuccPercent(succNum/float(dataProcessingBatchNum))
# output statistics for original model
print("Please refer to the file %s for statistics."%(dc.fileName))
dc.provideDetails()
dc.summarise()
dc.close()
if reTrain.numberOfNewExamples() == 0:
print "failed to find new examples for further training"
return
else:
print "ready for re-training ... "
# initialise a dataCollection instance
phase = "sndRound"
dc = dataCollection("%s_%s_%s_%s_%s_secondRound"%(startIndexOfImage,dataProcessingBatchNum, controlledSearch, firstRound_manipulation, sndRound_manipulation))
dc.addComment("%s new examples are founded.\n\n"%(reTrain.numberOfNewExamples()))
# update model with new data
reTrain.setReTrainedModelName("%s_%s_%s_%s"%(startIndexOfImage,dataProcessingBatchNum, controlledSearch,firstRound_manipulation))
model = reTrain.training()
updatedScore = reTrain.evaluateWithUpdatedModel()
dc.addComment("updated test score: %s\n\n"%updatedScore)
# finding adversarial examples from updated model
succNum = 0
for whichIndex in range(startIndexOfImage,startIndexOfImage + dataProcessingBatchNum):
print "\n\nprocessing input of index %s in the dataset: " %(str(whichIndex))
succ = handleOne(model,autoencoder,dc,reTrain,phase,whichIndex,sndRound_manipulation)
if succ == True: succNum += 1
dc.addSuccPercent(succNum/float(dataProcessingBatchNum))
# output statistics for updated model
print("Please refer to the file %s for statistics."%(dc.fileName))
dc.provideDetails()
dc.summarise()
dc.close()
###########################################################################
#
# checking with MCTS
#
############################################################################
def handleOne(model,autoencoder,dc,reTrain,phase,startIndexOfImage,manipulationType):
# visualisation, switch on if needed
#visualization(model,501)
#return
# get an image to interpolate
global np
image = NN.getImage(model,startIndexOfImage)
print("the shape of the input is "+ str(image.shape))
#superPixel_slic(image)
#return
dc.initialiseIndex(startIndexOfImage)
dc.initialiseLayer(startLayer)
# keep information for the original image
(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,image, path0)
# keep information for the activations
if startLayer == -1:
activations = image
else:
activations = NN.getActivationValue(model,startLayer,image)
if len(activations.shape) == 2:
output = np.squeeze(autoencoder.predict(np.expand_dims(np.expand_dims(activations,axis=0),axis=0)))
else:
output = np.squeeze(autoencoder.predict(np.expand_dims(activations,axis=0)))
if startLayer > -1:
path0="%s/%s_autoencoder.png"%(directory_pic_string,startIndexOfImage)
dataBasics.save(-1,output, path0)
print "handling activations of layer %s with shape %s ... "%(startLayer, str(activations.shape))
# initialise a search tree
st = mcts(model,autoencoder,image,activations,startLayer)
#st = mcts_geo(model,autoencoder,image,activations,startLayer)
if startLayer > -1:
visualizeOneLayer(model,image,startLayer)
st.visualizationMCTS()
st.setManipulationType(manipulationType)
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)
percent = st.diffPercent(st.rootIndex)
diffs = st.diffImage(st.rootIndex)
print "euclidean distance %s"%(eudist)
print "L1 distance %s"%(l1dist)
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_%s_modified_into_%s_with_confidence_%s.png"%(directory_pic_string,startIndexOfImage,phase,manipulationType, 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("difference between images: %s"%(diffImage(image,image1)))
st.showDecisionTree()
pixelImages = st.analysePixels()
advImages = st.analyseAdv.analyse()
for (v,pixelImage) in pixelImages:
path0="%s/%s_useful_%s.png"%(directory_pic_string,startIndexOfImage,v)
dataBasics.save(-1,pixelImage,path0)
for i in range(len(advImages)):
(advnum,advimg) = advImages[i]
(advClass,advConfident) = NN.predictWithImage(model,advimg)
advClassStr = dataBasics.LABELS(int(advClass))
path0="%s/%s_adv_%s_%s_%s.png"%(directory_pic_string,startIndexOfImage,i,advnum,advClassStr)
dataBasics.save(-1,advimg,path0)
print "number of adversarial examples found: %s"%(st.numAdv)
eudist = euclideanDistance(st.image,image1)
l1dist = l1Distance(st.image,image1)
percent = diffPercent(st.image,image1)
print "euclidean distance %s"%(eudist)
print "L1 distance %s"%(l1dist)
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.addManipulationPercentage(percent)
else:
print "failed to find an adversary image within prespecified bounded computational resource. "
newXtrain,newYtrain = st.re_training.returnData()
reTrain.addData(newXtrain,newYtrain)
st.destructor()
runningTime = time.time() - start_time
dc.addRunningTime(runningTime)
return re
if __name__ == "__main__":
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))