return load_string(scene, sdf_file=sdf_file, interpol=interpolation, fov=fov, res=cam_res) scene_target = create_scene(None, interpolation, cams_origins, fov, img_res) images_ref = list( render_torch(scene_target, spp=spp_ref, sensor_index=i) for i in range(len(cams_origins))) crop_size = scene_target.sensors()[0].film().crop_size() for i in range(len(cams_origins)): write_bitmap(f'{out_path}{i}_target.png', images_ref[i], crop_size) # Find differentiable scene parameters sdf_file = "data/sdf/init.vol" if restart_epoch == 0 else f"{out_path}sdf_e{restart_epoch}.vol" scene_init = create_scene(sdf_file, interpolation, cams_origins, fov, img_res) params = traverse(scene_init) #print(params) params.keep(['SDF.data', 'SDF.bsdf.reflectance.data']) if restart_epoch > 0 and increase_res: sdf = params['SDF.data'].numpy().reshape([sdf_res] * 3) sdf = double_sdf_res(sdf) params['SDF.data'] = sdf.flatten() params.update() params_torch = params.torch()
scene = load_file('cbox/cbox.xml') # Find differentiable scene parameters params = traverse(scene) # Discard all parameters except for one we want to differentiate params.keep(['red.reflectance.value']) # Print the current value and keep a backup copy param_ref = params['red.reflectance.value'].torch() print(param_ref) # Render a reference image (no derivatives used yet) image_ref = render_torch(scene, spp=8) crop_size = scene.sensors()[0].film().crop_size() write_bitmap('out_ref.png', image_ref, crop_size) # Change the left wall into a bright white surface params['red.reflectance.value'] = [.9, .9, .9] params.update() # Which parameters should be exposed to the PyTorch optimizer? params_torch = params.torch() # Construct a PyTorch Adam optimizer that will adjust 'params_torch' opt = torch.optim.Adam(params_torch.values(), lr=.2) objective = torch.nn.MSELoss() time_a = time.time() iterations = 100
scene = load_file('bunny/bunny.xml') # Find differentiable scene parameters params = traverse(scene) # Make a backup copy param_res = params['my_envmap.resolution'] param_ref = Float(params['my_envmap.data']) # Discard all parameters except for one we want to differentiate params.keep(['my_envmap.data']) # Render a reference image (no derivatives used yet) image_ref = render(scene, spp=16) crop_size = scene.sensors()[0].film().crop_size() write_bitmap('out_ref.png', image_ref, crop_size) # Change to a uniform white lighting environment params['my_envmap.data'] = ek.full(Float, 1.0, len(param_ref)) params.update() # Construct an Adam optimizer that will adjust the parameters 'params' opt = Adam(params, lr=.02) time_a = time.time() iterations = 100 for it in range(iterations): # Perform a differentiable rendering of the scene image = render(scene, optimizer=opt, unbiased=True, spp=1) write_bitmap('out_%03i.png' % it, image, crop_size)
return load_string(scene, sdf_file=sdf_file, interpol=interpolation, n_cams=n_cams, fov=fov, res=cam_res) scene_target = create_scene(None, interpolation, n_cams, fov, img_res) images_ref = list( render_torch(scene_target, spp=64, sensor_index=i) for i in range(n_cams)) crop_size = scene_target.sensors()[0].film().crop_size() for i in range(n_cams): write_bitmap(f'{out_path}{i}_target.png', images_ref[i], crop_size) # Find differentiable scene parameters sdf_file = "data/sdf/init.vol" if restart_epoch == 0 else f"{out_path}sdf_e{restart_epoch}.vol" scene_init = create_scene(sdf_file, interpolation, n_cams, fov, img_res) params = traverse(scene_init) if restart_epoch > 0 and increase_res: params = traverse(scene_init) sdf = params['SDF.data'].numpy().reshape([sdf_res] * 3) sdf = double_sdf_res(sdf) sdf_res = sdf.shape[0] sdf_file = f'{out_path}sdf_e{restart_epoch}_x2.vol' write_binary_grid3d(sdf_file, sdf) scene_init = create_scene(sdf_file, interpolation, n_cams, fov, img_res) params = traverse(scene_init)
# Load the Cornell Box Thread.thread().file_resolver().append('cbox') scene = load_file( 'C:/MyFile/code/ray tracing/misuba2/test/gpu_autodiff/cbox/cbox.xml') # Find differentiable scene parameters params = traverse(scene) # Keep track of derivatives with respect to one parameter param_0 = params['red.reflectance.value'] ek.set_requires_gradient(param_0) # Differentiable simulation image = render(scene, spp=4) # Assign the gradient [1, 1, 1] to the 'red.reflectance.value' input ek.set_gradient(param_0, [1, 1, 1], backward=False) # Forward-propagate previously assigned gradients Float.forward() # The gradients have been propagated to the output image image_grad = ek.gradient(image) # .. write them to a PNG file crop_size = scene.sensors()[0].film().crop_size() fname = 'C:/MyFile/code/ray tracing/misuba2/test/gpu_autodiff/output/out.png' write_bitmap(fname, image_grad, crop_size) print('Wrote forward differentiation image to: {}'.format(fname))
outpath = os.path.join('outputs/invert_infloasion/', outimg_dir) os.makedirs(outpath, exist_ok=True) out_config(outpath, render_config) # Write out config file losses = np.array([]) film = scene.sensors()[0].film() crop_size = film.crop_size() iterations = render_config['num_iteration'] for it in range(iterations): # Perform a differentiable rendering of the scene image = render(scene, optimizer=opt, unbiased=True, spp=4) if it % 10 == 0: write_bitmap(os.path.join(outpath, 'out_%04i.png' % it), image, crop_size) write_bitmap(os.path.join(outpath, 'texture_%04i.png' % it), params[opt_param_name], (param_res[1], param_res[0])) # Objective : MSE between 'image' and 'image_ref' ob_val = ek.hsum(ek.sqr(image - image_ref)) / len(image) # Back-propropagate errors to input parameters ek.backward(ob_val) # Optimizer : take a gradient step opt.step() # Compare iterate against ground-truth value err_ref = ek.hsum(ek.sqr(param_ref - params[opt_param_name])) losses = np.append(losses, err_ref)
total_sample_count = ek.hprod(film_size) * spp if sampler.wavefront_size() != total_sample_count: sampler.seed(UInt64.arange(total_sample_count)) # Enumerate discrete sample & pixel indices, and uniformly sample # positions within each pixel. pos = ek.arange(UInt32, total_sample_count) pos //= spp scale = Vector2f(1.0 / film_size[0], 1.0 / film_size[1]) pos = Vector2f(Float(pos % int(film_size[0])), Float(pos // int(film_size[0]))) pos += sampler.next_2d() # Sample rays starting from the camera sensor rays, weights = sensor.sample_ray_differential(time=0, sample1=sampler.next_1d(), sample2=pos * scale, sample3=0) # Intersect rays with the scene geometry surface_interaction = dummy_scene.ray_intersect(rays) result = surface_interaction.t + 1.0 result[~(surface_interaction.t == Infinity)] = 1 result[~surface_interaction.is_valid()] = 0 from mitsuba.python.autodiff import render, write_bitmap crop_size = scene.sensors()[0].film().crop_size() write_bitmap('renders/green_bin_mask.png', result, crop_size)
width = 128 height = 128 # spp: samples per pixel spp_ref = 64 spp_opt = 8 # Prepare output folder output_path = 'output/diffuser_smooth/' + task + '/' if not os.path.isdir(output_path): os.makedirs(output_path) # Generate the scene and image with a plain glass panel scene = make_scene(path_str, spp_ref, width, height) image_plain = render(scene) write_bitmap(output_path + "out_plain.png", image_plain, (width, height)) print("Writing " + "out_plain.png") params = traverse(scene) print(params) positions_buf = params['grid_mesh.vertex_positions_buf'] positions_initial = ravel(positions_buf) normals_initial = ravel(params['grid_mesh.vertex_normals_buf']) vertex_count = ek.slices(positions_initial) filename = 'scene/diffuser_surface_1.jpg' Thread.thread().file_resolver().append(os.path.dirname(filename)) # Create a texture with the reference displacement map disp_tex_1 = xml.load_dict({ "type" : "bitmap",
test = list() test.append(10.00) test.append(100.00) test.append(259.00) mitsuba.set_variant('gpu_autodiff_rgb') from mitsuba.core import Thread from mitsuba.core.xml import load_file from mitsuba.python.util import traverse # Absolute or relative path to the XML file filename = 'scenes/cboxwithdragon/cboxwithdragon.xml' dump_path = 'results/BSDFTest/' try: os.makedirs(dump_path) except: pass # Add the scene directory to the FileResolver's search path Thread.thread().file_resolver().append(os.path.dirname(filename)) # Load the scene scene = load_file(filename) # Render a reference image (no derivatives used yet) from mitsuba.python.autodiff import render, write_bitmap image_ref = render(scene, spp=8) crop_size = scene.sensors()[0].film().crop_size() write_bitmap(dump_path + 'out_ref.png', image_ref, crop_size)
# Find differentiable scene parameters params = traverse(scene) params.keep([diff_param_key]) owners = get_owners(params, scene) masks = generate_masks(owners, scene) from mitsuba.core import Color3f diff_param_ref = Color3f(params[diff_param_key]) #light_ref = Color3f(params['light.reflectance.value']) # Render a reference image (no derivatives used yet) from mitsuba.python.autodiff import render, write_bitmap image_ref = render(scene, spp=8) crop_size = scene.sensors()[0].film().crop_size() write_bitmap(base_path + 'reference.png', image_ref, crop_size) #write_bitmap('renders/mask.png', masks['green'], crop_size) # Change the left wall into a bright white surface params[diff_param_key] = [1.0, 0.1, 0.1] params.update() # Construct an Adam optimizer that will adjust the parameters 'params' from mitsuba.python.autodiff import SGD opt = SGD(params, lr=.2, momentum=0.9) mask_len = ek.hsum(masks[diff_param_owner])[0] errors = list() converged = False it = 0 while converged != True and it <= 100:
scale = Vector2f(1.0 / film_size[0], 1.0 / film_size[1]) pos = Vector2f(Float(pos % int(film_size[0])), Float(pos // int(film_size[0]))) pos += sampler.next_2d() # Sample rays starting from the camera sensor rays, weights = sensor.sample_ray_differential(time=0, sample1=sampler.next_1d(), sample2=pos * scale, sample3=0) # Intersect rays with the scene geometry surface_interaction = scene.ray_intersect(rays) #We're using the dragon center as an example here dragon_c = scene.shapes()[6].bbox().center() c = Vector3f(dragon_c) d = c - surface_interaction.p result = ek.norm(d) #Eliminate non valid intersection points result[~surface_interaction.is_valid()] = 0 #We divide all distances by the maximum distance to normalize max_dist = ek.hmax(result) result /= max_dist result = (1.0 - result) from mitsuba.python.autodiff import write_bitmap crop_size = scene.sensors()[0].film().crop_size() write_bitmap('renders/green_dist_map.png', result, crop_size)