def test03_sample_eval_pdf(variant_scalar_rgb, interaction): from mitsuba.core.math import InvPi from mitsuba.core.warp import square_to_uniform_sphere from mitsuba.render import BSDFContext from mitsuba.core.xml import load_string bsdf = load_string("""<bsdf version="2.0.0" type="twosided"> <bsdf type="diffuse"> <rgb name="reflectance" value="0.1, 0.1, 0.1"/> </bsdf> <bsdf type="diffuse"> <rgb name="reflectance" value="0.9, 0.9, 0.9"/> </bsdf> </bsdf>""") n = 5 ctx = BSDFContext() for u in ek.arange(UInt32, n): for v in ek.arange(UInt32, n): interaction.wi = square_to_uniform_sphere([u / float(n-1), v / float(n-1)]) up = ek.dot(interaction.wi, [0, 0, 1]) > 0 for x in ek.arange(UInt32, n): for y in ek.arange(UInt32, n): sample = [x / float(n-1), y / float(n-1)] (bs, s_value) = bsdf.sample(ctx, interaction, 0.5, sample) if ek.any(s_value > 0): # Multiply by square_to_cosine_hemisphere_theta s_value *= bs.wo[2] * InvPi if not up: s_value *= -1 e_value = bsdf.eval(ctx, interaction, bs.wo) p_pdf = bsdf.pdf(ctx, interaction, bs.wo) assert ek.allclose(s_value, e_value, atol=1e-2) assert ek.allclose(bs.pdf, p_pdf) assert not ek.any(ek.isnan(e_value) | ek.isnan(s_value))
def test06_rgb2spec_fetch_eval_mean(variant_scalar_spectral): from mitsuba.render import srgb_model_fetch, srgb_model_eval, srgb_model_mean from mitsuba.core import MTS_WAVELENGTH_MIN, MTS_WAVELENGTH_MAX, MTS_WAVELENGTH_SAMPLES import numpy as np rgb_values = np.array([ [0, 0, 0], [0.0129831, 0.0129831, 0.0129831], [0.0241576, 0.0241576, 0.0241576], [0.0423114, 0.0423114, 0.0423114], [0.0561285, 0.0561285, 0.0561285], [0.0703601, 0.0703601, 0.0703601], [0.104617, 0.104617, 0.104617], [0.171441, 0.171441, 0.171441], [0.242281, 0.242281, 0.242281], [0.814846, 0.814846, 0.814846], [0.913098, 0.913098, 0.913098], [0.930111, 0.930111, 0.930111], [0.955973, 0.955973, 0.955973], [0.973445, 0.973445, 0.973445], [0.982251, 0.982251, 0.982251], [0.991102, 0.991102, 0.991102], [1, 1, 1], ]) wavelengths = np.linspace(MTS_WAVELENGTH_MIN, MTS_WAVELENGTH_MAX, MTS_WAVELENGTH_SAMPLES) for i in range(rgb_values.shape[0]): rgb = rgb_values[i, :] assert np.all((rgb >= 0.0) & (rgb <= 1.0)), "Invalid RGB attempted" coeff = srgb_model_fetch(rgb) mean = srgb_model_mean(coeff) value = srgb_model_eval(coeff, wavelengths) assert not ek.any(ek.isnan(coeff)), "{} => coeff = {}".format( rgb, coeff) assert not ek.any(ek.isnan(mean)), "{} => mean = {}".format(rgb, mean) assert not ek.any(ek.isnan(value)), "{} => value = {}".format( rgb, value)
def test47_nan_propagation(m): for i in range(2): x = ek.arange(m.Float, 10) ek.enable_grad(x) f0 = m.Float(0) y = ek.select(x < (20 if i == 0 else 0), x, x * (f0 / f0)) ek.backward(y) g = ek.grad(x) if i == 0: assert g == 1 else: assert ek.all(ek.isnan(g)) for i in range(2): x = ek.arange(m.Float, 10) ek.enable_grad(x) f0 = m.Float(0) y = ek.select(x < (20 if i == 0 else 0), x, x * (f0 / f0)) ek.forward(x) g = ek.grad(y) if i == 0: assert g == 1 else: assert ek.all(ek.isnan(g))
diff_vertex_ref = [] diff_vertex_init = [] for j in range(10): opt = Adam(params_opt, lr = 0.001) # opt = SGD(params_opt, lr = 0.001, momentum = 0.9) for i in range(10): unravel(vertex_positions + Vector3f(0,0,1) * params_opt['displacements'], params[vertex_pos_key]) params.set_dirty(vertex_pos_key) params.update() image = render(scene) if j == 0 and i == 0: image_init = image if ek.any(ek.any(ek.isnan(params[vertex_pos_key]))): print("[WARNING] NaNs in the vertex positions.") if ek.any(ek.isnan(image)): print("[WARNING] NaNs in the image.") # Write a gamma encoded PNG image_np = image.numpy().reshape(height, width, 3) output_file = output_path + 'out_%03i_%03i.png' % (j, i) print("Writing image %s" % (output_file)) Bitmap(image_np).convert(pixel_format=Bitmap.PixelFormat.RGB, component_format=Struct.Type.UInt8, srgb_gamma=True).write(output_file) # Objective function if task == 'plain2bumpy' or task == 'bumpy2bumpy': loss = ek.hsum(ek.hsum(ek.sqr(image - image_ref))) / (height*width*3) if task == 'bumpy2plain':