Beispiel #1
0
 def __init__(self, name,
              width, height,
              row, col,
              nframes=3,
              scale=2,
              sheetfile='characters.png',
              folder=os.path.join('data','images')):
     pygame.sprite.Sprite.__init__(self)
     sheet = loadImage(sheetfile, folder)
     self.name = name
     self.scale = scale
     self.images = {}
     for d,direction in enumerate(['south','west','east','north']):
         self.images[direction] = []
         for i in range(nframes):
             image = pygame.Surface((width, height))
             image.blit(sheet, (0,0), (((col+i)*width, (row+d)*height), (width,height)))
             image.set_colorkey(image.get_at((0,0)))
             image = pygame.transform.scale(image, ((scale*width), (scale*height)))
             self.images[direction].append(image)
         self.images[direction].append(self.images[direction][1])
     self.direction = 'south'
     self.speed = 0
     self.image = self.images['south'][0]
     self.rect = self.image.get_rect()
     self.frame = 0.0
     self.aspeed = 0.25
     self.name = name
     self.cellsize = (width*scale, height*scale)
     self.timer = 0
Beispiel #2
0
 def __init__(self, name, solid,
              width, height,
              row, col,
              nframes=1,
              horizontal=True,
              addframe=True,
              scale = 3,
              sheetfile='things.png',
              folder = os.path.join('data','images')):
     pygame.sprite.Sprite.__init__(self)
     self.solid = solid
     sheet = loadImage(sheetfile, folder)
     self.images = []
     for frame in range(nframes):
         img = pygame.Surface((width,height))
         x = col*width
         y = row*height
         if horizontal:
             x += frame*width
         else:
             y += frame*height
         img.blit(sheet, (0,0), ((x,y),(width,height)))
         img.set_colorkey(img.get_at((0,0)))
         self.images.append(pygame.transform.scale(img, (width*scale,height*scale)))
     self.image = self.images[0]
     if addframe and nframes == 3:
         self.images.append(self.images[1])
     self.rect = self.image.get_rect()
     self.frame = 0.0
     self.aspeed = 0.125
     self.name = name
     self.opening = False
     self.animated = False
Beispiel #3
0
 def __init__(self,
              name,
              width,
              height,
              row,
              col,
              nframes=3,
              scale=2,
              sheetfile='characters.png',
              folder=os.path.join('data', 'images')):
     pygame.sprite.Sprite.__init__(self)
     sheet = loadImage(sheetfile, folder)
     self.name = name
     self.scale = scale
     self.images = {}
     for d, direction in enumerate(['south', 'west', 'east', 'north']):
         self.images[direction] = []
         for i in range(nframes):
             image = pygame.Surface((width, height))
             image.blit(sheet, (0, 0),
                        (((col + i) * width, (row + d) * height),
                         (width, height)))
             image.set_colorkey(image.get_at((0, 0)))
             image = pygame.transform.scale(image, ((scale * width),
                                                    (scale * height)))
             self.images[direction].append(image)
         self.images[direction].append(self.images[direction][1])
     self.direction = 'south'
     self.speed = 0
     self.image = self.images['south'][0]
     self.rect = self.image.get_rect()
     self.frame = 0.0
     self.aspeed = 0.25
     self.name = name
     self.cellsize = (width * scale, height * scale)
     self.timer = 0
Beispiel #4
0
def rankUsingPrograms():
    results = pickle.load(open(arguments.name, 'rb'))
    print " [+] Loaded %d synthesis results from %s." % (len(results),
                                                         arguments.name)

    def getProgramForParse(sequence):
        for r in results:
            if sequence == r.parse and r.usedPrior():
                return r
        return None

    def featuresOfParticle(p):
        r = getProgramForParse(p.sequence())
        if r != None and r.cost != None and r.source != None:
            programFeatures = mergeDictionaries({'failure': 0.0},
                                                parseSketchOutput(
                                                    r.source).features())
        else:
            programFeatures = {'failure': 1.0}
        parseFeatures = {
            'distance': p.distance[0] + p.distance[1],
            'logPrior': p.sequence().logPrior(),
            'logLikelihood': p.logLikelihood
        }
        return mergeDictionaries(parseFeatures, programFeatures)

    k = arguments.learnToRank
    topParticles = [
        loadTopParticles('drawings/expert-%d-parses' % j, k)
        for j in range(100)
    ]
    learningProblems = []
    for j, ps in enumerate(topParticles):
        gt = getGroundTruthParse('drawings/expert-%d.png' % j)
        positives = []
        negatives = []
        for p in ps:
            if p.sequence() == gt: positives.append(p)
            else: negatives.append(p)
        if positives != [] and negatives != []:
            learningProblems.append(
                (map(featuresOfParticle,
                     positives), map(featuresOfParticle, negatives)))

    featureIndices = list(
        set([
            f for pn in learningProblems for exs in pn for ex in exs
            for f in ex.keys()
        ]))

    def dictionaryToVector(featureMap):
        return [featureMap.get(f, 0.0) for f in featureIndices]

    learningProblems = [(map(dictionaryToVector,
                             positives), map(dictionaryToVector, negatives))
                        for positives, negatives in learningProblems]
    parameters = learnToRank(learningProblems)
    for f, p in zip(featureIndices, parameters):
        print f, p

    # showcases where it succeeds
    programAccuracy = 0
    oldAccuracy = 0
    for j, tp in enumerate(topParticles):
        if tp == []: continue

        gt = getGroundTruthParse('drawings/expert-%d.png' % j)
        # the_top_particles_according_to_the_learned_weights
        featureVectors = np.array(
            [dictionaryToVector(featuresOfParticle(p)) for p in tp])
        particleScores = featureVectors.dot(parameters)
        bestParticleUsingPrograms = max(zip(particleScores.tolist(), tp))[1]
        programPredictionCorrect = False
        if bestParticleUsingPrograms.sequence() == gt:
            print "Prediction using the program is correct."
            programPredictionCorrect = True
            programAccuracy += 1
        else:
            print "Prediction using the program is incorrect."
        oldPredictionCorrect = tp[0].sequence() == gt
        print "Was the old prediction correct?", oldPredictionCorrect
        oldAccuracy += int(oldPredictionCorrect)

        visualization = np.zeros((256, 256 * 3))
        visualization[:, :256] = 1 - frameImageNicely(
            loadImage('drawings/expert-%d.png' % j))
        visualization[:, 256:(256 * 2)] = frameImageNicely(
            fastRender(tp[0].sequence()))
        visualization[:, (256 * 2):(256 * 3)] = frameImageNicely(
            fastRender(bestParticleUsingPrograms.sequence()))
        visualization[:, 256] = 0.5
        visualization[:, 256 * 2] = 0.5
        visualization = 255 * visualization

        if not oldPredictionCorrect and programPredictionCorrect:
            fp = "../TikZpaper/figures/programSuccess%d.png" % j
            print "Great success! see %s" % fp
            saveMatrixAsImage(visualization, fp)

        if oldPredictionCorrect and not programPredictionCorrect:
            print "Minor setback!"
            print particleScores

    print programAccuracy, "vs", oldAccuracy
