Beispiel #1
0
def train_stack(opt):
  if opt["dataset"] == "omniglot":
    stack_size = asl.util.repl(mnist_size, 0, opt["nchannels"])
    ItemType = OmniGlot
    dataloader = asl.util.omniglotloader
    refresh_data = lambda dl: refresh_omniglot(dl, nocuda=opt["nocuda"])
  else:
    stack_size = asl.util.repl(mnist_size, 0, opt["nchannels"])
    ItemType = Mnist
    dataloader = asl.util.mnistloader
    refresh_data = lambda dl: refresh_mnist(dl, nocuda=opt["nocuda"])

  ## Data structures and functions
  class MatrixStack(asl.Type):
    typesize = stack_size

  tracegen = opt["tracegen"]
  trace = tracegen(opt["nitems"], opt["nrounds"])
  push = Push(MatrixStack, ItemType, arch=opt["arch"], arch_opt=opt["arch_opt"])
  pop = Pop(MatrixStack, ItemType, arch=opt["arch"], arch_opt=opt["arch_opt"])
  empty = ConstantNet(MatrixStack,
                      requires_grad=opt["learn_constants"],
                      init=opt["init"])

  def ref_sketch(items, r, runstate):
    return trace(items, r, runstate, push=list_push, pop=list_pop, empty=list_empty)

  class StackSketch(asl.Sketch):
    def sketch(self, items, r, runstate):
      """Example stack trace"""
      return trace(items, r, runstate, push=push, pop=pop, empty=empty)

  stack_sketch = StackSketch([List[OmniGlot]], [OmniGlot])
  nstack = ModuleDict({"push": push,
                       "pop": pop,
                       "empty": empty,
                       "stack_sketch": stack_sketch})

  # Cuda that shit
  asl.cuda(nstack, opt["nocuda"])

  # Hack to add random object as input to traces
  def refresh_with_random(x):
    # import pdb; pdb.set_trace()
    refreshed = refresh_data(x)
    return [refreshed[0], random.Random(0)]

  # Loss
  it = dataloader(opt["batch_size"], normalize=opt["normalize"])
  loss_gen = asl.single_ref_loss(stack_sketch,
                                 ref_sketch,
                                 it,
                                 refresh_with_random,
                                 accum=opt["accum"])
  if opt["learn_constants"]:
    parameters = nstack.parameters()
  else:
    parameters = torch.nn.ModuleList([push, pop]).parameters()

  return common.trainmodel(opt, nstack, loss_gen, parameters)
Beispiel #2
0
def train_set(opt):
  trace = tracegen2(opt["nitems"], opt["nrounds"])
  add = Add(MatrixSet, Mnist, arch=opt["arch"], arch_opt=opt["arch_opt"])
  card = Card(MatrixSet, MatrixInteger, arch=opt["arch"], arch_opt=opt["arch_opt"])
  empty = ConstantNet(MatrixSet,
                      requires_grad=opt["learn_constants"],
                      init=opt["init"])

  def ref_sketch(items, seed, runstate):
    return trace(items, seed, runstate, add=py_add, card=py_card, empty=py_empty_set)

  class SetSketch(asl.Sketch):
    def sketch(self, items, seed, runstate):
      """Example set trace"""
      return trace(items, seed, runstate, add=add, card=card, empty=empty)

  set_sketch = SetSketch([List[Mnist]], [Mnist])
  nset = ModuleDict({"add": add,
                     "card": card,
                     "empty": empty,
                     "set_sketch": set_sketch})

  # Cuda that shit
  asl.cuda(nset, opt["nocuda"])

  @dispatch(int)
  def bridge(pyint):
    width = mnist_size[1]
    height = mnist_size[2]
    res = asl.util.onehot2d(pyint, width, height, opt["batch_size"])
    res = Variable(res, requires_grad=False)
    return MatrixInteger(asl.cuda(res, opt["nocuda"]))

  # Loss (horrible horrible hacking)
  mnistiter = asl.util.mnistloader(opt["batch_size"])

  # Make two random number objects which should stay in concert
  model_random, ref_random = random.Random(0), random.Random(0)
  model_inputs = [mnistiter, model_random]
  ref_inputs = [mnistiter, ref_random]
  
  def refresh_inputs(x):
    mnist_dl, randomthing = x
    a = asl.refresh_iter(mnist_dl, lambda x: Mnist(asl.util.image_data(x)))
    return [a, random.Random(0)]

  loss_gen = asl.single_ref_loss_diff_inp(set_sketch,
                                          ref_sketch,
                                          model_inputs,
                                          ref_inputs,
                                          refresh_inputs,
                                          accum=opt["accum"])
  if opt["learn_constants"]:
    parameters = nset.parameters()
  else:
    parameters = torch.nn.ModuleList([add, card]).parameters()

  return common.trainmodel(opt, nset, loss_gen, parameters)
