def test_calculate_probability_image_given_hypothesis(self): fwm = i3d_vfm.VisionForwardModel(render_size=(200, 200), offscreen_rendering=True, custom_lighting=False) # create a cube, this will be our sample s = Shape(forward_model=fwm, viewpoint=[np.array([np.sqrt(8.0), 0.0, 90.0])], params={'MAX_PIXEL_VALUE': 255.0, 'LL_VARIANCE': 0.1}, parts=[CuboidPrimitive(position=[0.0, 0.0, 0.0], size=[1.0, 1.0, 1.0])]) # observed image image = np.load('test_images/test_image.npy') # calculate logp(image|sample) logp, logp_max = calculate_probability_image_given_hypothesis(image, s) # these are pre-calculated. NOTE these could change if VisionForwardModel changes. self.assertAlmostEqual(logp, -0.60814961737968254) self.assertAlmostEqual(logp_max, 0.0) # calculate logp(blank image|sample) image = np.zeros((1, 200, 200)) logp, logp_max = calculate_probability_image_given_hypothesis(image, s) self.assertAlmostEqual(logp, -1.5146551964550277) self.assertAlmostEqual(logp_max, -0.91822996736329843) # change ll_variance and see if logp_max changes accordingly s.params['LL_VARIANCE'] = 0.01 logp, logp_max = calculate_probability_image_given_hypothesis(image, s) self.assertAlmostEqual(logp_max / 10.0, -0.91822996736329843)
def test_calculate_probability_image_given_hypothesis(self): fwm = i3d_vfm.VisionForwardModel(render_size=(200, 200), offscreen_rendering=True, custom_lighting=False) # create a cube, this will be our sample s = Shape(forward_model=fwm, viewpoint=[np.array([np.sqrt(8.0), 0.0, 90.0])], params={ 'MAX_PIXEL_VALUE': 255.0, 'LL_VARIANCE': 0.1 }, parts=[ CuboidPrimitive(position=[0.0, 0.0, 0.0], size=[1.0, 1.0, 1.0]) ]) # observed image image = np.load('test_images/test_image.npy') # calculate logp(image|sample) logp, logp_max = calculate_probability_image_given_hypothesis(image, s) # these are pre-calculated. NOTE these could change if VisionForwardModel changes. self.assertAlmostEqual(logp, -0.60814961737968254) self.assertAlmostEqual(logp_max, 0.0) # calculate logp(blank image|sample) image = np.zeros((1, 200, 200)) logp, logp_max = calculate_probability_image_given_hypothesis(image, s) self.assertAlmostEqual(logp, -1.5146551964550277) self.assertAlmostEqual(logp_max, -0.91822996736329843) # change ll_variance and see if logp_max changes accordingly s.params['LL_VARIANCE'] = 0.01 logp, logp_max = calculate_probability_image_given_hypothesis(image, s) self.assertAlmostEqual(logp_max / 10.0, -0.91822996736329843)
def test_render_cube_no_lighting(self): # test object part1 = CuboidPrimitive([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]) s = Shape(forward_model=None, parts=[part1]) r = self.fwm_no_lighting.render(s) correct = np.load('test_images/cube_no_lighting.npy') self.assertAlmostEqual(np.sum(np.abs(r - correct)), 0.0) # test changing viewpoint for i, pos in enumerate(self.camera_pos): s.viewpoint = [pos] r = self.fwm_no_lighting.render(s) self.assertAlmostEqual(np.sum(np.abs(r - correct[i])), 0.0)
def test_calculate_similarity_image_given_image(self): fwm = i3d_vfm.VisionForwardModel(render_size=(200, 200), offscreen_rendering=True, custom_lighting=False) # create two samples s1 = Shape(forward_model=fwm, viewpoint=[np.array([np.sqrt(8.0), 0.0, 90.0])], params={ 'MAX_PIXEL_VALUE': 255.0, 'LL_VARIANCE': 0.1 }, parts=[ CuboidPrimitive(position=[0.0, 0.0, 0.0], size=[1.0, 1.0, 1.0]) ]) s2 = Shape(forward_model=fwm, viewpoint=[np.array([np.sqrt(8.0), 0.0, 90.0])], params={ 'MAX_PIXEL_VALUE': 255.0, 'LL_VARIANCE': 0.1 }, parts=[ CuboidPrimitive(position=[0.0, 0.0, 0.0], size=[.5, .5, .5]) ]) # observed image image = np.load('test_images/test_image.npy') # calculate similarity using two samples logp_avg, logp_wavg, logp_best, logp_wbest = calculate_similarity_image_given_image( image, [s1, s2], [np.log(2.0), np.log(1.0)]) # these are pre-calculated. NOTE these could change if VisionForwardModel changes. self.assertAlmostEqual(logp_avg, np.log(0.3549830885871178)) self.assertAlmostEqual(logp_wavg, np.log(0.41810779467381104)) self.assertAlmostEqual(logp_best, np.log(0.58679820659933835)) self.assertAlmostEqual(logp_wbest, np.log(0.72453213773289238)) # calculate similarity from one sample logp_avg, logp_wavg, logp_best, logp_wbest = calculate_similarity_image_given_image( image, [s1], [np.log(2.0)]) # these are pre-calculated. NOTE these could change if VisionForwardModel changes. self.assertAlmostEqual(logp_avg, np.log(0.54435720684719735)) self.assertAlmostEqual(logp_wavg, np.log(0.54435720684719735)) self.assertAlmostEqual(logp_best, np.log(1.0)) self.assertAlmostEqual(logp_wbest, np.log(1.0))
def test_render_cube_custom_lighting(self): # test object part1 = CuboidPrimitive([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]) s = Shape(forward_model=None, parts=[part1]) r = self.fwm_custom_lighting.render(s) correct = np.load('test_images/cube_custom_lighting.npy') self.assertAlmostEqual(np.sum(np.abs(r - correct)), 0.0) # maximum intensity value for custom lighting should be around 174-175. This value is used in our likelihood # calculations; therefore, it is important to make sure that it does not change. self.assertTrue(175.0 > np.max(r) > 174.0) # test changing viewpoint for i, pos in enumerate(self.camera_pos): s.viewpoint = [pos] r = self.fwm_custom_lighting.render(s) self.assertAlmostEqual(np.sum(np.abs(r - correct[i])), 0.0)
def test_render_cube_object_no_lighting(self): # test object part1 = CuboidPrimitive([0.0, 0.0, 0.0], [0.8, 1.0, 0.4]) part2 = CuboidPrimitive([0.6, 0.0, 0.0], [0.4, 0.4, 0.3]) part3 = CuboidPrimitive([0.0, 0.0, 0.50], [0.2, 0.6, 0.6]) s = Shape(forward_model=None, parts=[part1, part2, part3]) r = self.fwm_no_lighting.render(s) correct = np.load('test_images/cube_object_no_lighting.npy') self.assertAlmostEqual(np.sum(np.abs(r - correct)), 0.0)
def test_save_render(self): # test object part1 = CuboidPrimitive([0.0, 0.0, 0.0], [0.8, 1.0, 0.4]) part2 = CuboidPrimitive([0.6, 0.0, 0.0], [0.4, 0.4, 0.3]) part3 = CuboidPrimitive([0.0, 0.0, 0.50], [0.2, 0.6, 0.6]) s = Shape(forward_model=None, parts=[part1, part2, part3]) self.fwm_custom_lighting.save_render('test_images/r.png', s) for i in range(len(self.fwm_custom_lighting.camera_pos)): r = spi.imread('test_images/r_{0:d}.png'.format(i)) correct = spi.imread( 'test_images/cube_object_custom_lighting_{0:d}.png'.format(i)) self.assertAlmostEqual(np.sum(np.abs(r - correct)), 0.0) os.remove('test_images/r_{0:d}.png'.format(i))
def test_render_blank(self): # blank image s = Shape(forward_model=None, parts=[]) r = self.fwm_no_lighting.render(s) self.assertTupleEqual(r.shape, (40, 200, 200)) self.assertAlmostEqual(np.sum(r), 0.0)