Beispiel #5
0
def viewSynthesisResults(arguments):
    results = pickle.load(open(arguments.name, 'rb'))
    print " [+] Loaded %d synthesis results." % (len(results))

    interestingExtrapolations = [
        7,
        #14,
        17,
        29,
        #35,
        52,
        57,
        63,
        70,
        72,
        88,
        #99]
    ]
    interestingExtrapolations = [
        (16, 12),  #*
        #(17,0),
        (18, 0),  #*
        #(22,0),
        #(23,0),
        #(29,12),
        #(31,27),
        (34, 0),  #*
        #(36,0),
        #(38,12),
        (39, 0),  #*
        #(41,1),
        #(51,1),
        #(52,12),
        #(57,0),
        #(58,0),
        (60, 0),  #*
        #(63,0),
        (66, 2),  #*
        (71, 1),  #*
        #(72,0),
        #(73,0),
        #(74,10),
        #(75,5),
        #(79,0),
        #(85,1),
        (86, 0),  #*
        #(88,0),
        (90, 2),  #*
        #(92,0),
        #(95,8)
    ]

    #interestingExtrapolations = list(range(100))

    latex = []
    extrapolationMatrix = []

    programFeatures = {}

    for expertIndex in list(range(100)):
        f = 'drawings/expert-%d.png' % expertIndex
        parse = getGroundTruthParse(f)
        if parse == None:
            print "No ground truth for %d" % expertIndex
            assert False

        relevantResults = [
            r for r in results if r.job.originalDrawing == f and r.cost != None
        ]
        if relevantResults == []:
            print "No synthesis result for %s" % f
            result = None
        else:
            result = min(relevantResults, key=lambda r: r.cost)
            equallyGoodResults = [
                r for r in relevantResults if r.cost <= result.cost + 1
            ]
            if len(equallyGoodResults) > 1:
                print "Got %d results for %d" % (len(equallyGoodResults),
                                                 expertIndex)

            programs = [ r.program.fixStringParameters().\
                         fixReflections(result.job.parse.canonicalTranslation()).removeDeadCode()
                         for r in equallyGoodResults ]
            gt = result.job.parse.canonicalTranslation()
            badPrograms = [
                p for p in programs
                if p.convertToSequence().canonicalTranslation() != gt
            ]
            if badPrograms:
                print " [-] WARNING: Got %d programs that are inconsistent with ground truth" % (
                    len(badPrograms))
            if False:
                for program in programs:
                    prediction = program.convertToSequence(
                    ).canonicalTranslation()
                    actual = gt
                    if not (prediction == actual):
                        print "FATAL: program does notproduce spec"
                        print "Specification:"
                        print actual
                        print "Program:"
                        print program
                        print program.pretty()
                        print "Program output:"
                        print prediction
                        print set(map(str, prediction.lines))
                        print set(map(str, actual.lines))
                        print set(map(str, actual.lines)) ^ set(
                            map(str, prediction.lines))
                        assert False

        if result == None and arguments.extrapolate:
            print "Synthesis failure for %s" % f
            continue

        print " [+] %s" % f
        print "\t(synthesis time: %s)" % (result.time if result else None)
        print

        if arguments.debug:
            print result.source

        if result != None:
            syntaxTree = result.program.fixStringParameters()
            syntaxTree = syntaxTree.fixReflections(
                result.job.parse.canonicalTranslation())
            print syntaxTree.pretty()
            print syntaxTree.features()
            print syntaxTree.convertToSequence()
            #showImage(fastRender(syntaxTree.convertToSequence()) + loadImage(f)*0.5 + fastRender(result.parse))
            programFeatures[f] = syntaxTree.features()

        if arguments.extrapolate:
            extrapolations = proposeExtrapolations(programs)
            if extrapolations:
                framedExtrapolations = [1 - frameImageNicely(loadImage(f))] + \
                                       [ frameImageNicely(t.draw(adjustCanvasSize = True))
                                         for t in extrapolations ]
                a = 255 * makeImageArray(framedExtrapolations)
                extrapolationMatrix.append(a)
                print "Saving extrapolation column to", 'extrapolations/expert-%d-extrapolation.png' % expertIndex
                saveMatrixAsImage(
                    a,
                    'extrapolations/expert-%d-extrapolation.png' % expertIndex)

        if not arguments.extrapolate:
            rightEntryOfTable = '''
        \\begin{minipage}{10cm}
        \\begin{verbatim}
%s
        \\end{verbatim}
\\end{minipage}
''' % (syntaxTree.pretty() if result != None else "Solver timeout")
        else:
            rightEntryOfTable = ""
        if False and extrapolations != [] and arguments.extrapolate:

            #print e
            rightEntryOfTable = '\\includegraphics[width = 5cm]{../TikZ/extrapolations/expert-%d-extrapolation.png}' % expertIndex
        if rightEntryOfTable != "":
            parseImage = '\\includegraphics[width = 5cm]{../TikZ/drawings/expert-%d-parses/0.png}' % expertIndex
            if not os.path.exists(
                    'drawings/expert-%d-parses/0.png' % expertIndex):
                parseImage = "Sampled no finished traces."
            latex.append('''
            \\begin{tabular}{lll}
    \\includegraphics[width = 5cm]{../TikZ/drawings/expert-%d.png}&
            %s&
    %s
    \\end{tabular}        
            ''' % (expertIndex, parseImage, rightEntryOfTable))
            print

    if arguments.latex:
        latex = '%s' % ("\\\\\n".join(latex))
        name = "extrapolations.tex" if arguments.extrapolate else "synthesizerOutputs.tex"
        with open('../TikZpaper/%s' % name, 'w') as handle:
            handle.write(latex)
        print "Wrote output to ../TikZpaper/%s" % name

    if arguments.similarity:
        analyzeFeatures(programFeatures)

    if arguments.extrapolate:
        #}make the big matrix
        bigMatrix = np.zeros((max([m.shape[0] for m in extrapolationMatrix]),
                              256 * len(extrapolationMatrix)))
        for j, r in enumerate(extrapolationMatrix):
            bigMatrix[0:r.shape[0], 256 * j:256 * (j + 1)] = r
        saveMatrixAsImage(bigMatrix, 'extrapolations/allTheExtrapolations.png')