Beispiel #3
0
def train_queue():
    # Get options from command line
    opt = asl.opt.handle_args(mnist_args)
    opt = asl.opt.handle_hyper(opt, __file__)

    class Enqueue(asl.Function, asl.Net):
        def __init__(self="Enqueue", name="Enqueue", **kwargs):
            asl.Function.__init__(self, [MatrixQueue, Mnist], [MatrixQueue])
            asl.Net.__init__(self, name, **kwargs)

    class Dequeue(asl.Function, asl.Net):
        def __init__(self="Dequeue", name="Dequeue", **kwargs):
            asl.Function.__init__(self, [MatrixQueue], [MatrixQueue, Mnist])
            asl.Net.__init__(self, name, **kwargs)

    nqueue = ModuleDict({
        'enqueue':
        Enqueue(arch=opt.arch, arch_opt=opt.arch_opt),
        'dequeue':
        Dequeue(arch=opt.arch, arch_opt=opt.arch_opt),
        'empty':
        ConstantNet(MatrixQueue)
    })

    queue_sketch = QueueSketch([List[Mnist]], [Mnist], nqueue, ref_queue())
    asl.cuda(queue_sketch)

    # Loss
    mnistiter = asl.util.mnistloader(opt.batch_size)
    loss_gen = asl.sketch.loss_gen_gen(
        queue_sketch, mnistiter, lambda x: Mnist(asl.util.data.train_data(x)))

    # Optimization details
    optimizer = optim.Adam(nqueue.parameters(), lr=opt.lr)
    asl.opt.save_opt(opt)
    if opt.resume_path is not None and opt.resume_path != '':
        asl.load_checkpoint(opt.resume_path, nqueue, optimizer)

    asl.train(loss_gen,
              optimizer,
              maxiters=100000,
              cont=asl.converged(1000),
              callbacks=[
                  asl.print_loss(100), common.plot_empty, common.plot_observes,
                  asl.save_checkpoint(1000, nqueue)
              ],
              log_dir=opt.log_dir)
Beispiel #4
0
def train_queue():
  # Get options from command line
  opt = asl.opt.handle_args(mnist_args)
  opt = asl.opt.handle_hyper(opt, __file__)

  class Enqueue(asl.Function, asl.Net):
    def __init__(self="Enqueue", name="Enqueue", **kwargs):
      asl.Function.__init__(self, [MatrixQueue, Mnist], [MatrixQueue])
      asl.Net.__init__(self, name, **kwargs)

  class Dequeue(asl.Function, asl.Net):
    def __init__(self="Dequeue", name="Dequeue", **kwargs):
      asl.Function.__init__(self, [MatrixQueue], [MatrixQueue, Mnist])
      asl.Net.__init__(self, name, **kwargs)

  nqueue = ModuleDict({'enqueue': Enqueue(arch=opt.arch,
                                          arch_opt=opt.arch_opt),
                       'dequeue': Dequeue(arch=opt.arch,
                                          arch_opt=opt.arch_opt),
                       'empty': ConstantNet(MatrixQueue)})

  queue_sketch = QueueSketch([List[Mnist]], [Mnist], nqueue, ref_queue())
  asl.cuda(queue_sketch)

  # Loss
  mnistiter = asl.util.mnistloader(opt.batch_size)
  loss_gen = asl.sketch.loss_gen_gen(queue_sketch,
                                     mnistiter,
                                     lambda x: Mnist(asl.util.data.train_data(x)))

  # Optimization details
  optimizer = optim.Adam(nqueue.parameters(), lr=opt.lr)
  asl.opt.save_opt(opt)
  if opt.resume_path is not None and opt.resume_path != '':
    asl.load_checkpoint(opt.resume_path, nqueue, optimizer)

  asl.train(loss_gen, optimizer, maxiters=100000,
        cont=asl.converged(1000),
        callbacks=[asl.print_loss(100),
                   common.plot_empty,
                   common.plot_observes,
                   asl.save_checkpoint(1000, nqueue)],
        log_dir=opt.log_dir)
