|
| 1 | +# Copyright 2020 The TensorFlow Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for radiance ray rendering.""" |
| 15 | + |
| 16 | +from absl.testing import flagsaver |
| 17 | +from absl.testing import parameterized |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +from tensorflow_graphics.rendering.volumetric import ray_density |
| 21 | +from tensorflow_graphics.util import test_case |
| 22 | + |
| 23 | + |
| 24 | +def generate_random_test_ray_render(): |
| 25 | + """Generates random test for the voxels rendering functions.""" |
| 26 | + batch_shape = np.random.randint(1, 3) |
| 27 | + n_rays = np.random.randint(1, 512) |
| 28 | + random_ray_values = np.random.uniform(size=[batch_shape] + [n_rays, 1]) |
| 29 | + random_ray_dists = random_ray_values[..., -1] |
| 30 | + return random_ray_values, random_ray_dists |
| 31 | + |
| 32 | + |
| 33 | +class RayDensityTest(test_case.TestCase): |
| 34 | + |
| 35 | + @parameterized.parameters( |
| 36 | + ((6, 1), (6,)), |
| 37 | + ((8, 16, 44, 1), (8, 16, 44)), |
| 38 | + ((12, 8, 16, 22, 1), (12, 8, 16, 22)), |
| 39 | + ((32, 32, 256, 1), (32, 32, 256)), |
| 40 | + ((32, 32, 256, 1), (1, 1, 256)), |
| 41 | + ((32, 32, 256, 1), (256,)), |
| 42 | + ) |
| 43 | + def test_render_shape_exception_not_raised(self, *shapes): |
| 44 | + """Tests that the shape exceptions are not raised.""" |
| 45 | + self.assert_exception_is_not_raised(ray_density.compute_density, shapes) |
| 46 | + |
| 47 | + @parameterized.parameters( |
| 48 | + ("must have a rank greater than 1", ((1,), (3,))), |
| 49 | + ("must have exactly 1 dimensions in axis -1", ((44, 4), (44, 1))), |
| 50 | + ("Not all batch dimensions are broadcast-compatible.", |
| 51 | + ((32, 32, 256, 1,), (32, 16, 256,))), |
| 52 | + ("must have the same number of dimensions", |
| 53 | + ((32, 32, 128, 1,), (32, 32, 555,))), |
| 54 | + ) |
| 55 | + def test_render_shape_exception_raised(self, error_msg, shape): |
| 56 | + """Tests that the shape exception is raised.""" |
| 57 | + self.assert_exception_is_raised(ray_density.compute_density, |
| 58 | + error_msg, shape) |
| 59 | + |
| 60 | + @flagsaver.flagsaver(tfg_add_asserts_to_graph=False) |
| 61 | + def test_render_jacobian_random(self): |
| 62 | + """Tests the Jacobian of render.""" |
| 63 | + point_values, point_distance = generate_random_test_ray_render() |
| 64 | + self.assert_jacobian_is_correct_fn( |
| 65 | + lambda x: ray_density.compute_density(x, point_distance)[0], |
| 66 | + [point_values]) |
| 67 | + self.assert_jacobian_is_correct_fn( |
| 68 | + lambda x: ray_density.compute_density(point_values, x)[0], |
| 69 | + [point_distance]) |
| 70 | + |
| 71 | + def test_render_preset(self): |
| 72 | + """Checks that render returns the expected value.""" |
| 73 | + |
| 74 | + image_rays = np.zeros((128, 128, 64, 1)) |
| 75 | + image_rays[32:96, 32:96, 16:32, :] = 1 |
| 76 | + distances = np.zeros((128, 128, 64)) + 1.5 |
| 77 | + target_image = np.zeros((128, 128, 1)) |
| 78 | + target_image[32:96, 32:96, :] = 1 |
| 79 | + rendered_image, *_ = ray_density.compute_density(image_rays, distances) |
| 80 | + self.assertAllClose(rendered_image, target_image) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + test_case.main() |
0 commit comments