# Function definitions:
def nothing(x):
    pass

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
ap.add_argument("-o", "--option", required=True,
                help="Option selecting the color to be checked")
ap.add_argument("-c", "--conf", required=True,
                help="Path to the Json file in which to save the threshold values")
args = vars(ap.parse_args())

# load the image:
state,image = utils.loadImage(args["image"])

# Check that the input image was loaded correctly
if state == utils.SUCCESS:

    # Creating a window for later use
    window_title = "HSV Threshold configuration (" + args["option"] + ")"
    cv2.namedWindow(window_title)

    # Get previous configuration values:
    state,configuration = utils.getConfigurationValues(args["option"],args["conf"])
    
    # Check that the values were retrieved correctly
    if state == utils.SUCCESS:
    
        # Creating track bar
Beispiel #7
0
def viewSynthesisResults(arguments):
    results = pickle.load(open(arguments.name,'rb'))
    print " [+] Loaded %d synthesis results."%(len(results))

    interestingExtrapolations = [7,
                                 #14,
                                 17,
                                 29,
                                 #35,
                                 52,
                                 57,
                                 63,
                                 70,
                                 72,
                                 88,
                                 #99]
                                 ]
    interestingExtrapolations = [(14,0),
                                 (23,0),
                                 (31,12),
                                 (35,0),
                                 (38,0),
                                 (39,10),
                                 (58,11),
                                 (75,0),
                                 (93,0),
                                  (99,0)]
    interestingExtrapolations = list(range(100))
                                 

    latex = []
    extrapolationMatrix = []

    programFeatures = {}

    for expertIndex in range(100):

        if arguments.extrapolate:
            if not any([ e == expertIndex or isinstance(e,tuple) and e[0] == expertIndex
                         for e in interestingExtrapolations ]):
                continue
            
        
        f = 'drawings/expert-%d.png'%expertIndex
        parse = getGroundTruthParse(f)
        if parse == None:
            print "No ground truth for %d"%expertIndex
            assert False
            
        parts = set(map(str,parse.lines))
        result = None
        for r in results:
            if isinstance(r,SynthesisResult) and r.originalDrawing == f:
                if set(map(str,r.parse.lines)) == parts and r.usedPrior() == (not arguments.noPrior):
                    result = r
                    break
        if expertIndex == 38: result = icingResult
        if result == None:
            print "No synthesis result for %s"%f
            if arguments.extrapolate: continue

        if result.source == None:
            print "Synthesis failure for %s"%f
            if arguments.extrapolate: continue

        print " [+] %s"%f
        print "\t(synthesis time: %f)"%(result.time)
        print

        if arguments.debug:
            print result.source

        if result != None and result.source != None:
            syntaxTree = parseSketchOutput(result.source)
            syntaxTree = syntaxTree.fixReflections(result.parse.canonicalTranslation())
            print syntaxTree
            print syntaxTree.features()
            print syntaxTree.convertToPython()
            print syntaxTree.convertToSequence()
            #showImage(fastRender(syntaxTree.convertToSequence()) + loadImage(f)*0.5 + fastRender(result.parse))
            programFeatures[f] = syntaxTree.features()

        extrapolations = []
        if arguments.extrapolate:
            syntaxTree = syntaxTree.explode()
            trace = syntaxTree.convertToSequence()
            print trace
            originalHasCollisions = result.parse.hasCollisions()
            print "COLLISIONS",originalHasCollisions

            framedExtrapolations = []
            for e in syntaxTree.extrapolations():
                t = e.convertToSequence()
                if not originalHasCollisions and t.removeDuplicates().hasCollisions(): continue
                if t == trace: continue
                if any([t == o for o in extrapolations ]): continue
                extrapolations.append(t)

                framedExtrapolations.append(1 - frameImageNicely(1 - t.framedRendering(result.parse)))

                if len(framedExtrapolations) > 20:
                    break
                
            if framedExtrapolations != []:
                for crap in interestingExtrapolations:
                    if isinstance(crap,tuple) and crap[0] == expertIndex:
                        framedExtrapolations = [framedExtrapolations[crap[1]]]
                        break
                
                        
                if arguments.debug:
                    framedExtrapolations = [loadImage(f), fastRender(syntaxTree.convertToSequence())] + framedExtrapolations
                else:
                    framedExtrapolations = [frameImageNicely(loadImage(f))] + framedExtrapolations
                a = np.zeros((256*len(framedExtrapolations),256))
                for j,e in enumerate(framedExtrapolations):
                    a[j*256:(1+j)*256,:] = 1 - e
                    a[j*256,:] = 0.5
                    a[(1+j)*256-1,:] = 0.5
                a[0,:] = 0.5
                a[:,0] = 0.5
                a[:,255] = 0.5
                a[256*len(framedExtrapolations)-1,:] = 0.5
                a = 255*a
                # to show the first one
                #a = a[:(256*2),:]
                extrapolationMatrix.append(a)
                saveMatrixAsImage(a,'extrapolations/expert-%d-extrapolation.png'%expertIndex)

            
        
        if not arguments.extrapolate:
            rightEntryOfTable = '''
        \\begin{minipage}{10cm}
        \\begin{verbatim}
%s
        \\end{verbatim}
\\end{minipage}
'''%(parseSketchOutput(result.source).pretty() if result != None and result.source != None else "Solver timeout")
        else:
            rightEntryOfTable = ""
        if extrapolations != [] and arguments.extrapolate:
            #}make the big matrix
            bigMatrix = np.zeros((max([m.shape[0] for m in extrapolationMatrix ]),256*len(extrapolationMatrix)))
            for j,r in enumerate(extrapolationMatrix):
                bigMatrix[0:r.shape[0],256*j:256*(j+1)] = r
            if len(extrapolations) < 10 and False:
                saveMatrixAsImage(bigMatrix,'../TikZpaper/figures/extrapolationMatrix.png')
            else:
                saveMatrixAsImage(bigMatrix,'../TikZpaper/figures/extrapolationMatrixSupplement.png')
            print e
            rightEntryOfTable = '\\includegraphics[width = 5cm]{../TikZ/extrapolations/expert-%d-extrapolation.png}'%expertIndex
        if rightEntryOfTable != "":
            parseImage = '\\includegraphics[width = 5cm]{../TikZ/drawings/expert-%d-parses/0.png}'%expertIndex
            if not os.path.exists('drawings/expert-%d-parses/0.png'%expertIndex):
                parseImage = "Sampled no finished traces."
            latex.append('''
            \\begin{tabular}{lll}
    \\includegraphics[width = 5cm]{../TikZ/drawings/expert-%d.png}&
            %s&
    %s
    \\end{tabular}        
            '''%(expertIndex, parseImage, rightEntryOfTable))
            print

    if arguments.latex:
        latex = '%s'%("\\\\\n".join(latex))
        name = "extrapolations.tex" if arguments.extrapolate else "synthesizerOutputs.tex"
        with open('../TikZpaper/%s'%name,'w') as handle:
            handle.write(latex)
        print "Wrote output to ../TikZpaper/%s"%name

    if arguments.similarity:
        analyzeFeatures(programFeatures)
