Esempio n. 1
0
  def testSelectBackTrack(self):
    objset = sg.ObjectSet(n_epoch=100)
    objset.add(sg.Object(when='now'), epoch_now=5)
    epoch_now = 10
    l1 = len(objset.select(epoch_now, when='latest', n_backtrack=4))
    self.assertEqual(l1, 0)

    objset = sg.ObjectSet(n_epoch=100)
    objset.add(sg.Object(when='now'), epoch_now=5)
    epoch_now = 10
    l1 = len(objset.select(epoch_now, when='latest', n_backtrack=5))
    self.assertEqual(l1, 1)
Esempio n. 2
0
  def testDecodeRenderPool(self):
    img_size = 112
    grid_size = 7
    prefs = constants.get_prefs(grid_size)

    loc_xy = sg.Loc([0.8, 0.3])

    n_epoch = 5
    objset = sg.ObjectSet(n_epoch=n_epoch)
    obj = sg.Object([loc_xy, sg.Shape('square'), sg.Color('blue')],
                    when='now')
    objset.add(obj, epoch_now=0)

    movie = sg.render(objset, img_size=img_size)
    frame = movie.sum(axis=-1, keepdims=True)

    in_imgs = tf.placeholder('float', [None, img_size, img_size, 1])
    out = tf.contrib.layers.avg_pool2d(in_imgs, 16, 16)

    with tf.Session() as sess:
      out_ = sess.run(out, feed_dict={in_imgs: frame})

    out_ = np.reshape(out_, (n_epoch, -1))
    out_ = (out_.T / (1e-7 + out_.sum(axis=1))).T
    loc_decoded = np.dot(out_, prefs)[0]
    print('Input loc ' + str(loc_xy))
    print('Decoded loc' + str(loc_decoded))
    dist = ((loc_decoded[0] - loc_xy.value[0])**2 +
            (loc_decoded[1] - loc_xy.value[1])**2)
    self.assertLess(dist, 0.01)
Esempio n. 3
0
    def generate_objset(self, n_epoch, n_distractor=1, average_memory_span=2):
        """Guess objset for all n_epoch.

    Mathematically, the average_memory_span is n_max_backtrack/3

    Args:
      n_epoch: int, total number of epochs
      n_distractor: int, number of distractors to add
      average_memory_span: int, the average number of epochs by which an object
        need to be held in working memory, if needed at all

    Returns:
      objset: full objset for all n_epoch
    """
        n_max_backtrack = int(average_memory_span * 3)
        objset = sg.ObjectSet(n_epoch=n_epoch, n_max_backtrack=n_max_backtrack)

        # Guess objects
        # Importantly, we generate objset backward in time
        epoch_now = n_epoch - 1
        while epoch_now >= 0:
            for _ in range(n_distractor):
                objset.add_distractor(epoch_now)  # distractor
            objset = self.guess_objset(objset, epoch_now)
            epoch_now -= 1

        return objset
Esempio n. 4
0
  def testAddIfExist(self):
    objset = sg.ObjectSet(n_epoch=1)
    epoch_now = 0
    objset.add(sg.Object(when='now'), epoch_now, add_if_exist=False)
    objset.add(sg.Object(when='now'), epoch_now, add_if_exist=False)

    self.assertEqual(len(objset), 1)
Esempio n. 5
0
  def testAddLast1(self):
    objset = sg.ObjectSet(n_epoch=100)
    objset.add(sg.Object(when='now'), epoch_now=0)
    objset.add(sg.Object(when='now', deletable=True), epoch_now=1)
    objset.add(sg.Object(when='last1'), epoch_now=2, add_if_exist=False)

    self.assertEqual(len(objset), 1)
Esempio n. 6
0
  def testSelectNow(self):
    objset = sg.ObjectSet(n_epoch=1)
    epoch_now = 0
    objset.add(sg.Object(when='now'), epoch_now)
    subset = objset.select(epoch_now, when='now')

    self.assertEqual(len(subset), 1)
Esempio n. 7
0
 def testGetGuessObjset(self):
     objset = sg.ObjectSet(n_epoch=10)
     objs = tg.Select()
     task = tg.Task(tg.GetColor(objs))
     epoch_now = 1
     objset = task.guess_objset(objset, epoch_now)
     l = len(objset.select(epoch_now, when='now'))
     self.assertEqual(1, l)