Beispiel #5
0
    def sketch(self, noise):
      """Generate clevr image"""
      # Add object 1
      #FIXME this is a hack
      noisetensor = asl.cuda(Variable(torch.rand((1,) + SMALL_IMG_SIZE)), opt.nocuda)
      nnoise = Noise(expand_to_batch(noisetensor, opt.batch_size))
      (object1, ) = nclevrgen.gen_object(nnoise)
      scene = nclevrgen.empty_scene
      (scene, ) = nclevrgen.add_object(scene, object1)

      # Add object 2
      (object2, ) = nclevrgen.gen_object(nnoise)
      (scene, ) = nclevrgen.add_object(scene, object2)

      # Add object 3
      (object3, ) = nclevrgen.gen_object(nnoise)
      (scene, ) = nclevrgen.add_object(scene, object3)

      (img, ) = nclevrgen.render(scene)
      asl.observe(img, 'rendered_img')
      return (img, )
Beispiel #6
0
        def sketch(self, noise):
            """Generate clevr image"""
            # Add object 1
            #FIXME this is a hack
            noisetensor = asl.cuda(
                Variable(torch.rand((1, ) + SMALL_IMG_SIZE)), opt.nocuda)
            nnoise = Noise(expand_to_batch(noisetensor, opt.batch_size))
            (object1, ) = nclevrgen.gen_object(nnoise)
            scene = nclevrgen.empty_scene
            (scene, ) = nclevrgen.add_object(scene, object1)

            # Add object 2
            (object2, ) = nclevrgen.gen_object(nnoise)
            (scene, ) = nclevrgen.add_object(scene, object2)

            # Add object 3
            (object3, ) = nclevrgen.gen_object(nnoise)
            (scene, ) = nclevrgen.add_object(scene, object3)

            (img, ) = nclevrgen.render(scene)
            asl.observe(img, 'rendered_img')
            return (img, )
Beispiel #7
0
def image_data(data, nocuda=False):
  "Extract image data from mnist (and not classification)"
  return asl.cuda(Variable(data[0]), nocuda)