Beispiel #8
0
               if x != y]
    for (x,y),d in zip(indexes, distances):
        matrix[x,y] += d

    matrix = matrix + matrix.T
    print "MATRIX:"
    print matrix.tolist()
    return matrix

if __name__ == '__main__':
    worker = RecognitionModel(DummyArguments())
    worker.loadDistanceCheckpoint("checkpoints/distance.checkpoint")

    imageNames = ['drawings/expert-%d.png'%j
                  for j in range(100) ]
    images = [ loadImage('/om/user/ellisk/%s'%n)
               for n in imageNames ]
    jobs = [(x,y) for j,x in enumerate(images)
            for k,y in enumerate(images)
            if j != k ]
    distances = []
    while jobs != []:
        batch = jobs[:100]
        jobs = jobs[100:]

        print "%d jobs remaining"%len(jobs)

        result = worker.learnedDistances(np.array([x for x,_ in batch ]),
                                         np.array([x for _,x in batch ]))
        for j in range(len(batch)):
            distances.append(result[j,1] + result[j,0])
Beispiel #9
0
    def initialize(self, scale = 3):
        if self.initialized:
            return self
        self.initialized = True
        # load all the tiles
        self.tilefile = loadImage( self.tilefile, self.imagefolder, colorkey=None)
        mapfile = open(os.path.join(self.datafolder, self.tilemap), 'r')
        maplines = mapfile.read().splitlines()
        self.dict = {}
        for x in maplines:
            mapping = [s.strip() for s in x.split(',')]
            key = mapping[6]
            self.dict[key] = {}
            w,h = int(mapping[2]), int(mapping[3])
            row, col = int(mapping[4]), int(mapping[5])
            self.dict[key]['name'] = mapping[0]
            self.dict[key]['file'] = mapping[1]
            self.dict[key]['width'] = w
            self.dict[key]['height'] = h
            self.dict[key]['row'] =  row
            self.dict[key]['col'] = col
            self.dict[key]['roomkey'] = mapping[6]
            self.dict[key]['wall'] = mapping[7] == 'wall'
                
            # find image for this tile
            #w, h = self.dict[key]['width'], self.dict[key]['height']
            #row, col = self.dict[key]['row'], self.dict[key]['col']
            image = pygame.Surface((w,h))
            image.blit(self.tilefile, (0,0),
                       ((col*w, row*h), (w,h)))
            image = pygame.transform.scale(image, (scale*w, scale*h))
            letter = self.dict[key]['roomkey']
            if letter in '12346':
                w,h = image.get_size()
                image.set_colorkey(image.get_at((w/2,h/2)))
            elif letter in '578':
                image.set_colorkey(image.get_at((0,0)))
            self.dict[key]['image'] = image

        # tile the room
        roomfile = open(os.path.join(self.datafolder, self.room),'r')
        roomrows = roomfile.read().splitlines()
        self.tiles = pygame.sprite.RenderPlain()
        self.tileArray = [[None for x in range(len(roomrows[0]))]
                          for y in range(len(roomrows))]
        for roomrow, keys in enumerate(roomrows):
            for roomcol, letter in enumerate(keys):
                if letter == '.':
                    newTile = EmptyTile()
                else:
                    data = self.dict[letter]
                    newTile = Tile(data['image'],
                                   (roomcol*scale*w, roomrow*scale*h),
                                   data['wall'])
                    if data['wall']:
                        newBlock = InvisibleBlock(
                            pygame.Rect(roomcol*scale*w, roomrow*scale*h,
                                        scale*w, scale*h))
                        self.invisibleBlocks.append(newBlock)
                    self.tiles.add(newTile)
                self.tileArray[roomrow][roomcol] = newTile
        return self
