def testAttributeEqual(self): color1 = sg.Color('red') color2 = sg.Color('green') color3 = sg.Color('red') self.assertFalse(color1 == color2) self.assertTrue(color1 == color3)
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'))
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)
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'))
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))
def testExistSpaceGuessObjset(self): # When should_be is False, there is a objset = sg.ObjectSet(n_epoch=10) objs1 = tg.Select(color=sg.Color('red'), when='now') loc = tg.GetLoc(objs1) objs2 = tg.Select(loc=loc, color=sg.Color('blue'), when='now', space_type='left') task = tg.Task(tg.Exist(objs2)) for epoch_now in range(1, 10)[::-1]: 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)
def testExistColorSpaceGuessObjset(self): for space_type in ['left', 'right', 'top', 'bottom']: objs1 = tg.Select(color=sg.Color('red'), when='now') loc = tg.GetLoc(objs1) objs2 = tg.Select(loc=loc, color=sg.Color('blue'), when='now', space_type=space_type) task = tg.Task(tg.Exist(objs2)) n_epoch = 100 objset = sg.ObjectSet(n_epoch=n_epoch) for i in range(0, 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)
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)
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)
def testGetTimeCall(self): obj1 = tg.Select(color=sg.Color('red'), when='latest') time1 = tg.GetTime(obj1) n_epoch = 10 objset = sg.ObjectSet(n_epoch=n_epoch, n_max_backtrack=100) epoch_add = 1 time1_eval = time1(objset, epoch_add) self.assertEqual(time1_eval, const.INVALID) objset.add(sg.Object([sg.Color('red')], when='now'), epoch_add) for epoch_now in range(epoch_add, n_epoch - 1): time1_eval = time1(objset, epoch_now) self.assertEqual(time1_eval, epoch_add)
def testGetSpaceGuessObjset(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.GetShape(objs2)) n_epoch = 2 objset = sg.ObjectSet(n_epoch=n_epoch) for i_epoch in range(n_epoch)[::-1]: objset = task.guess_objset(objset, i_epoch) o1 = objset.select(0, color=sg.Color('red'), when='now')[0] o2 = objset.select(1, color=sg.Color('blue'), when='now')[0] self.assertLess(o2.loc.value[0], o1.loc.value[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)
def testExistGuessObjset(self): objset = sg.ObjectSet(n_epoch=10) objs1 = tg.Select(color=sg.Color('red'), when='now') task = tg.Task(tg.Exist(objs1)) epoch_now = 1 objset = task.guess_objset(objset, epoch_now, should_be=True) l1 = len(objset.select(epoch_now, color=sg.Color('red'), when='now')) l2 = len(objset.select(epoch_now, color=sg.Color('blue'), when='now')) self.assertEqual(1, l1) self.assertEqual(0, l2) self.assertTrue(task(objset, epoch_now)) epoch_now = 2 objset = task.guess_objset(objset, epoch_now, should_be=False) l1 = len(objset.select(epoch_now, color=sg.Color('red'), when='now')) self.assertEqual(0, l1) self.assertFalse(task(objset, epoch_now))
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)
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)
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'))
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))
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)
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)))
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)
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)
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))
def testGetGuessObjsetLast1(self): objs1 = tg.Select(color=sg.Color('red'), when='last1') task = tg.Task(tg.GetShape(objs1)) n_epoch = 10 objset = task.generate_objset(n_epoch, n_distractor=0, average_memory_span=2) target = [task(objset, epoch_now) for epoch_now in range(n_epoch)] n_invalid = sum([t == const.INVALID for t in target]) self.assertLessEqual(n_invalid, 1)
def testGetSpaceCall(self): objs0 = tg.Select(color=sg.Color('red'), when='last1') objs1 = tg.Select(loc=tg.GetLoc(objs0), when='now', space_type='left') task1 = tg.Task(tg.Exist(objs1)) objs2 = tg.Select(loc=tg.GetLoc(objs0), when='now', space_type='right') task2 = tg.Task(tg.Exist(objs2)) objs3 = tg.Select(loc=tg.GetLoc(objs0), when='now', space_type='top') task3 = tg.Task(tg.Exist(objs3)) objs4 = tg.Select(loc=tg.GetLoc(objs0), when='now', space_type='bottom') task4 = tg.Task(tg.Exist(objs4)) objset = sg.ObjectSet(n_epoch=2) obj1 = sg.Object([sg.Loc([0.5, 0.5]), sg.Color('red')], when='now') objset.add(obj1, epoch_now=0) obj1 = sg.Object([sg.Loc([0.2, 0.3])], when='now') objset.add(obj1, epoch_now=1) self.assertTrue(task1(objset, epoch_now=1)) self.assertFalse(task2(objset, epoch_now=1)) self.assertTrue(task3(objset, epoch_now=1)) self.assertFalse(task4(objset, epoch_now=1))
def testAverageMemorySpan(self): n_epoch = 1000 average_memory_span = 10 objs1 = tg.Select(color=sg.Color('red'), when='last1') task = tg.Task(tg.GetShape(objs1)) time1 = tg.GetTime(objs1) objset = task.generate_objset(n_epoch, n_distractor=1, average_memory_span=average_memory_span) memory_spans = list() for epoch_now in range(1, n_epoch): time1_val = time1(objset, epoch_now) memory_span = epoch_now - time1_val memory_spans.append(memory_span) avg_mem_span_estimation = sum(memory_spans) * 1.0 / len(memory_spans) self.assertLess(abs(avg_mem_span_estimation - average_memory_span), 2)
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
def manualAverageMemorySpan(self): n_epoch = 6 average_memory_span = 1.67 memory_spans = list() for _ in range(3000): objs1 = tg.Select(color=sg.Color('red'), when='last1') task = tg.Task(tg.GetShape(objs1)) time1 = tg.GetTime(objs1) objset = task.generate_objset( n_epoch, n_distractor=1, average_memory_span=average_memory_span) for epoch_now in range(1, n_epoch): time1_val = time1(objset, epoch_now) memory_span = epoch_now - time1_val memory_spans.append(memory_span) avg_mem_span_estimation = sum(memory_spans) * 1.0 / len(memory_spans) print("avg_mem_span_estimation:", avg_mem_span_estimation)
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)
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)
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'))