Beispiel #8
0
def train_clevrgen(opt):
  class AddObject(asl.Function, asl.Net):
    "Add object to scene"
    def __init__(self, name="AddObject", **kwargs):
      asl.Function.__init__(self, [Scene, Object], [Scene])
      asl.Net.__init__(self, name, **kwargs)

  class Render(asl.Function, asl.Net):
    "Render a scene to an image"
    def __init__(self, name="Render", **kwargs):
      asl.Function.__init__(self, [Scene], [Image])
      asl.Net.__init__(self, name, **kwargs)

  class GenObject(asl.Function, asl.Net):
    "Sample an object from noise"
    def __init__(self, name="GenObject", **kwargs):
      asl.Function.__init__(self, [Noise], [Object])
      asl.Net.__init__(self, name, **kwargs)

  nclevrgen = ModuleDict({'add_object': AddObject(arch=opt.arch,
                                                  arch_opt=opt.arch_opt),
                          'render': Render(arch=opt.arch,
                                           arch_opt=opt.arch_opt),
                          'gen_object': Render(arch=opt.arch,
                                               arch_opt=opt.arch_opt),
                          'empty_scene': ConstantNet(Scene)})

  class Discriminate(asl.Function, asl.Net):
    "Is ``Image`` from data distribution?"
    def __init__(self, name="Discriminate", **kwargs):
      asl.Function.__init__(self, [Image], [Probability])
      asl.Net.__init__(self, name, **kwargs)

  discriminate = Discriminate(arch=opt.arch, arch_opt=opt.arch_opt)

  class ClevrGen(asl.Sketch):
    """Generate clevr image from noise"""

    def sketch(self, noise):
      """Generate clevr image"""
      # Add object 1
      #FIXME this is a hack
      noisetensor = asl.cuda(Variable(torch.rand((1,) + SMALL_IMG_SIZE)), opt.nocuda)
      nnoise = Noise(expand_to_batch(noisetensor, opt.batch_size))
      (object1, ) = nclevrgen.gen_object(nnoise)
      scene = nclevrgen.empty_scene
      (scene, ) = nclevrgen.add_object(scene, object1)

      # Add object 2
      (object2, ) = nclevrgen.gen_object(nnoise)
      (scene, ) = nclevrgen.add_object(scene, object2)

      # Add object 3
      (object3, ) = nclevrgen.gen_object(nnoise)
      (scene, ) = nclevrgen.add_object(scene, object3)

      (img, ) = nclevrgen.render(scene)
      asl.observe(img, 'rendered_img')
      return (img, )

  def ref_img_gen(img_iter):
    img = next(img_iter)
    asl.observe(img, 'rendered_img')
    return (img, )

  @dispatch(Image, Image)
  def dist(x, y):
    return discriminate(x.value, y.value)

  clevrgen_sketch = ClevrGen([Noise], [Image])

  # Cuda everything
  asl.cuda(nclevrgen, opt.nocuda)
  asl.cuda(clevrgen_sketch, opt.nocuda)

  # Loss
  img_dl = clevr_img_dl(opt.batch_size, normalize=False)
  loss_gen = asl.ref_loss_gen(clevrgen_sketch,
                              ref_img_gen,
                              img_dl,
                              lambda ten: Image(asl.cuda(Variable(ten),
                                                opt.nocuda)))

  # Optimization details
  parameters = nclevrgen.parameters()
  optimizer = optim.Adam(parameters, lr=opt.lr)
  asl.opt.save_opt(opt)
  if opt.resume_path is not None and opt.resume_path != '':
    asl.load_checkpoint(opt.resume_path, nclevrgen, optimizer)

  asl.train(loss_gen, optimizer, maxiters=100000,
            cont=asl.converged(1000),
            callbacks=[asl.print_loss(100),
                       #  common.plot_empty,
                       common.log_observes,
                       common.plot_observes,
                       #  common.plot_internals,
                       asl.save_checkpoint(1000, nclevrgen)],
            log_dir=opt.log_dir)
Beispiel #9
0
Datei: data.py Projekt: zenna/asl
def image_data(data, nocuda=False):
    "Extract image data from mnist (and not classification)"
    return asl.cuda(Variable(data[0]), nocuda)
