Example #1
0
    def testGenerateObjset(self):
        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))

        task.generate_objset(n_epoch=20, n_distractor=3, average_memory_span=3)
Example #2
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'))
Example #3
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'))
Example #4
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)
Example #5
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))
Example #6
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)
Example #7
0
  def testMergeObject(self):
    obj1 = sg.Object([sg.Shape('circle')])
    obj2 = sg.Object([sg.Color('red')])

    merged = obj1.merge(obj2)
    self.assertTrue(merged)

    obj1 = sg.Object([sg.Shape('circle')])
    obj2 = sg.Object([sg.Shape('a')])

    merged = obj1.merge(obj2)
    self.assertFalse(merged)
Example #8
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)
Example #9
0
def obj_str(loc=None, color=None, shape=None, when=None, space_type=None):
    """Get a string describing an object with attributes."""

    loc = loc or sg.Loc(None)
    color = color or sg.Color(None)
    shape = shape or sg.Shape(None)

    sentence = []
    if when is not None:
        sentence.append(when)
    if isinstance(color, sg.Attribute) and color.has_value:
        sentence.append(str(color))
    if isinstance(shape, sg.Attribute) and shape.has_value:
        sentence.append(str(shape))
    else:
        sentence.append('object')
    if isinstance(color, Operator):
        sentence += ['with', str(color)]
    if isinstance(shape, Operator):
        if isinstance(color, Operator):
            sentence.append('and')
        sentence += ['with', str(shape)]
    if isinstance(loc, Operator):
        sentence += ['on', space_type, 'of', str(loc)]
    return ' '.join(sentence)
Example #10
0
    def testIsFinal(self):
        objs1 = tg.Select(color=sg.Color('red'),
                          shape=sg.Shape('square'),
                          when='now')
        objs2 = tg.Select(color=sg.Color('blue'),
                          shape=sg.Shape('square'),
                          when='now')
        objs3 = tg.Select(shape=sg.Shape('circle'), when='last1')
        bool1 = tg.Exist(objs1)
        go1 = tg.Go(objs2)
        go2 = tg.Go(objs3)
        op = tg.Switch(bool1, go1, go2, both_options_avail=False)

        self.assertFalse(op.parent)
        self.assertTrue(bool1.parent)
        self.assertTrue(go1.parent)
Example #11
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)
Example #12
0
    def testOperatorSize(self):
        objs1 = tg.Select(shape=sg.Shape('circle'), when='now')
        color1 = tg.GetColor(objs1)
        objs2 = tg.Select(color=color1, shape=sg.Shape('square'), when='now')
        exist = tg.Exist(objs2)
        task = tg.Task(exist)
        self.assertEqual(task.operator_size, 4)

        objs1 = tg.Select(when='last1')
        objs2 = tg.Select(when='now')
        s1 = tg.GetShape(objs1)
        s2 = tg.GetShape(objs2)
        c1 = tg.GetColor(objs1)
        c2 = tg.GetColor(objs2)
        bool1 = tg.And(tg.IsSame(s1, s2), tg.IsSame(c1, c2))
        task = tg.Task(bool1)
        self.assertEqual(task.operator_size, 9)
Example #13
0
    def testAndCompareColorGuessObjset(self):
        objs1 = tg.Select(shape=sg.Shape('circle'), when='last1')
        objs2 = tg.Select(shape=sg.Shape('square'), when='last1')
        objs3 = tg.Select(shape=sg.Shape('triangle'), when='last1')
        objs4 = tg.Select(shape=sg.Shape('hbar'), when='last1')
        a1 = tg.Get('color', objs1)
        a2 = tg.Get('color', objs2)
        a3 = tg.Get('color', objs3)
        a4 = tg.Get('color', objs4)
        task = tg.Task(tg.And(tg.IsSame(a1, a2), tg.IsSame(a3, a4)))

        n_epoch = 10
        for i in range(100):
            epoch_now = random.randint(1, n_epoch - 1)
            objset = sg.ObjectSet(n_epoch=n_epoch)
            should_be = random.random() > 0.5
            objset = task.guess_objset(objset, epoch_now, should_be=should_be)
            self.assertEqual(task(objset, epoch_now), should_be)
Example #14
0
    def testAndGuessObjset(self):
        objs1 = tg.Select(when='last1')
        objs2 = tg.Select(when='now')
        s1 = tg.GetShape(objs1)
        s2 = tg.GetShape(objs2)
        c1 = tg.GetColor(objs1)
        c2 = tg.GetColor(objs2)
        task = tg.Task(tg.And(tg.IsSame(s1, s2), tg.IsSame(c1, c2)))

        objset = sg.ObjectSet(n_epoch=10)
        obj0 = sg.Object([sg.Color('green'), sg.Shape('square')], when='now')
        obj1 = sg.Object([sg.Color('red'), sg.Shape('circle')], when='now')
        objset.add(obj0, epoch_now=0)
        objset.add(obj1, epoch_now=1)
        objset = task.guess_objset(objset, epoch_now=2, should_be=True)
        obj2 = objset.last_added_obj
        self.assertEqual(obj1.color.value, obj2.color.value)
        self.assertEqual(obj1.shape.value, obj2.shape.value)