Esempio n. 8
0
  def testDeletable(self):
    objset = sg.ObjectSet(n_epoch=1)
    epoch_now = 0
    objset.add(sg.Object(when='now', deletable=True), epoch_now)
    objset.add(sg.Object(when='now'), epoch_now, add_if_exist=True)

    # The first object should have been deleted
    self.assertEqual(len(objset), 1)
Esempio n. 9
0
 def testSelectGetExpectedInputShouldBeTrue2(self):
     objset = sg.ObjectSet(n_epoch=10)
     select = tg.Select(color=sg.Color('red'), when='now')
     should_be = [sg.Object([sg.Shape('circle')])]
     objset, loc, color, space = select.get_expected_input(should_be,
                                                           objset,
                                                           epoch_now=1)
     objs = select(objset, epoch_now=1)
     self.assertEqual(objs[0].shape, sg.Shape('circle'))
Esempio n. 10
0
 def testSelectGetExpectedInputShouldBeEmpty1(self):
     objset = sg.ObjectSet(n_epoch=10)
     select = tg.Select(color=sg.Color('red'), when='now')
     should_be = []
     objset, loc, color, space = select.get_expected_input(should_be,
                                                           objset,
                                                           epoch_now=1)
     objs = select(objset, epoch_now=1)
     self.assertFalse(objs)
Esempio n. 11
0
  def testSelectLast(self):
    objset = sg.ObjectSet(n_epoch=2)
    objset.add(sg.Object(when='now'), epoch_now=0)
    objset.add(sg.Object(when='now'), epoch_now=1)
    objset.add(sg.Object(when='now'), epoch_now=1, add_if_exist=True)

    epoch_now = 1
    self.assertEqual(2, len(objset.select(epoch_now, when='latest')))
    self.assertEqual(1, len(objset.select(epoch_now, when='last1')))
Esempio n. 12
0
 def testSelectGetExpectedInputShouldBeTrue1(self):
     objset = sg.ObjectSet(n_epoch=10)
     select = tg.Select(color=sg.Color('red'), when='now')
     should_be = [sg.Object([sg.Loc([0.5, 0.5])])]
     objset, loc, color, space = select.get_expected_input(should_be,
                                                           objset,
                                                           epoch_now=1)
     objs = select(objset, epoch_now=1)
     self.assertTupleEqual(objs[0].loc.value, (0.5, 0.5))
Esempio n. 13
0
 def testBasicIsSameGuessObject(self):
     objset = sg.ObjectSet(n_epoch=10)
     objs1 = tg.Select(shape=sg.Shape('square'), when='now')
     attr1 = tg.GetColor(objs1)
     task = tg.Task(tg.IsSame(attr1, sg.Color('red')))
     epoch_now = 1
     objset = task.guess_objset(objset, epoch_now, should_be=True)
     c1 = objset.select(epoch_now, shape=sg.Shape('square'),
                        when='now')[0].color
     self.assertEqual(c1, sg.Color('red'))
Esempio n. 14
0
    def testGetGuessObjsetShouldBe(self):
        objset = sg.ObjectSet(n_epoch=10)
        objs = tg.Select(when='now')
        task = tg.Task(tg.GetColor(objs))

        for epoch_now in range(10):
            color = sg.random_color()
            objset = task.guess_objset(objset, epoch_now, should_be=color)
            o = objset.select(epoch_now, when='now')[0]
            self.assertEqual(color, o.color)
Esempio n. 15
0
def generate_objset(task, n_epoch=30, distractor=True):
    objset = sg.ObjectSet(n_epoch=n_epoch)

    # Guess objects
    for epoch_now in range(n_epoch):
        if distractor:
            objset.add(sg.Object(when='now', deletable=True), epoch_now)
        objset = task.get_expected_input(objset, epoch_now)

    return objset
Esempio n. 16
0
    def testGetShapeOf(self):
        objs1 = tg.Select(color=sg.Color('blue'), when='last1')
        shape = tg.GetShape(objs1)
        objs2 = tg.Select(shape=shape, color=sg.Color('red'), when='now')
        task = tg.Task(tg.Exist(objs2))

        n_epoch = 5
        objset = sg.ObjectSet(n_epoch=n_epoch)
        for i_epoch in range(n_epoch):
            objset = task.guess_objset(objset, i_epoch)
