Esempio n. 1
0
    def sampleNext(self, actionSequence, depth, availableActionIDs,
                   usedActionIDs):
        #print spansPath.keys()
        activations1 = self.applyGeoManipulation(self.activations,
                                                 actionSequence)
        (newClass, newConfident) = self.predictWithActivations(activations1)
        #print euclideanDistance(self.activations,activations1), newConfident, newClass
        (distMethod, distVal) = controlledSearch
        if distMethod == "euclidean":
            dist = 1 - euclideanDistance(activations1, self.activations)
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "L1":
            dist = 1 - l1Distance(activations1, self.activations)
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "Percentage":
            dist = 1 - diffPercent(activations1, self.activations)
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "NumDiffs":
            dist = self.activations.size - diffPercent(
                activations1, self.activations) * self.activations.size
            termValue = 0.0
            termByDist = dist < self.activations.size - distVal

        if newClass != self.originalClass:
            print("sampling a path ends in a terminal node with depth %s... " %
                  depth)
            self.decisionTree.addOnePath(dist, actionSequence)
            self.re_training.addDatum(activations1, self.originalClass)
            if self.bestCase[0] < dist:
                self.bestCase = (dist, spansPath, numSpansPath)
            return (depth == 0, dist)
        elif termByDist == True:
            print(
                "sampling a path ends by controlled search with depth %s ... "
                % depth)
            return (depth == 0, termValue)
        else:
            #print("continue sampling node ... ")
            #allChildren = initialisePixelSets(self.model,self.activations,spansPath.keys())

            randomActionIndex = random.choice(
                list(set(availableActionIDs) - set(usedActionIDs))
            )  #random.randint(0, len(allChildren)-1)
            action = self.actions[randomActionIndex]
            availableActionIDs.remove(randomActionIndex)
            usedActionIDs.append(randomActionIndex)

            newActSeq = actionSequence + [actioin]
            return self.sampleNext(newActSeq, depth + 1, availableActionIDs,
                                   usedActionIDs)
Esempio n. 2
0
 def terminatedByControlledSearch(self,index): 
     activations1 = applyManipulation(self.activations,self.spans[index],self.numSpans[index])
     (distMethod,distVal) = controlledSearch
     if distMethod == "euclidean": 
         dist = euclideanDistance(activations1,self.activations) 
     elif distMethod == "L1": 
         dist = l1Distance(activations1,self.activations) 
     elif distMethod == "Percentage": 
         dist = diffPercent(activations1,self.activations)
     elif distMethod == "NumDiffs": 
         dist = diffPercent(activations1,self.activations)
     nprint("terminated by controlled search")
     return dist > distVal 
Esempio n. 3
0
 def splitImages(self, remainImages):
     if remainImages != []:
         newSet = [remainImages[0]]
         remainImages.pop(0)
         similarIndex = []
         for i in range(len(remainImages)):
             if l1Distance(remainImages[i], self.image) < self.localdist[1]:
                 newSet.append(remainImages[i])
                 similarIndex.append(i)
         similarIndex = sorted(similarIndex, reverse=True)
         for i in similarIndex:
             remainImages.pop(i)
         self.advSets.append(newSet)
         self.splitImages(remainImages)
Esempio n. 4
0
 def l1Dist(self, index):
     activations1 = self.applyGeoManipulation(self.activations,
                                              self.actSeq[index])
     return l1Distance(self.activations, activations1)
Esempio n. 5
0
 def l1Dist(self,index): 
     activations1 = applyManipulation(self.activations,self.spans[index],self.numSpans[index])
     return l1Distance(self.activations,activations1)