Beispiel #10
0
B = 120.0  #Pixel min/max encourages pixels to be in the range of [-B, +B]

alphaNormLossWeight = 0.0001
TVNormLossWeight = args.tvnorm_weight
styleLossWeight = args.style_weight
contentLossWeight = args.content_weight

learningRate = args.learning_rate
numIters = args.train_iters
showEveryN = 500
##################

with tf.Session() as sess:
    inputTensor = tf.placeholder(
        tf.float32, shape=[None, imageShape[0], imageShape[1], imageShape[2]])
    contentImage = np.array(utils.loadImage(contentPath, imageShape))
    styleImage = utils.loadImage(stylePath, imageShape)
    styleBalanceImage = utils.loadImage(contentPath, imageShape)
    model = net.Vgg19()
    model.build(inputTensor, imageShape)
    contentData = eval('sess.run(model.' + contentLayer +
                       ',feed_dict={inputTensor:contentImage})')
    for styleLayer in styleLayers:
        styleData[styleLayer] = np.array(
            eval('sess.run(model.' + styleLayer +
                 ',feed_dict={inputTensor:styleImage})'))


def buildStyleLoss(model):
    totalStyleLoss = []
    for index, styleLayer in enumerate(styleLayers):
Beispiel #11
0
def viewSynthesisResults(arguments):
    results = pickle.load(open(arguments.name,'rb'))
    print " [+] Loaded %d synthesis results."%(len(results))

    interestingExtrapolations = [7,
                                 #14,
                                 17,
                                 29,
                                 #35,
                                 52,
                                 57,
                                 63,
                                 70,
                                 72,
                                 88,
                                 #99]
                                 ]
    interestingExtrapolations = [(16,12),#*
                                 #(17,0),
                                 (18,0),#*
                                 #(22,0),
                                 #(23,0),
                                 #(29,12),
                                 #(31,27),
                                 (34,0),#*
                                 #(36,0),
                                 #(38,12),
                                 (39,0),#*
                                 #(41,1),
                                 #(51,1),
                                 #(52,12),
                                 #(57,0),
                                 #(58,0),
                                 (60,0),#*
                                 #(63,0),
                                 (66,2),#*
                                 (71,1),#*
                                 #(72,0),
        #(73,0),
                                 #(74,10),
                                 #(75,5),
                                 #(79,0),
                                 #(85,1),
                                 (86,0),#*
                                 #(88,0),
                                 (90,2),#*
                                 #(92,0),
                                 #(95,8)
    ]
    
    #interestingExtrapolations = list(range(100))
                                 

    latex = []
    extrapolationMatrix = []

    programFeatures = {}

    for expertIndex in list(range(100)):# + [101]:
        if arguments.extrapolate:
            if not any([ e == expertIndex or isinstance(e,tuple) and e[0] == expertIndex
                         for e in interestingExtrapolations ]):
                continue
            
        
        f = 'drawings/expert-%d.png'%expertIndex
        parse = getGroundTruthParse(f)
        if parse == None:
            print "No ground truth for %d"%expertIndex
            assert False
            
        relevantResults = [ r for r in results if r.job.originalDrawing == f and r.cost != None ]
        if relevantResults == []:
            print "No synthesis result for %s"%f
            result = None
        else:
            result = min(relevantResults, key = lambda r: r.cost)
            equallyGoodResults = [ r for r in relevantResults if r.cost <= result.cost + 1 ]
            if len(equallyGoodResults) > 1:
                print "Got %d results for %d"%(len(equallyGoodResults),expertIndex)

            programs = [ r.program.fixReflections(result.job.parse.canonicalTranslation()).removeDeadCode()
                         for r in equallyGoodResults ]

        if result == None and arguments.extrapolate:
            print "Synthesis failure for %s"%f
            continue

        print " [+] %s"%f
        print "\t(synthesis time: %s)"%(result.time if result else None)
        print

        if arguments.debug:
            print result.source

        if result != None:
            syntaxTree = result.program
            syntaxTree = syntaxTree.fixReflections(result.job.parse.canonicalTranslation())
            print syntaxTree
            print syntaxTree.features()
            print syntaxTree.convertToPython()
            print syntaxTree.convertToSequence()
            #showImage(fastRender(syntaxTree.convertToSequence()) + loadImage(f)*0.5 + fastRender(result.parse))
            programFeatures[f] = syntaxTree.features()

        extrapolations = []
        if arguments.extrapolate:
            syntaxTree = syntaxTree.explode()
            trace = syntaxTree.convertToSequence().removeDuplicates()
            print "original trace:"
            print trace
            originalUndesirability = parse.undesirabilityVector()
            print "original undesirability",originalUndesirability

            framedExtrapolations = []
            extrapolationGenerators = [ program.explode().extrapolations() for program in programs ]
            for e in interleaveGenerators(extrapolationGenerators):
                t = e.convertToSequence().removeDuplicates()
                newUndesirability = t.undesirabilityVector()
                if (newUndesirability > originalUndesirability).sum() > 0: continue
                if t.canonicalTranslation() == trace.canonicalTranslation(): continue
                if any([t.canonicalTranslation() == o.canonicalTranslation() for o in extrapolations ]): continue
                extrapolations.append(t)

                framedExtrapolations.append(1 - frameImageNicely(t.draw(adjustCanvasSize = True)))

                if len(framedExtrapolations) > 30:
                    break
                
            if framedExtrapolations != []:
                for crap in interestingExtrapolations:
                    if isinstance(crap,tuple) and crap[0] == expertIndex:
                        print "Just taking the %d extrapolation..."%(crap[1])
                        framedExtrapolations = [framedExtrapolations[crap[1]]]
                        break
                
                        
                if arguments.debug:
                    framedExtrapolations = [loadImage(f), syntaxTree.convertToSequence().draw()] + framedExtrapolations
                else:
                    framedExtrapolations = [frameImageNicely(loadImage(f))] + framedExtrapolations
                a = np.zeros((256*len(framedExtrapolations),256))
                for j,e in enumerate(framedExtrapolations):
                    a[j*256:(1+j)*256,:] = 1 - e
                    a[j*256,:] = 0.5
                    a[(1+j)*256-1,:] = 0.5
                a[0,:] = 0.5
                a[:,0] = 0.5
                a[:,255] = 0.5
                a[256*len(framedExtrapolations)-1,:] = 0.5
                a = 255*a
                # to show the first one
                #a = a[:(256*2),:]
                extrapolationMatrix.append(a)
                print "Saving extrapolation column to",'extrapolations/expert-%d-extrapolation.png'%expertIndex
                saveMatrixAsImage(a,'extrapolations/expert-%d-extrapolation.png'%expertIndex)

            
        
        if not arguments.extrapolate:
            rightEntryOfTable = '''
        \\begin{minipage}{10cm}
        \\begin{verbatim}
%s
        \\end{verbatim}
\\end{minipage}
'''%(syntaxTree.pretty() if result != None else "Solver timeout")
        else:
            rightEntryOfTable = ""
        if False and extrapolations != [] and arguments.extrapolate:
            
            #print e
            rightEntryOfTable = '\\includegraphics[width = 5cm]{../TikZ/extrapolations/expert-%d-extrapolation.png}'%expertIndex
        if rightEntryOfTable != "":
            parseImage = '\\includegraphics[width = 5cm]{../TikZ/drawings/expert-%d-parses/0.png}'%expertIndex
            if not os.path.exists('drawings/expert-%d-parses/0.png'%expertIndex):
                parseImage = "Sampled no finished traces."
            latex.append('''
            \\begin{tabular}{lll}
    \\includegraphics[width = 5cm]{../TikZ/drawings/expert-%d.png}&
            %s&
    %s
    \\end{tabular}        
            '''%(expertIndex, parseImage, rightEntryOfTable))
            print

    if arguments.latex:
        latex = '%s'%("\\\\\n".join(latex))
        name = "extrapolations.tex" if arguments.extrapolate else "synthesizerOutputs.tex"
        with open('../TikZpaper/%s'%name,'w') as handle:
            handle.write(latex)
        print "Wrote output to ../TikZpaper/%s"%name

    if arguments.similarity:
        analyzeFeatures(programFeatures)

    if arguments.extrapolate:
        #}make the big matrix
        bigMatrix = np.zeros((max([m.shape[0] for m in extrapolationMatrix ]),256*len(extrapolationMatrix)))
        for j,r in enumerate(extrapolationMatrix):
            bigMatrix[0:r.shape[0],256*j:256*(j+1)] = r
        saveMatrixAsImage(bigMatrix,'extrapolations/allTheExtrapolations.png')