Example #15
0
  def testHasValue(self):
    loc = sg.Loc(None)
    shape = sg.Shape(None)
    color = sg.Color(None)
    space = sg.Space(None)

    self.assertFalse(loc.has_value)
    self.assertFalse(shape.has_value)
    self.assertFalse(color.has_value)
    self.assertFalse(space.has_value)
Example #16
0
  def testAnotherAttr(self):
    color = sg.Color('red')
    shape = sg.Shape('circle')
    space = sg.Space([(0.3, 0.7), (0.4, 0.7)])
    for i in range(1000):
      self.assertNotEqual(sg.another_attr(color), color)

    for i in range(1000):
      self.assertNotEqual(sg.another_attr(shape), shape)

    for i in range(1000):
      self.assertFalse(space.include(sg.another_attr(space)))
Example #17
0
    def testIsSameGuessObjsetWithDistractors(self):
        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))

        n_epoch = 10
        objset = sg.ObjectSet(n_epoch=n_epoch)
        obj1 = sg.Object(
            [sg.Color('green'), sg.Shape('square')],
            when='now',
            deletable=True)
        objset.add(obj1, 0, add_if_exist=True)
        obj1 = sg.Object([sg.Color('red'), sg.Shape('circle')],
                         when='now',
                         deletable=True)
        objset.add(obj1, 0, add_if_exist=True)
        objset = task.guess_objset(objset, 0, should_be=True)
        objset.add_distractor(1)
        objset = task.guess_objset(objset, 1, should_be=True)
        self.assertTrue(task(objset, 1))
Example #18
0
    def testSelectCall(self):
        objset = sg.ObjectSet(n_epoch=10)
        attrs = [sg.Loc([0.5, 0.5]), sg.Shape('circle'), sg.Color('red')]
        objset.add(sg.Object(attrs, when='now'), epoch_now=1)

        select = tg.Select(color=sg.Color('red'), when='now')
        self.assertTrue(select(objset, epoch_now=1))
        self.assertFalse(select(objset, epoch_now=2))
        select = tg.Select(color=sg.Color('blue'), when='now')
        self.assertFalse(select(objset, epoch_now=1))

        select = tg.Select(shape=sg.Shape('circle'), when='now')
        self.assertTrue(select(objset, epoch_now=1))
        self.assertFalse(select(objset, epoch_now=2))

        select = tg.Select(loc=sg.Loc([0.6, 0.6]),
                           when='now',
                           space_type='left')
        self.assertTrue(select(objset, epoch_now=1))
        select = tg.Select(loc=sg.Loc([0.6, 0.6]),
                           when='now',
                           space_type='top')
        self.assertTrue(select(objset, epoch_now=1))

        select = tg.Select(color=sg.Color('red'), when='last1')
        self.assertFalse(select(objset, epoch_now=1))
        self.assertTrue(select(objset, epoch_now=2))

        select = tg.Select(color=sg.Color('red'), when='latest')
        self.assertTrue(select(objset, epoch_now=1))
        self.assertTrue(select(objset, epoch_now=2))

        attrs = [sg.Loc([0.7, 0.7]), sg.Shape('square'), sg.Color('red')]
        objset.add(sg.Object(attrs, when='now'), epoch_now=1)
        select = tg.Select(color=sg.Color('red'), when='latest')
        self.assertEqual(len(select(objset, epoch_now=1)), 2)
Example #19
0
    def __init__(self,
                 loc=None,
                 color=None,
                 shape=None,
                 when=None,
                 space_type=None):
        super(Select, self).__init__()

        loc = loc or sg.Loc(None)
        color = color or sg.Color(None)
        shape = shape or sg.Shape(None)

        if isinstance(loc, Operator) or loc.has_value:
            assert space_type is not None

        self.loc, self.color, self.shape = loc, color, shape
        self.set_child([loc, color, shape])

        self.when = when
        self.space_type = space_type
Example #20
0
  def testDecodeRender(self):
    img_size = 112
    prefs = constants.get_prefs(img_size)

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

    objset = sg.ObjectSet(n_epoch=1)
    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)
    self.assertEqual(list(movie.shape), [1, img_size, img_size, 3])

    movie = movie.sum(axis=-1)  # sum across color
    movie /= movie.sum()
    movie = np.reshape(movie, (1, -1))

    loc_decoded = np.dot(movie, prefs)[0]
    dist = ((loc_decoded[0] - loc_xy.value[0])**2 +
            (loc_decoded[1] - loc_xy.value[1])**2)
    self.assertLess(dist, 0.01)
Example #21
0
 def testCopyAttributes(self):
   attrs1 = [sg.Space(None), sg.Color('red'), sg.Shape('blue')]
   attrs2 = copy.copy(attrs1)
   attrs2[1] = sg.Color('blue')
   self.assertEqual(attrs1[1], sg.Color('red'))
   self.assertEqual(attrs2[1], sg.Color('blue'))
Example #22
0
 def testHashing(self):
     should_be_dict = dict()
     op = tg.Select(shape=sg.Shape('circle'), when='last1')
     should_be_dict[op] = 1
     self.assertEqual(should_be_dict[op], 1)