Beispiel #10
0
def train_clevrgen(opt):
    class AddObject(asl.Function, asl.Net):
        "Add object to scene"

        def __init__(self, name="AddObject", **kwargs):
            asl.Function.__init__(self, [Scene, Object], [Scene])
            asl.Net.__init__(self, name, **kwargs)

    class Render(asl.Function, asl.Net):
        "Render a scene to an image"

        def __init__(self, name="Render", **kwargs):
            asl.Function.__init__(self, [Scene], [Image])
            asl.Net.__init__(self, name, **kwargs)

    class GenObject(asl.Function, asl.Net):
        "Sample an object from noise"

        def __init__(self, name="GenObject", **kwargs):
            asl.Function.__init__(self, [Noise], [Object])
            asl.Net.__init__(self, name, **kwargs)

    nclevrgen = ModuleDict({
        'add_object':
        AddObject(arch=opt.arch, arch_opt=opt.arch_opt),
        'render':
        Render(arch=opt.arch, arch_opt=opt.arch_opt),
        'gen_object':
        Render(arch=opt.arch, arch_opt=opt.arch_opt),
        'empty_scene':
        ConstantNet(Scene)
    })

    class Discriminate(asl.Function, asl.Net):
        "Is ``Image`` from data distribution?"

        def __init__(self, name="Discriminate", **kwargs):
            asl.Function.__init__(self, [Image], [Probability])
            asl.Net.__init__(self, name, **kwargs)

    discriminate = Discriminate(arch=opt.arch, arch_opt=opt.arch_opt)

    class ClevrGen(asl.Sketch):
        """Generate clevr image from noise"""
        def sketch(self, noise):
            """Generate clevr image"""
            # Add object 1
            #FIXME this is a hack
            noisetensor = asl.cuda(
                Variable(torch.rand((1, ) + SMALL_IMG_SIZE)), opt.nocuda)
            nnoise = Noise(expand_to_batch(noisetensor, opt.batch_size))
            (object1, ) = nclevrgen.gen_object(nnoise)
            scene = nclevrgen.empty_scene
            (scene, ) = nclevrgen.add_object(scene, object1)

            # Add object 2
            (object2, ) = nclevrgen.gen_object(nnoise)
            (scene, ) = nclevrgen.add_object(scene, object2)

            # Add object 3
            (object3, ) = nclevrgen.gen_object(nnoise)
            (scene, ) = nclevrgen.add_object(scene, object3)

            (img, ) = nclevrgen.render(scene)
            asl.observe(img, 'rendered_img')
            return (img, )

    def ref_img_gen(img_iter):
        img = next(img_iter)
        asl.observe(img, 'rendered_img')
        return (img, )

    @dispatch(Image, Image)
    def dist(x, y):
        return discriminate(x.value, y.value)

    clevrgen_sketch = ClevrGen([Noise], [Image])

    # Cuda everything
    asl.cuda(nclevrgen, opt.nocuda)
    asl.cuda(clevrgen_sketch, opt.nocuda)

    # Loss
    img_dl = clevr_img_dl(opt.batch_size, normalize=False)
    loss_gen = asl.ref_loss_gen(
        clevrgen_sketch, ref_img_gen, img_dl,
        lambda ten: Image(asl.cuda(Variable(ten), opt.nocuda)))

    # Optimization details
    parameters = nclevrgen.parameters()
    optimizer = optim.Adam(parameters, lr=opt.lr)
    asl.opt.save_opt(opt)
    if opt.resume_path is not None and opt.resume_path != '':
        asl.load_checkpoint(opt.resume_path, nclevrgen, optimizer)

    asl.train(
        loss_gen,
        optimizer,
        maxiters=100000,
        cont=asl.converged(1000),
        callbacks=[
            asl.print_loss(100),
            #  common.plot_empty,
            common.log_observes,
            common.plot_observes,
            #  common.plot_internals,
            asl.save_checkpoint(1000, nclevrgen)
        ],
        log_dir=opt.log_dir)
Beispiel #11
0
 def bridge(pyint):
   width = mnist_size[1]
   height = mnist_size[2]
   res = asl.util.onehot2d(pyint, width, height, opt["batch_size"])
   res = Variable(res, requires_grad=False)
   return MatrixInteger(asl.cuda(res, opt["nocuda"]))