Esempio n. 6
0
    def sampleNext(self): 
        activations1 = applyManipulation(self.activations,self.spansPath,self.numSpansPath)
        (newClass,newConfident) = self.predictWithActivations(activations1)
        (distMethod,distVal) = controlledSearch
        if distMethod == "euclidean": 
            dist = 1 - euclideanDistance(activations1,self.activations) 
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "L1": 
            dist = 1 - l1Distance(activations1,self.activations) 
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "Percentage": 
            dist = 1 - diffPercent(activations1,self.activations)
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "NumDiffs": 
            dist = self.activations.size - diffPercent(activations1,self.activations) * self.activations.size
            termValue = 0.0
            termByDist = dist < self.activations.size - distVal

        #if termByDist == False and newConfident < 0.5 and self.depth <= 3: 
        #    termByDist = True

        if newClass != self.originalClass and newConfident > effectiveConfidenceWhenChanging:
            # and newClass == dataBasics.next_index(self.originalClass,self.originalClass): 
            nprint("sampling a path ends in a terminal node with self.depth %s... "%self.depth)
            
            (self.spansPath,self.numSpansPath) = self.scrutinizePath(self.spansPath,self.numSpansPath,newClass)
            
            self.decisionTree.addOnePath(dist,self.spansPath,self.numSpansPath)
            self.numAdv += 1
            self.analyseAdv.addAdv(activations1)
            self.getUsefulPixels(self.accDims,self.d)
                
            self.re_training.addDatum(activations1,self.originalClass)
            if self.bestCase[0] < dist: self.bestCase = (dist,self.spansPath,self.numSpansPath)
            return (self.depth == 0, dist)
        elif termByDist == True: 
            nprint("sampling a path ends by controlled search with self.depth %s ... "%self.depth)
            self.re_training.addDatum(activations1,self.originalClass)
            return (self.depth == 0, termValue)
        elif list(set(self.availableActionIDs)-set(self.usedActionIDs)) == []: 
            nprint("sampling a path ends with self.depth %s because no more actions can be taken ... "%self.depth)
            return (self.depth == 0, termValue)        
        else: 
            #print("continue sampling node ... ")
            #allChildren = initialisePixelSets(self.model,self.activations,self.spansPath.keys())
            randomActionIndex = random.choice(list(set(self.availableActionIDs)-set(self.usedActionIDs))) #random.randint(0, len(allChildren)-1)
            (span,numSpan,_) = self.actions[randomActionIndex]
            self.availableActionIDs.remove(randomActionIndex)
            self.usedActionIDs.append(randomActionIndex)
            newSpanPath = self.mergeSpan(self.spansPath,span)
            newNumSpanPath = self.mergeNumSpan(self.numSpansPath,numSpan)
            activations2 = applyManipulation(self.activations,newSpanPath,newNumSpanPath)
            (newClass2,newConfident2) = self.predictWithActivations(activations2)
            confGap2 = newConfident - newConfident2
            if newClass2 == newClass: 
                self.accDims.append((randomActionIndex,confGap2))
            else: self.accDims.append((randomActionIndex,1.0))

            self.spansPath = newSpanPath
            self.numSpansPath = newNumSpanPath
            self.depth = self.depth+1
            self.availableActionIDs = self.availableActionIDs
            self.usedActionIDs = self.usedActionIDs 
            self.accDims = self.accDims
            self.d = self.d
            return self.sampleNext()
Esempio n. 7
0
    def sampleNextSndRound(self, spansPath, numSpansPath, depth,
                           availableActionIDs, usedActionIDs, accDims, d):
        activations1 = applyManipulation(self.activations, spansPath,
                                         numSpansPath)
        (newClass, newConfident) = self.predictWithActivations(activations1)
        (distMethod, distVal) = controlledSearch
        distVal = distVal / float(2)
        print("second depth-first search in %s" % (distVal))
        if distMethod == "euclidean":
            dist = 1 - euclideanDistance(activations1, self.pocketActivations)
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "L1":
            dist = 1 - l1Distance(activations1, self.pocketActivations)
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "Percentage":
            dist = 1 - diffPercent(activations1, self.pocketActivations)
            termValue = 0.0
            termByDist = dist < 1 - distVal
        elif distMethod == "NumDiffs":
            dist = self.activations.size - diffPercent(
                activations1,
                self.pocketActivations) * self.pocketActivations.size
            termValue = 0.0
            termByDist = dist < self.pocketActivations.size - distVal

        if newClass == self.originalClass:
            # and newClass == dataBasics.next_index(self.originalClass,self.originalClass):
            nprint(
                "sampling a path ends in a terminal node with depth %s... " %
                depth)
            self.pocketTree.addOnePath(dist, spansPath, numSpansPath)
            print("found a pocket!")
            return (depth == 0, dist)
        elif termByDist == True:
            nprint(
                "sampling a path ends by controlled search with depth %s ... "
                % depth)
            return (depth == 0, termValue)
        elif list(set(availableActionIDs) - set(usedActionIDs)) == []:
            nprint(
                "sampling a path ends with depth %s because no more actions can be taken ... "
                % depth)
            return (depth == 0, termValue)
        else:
            #print("continue sampling node ... ")
            #allChildren = initialisePixelSets(self.model,self.activations,spansPath.keys())
            randomActionIndex = random.choice(
                list(set(availableActionIDs) - set(usedActionIDs))
            )  #random.randint(0, len(allChildren)-1)
            (span, numSpan, _) = self.actions[randomActionIndex]
            availableActionIDs.remove(randomActionIndex)
            usedActionIDs.append(randomActionIndex)
            newSpanPath = self.mergeSpan(spansPath, span)
            newNumSpanPath = self.mergeNumSpan(numSpansPath, numSpan)
            activations2 = applyManipulation(self.activations, newSpanPath,
                                             newNumSpanPath)
            (newClass2,
             newConfident2) = self.predictWithActivations(activations2)
            confGap2 = newConfident - newConfident2
            if newClass2 == newClass:
                accDims.append((randomActionIndex, confGap2))
            else:
                accDims.append((randomActionIndex, 1.0))

            return self.sampleNextSndRound(newSpanPath, newNumSpanPath,
                                           depth + 1, availableActionIDs,
                                           usedActionIDs, accDims, d)