Skip to content

Commit

Permalink
Rasterization backend golden tests.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 364837146
  • Loading branch information
podlipensky authored and Copybara-Service committed Mar 24, 2021
1 parent 2d026de commit 705ed34
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
10 changes: 2 additions & 8 deletions tensorflow_graphics/rendering/tests/interpolate_test.py
Expand Up @@ -13,8 +13,6 @@
# limitations under the License.
"""Tests for tensorflow_graphics.rendering.tests.interpolate."""

import os

import tensorflow as tf

from tensorflow_graphics.rendering import interpolate
Expand Down Expand Up @@ -67,12 +65,8 @@ def test_renders_colored_cube(self):
rendered = interpolate.interpolate_vertex_attribute(vertex_rgba,
rasterized).value

image_path = tf.compat.v1.resource_loader.get_path_to_datafile(
os.path.join('test_data', 'Unlit_Cube_0_0.png'))
file = tf.io.read_file(image_path)
baseline_image = tf.cast(tf.image.decode_image(file), tf.float32) / 255.0
baseline_image = tf.expand_dims(baseline_image, axis=0)
baseline_image = tf.reshape(baseline_image, rendered.shape)
baseline_image = rasterization_test_utils.load_baseline_image(
'Unlit_Cube_0_0.png', rendered.shape)

images_near, error_message = rasterization_test_utils.compare_images(
self, baseline_image, rendered)
Expand Down
Expand Up @@ -17,12 +17,16 @@
import tensorflow as tf

from tensorflow_graphics.rendering import rasterization_backend
from tensorflow_graphics.rendering.tests import rasterization_test_utils
from tensorflow_graphics.util import test_case


class RasterizationBackendTestBase(test_case.TestCase):
"""Base class for CPU/GPU rasterization backend tests."""

IMAGE_WIDTH = 640
IMAGE_HEIGHT = 480

def setUp(self):
super().setUp()
self._backend = rasterization_backend.RasterizationBackends.OPENGL
Expand Down Expand Up @@ -75,8 +79,9 @@ def test_rasterizer_return_correct_batch_shapes(self, shapes, dtypes,
"""Tests that supported backends return correct shape."""
placeholders = self._create_placeholders(shapes, dtypes)
frame_buffer = rasterization_backend.rasterize(
placeholders[0], placeholders[1], placeholders[2], (600, 800),
enable_cull_face, self._num_layers, self._backend)
placeholders[0], placeholders[1], placeholders[2],
(self.IMAGE_WIDTH, self.IMAGE_HEIGHT), enable_cull_face,
self._num_layers, self._backend)
batch_size = shapes[0][0]
self.assertEqual([batch_size],
frame_buffer.triangle_id.get_shape().as_list()[:-3])
Expand All @@ -94,7 +99,8 @@ def test_rasterizer_rasterize_exception_raised(self, shapes, dtypes, backend):
placeholders = self._create_placeholders(shapes, dtypes)
with self.assertRaisesRegexp(KeyError, 'Backend is not supported'):
rasterization_backend.rasterize(placeholders[0], placeholders[1],
placeholders[2], (600, 800),
placeholders[2],
(self.IMAGE_WIDTH, self.IMAGE_HEIGHT),
self._enable_cull_face, self._num_layers,
backend)

Expand All @@ -105,8 +111,9 @@ def test_rasterizer_all_vertices_visible(self):
triangles = tf.convert_to_tensor([[0, 1, 2]], dtype=tf.int32)
view_projection_matrix = tf.expand_dims(tf.eye(4), axis=0)
frame_buffer = rasterization_backend.rasterize(
vertices, triangles, view_projection_matrix, (100, 100),
self._enable_cull_face, self._num_layers, self._backend)
vertices, triangles, view_projection_matrix,
(self.IMAGE_WIDTH, self.IMAGE_HEIGHT), self._enable_cull_face,
self._num_layers, self._backend)
self.assertAllEqual(frame_buffer.triangle_id.shape[:-1],
frame_buffer.vertex_ids.shape[:-1])
# Assert that triangle is visible.
Expand All @@ -115,3 +122,28 @@ def test_rasterizer_all_vertices_visible(self):
# Assert that all three vertices are visible.
self.assertAllLess(frame_buffer.triangle_id, 3)
self.assertAllGreaterEqual(frame_buffer.triangle_id, 0)

def test_render_simple_triangle(self):
"""Directly renders a rasterized triangle's barycentric coordinates."""
w_vector = tf.constant([1.0, 1.0, 1.0], dtype=tf.float32)
clip_init = tf.constant(
[[[-0.5, -0.5, 0.8], [0.0, 0.5, 0.3], [0.5, -0.5, 0.3]]],
dtype=tf.float32)
clip_coordinates = clip_init * tf.reshape(w_vector, [1, 3, 1])
triangles = tf.constant([[0, 1, 2]], dtype=tf.int32)

face_culling_enabled = False
framebuffer = rasterization_backend.rasterize(
clip_coordinates, triangles, tf.eye(4, batch_shape=[1]),
(self.IMAGE_WIDTH, self.IMAGE_HEIGHT), face_culling_enabled,
self._num_layers, self._backend)
ones_image = tf.ones([1, self.IMAGE_HEIGHT, self.IMAGE_WIDTH, 1])
rendered_coordinates = tf.concat(
[framebuffer.barycentrics.value, ones_image], axis=-1)

baseline_image = rasterization_test_utils.load_baseline_image(
'Simple_Triangle.png', rendered_coordinates.shape)

images_near, error_message = rasterization_test_utils.compare_images(
self, baseline_image, rendered_coordinates)
self.assertTrue(images_near, msg=error_message)
23 changes: 23 additions & 0 deletions tensorflow_graphics/rendering/tests/rasterization_test_utils.py
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
"""Util functions for rasterization tests."""

import os

import numpy as np
import tensorflow as tf

Expand Down Expand Up @@ -101,3 +103,24 @@ def compare_images(test_case,
error_msg = "{:.2%} pixels are not equal to baseline image pixels.".format(
test_case.evaluate(perc_outliers) * 100.0)
return test_case.evaluate(perc_outliers < max_outlier_fraction), error_msg


def load_baseline_image(filename, image_shape=None):
"""Loads baseline image and makes sure it is of the right shape.
Args:
filename: file name of the image to load.
image_shape: expected shape of the image.
Returns:
tf.Tensor with baseline image
"""
image_path = tf.compat.v1.resource_loader.get_path_to_datafile(
os.path.join("test_data", filename))
file = tf.io.read_file(image_path)
baseline_image = tf.cast(tf.image.decode_image(file), tf.float32) / 255.0
baseline_image = tf.expand_dims(baseline_image, axis=0)
if image_shape is not None:
# Graph-mode requires image shape to be known in advance.
baseline_image = tf.ensure_shape(baseline_image, image_shape)
return baseline_image
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 705ed34

Please sign in to comment.