Esempio n. 17
0
  def testAddWhenNone(self):
    objset = sg.ObjectSet(n_epoch=100, n_max_backtrack=5)
    objset.add(sg.Object(when=None), epoch_now=0)
    self.assertEqual(objset.last_added_obj.epoch, [0, 100])

    l1 = len(objset.select(epoch_now=10, when='now'))
    l2 = len(objset.select(epoch_now=10, when='latest'))
    l3 = len(objset.select(epoch_now=10, when='last1'))
    self.assertEqual(l1, 1)
    self.assertEqual(l2, 1)
    self.assertEqual(l3, 1)
Esempio n. 18
0
    def testExistColorOfGuessObjset(self):
        objs1 = tg.Select(shape=sg.Shape('circle'), when='last1')
        color = tg.GetColor(objs1)
        objs2 = tg.Select(color=color, shape=sg.Shape('square'), when='now')
        task = tg.Task(tg.Exist(objs2))

        n_epoch = 10
        objset = sg.ObjectSet(n_epoch=n_epoch)
        objset = task.guess_objset(objset, 1, should_be=True)
        objset = task.guess_objset(objset, 3, should_be=False)
        self.assertTrue(task(objset, 1))
        self.assertFalse(task(objset, 3))
Esempio n. 19
0
 def testSelectGetExpectedInputShouldBeTrue3(self):
     objset = sg.ObjectSet(n_epoch=10)
     select = tg.Select(color=sg.Color('red'), when='now')
     should_be = [sg.Object(when='now')]
     objset, loc, color, space = select.get_expected_input(should_be,
                                                           objset,
                                                           epoch_now=1)
     objset, loc, color, space = select.get_expected_input(should_be,
                                                           objset,
                                                           epoch_now=1)
     objs = select(objset, epoch_now=1)
     self.assertEqual(len(objs), 1)
Esempio n. 20
0
  def testSelectNowLoc(self):
    objset = sg.ObjectSet(n_epoch=1)
    epoch_now = 0
    loc = sg.Loc([0.3, 0.3])
    objset.add(sg.Object([loc], when='now'), epoch_now)
    space1 = sg.Space([(0.2, 0.4), (0.1, 0.5)])
    space2 = sg.Space([(0.5, 0.7), (0.1, 0.5)])
    subset1 = objset.select(epoch_now, space=space1, when='now')
    subset2 = objset.select(epoch_now, space=space2, when='now')

    self.assertEqual(len(subset1), 1)
    self.assertEqual(len(subset2), 0)
Esempio n. 21
0
 def testSelectGetExpectedInputShouldBeTrue4(self):
     objset = sg.ObjectSet(n_epoch=10)
     select = tg.Select(loc=sg.Loc([0.5, 0.5]),
                        when='now',
                        space_type='left')
     should_be = [sg.Object(when='now')]
     objset, loc, color, space = select.get_expected_input(should_be,
                                                           objset,
                                                           epoch_now=1)
     objs = select(objset, epoch_now=1)
     self.assertLess(objs[0].loc.value[0], 0.5)
     self.assertTrue(isinstance(loc, tg.Skip))
Esempio n. 22
0
 def testAddObjectInSpace(self):
   objset = sg.ObjectSet(n_epoch=1)
   space1 = sg.Space([(0, 1), (0, 0.5)])
   space2 = sg.Space([(0, 1), (0.5, 1)])
   space3 = sg.Space([(0, 1), (0, 0.5)])
   epoch_now = 0
   objset.add(sg.Object([space1], when='now'), epoch_now)
   self.assertEqual(len(objset), 1)
   objset.add(sg.Object([space2], when='now'), epoch_now)
   self.assertEqual(len(objset), 2)
   objset.add(sg.Object([space3], when='now'), epoch_now)
   self.assertEqual(len(objset), 2)
Esempio n. 23
0
 def testSelectGetExpectedInputShouldBeEmpty2(self):
     objset = sg.ObjectSet(n_epoch=10)
     select = tg.Select(loc=sg.Loc([0.5, 0.5]),
                        when='now',
                        space_type='left')
     should_be = []
     objset, loc, color, space = select.get_expected_input(should_be,
                                                           objset,
                                                           epoch_now=1)
     objs = select(objset, epoch_now=1)
     self.assertFalse(objs)
     self.assertEqual(len(objset), 1)