Beispiel #12
0
def train_clevrlang(opt):
    describe = Describe(arch=opt.arch, arch_opt=opt.arch_opt)
    which_image = WhichImage(arch=opt.arch, arch_opt=opt.arch_opt)

    class PermuteTest(asl.Sketch):
        """Generate clevr image from noise"""
        def sketch(
            self,
            images,
        ):
            # imgj = images[rand_img_id]
            import pdb
            pdb.set_trace()
            (sentence, ) = describe(images, rand_img_id)
            permuted_images = random.sample(images, len(images))
            (score, ) = which_image(permuted_images, sentence)
            asl.observe(score, "score")
            return (score, )

    def target_score_gen(images, rand_img_id):
        asl.observe(rand_img_id, "score")
        return (rand_img_id, )

    permute_test = PermuteTest([Image for _ in range(nimages)], [Integer])

    # Cuda everything
    asl.cuda(describe, opt.nocuda)
    asl.cuda(which_image, opt.nocuda)

    # Generators CLEVR img generator
    img_dl = clevr_img_dl(opt.batch_size, normalize=False)
    chooseimage = infinitesamples[asl.onehot(3, 4, 20)]

    def refresh_clevr(dl):
        "Extract image data and convert tensor to Mnist data type"
        return [asl.refresh_iter(dl, lambda x: Image(x))]

    # Loss
    loss_gen = asl.ref_loss_gen(permute_test, target_score_gen, img_dl,
                                refresh_clevr)

    # Optimization details
    parameters = list(describe.parameters()) + list(which_image.parameters())
    print("LEARNING RATE", opt.lr)
    optimizer = optim.Adam(parameters, lr=opt.lr)
    asl.opt.save_opt(opt)
    if opt.resume_path is not None and opt.resume_path != '':
        asl.load_checkpoint(opt.resume_path, permute_test, optimizer)

    asl.train(
        loss_gen,
        optimizer,
        maxiters=100000,
        cont=asl.converged(1000),
        callbacks=[
            asl.print_loss(100),
            #  common.plot_empty,
            common.plot_observes,
            common.plot_observes,
            #  common.plot_internals,
            asl.save_checkpoint(1000, permute_test)
        ],
        log_dir=opt.log_dir)
Beispiel #13
0
def train_stack(opt):
    if opt["dataset"] == "omniglot":
        stack_size = asl.util.repl(mnist_size, 0, opt["nchannels"])
        ItemType = OmniGlot
        dataloader = asl.util.omniglotloader
        refresh_data = lambda dl: refresh_omniglot(dl, nocuda=opt["nocuda"])
    else:
        stack_size = asl.util.repl(mnist_size, 0, opt["nchannels"])
        ItemType = Mnist
        dataloader = asl.util.mnistloader
        refresh_data = lambda dl: refresh_mnist(dl, nocuda=opt["nocuda"])

    ## Data structures and functions
    class MatrixStack(asl.Type):
        typesize = stack_size

    tracegen = opt["tracegen"]
    trace = tracegen(opt["nitems"], opt["nrounds"])
    push = Push(MatrixStack,
                ItemType,
                arch=opt["arch"],
                arch_opt=opt["arch_opt"])
    pop = Pop(MatrixStack,
              ItemType,
              arch=opt["arch"],
              arch_opt=opt["arch_opt"])
    empty = ConstantNet(MatrixStack,
                        requires_grad=opt["learn_constants"],
                        init=opt["init"])

    def ref_sketch(items, r, runstate):
        return trace(items,
                     r,
                     runstate,
                     push=list_push,
                     pop=list_pop,
                     empty=list_empty)

    class StackSketch(asl.Sketch):
        def sketch(self, items, r, runstate):
            """Example stack trace"""
            return trace(items, r, runstate, push=push, pop=pop, empty=empty)

    stack_sketch = StackSketch([List[OmniGlot]], [OmniGlot])
    nstack = ModuleDict({
        "push": push,
        "pop": pop,
        "empty": empty,
        "stack_sketch": stack_sketch
    })

    # Cuda that shit
    asl.cuda(nstack, opt["nocuda"])

    # Hack to add random object as input to traces
    def refresh_with_random(x):
        # import pdb; pdb.set_trace()
        refreshed = refresh_data(x)
        return [refreshed[0], random.Random(0)]

    # Loss
    it = dataloader(opt["batch_size"], normalize=opt["normalize"])
    loss_gen = asl.single_ref_loss(stack_sketch,
                                   ref_sketch,
                                   it,
                                   refresh_with_random,
                                   accum=opt["accum"])
    if opt["learn_constants"]:
        parameters = nstack.parameters()
    else:
        parameters = torch.nn.ModuleList([push, pop]).parameters()

    return common.trainmodel(opt, nstack, loss_gen, parameters)