Beispiel #12
0
    def initialize(self, scale=3):
        if self.initialized:
            return self
        self.initialized = True
        # load all the tiles
        self.tilefile = loadImage(self.tilefile,
                                  self.imagefolder,
                                  colorkey=None)
        mapfile = open(os.path.join(self.datafolder, self.tilemap), 'r')
        maplines = mapfile.read().splitlines()
        self.dict = {}
        for x in maplines:
            mapping = [s.strip() for s in x.split(',')]
            key = mapping[6]
            self.dict[key] = {}
            w, h = int(mapping[2]), int(mapping[3])
            row, col = int(mapping[4]), int(mapping[5])
            self.dict[key]['name'] = mapping[0]
            self.dict[key]['file'] = mapping[1]
            self.dict[key]['width'] = w
            self.dict[key]['height'] = h
            self.dict[key]['row'] = row
            self.dict[key]['col'] = col
            self.dict[key]['roomkey'] = mapping[6]
            self.dict[key]['wall'] = mapping[7] == 'wall'

            # find image for this tile
            #w, h = self.dict[key]['width'], self.dict[key]['height']
            #row, col = self.dict[key]['row'], self.dict[key]['col']
            image = pygame.Surface((w, h))
            image.blit(self.tilefile, (0, 0), ((col * w, row * h), (w, h)))
            image = pygame.transform.scale(image, (scale * w, scale * h))
            letter = self.dict[key]['roomkey']
            if letter in '12346':
                w, h = image.get_size()
                image.set_colorkey(image.get_at((w / 2, h / 2)))
            elif letter in '578':
                image.set_colorkey(image.get_at((0, 0)))
            self.dict[key]['image'] = image

        # tile the room
        roomfile = open(os.path.join(self.datafolder, self.room), 'r')
        roomrows = roomfile.read().splitlines()
        self.tiles = pygame.sprite.RenderPlain()
        self.tileArray = [[None for x in range(len(roomrows[0]))]
                          for y in range(len(roomrows))]
        for roomrow, keys in enumerate(roomrows):
            for roomcol, letter in enumerate(keys):
                if letter == '.':
                    newTile = EmptyTile()
                else:
                    data = self.dict[letter]
                    newTile = Tile(data['image'],
                                   (roomcol * scale * w, roomrow * scale * h),
                                   data['wall'])
                    if data['wall']:
                        newBlock = InvisibleBlock(
                            pygame.Rect(roomcol * scale * w,
                                        roomrow * scale * h, scale * w,
                                        scale * h))
                        self.invisibleBlocks.append(newBlock)
                    self.tiles.add(newTile)
                self.tileArray[roomrow][roomcol] = newTile
        return self
