def parse_camera(node): fov = tf.constant([45.0]) position = None look_at = None up = None clip_near = 1e-2 resolution = [256, 256] for child in node: if 'name' in child.attrib: if child.attrib['name'] == 'fov': fov = tf.constant([float(child.attrib['value'])]) elif child.attrib['name'] == 'toWorld': has_lookat = False for grandchild in child: if grandchild.tag.lower() == 'lookat': has_lookat = True position = parse_vector(grandchild.attrib['origin']) look_at = parse_vector(grandchild.attrib['target']) up = parse_vector(grandchild.attrib['up']) if not has_lookat: print( 'Unsupported Mitsuba scene format: please use a look at transform' ) assert (False) if child.tag == 'film': for grandchild in child: if 'name' in grandchild.attrib: if grandchild.attrib['name'] == 'width': resolution[0] = int(grandchild.attrib['value']) elif grandchild.attrib['name'] == 'height': resolution[1] = int(grandchild.attrib['value']) return pyredner.Camera(position=position, look_at=look_at, up=up, fov=fov, clip_near=clip_near, resolution=resolution)
# Use GPU if available pyredner.set_use_gpu( tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None)) # Set up the scene using Pytorch tensor with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): position = tf.Variable([0.0, 0.0, -5.0], dtype=tf.float32) look_at = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32) up = tf.Variable([0.0, 1.0, 0.0], dtype=tf.float32) fov = tf.Variable([45.0], dtype=tf.float32) clip_near = 1e-2 resolution = (256, 256) cam = pyredner.Camera(position=position, look_at=look_at, up=up, fov=fov, clip_near=clip_near, resolution=resolution) mat_perlin = pyredner.Material(diffuse_reflectance=diffuse, specular_reflectance=specular, roughness=roughness) with tf.device(pyredner.get_device_name()): mat_black = pyredner.Material( diffuse_reflectance=tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32)) materials = [mat_perlin, mat_black] vertices = tf.Variable([[-1.5, -1.5, 0.0], [-1.5, 1.5, 0.0], [1.5, -1.5, 0.0], [1.5, 1.5, 0.0]], dtype=tf.float32) indices = tf.constant([[0, 1, 2], [1, 3, 2]], dtype=tf.int32) uvs = tf.Variable([[0.05, 0.05], [0.05, 0.95], [0.95, 0.05], [0.95, 0.95]],
# memory allocation to reduce page faults. import os os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' import tensorflow as tf tf.compat.v1.enable_eager_execution() import pyredner_tensorflow as pyredner # Use GPU if available pyredner.set_use_gpu( tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None)) with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): cam = pyredner.Camera( position=tf.Variable([0.0, 0.0, -5.0], dtype=tf.float32), look_at=tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32), up=tf.Variable([0.0, 1.0, 0.0], dtype=tf.float32), fov=tf.Variable([45.0], dtype=tf.float32), # in degree clip_near=1e-2, # needs to > 0 resolution=(256, 256), fisheye=False) with tf.device(pyredner.get_device_name()): mat_grey = pyredner.Material( diffuse_reflectance=tf.Variable([0.4, 0.4, 0.4], dtype=tf.float32), specular_reflectance=tf.Variable([0.5, 0.5, 0.5], dtype=tf.float32), roughness=tf.Variable([0.05], dtype=tf.float32)) materials = [mat_grey] with tf.device(pyredner.get_device_name()): vertices, indices, uvs, normals = pyredner.generate_sphere(128, 64) shape_sphere = pyredner.Shape(vertices=vertices,
# Use GPU if available pyredner.set_use_gpu( tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None)) # Set up the scene with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): position = tf.Variable([0.0, 0.0, -1.0], dtype=tf.float32) look_at = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32) up = tf.Variable([0.0, 1.0, 0.0], dtype=tf.float32) fov = tf.Variable([45.0], dtype=tf.float32) clip_near = 1e-2 resolution = (256, 256) cam = pyredner.Camera(position=position, look_at=look_at, up=up, fov=fov, clip_near=clip_near, resolution=resolution, fisheye=True) with tf.device(pyredner.get_device_name()): mat_grey = pyredner.Material(diffuse_reflectance = \ tf.Variable([0.5, 0.5, 0.5], dtype=tf.float32)) materials = [mat_grey] vertices = tf.Variable( [[-1.7, 1.0, 0.0], [1.0, 1.0, 0.0], [-0.5, -1.0, 0.0]], dtype=tf.float32) indices = tf.constant([[0, 1, 2]], dtype=tf.int32) shape_triangle = pyredner.Shape(vertices, indices, 0) light_vertices = tf.Variable([[-1.0, -1.0, -9.0], [1.0, -1.0, -9.0], [-1.0, 1.0, -9.0], [1.0, 1.0, -9.0]],
# Perturb the scene, this is our initial guess cam = scene.camera cam_position = cam.position with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): cam_translation = tf.Variable([-0.2, 0.2, -0.2], dtype=tf.float32, trainable=True, use_resource=True) with tf.device(pyredner.get_device_name()): diffuse_reflectance = tf.Variable([0.3, 0.3, 0.3], dtype=tf.float32, trainable=True, use_resource=True) specular_reflectance = tf.Variable([0.5, 0.5, 0.5], dtype=tf.float32, trainable=True, use_resource=True) roughness = tf.Variable([0.2], dtype=tf.float32, trainable=True, use_resource=True) scene.materials[-1].diffuse_reflectance = pyredner.Texture(diffuse_reflectance) scene.materials[-1].specular_reflectance = pyredner.Texture(specular_reflectance) scene.materials[-1].roughness = pyredner.Texture(roughness) scene.camera = pyredner.Camera(position = cam_position + cam_translation, look_at = cam.look_at + cam_translation, up = cam.up, fov = cam.fov, clip_near = cam.clip_near, resolution = cam.resolution, fisheye = False) scene_args = pyredner.serialize_scene( scene = scene, num_samples = 1024, max_bounces = 2) # Render the initial guess img = pyredner.render(1, *scene_args) pyredner.imwrite(img, 'results/test_teapot_reflectance/init.png') diff = tf.abs(target - img) pyredner.imwrite(diff, 'results/test_teapot_reflectance/init_diff.png') lr = 1e-2 optimizer = tf.compat.v1.train.AdamOptimizer(lr)
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' import tensorflow as tf tf.compat.v1.enable_eager_execution() import pyredner_tensorflow as pyredner # From the test_single_triangle.py test case but with viewport # Use GPU if available pyredner.set_use_gpu( tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None)) with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): cam = pyredner.Camera( position=tf.Variable([0.0, 0.0, -5.0], dtype=tf.float32), look_at=tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32), up=tf.Variable([0.0, 1.0, 0.0], dtype=tf.float32), fov=tf.Variable([45.0], dtype=tf.float32), # in degree clip_near=1e-2, # needs to > 0 resolution=(1024, 1024), viewport=(200, 300, 700, 800)) with tf.device(pyredner.get_device_name()): mat_grey = pyredner.Material( diffuse_reflectance=tf.Variable([0.5, 0.5, 0.5], dtype=tf.float32)) materials = [mat_grey] with tf.device(pyredner.get_device_name()): shape_triangle = pyredner.Shape(vertices=tf.Variable( [[-1.7, 1.0, 0.0], [1.0, 1.0, 0.0], [-0.5, -1.0, 0.0]], dtype=tf.float32), indices=tf.constant([[0, 1, 2]], dtype=tf.int32),
tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None)) position = tf.Variable([1.0, 0.0, -3.0], dtype=tf.float32) look_at = tf.Variable([1.0, 0.0, 0.0], dtype=tf.float32) up = tf.Variable([0.0, 1.0, 0.0], dtype=tf.float32) fov = tf.Variable([45.0], dtype=tf.float32) clip_near = 1e-2 # randomly generate distortion parameters tf.random.set_seed(1234) target_distort_params = (tf.random.uniform([8]) - 0.5) * 0.05 resolution = (256, 256) cam = pyredner.Camera(position=position, look_at=look_at, up=up, fov=fov, clip_near=clip_near, resolution=resolution, distortion_params=target_distort_params) checkerboard_texture = pyredner.imread('scenes/teapot.png') mat_checkerboard = pyredner.Material(\ diffuse_reflectance = checkerboard_texture) mat_black = pyredner.Material(\ diffuse_reflectance = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32)) plane = pyredner.Object(vertices=tf.Variable([[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [1.0, -1.0, 0.0], [1.0, 1.0, 0.0]]), indices=tf.constant([[0, 1, 2], [1, 3, 2]],
# Use GPU if available pyredner.set_use_gpu( tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None)) # Set up the scene with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): position = tf.Variable([0.0, 0.0, -5.0], dtype=tf.float32) look_at = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32) up = tf.Variable([0.0, 1.0, 0.0], dtype=tf.float32) fov = tf.Variable([45.0], dtype=tf.float32) # in degree clip_near = 1e-2 # needs to > 0 resolution = (256, 256) cam = pyredner.Camera(position=position, look_at=look_at, up=up, fov=fov, clip_near=clip_near, resolution=(256, 256)) with tf.device(pyredner.get_device_name()): mat_grey = pyredner.Material( diffuse_reflectance=tf.Variable([0.5, 0.5, 0.5], dtype=tf.float32)) materials = [mat_grey] vertices = tf.Variable( [[-1.7, 1.0, 0.0], [1.0, 1.0, 0.0], [-0.5, -1.0, 0.0]], dtype=tf.float32) indices = tf.constant([[0, 1, 2]], dtype=tf.int32) shape_triangle = pyredner.Shape(vertices, indices, 0) light_vertices = tf.Variable([[-1.0, -1.0, -9.0], [1.0, -1.0, -9.0], [-1.0, 1.0, -9.0], [1.0, 1.0, -9.0]], dtype=tf.float32)