Esempio n. 24
0
    def testGetCall(self):
        objset = sg.ObjectSet(n_epoch=10)
        obj1 = tg.Select(color=sg.Color('red'), when='now')
        color1 = tg.GetColor(obj1)

        epoch_now = 1
        color1_eval = color1(objset, epoch_now)
        self.assertEqual(color1_eval, const.INVALID)

        objset.add(sg.Object([sg.Color('red')], when='now'), epoch_now)
        color1_eval = color1(objset, epoch_now)
        self.assertEqual(color1_eval, sg.Color('red'))
Esempio n. 25
0
 def testGoGuessObjset(self):
     objset = sg.ObjectSet(n_epoch=10)
     objs1 = tg.Select(shape=sg.Shape('square'), when='now')
     task = tg.Task(tg.Go(objs1))
     epoch_now = 1
     objset = task.guess_objset(objset, epoch_now)
     l1 = len(objset.select(epoch_now, shape=sg.Shape('square'),
                            when='now'))
     l2 = len(objset.select(epoch_now, shape=sg.Shape('circle'),
                            when='now'))
     self.assertEqual(1, l1)
     self.assertEqual(0, l2)
Esempio n. 26
0
    def testExistColorOfGuessObjsetManyEpochs(self):
        objs1 = tg.Select(shape=sg.Shape('circle'), when='last1')
        color = tg.GetColor(objs1)
        objs2 = tg.Select(color=color, shape=sg.Shape('square'), when='now')
        task = tg.Task(tg.Exist(objs2))

        n_epoch = 100
        objset = sg.ObjectSet(n_epoch=n_epoch)
        for i in range(1, n_epoch)[::-1]:
            should_be = random.random() > 0.5
            objset = task.guess_objset(objset, i, should_be=should_be)
            self.assertEqual(task(objset, i), should_be)
Esempio n. 27
0
  def testShiftObjset(self):
    objset = sg.ObjectSet(n_epoch=2)
    epoch_now = 0
    objset.add(sg.Object(when='now'), epoch_now)

    objset.shift(1)
    self.assertEqual(objset.n_epoch, 3)
    self.assertEqual(objset.set[0].epoch, [1, 2])
    subset = objset.select(1, when='now')
    self.assertEqual(len(subset), 1)

    objset.shift(-2)
    self.assertEqual(len(objset), 0)
Esempio n. 28
0
  def testAddDistractor(self):
    n_epoch = 30
    objset = sg.ObjectSet(n_epoch=n_epoch)

    # Guess objects
    for epoch_now in range(n_epoch):
      objset.add_distractor(epoch_now)  # distractor

    self.assertEqual(len(objset), n_epoch)

    # Make sure all distractors are deleted if they enter the select process
    for epoch_now in range(n_epoch):
      _ = objset.select(epoch_now, when='now')

    self.assertEqual(len(objset), 0)
Esempio n. 29
0
    def testExistColorSpaceGuessObjsetManyEpochs(self):
        objs1 = tg.Select(color=sg.Color('red'), when='last1')
        loc = tg.GetLoc(objs1)
        objs2 = tg.Select(loc=loc,
                          color=sg.Color('blue'),
                          when='now',
                          space_type='left')
        task = tg.Task(tg.Exist(objs2))

        n_epoch = 10
        objset = sg.ObjectSet(n_epoch=n_epoch)
        objset = task.guess_objset(objset, 2, should_be=False)
        objset = task.guess_objset(objset, 1, should_be=True)
        self.assertFalse(task(objset, 2))
        self.assertTrue(task(objset, 1))
Esempio n. 30
0
    def testIsSameGuessObjsetLast1(self):
        objset = sg.ObjectSet(n_epoch=10)
        objs1 = tg.Select(shape=sg.Shape('square'), when='last1')
        objs2 = tg.Select(shape=sg.Shape('circle'), when='last1')
        attr1 = tg.GetColor(objs1)
        attr2 = tg.GetColor(objs2)
        task = tg.Task(tg.IsSame(attr1, attr2))

        objset = task.guess_objset(objset, epoch_now=1, should_be=True)
        self.assertEqual(2, len(objset.select(epoch_now=0, when='now')))
        c1 = objset.select(epoch_now=0, shape=sg.Shape('square'),
                           when='now')[0].color
        c2 = objset.select(epoch_now=0, shape=sg.Shape('circle'),
                           when='now')[0].color
        self.assertEqual(c1, c2)