Beispiel #13
0
def analyzeFeatures(featureMaps):
    from sklearn.decomposition import PCA, NMF
    from sklearn import preprocessing
    from sklearn.manifold import MDS
    import matplotlib.pyplot as plot
    import matplotlib.image as image

    # collect together a whole of the different names for features
    featureNames = list(set([k for f in featureMaps.values() for k in f]))

    imageNames = featureMaps.keys()

    # Convert feature maps into vectors
    featureVectors = [[featureMaps[k].get(name, 0) for name in featureNames]
                      for k in imageNames]

    print "Feature vectors:"
    for j, n in enumerate(imageNames):
        print n
        print featureMaps[n]
        print featureVectors[j]

    # Figure out things that are close / far as measured by different metrics
    percentile = 80
    imageDistances = learnedDistanceMatrix(None)
    numberOfPrograms = len(featureVectors)
    programDistances = np.zeros((numberOfPrograms, numberOfPrograms))
    featureVectors = preprocessing.scale(np.array(featureVectors))
    for j in range(numberOfPrograms):
        for k in range(numberOfPrograms):
            programDistances[j, k] = (
                (featureVectors[j, :] - featureVectors[k, :]) *
                (featureVectors[j, :] - featureVectors[k, :])).sum()
    smallDistance = np.percentile(programDistances, 100 - percentile)
    bigDistance = np.percentile(programDistances, percentile)
    closePrograms = set([(n1, n2) for j, n1 in enumerate(imageNames)
                         for k, n2 in enumerate(imageNames)
                         if n1 < n2 and programDistances[j, k] < smallDistance
                         ])
    farPrograms = set([(n1, n2) for j, n1 in enumerate(imageNames)
                       for k, n2 in enumerate(imageNames)
                       if n1 < n2 and programDistances[j, k] > bigDistance])
    smallDistance = np.percentile(imageDistances, 100 - percentile)
    bigDistance = np.percentile(imageDistances, percentile)
    imageNames = ["drawings/expert-%d.png" % j for j in range(100)]
    closeImages = set([(n1, n2) for j, n1 in enumerate(imageNames)
                       for k, n2 in enumerate(imageNames)
                       if n1 < n2 and imageDistances[j, k] < smallDistance])
    farImages = set([(n1, n2) for j, n1 in enumerate(imageNames)
                     for k, n2 in enumerate(imageNames)
                     if n1 < n2 and imageDistances[j, k] > bigDistance])
    programOptions = [(closePrograms, 'close in program space'),
                      (farPrograms, 'distant in program space')]
    imageOptions = [(closeImages, 'close in image space'),
                    (farImages, 'far in image space')]
    for programSet, programName in programOptions:
        for imageSet, imageName in imageOptions:
            overlap = programSet & imageSet
            print programName, '&', imageName, 'have overlap', len(overlap)
            overlap = list(sorted(list(overlap)))
            indices = np.random.choice(range(len(overlap)),
                                       size=min(100, len(overlap)),
                                       replace=False)
            overlap = [overlap[j] for j in indices]

            matrix = 1 - np.concatenate([
                np.concatenate((loadImage(n1), loadImage(n2)), axis=0)
                for n1, n2 in overlap
            ],
                                        axis=1)
            saveMatrixAsImage(matrix * 255,
                              "similarity/%s%s.png" % (programName, imageName))

    assert False

    for algorithm in [0, 1, 2]:
        if algorithm == 0:
            learner = PCA()
            transformedFeatures = learner.fit_transform(
                preprocessing.scale(np.array(featureVectors)))
            print learner.explained_variance_ratio_
        if algorithm == 1:
            learner = NMF(2)
            transformedFeatures = learner.fit_transform(
                preprocessing.scale(np.array(featureVectors), with_mean=False))
        if algorithm == 2:
            imageNames = ["drawings/expert-%d.png" % j for j in range(100)]
            distances = learnedDistanceMatrix(map(loadImage, imageNames))
            learner = MDS(dissimilarity='precomputed')
            transformedFeatures = learner.fit_transform(distances)

        print transformedFeatures
        maximumExtent = max([
            transformedFeatures[:, 0].max() - transformedFeatures[:, 0].min(),
            transformedFeatures[:, 1].max() - transformedFeatures[:, 1].min()
        ])
        print maximumExtent
        w = 0.1 * maximumExtent

        if algorithm < 2:
            print learner.components_
            for dimension in range(2):
                coefficients = learner.components_[dimension]
                print "Dimension %d:" % (dimension + 1)
                for j, n in enumerate(featureNames):
                    print n, '\t', learner.components_[dimension, j]
                print

        showProbability = []
        for j in range(len(imageNames)):
            overlapping = 0
            for k in range(len(imageNames)):
                if j == k: continue
                d = abs(transformedFeatures[j, :2] -
                        transformedFeatures[k, :2])
                if d[0] < 2 * w and d[1] < 2 * w:
                    overlapping += 1

            showProbability.append(1.0 / (1 + overlapping * 0.3))

        for index in range(50):
            f, a = plot.subplots()
            imageIsShown = [random.random() < sp for sp in showProbability]
            coolImages = [38, 39, 12, 26, 46, 47, 76, 71, 75, 80, 89]
            for coolImage in coolImages:
                coolImage = 'drawings/expert-%d.png' % coolImage
                for j, imageName in enumerate(imageNames):
                    if imageName == coolImage:
                        imageIsShown[j] = True
                        break

            imageCoordinates = [
                transformedFeatures[j, :2] for j in range(len(imageNames))
            ]
            imageCoordinates = diffuseImagesOutwards(imageCoordinates, w,
                                                     imageIsShown)

            for j, imageName in enumerate(imageNames):
                if not imageIsShown[j]: continue

                i = 1 - image.imread(imageName)
                i = 1 - removeBorder(i)
                x = imageCoordinates[j][0]
                y = imageCoordinates[j][1]

                a.imshow(i,
                         aspect='auto',
                         extent=(x - w, x + w, y - w, y + w),
                         zorder=-1,
                         cmap=plot.get_cmap('Greys'))
                a.arrow(x,
                        y,
                        transformedFeatures[j, 0] - x,
                        transformedFeatures[j, 1] - y,
                        head_width=0.04,
                        head_length=0.1,
                        fc='k',
                        ec='k',
                        zorder=-2)

            a.scatter(transformedFeatures[:, 0], transformedFeatures[:, 1])
            a.get_yaxis().set_visible(False)
            a.get_xaxis().set_visible(False)

            n = ['PCA', 'NMF', 'MDS'][algorithm]
            plot.savefig('%s/%s%d.png' % (n, n, index), bbox_inches='tight')
Beispiel #14
0
    #            if x != y]
    # for (x,y),d in zip(indexes, distances):
    #     matrix[x,y] += d

    # matrix = matrix + matrix.T
    # print "MATRIX:"
    # print matrix.tolist()
    # return matrix


if __name__ == '__main__':
    worker = RecognitionModel(DummyArguments())
    worker.loadDistanceCheckpoint("checkpoints/distance.checkpoint")

    imageNames = ['drawings/expert-%d.png' % j for j in range(100)]
    images = [loadImage(n) for n in imageNames]
    jobs = [(x, y) for j, x in enumerate(images) for k, y in enumerate(images)
            if j != k]
    distances = []
    while jobs != []:
        batch = jobs[:100]
        jobs = jobs[100:]

        print "%d jobs remaining" % len(jobs)

        result = worker.learnedDistances(np.array([x for x, _ in batch]),
                                         np.array([x for _, x in batch]))
        for j in range(len(batch)):
            distances.append(result[j, 1] + result[j, 0])

    matrix = np.zeros((len(images), len(images)))