def test_backward_case1(): """Backward if non-zero gradient is out of a face.""" vertices = [[0.8, 0.8, 1.], [0.0, -0.5, 1.], [0.2, -0.4, 1.]] faces = [[0, 1, 2]] pxi = 35 pyi = 25 grad_ref = [ [1.6725862, -0.26021874, 0.], [1.41986704, -1.64284933, 0.], [0., 0., 0.], ] renderer = Renderer() renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False renderer.light_intensity_ambient = 1.0 renderer.light_intensity_directional = 0.0 vertices = cp.array(vertices, 'float32') faces = cp.array(faces, 'int32') textures = cp.ones((faces.shape[0], 4, 4, 4, 3), 'float32') grad_ref = cp.array(grad_ref, 'float32') vertices, faces, textures, grad_ref = utils.to_minibatch( (vertices, faces, textures, grad_ref)) vertices = chainer.Variable(vertices) images = renderer.render(vertices, faces, textures) images = cf.mean(images, axis=1) loss = cf.sum(cf.absolute(images[:, pyi, pxi] - 1)) loss.backward() chainer.testing.assert_allclose(vertices.grad, grad_ref, rtol=1e-2)
def test_backward_silhouette(): """Backward if non-zero gradient is out of a face.""" grad_ref = [ [1.6725862, -0.26021874, 0.], [1.41986704, -1.64284933, 0.], [0., 0., 0.], ] vertices = [[0.8, 0.8, 1.], [0.0, -0.5, 1.], [0.2, -0.4, 1.]] faces = [[0, 1, 2]] vertices = cp.array(vertices, 'float32') faces = cp.array(faces, 'int32') grad_ref = cp.array(grad_ref, 'float32') vertices, faces, grad_ref = utils.to_minibatch((vertices, faces, grad_ref)) pxi = 35 pyi = 25 renderer = Renderer() renderer.image_size = 64 renderer.anti_aliasing = False renderer.fill_back = False renderer.perspective = False print(vertices.shape) print(faces.shape) vertices = chainer.Variable(vertices) images = renderer.render_silhouettes(vertices, faces) loss = cf.sum(cf.absolute(images[:, pyi, pxi] - 1)) loss.backward() chainer.testing.assert_allclose(vertices.grad, grad_ref, rtol=1e-2)
def test_forward_ch_1(): """Rendering a teapot without anti-aliasing.""" # load teapot vertices, faces, textures = utils.load_teapot_batch() # create renderer renderer = Renderer() renderer.image_size = 256 renderer.anti_aliasing = False # render images = renderer.render(vertices, faces, textures) images = images.data.get() image = images[2] image = image.transpose((1, 2, 0)) scipy.misc.imsave('./tests/data/test_rasterize1.png', image)
def test_backward_silhouette_ch_2(): """Backward if non-zero gradient is on a face.""" vertices = np.array([[0.8, 0.8, 1.], [-0.5, -0.8, 1.], [0.8, -0.8, 1.]]) faces = np.array([[0, 1, 2]]) pyi = 40 pxi = 50 grad_ref = np.array([ [0.98646867, 1.04628897, 0.], [-1.03415668, -0.10403691, 0.], [3.00094461, -1.55173182, 0.], ]) renderer = Renderer() renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False # Prepare chainer inputs vertices = cp.array(vertices, 'float32') faces = cp.array(faces, 'int32') grad_ref = cp.array(grad_ref, 'float32') vertices, faces, grad_ref = utils.to_minibatch((vertices, faces, grad_ref)) vertices = chainer.Variable(vertices) images = renderer.render_silhouettes(vertices, faces) loss = cf.sum(cf.absolute(images[:, pyi, pxi])) loss.backward() chainer.testing.assert_allclose(vertices.grad, grad_ref, rtol=1e-2)
def __init__(self, filename_obj, filename_ref): super(Model, self).__init__() with self.init_scope(): # load .obj vertices, faces = load_obj(filename_obj) self.vertices = chainer.Parameter(vertices[None, :, :]) self.faces = faces[None, :, :] # create textures texture_size = 2 textures = np.ones((1, self.faces.shape[1], texture_size, texture_size, texture_size, 3), 'float32') self.textures = textures # load reference image self.image_ref = scipy.misc.imread(filename_ref).astype( 'float32').mean(-1) / 255. # setup renderer renderer = Renderer() renderer.anti_aliasing = False self.renderer = renderer
def test_forward_th_2(): """Rendering a teapot without anti-aliasing.""" # load teapot vertices, faces, textures = utils.load_teapot_batch_th() # create renderer renderer = Renderer() renderer.image_size = 256 renderer.anti_aliasing = False # render images_th = renderer.render(cp.asarray(vertices.cpu().numpy()), cp.asarray(faces.cpu().numpy()), cp.asarray(textures.cpu().numpy())) images = images_th.data.get() image = images[2] image = image.transpose((1, 2, 0)) scipy.misc.imsave('./tests/data/test_rasterize_th1.png', image) ref = scipy.misc.imread('./tests/data/test_rasterize1.png') ref = ref.astype('float32') assert np.abs((ref / 255 - image)).mean() < 1e-3
def test_forward_th(): """Whether a silhouette by neural renderer matches that by Blender.""" # load teapot vertices, faces, textures = utils.load_teapot_batch() vertices_th, faces_th, textures_th = utils.load_teapot_batch_th() # create renderer renderer_th = RendererTh() renderer_th.image_size = 256 renderer_th.anti_aliasing = False renderer = Renderer() renderer.fill_back = False renderer.image_size = 256 renderer.anti_aliasing = False images = renderer.render_silhouettes(vertices, faces) images_th = renderer.render_silhouettes( cp.asarray(vertices_th.cpu().numpy()), cp.asarray(faces_th.cpu().numpy())) assert (images_th - images).data.get().sum() == 0
def test_forward_chainer(): """Whether a silhouette by neural renderer matches that by Blender.""" # load teapot vertices, faces, textures = utils.load_teapot_batch() # create renderer renderer = Renderer() renderer.fill_back = False renderer.image_size = 256 renderer.anti_aliasing = False images = renderer.render_silhouettes(vertices, faces) images = images.data.get() image = images[2] # load reference image by blender ref = scipy.misc.imread('./tests/data/teapot_blender.png') ref = ref.astype('float32') ref = (ref.min(-1) != 255).astype('float32') chainer.testing.assert_allclose(ref, image)