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 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 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 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)
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)
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))
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 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 __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 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 testAddFixLoc(self): loc = [0.2, 0.8] objset = sg.ObjectSet(n_epoch=1) obj = sg.Object([sg.Loc(loc)], when='now') objset.add(obj, epoch_now=0) self.assertEqual(list(obj.loc.value), loc)