Esempio n. 1
0
def visualizeSolutions(solutions, export, tasks=None):

    if tasks is None:
        tasks = list(solutions.keys())
        tasks.sort(key=lambda t: len(t.plan))

    matrix = []
    for t in tasks:
        i = renderPlan(centerTower(t.plan), pretty=True, Lego=True)
        if solutions[t].empty: i = i / 3.
        matrix.append(i)

    # Only visualize if it has something to visualize.
    if len(matrix) > 0:
        matrix = montage(matrix)
        import scipy.misc
        scipy.misc.imsave(export, matrix)
    else:
        eprint("Tried to visualize solutions, but none to visualize.")
Esempio n. 2
0
def dreamOfTowers(grammar, prefix, N=250, make_montage=True):
    request = arrow(ttower, ttower)
    randomTowers = [
        tuple(centerTower(t)) for _ in range(N) for program in
        [grammar.sample(request, maximumDepth=12, maxAttempts=100)]
        if program is not None
        for t in [executeTower(program, timeout=0.5) or []]
        if len(t) >= 1 and len(t) < 100 and towerLength(t) <= 360.
    ]
    matrix = [renderPlan(p, Lego=True, pretty=True) for p in randomTowers]

    # Only visualize if it has something to visualize.
    if len(matrix) > 0:
        import scipy.misc
        if make_montage:
            matrix = montage(matrix)
            scipy.misc.imsave('%s.png' % prefix, matrix)
        else:
            for n, i in enumerate(matrix):
                scipy.misc.imsave(f'{prefix}/{n}.png', i)
    else:
        eprint("Tried to visualize dreams, but none to visualize.")
Esempio n. 3
0
def main(arguments):
    """
    Takes the return value of the `commandlineArguments()` function as input and
    trains/tests the model on a set of tower-building tasks.
    """

    # The below global statement is required since primitives is modified within main().
    # TODO(lcary): use a function call to retrieve and declare primitives instead.
    global primitives

    import scipy.misc

    g0 = Grammar.uniform({
        "new": new_primitives,
        "old": primitives
    }[arguments.pop("primitives")],
                         continuationType=ttower)

    checkpoint = arguments.pop("visualize")
    if checkpoint is not None:
        with open(checkpoint, 'rb') as handle:
            primitives = pickle.load(handle).grammars[-1].primitives
        visualizePrimitives(primitives)
        sys.exit(0)
    checkpoint = arguments.pop("solutions")
    if checkpoint is not None:
        with open(checkpoint, 'rb') as handle:
            solutions = pickle.load(handle).taskSolutions
        visualizeSolutions(solutions, checkpoint + ".solutions.png")
        animateSolutions(checkpoint)
        sys.exit(0)
    checkpoint = arguments.pop("dream")
    if checkpoint is not None:
        with open(checkpoint, 'rb') as handle:
            g = pickle.load(handle).grammars[-1]
        os.system("mkdir  -p data/tower_dreams")
        dreamOfTowers(g, "data/tower_dreams", make_montage=False)
        sys.exit(0)

    tasks = arguments.pop("tasks")
    if tasks == "new":
        tasks = makeSupervisedTasks()
    elif tasks == "old":
        tasks = makeOldSupervisedTasks()
    else:
        assert False

    test, train = testTrainSplit(tasks, arguments.pop("split"))
    eprint("Split %d/%d test/train" % (len(test), len(train)))

    # Make a montage for the paper
    shuffledTrain = list(train)
    shuffledTest = list(test)
    random.shuffle(shuffledTrain)
    shuffledTrain = shuffledTrain + [None] * (60 - len(shuffledTrain))
    random.shuffle(shuffledTest)
    shuffledTest = shuffledTest + [None] * (60 - len(shuffledTest))
    SupervisedTower.exportMany("/tmp/every_tower.png",
                               shuffledTrain + shuffledTest,
                               shuffle=False,
                               columns=10)
    for j, task in enumerate(tasks):
        task.exportImage(f"/tmp/tower_task_{j}.png")
    for k, v in dSLDemo().items():
        scipy.misc.imsave(f"/tmp/tower_dsl_{k}.png", v)
        os.system(
            f"convert /tmp/tower_dsl_{k}.png -channel RGB -negate /tmp/tower_dsl_{k}.png"
        )

    timestamp = datetime.datetime.now().isoformat()
    outputDirectory = "experimentOutputs/towers/%s" % timestamp
    os.system("mkdir -p %s" % outputDirectory)

    os.system("mkdir  -p data/tower_dreams_initial")
    dreamOfTowers(g0, "data/tower_dreams_initial", make_montage=False)

    evaluationTimeout = 0.005
    generator = ecIterator(g0,
                           train,
                           testingTasks=test,
                           outputPrefix="%s/tower" % outputDirectory,
                           evaluationTimeout=evaluationTimeout,
                           **arguments)

    dreamOfTowers(g0, "%s/random_0" % outputDirectory)

    for result in generator:
        continue
        iteration = len(result.learningCurve)
        newTowers = [
            tuple(centerTower(executeTower(frontier.sample().program)))
            for frontier in result.taskSolutions.values() if not frontier.empty
        ]
        try:
            fn = '%s/solutions_%d.png' % (outputDirectory, iteration)
            visualizeSolutions(result.taskSolutions, fn, train)
            eprint("Exported solutions to %s\n" % fn)
            dreamOfTowers(result.grammars[-1],
                          '%s/random_%d' % (outputDirectory, iteration))
        except ImportError:
            eprint("Could not import required libraries for exporting towers.")
        primitiveFilename = '%s/primitives_%d.png' % (outputDirectory,
                                                      iteration)
        visualizePrimitives(result.grammars[-1].primitives, primitiveFilename)
        eprint("Exported primitives to", primitiveFilename)
Esempio n. 4
0
 def k():
     plan = e.evaluate([])(lambda s: (s, []))(0)[1]
     if centerTower(plan) == centerTower(self.plan): return 0.
     return NEGATIVEINFINITY
Esempio n. 5
0
 def logLikelihood(self, e, timeout=None):
     from dreamcoder.domains.tower.tower_common import centerTower
     yh = executeTower(e, timeout)
     if yh is not None and centerTower(yh) == centerTower(self.plan): return 0.
     return NEGATIVEINFINITY