def test_backward_case1(self): """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 = nr.Renderer(camera_mode='look_at') renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False vertices = torch.from_numpy(np.array(vertices, np.float32)).cuda() faces = torch.from_numpy(np.array(faces, np.int32)).cuda() grad_ref = torch.from_numpy(np.array(grad_ref, np.float32)).cuda() vertices, faces, grad_ref = utils.to_minibatch((vertices, faces, grad_ref)) vertices.requires_grad = True images = renderer(vertices, faces, mode='silhouettes') loss = torch.sum(torch.abs(images[:, pyi, pxi] - 1)) loss.backward() assert(torch.allclose(vertices.grad, grad_ref, rtol=1e-2))
def test_backward_case2(self): """Backward if non-zero gradient is on a face.""" vertices = [ [0.8, 0.8, 1.], [-0.5, -0.8, 1.], [0.8, -0.8, 1.]] faces = [[0, 1, 2]] pyi = 40 pxi = 50 grad_ref = [ [0.98646867, 1.04628897, 0.], [-1.03415668, - 0.10403691, 0.], [3.00094461, - 1.55173182, 0.], ] renderer = nr.Renderer(camera_mode='look_at') renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False vertices = torch.from_numpy(np.array(vertices, np.float32)).cuda() faces = torch.from_numpy(np.array(faces, np.int32)).cuda() grad_ref = torch.from_numpy(np.array(grad_ref, np.float32)).cuda() vertices, faces, grad_ref = utils.to_minibatch((vertices, faces, grad_ref)) vertices.requires_grad = True images = renderer(vertices, faces, mode='silhouettes') loss = torch.sum(torch.abs(images[:, pyi, pxi])) loss.backward() assert(torch.allclose(vertices.grad, grad_ref, rtol=1e-2))
def test_backward_case1(self): vertices = [ [-0.9, -0.9, 2.], [-0.8, 0.8, 1.], [0.8, 0.8, 0.5]] faces = [[0, 1, 2]] renderer = neural_renderer.Renderer() renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False renderer.camera_mode = 'none' vertices = cp.array(vertices, 'float32') faces = cp.array(faces, 'int32') vertices, faces = utils.to_minibatch((vertices, faces)) vertices = chainer.Variable(vertices) images = renderer.render_depth(vertices, faces) loss = cf.sum(cf.square(images[0, 15, 20] - 1)) loss.backward() grad = vertices.grad.get() grad2 = np.zeros_like(grad) for i in range(3): for j in range(3): eps = 1e-3 vertices2 = vertices.data.copy() vertices2[i, j] += eps images = renderer.render_depth(vertices2, faces) loss2 = cf.sum(cf.square(images[0, 15, 20] - 1)) grad2[i, j] = ((loss2 - loss) / eps).data.get() chainer.testing.assert_allclose(grad, grad2, atol=1e-3)
def test_backward_case1(self): vertices = [ [-0.9, -0.9, 2.], [-0.8, 0.8, 1.], [0.8, 0.8, 0.5]] faces = [[0, 1, 2]] renderer = nr.Renderer(camera_mode='look_at') renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False renderer.camera_mode = 'none' vertices = torch.from_numpy(np.array(vertices, np.float32)).cuda() faces = torch.from_numpy(np.array(faces, np.int32)).cuda() vertices, faces = utils.to_minibatch((vertices, faces)) vertices.requires_grad = True images = renderer(vertices, faces, mode='depth') loss = torch.sum((images[0, 15, 20] - 1)**2) loss.backward() grad = vertices.grad.clone() grad2 = np.zeros_like(grad) for i in range(3): for j in range(3): eps = 1e-3 vertices2 = vertices.clone() vertices2[i, j] += eps images = renderer.render_depth(vertices2, faces) loss2 = torch.sum((images[0, 15, 20] - 1)**2) grad2[i, j] = ((loss2 - loss) / eps).item() assert(np.allclose(grad, grad2, atol=1e-3))
def test_backward_case1(self): vertices = [[-0.9, -0.9, 2.], [-0.8, 0.8, 1.], [0.8, 0.8, 0.5]] faces = [[0, 1, 2]] renderer = neural_renderer.Renderer() renderer.image_size = 64 renderer.anti_aliasing = False renderer.perspective = False renderer.camera_mode = 'none' vertices = cp.array(vertices, 'float32') faces = cp.array(faces, 'int32') vertices, faces = utils.to_minibatch((vertices, faces)) vertices = chainer.Variable(vertices) images = renderer.render_depth(vertices, faces) loss = cf.sum(cf.square(images[0, 15, 20] - 1)) loss.backward() grad = vertices.grad.get() grad2 = np.zeros_like(grad) for i in range(3): for j in range(3): eps = 1e-3 vertices2 = vertices.data.copy() vertices2[i, j] += eps images = renderer.render_depth(vertices2, faces) loss2 = cf.sum(cf.square(images[0, 15, 20] - 1)) grad2[i, j] = ((loss2 - loss) / eps).data.get() chainer.testing.assert_allclose(grad, grad2, atol=1e-3)
def test_backward_case1(self): """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 = nr.Renderer() renderer.camera.position = self.position renderer.camera.image_size = 64 renderer.anti_aliasing = False renderer.camera.perspective = False renderer.light_intensity_ambient = 1.0 renderer.light_intensity_directional = 0.0 vertices = torch.from_numpy(np.array(vertices, dtype=np.float32)).cuda() faces = torch.from_numpy(np.array(faces, dtype=np.int32)).cuda() textures = torch.ones(faces.shape[0], 4, 4, 4, 3, dtype=torch.float32).cuda() grad_ref = torch.from_numpy(np.array(grad_ref, dtype=np.float32)).cuda() vertices, faces, textures, grad_ref = utils.to_minibatch( (vertices, faces, textures, grad_ref)) vertices, faces, textures, grad_ref = vertices.cuda(), faces.cuda( ), textures.cuda(), grad_ref.cuda() vertices.requires_grad = True images = renderer(vertices, faces, textures) images = torch.mean(images, dim=1) loss = torch.sum(torch.abs(images[:, pyi, pxi] - 1)) loss.backward() assert (torch.allclose(vertices.grad, grad_ref, rtol=1e-2))
def test_backward_case2(self): """Backward if non-zero gradient is on a face.""" vertices = [[0.8, 0.8, 1.], [-0.5, -0.8, 1.], [0.8, -0.8, 1.]] faces = [[0, 1, 2]] pyi = 40 pxi = 50 grad_ref = [ [0.98646867, 1.04628897, 0.], [-1.03415668, -0.10403691, 0.], [3.00094461, -1.55173182, 0.], ] renderer = nr.Renderer() renderer.position = self.position renderer.camera.image_size = 64 renderer.anti_aliasing = False renderer.camera.perspective = False renderer.light_intensity_ambient = 1.0 renderer.light_intensity_directional = 0.0 vertices = torch.from_numpy(np.array(vertices, dtype=np.float32)).cuda() faces = torch.from_numpy(np.array(faces, dtype=np.int32)).cuda() textures = torch.ones(faces.shape[0], 4, 4, 4, 3, dtype=torch.float32).cuda() grad_ref = torch.from_numpy(np.array(grad_ref, dtype=np.float32)).cuda() vertices, faces, textures, grad_ref = utils.to_minibatch( (vertices, faces, textures, grad_ref)) vertices.requires_grad = True images = renderer(vertices, faces, textures) images = torch.mean(images, dim=1) loss = torch.sum(torch.abs(images[:, pyi, pxi])) loss.backward() assert (torch.allclose(vertices.grad, grad_ref, rtol=1e-2))
def test_backward_case2(self): """Backward if non-zero gradient is on a face.""" vertices = [ [0.8, 0.8, 1.], [-0.5, -0.8, 1.], [0.8, -0.8, 1.]] faces = [[0, 1, 2]] pyi = 40 pxi = 50 grad_ref = [ [0.98646867, 1.04628897, 0.], [-1.03415668, - 0.10403691, 0.], [3.00094461, - 1.55173182, 0.], ] renderer = neural_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])) loss.backward() grad_ref = cp.array(grad_ref, 'float32') chainer.testing.assert_allclose(vertices.grad, grad_ref, rtol=1e-2)
def test_backward_case1(self): """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 = neural_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)