Skip to content

Commit

Permalink
Estimator comparison (Veach scene)
Browse files Browse the repository at this point in the history
  • Loading branch information
tizian authored and Tizian Zeltner committed Jul 29, 2022
1 parent 1cd7a4a commit 037830e
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ results/.ipynb_checkpoints
results/estimator_comparison_simple
results/estimator_comparison_optimization
results/estimator_comparison_attached_reparam
results/estimator_comparison_veach
274 changes: 274 additions & 0 deletions results/Estimator comparison (Veach).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "43337124",
"metadata": {
"ExecuteTime": {
"end_time": "2022-07-28T11:40:41.771668Z",
"start_time": "2022-07-28T11:40:41.739437Z"
}
},
"outputs": [],
"source": [
"import mitsuba as mi\n",
"mi.set_variant(\"llvm_ad_rgb\")\n",
"import drjit as dr\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import cmap_diff\n",
"%config InlineBackend.figure_formats = ['svg']\n",
"%matplotlib inline\n",
"\n",
"import time\n",
"import os\n",
"base_dir = 'estimator_comparison_veach'\n",
"if not os.path.exists(base_dir):\n",
" os.makedirs(base_dir)\n",
" \n",
"mi.Thread.thread().logger().set_log_level(mi.LogLevel.Warn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd6b3dbf",
"metadata": {
"ExecuteTime": {
"end_time": "2022-07-28T11:40:42.753437Z",
"start_time": "2022-07-28T11:40:42.551628Z"
}
},
"outputs": [],
"source": [
"scene = mi.load_file('scenes/veach.xml')\n",
"params = mi.traverse(scene)\n",
"param_key = 'plates_top.bsdf.alpha.data'\n",
"params.keep([param_key])\n",
"\n",
"roughness_tex = mi.TensorXf(params[param_key])\n",
"\n",
"diff_methods = [\n",
" 'es_detached',\n",
" \n",
" 'bs_detached',\n",
" 'bs_attached',\n",
" \n",
" 'mis_detached_detached',\n",
" 'mis_attached_attached',\n",
" 'mis_detached_attached',\n",
" 'mis_attached_detached',\n",
" \n",
" 'bs_detached_diff',\n",
" 'mis_detached_detached_diff',\n",
" \n",
" 'bs_attached_reparam',\n",
" 'mis_attached_attached_reparam',\n",
"]\n",
"diff_method_names = [\n",
" 'Detached emitter sampling',\n",
" \n",
" 'Detached BSDF sampling',\n",
" 'Attached BSDF sampling (BIASED!)',\n",
" \n",
" 'Detached MIS weights, detached BSDF sampling,\\ndetached emitter sampling',\n",
" 'Attached MIS weights, attached BSDF sampling,\\ndetached emitter sampling (BIASED!)',\n",
" 'Detached MIS weights, attached BSDF sampling,\\ndetached emitter sampling (BIASED!)',\n",
" 'Attached MIS weights, detached BSDF sampling,\\ndetached emitter sampling',\n",
" \n",
" 'Detached diff. BSDF sampling',\n",
" 'Detached MIS weights, detached diff. BSDF sampling,\\ndetached emitter sampling',\n",
" \n",
" 'Attached reparam. BSDF sampling',\n",
" 'Attached MIS weights, attached reparam. BSDF sampling,\\ndetached emitter sampling'\n",
"]\n",
"diff_method_spps = [\n",
" 128,\n",
" \n",
" 128,\n",
" 128,\n",
" \n",
" 128,\n",
" 128,\n",
" 128,\n",
" 128,\n",
" \n",
" 128,\n",
" 128,\n",
" \n",
" 64,\n",
" 64,\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c137fe74",
"metadata": {
"ExecuteTime": {
"end_time": "2022-07-28T11:38:43.576475Z",
"start_time": "2022-07-28T11:38:03.315102Z"
}
},
"outputs": [],
"source": [
"# Render a converged primal image and a finite differences gradient image for comparison\n",
"# (This might take a while ...)\n",
"eps = 1e-3\n",
"fd_spp = 2048\n",
"\n",
"integrator = mi.load_dict({'type': 'path', 'max_depth': 2})\n",
"image = mi.render(scene, params, integrator=integrator, seed=0, spp=fd_spp)\n",
"outname = \"{}/primal.exr\".format(base_dir)\n",
"mi.util.convert_to_bitmap(image, uint8_srgb=False).write(outname)\n",
"\n",
"roughness_tex_fd = roughness_tex + eps\n",
"params[param_key] = roughness_tex_fd\n",
"params.update()\n",
"\n",
"image_fd = mi.render(scene, params, integrator=integrator, seed=0, spp=fd_spp)\n",
"outname = \"{}/grad_fd.exr\".format(base_dir)\n",
"mi.util.convert_to_bitmap((image_fd - image) / eps, uint8_srgb=False).write(outname)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a705a08",
"metadata": {
"ExecuteTime": {
"end_time": "2022-07-28T11:41:34.283391Z",
"start_time": "2022-07-28T11:40:44.418618Z"
}
},
"outputs": [],
"source": [
"for method, method_name, spp in zip(diff_methods, diff_method_names, diff_method_spps):\n",
" print(\"* {}\".format(method_name))\n",
" print(\" spp = {}\".format(spp))\n",
" integrator = mi.load_dict({'type': 'estimator_comparison',\n",
" 'method': method,\n",
" 'reparam_kappa': 1e5,\n",
" 'reparam_rays': 32})\n",
" \n",
" # Diff. input parameter\n",
" pi = mi.Float(0.0)\n",
" dr.enable_grad(pi)\n",
" dr.set_grad(pi, 1.0)\n",
" params[param_key] = roughness_tex + pi\n",
" params.update()\n",
" \n",
" start = time.time()\n",
" \n",
" image_grad = mi.Float(0.0)\n",
" if 'diff' in method:\n",
" # Differential sampling strategy, use antithetic sampling.\n",
" # Note that we use the same seed twice, and use half the number of samples for each pass.\n",
" image = mi.render(scene, params,\n",
" integrator=integrator, seed=0, spp=spp//2, antithetic_pass=False)\n",
" dr.forward(pi)\n",
" image_grad = dr.grad(image)\n",
" \n",
" params[param_key] = roughness_tex + pi\n",
" params.update()\n",
"\n",
" image = mi.render(scene, params,\n",
" integrator=integrator, seed=0, spp=spp//2, antithetic_pass=True)\n",
" dr.forward(pi)\n",
" image_grad += dr.grad(image)\n",
"\n",
" # Average both passes\n",
" image_grad *= 0.5\n",
" else:\n",
" # Produce differentiable rendering\n",
" image = mi.render(scene, params, integrator=integrator, seed=0, spp=spp)\n",
" # And propagate derivatives forwards through it\n",
" dr.forward(pi)\n",
" image_grad = dr.grad(image)\n",
" \n",
" # Save output gradient image\n",
" outname = \"{}/grad_{}.exr\".format(base_dir, method)\n",
" mi.util.convert_to_bitmap(image_grad, uint8_srgb=False).write(outname)\n",
" \n",
" end = time.time() \n",
" print(\" took {:.2f} seconds\".format(end - start))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7315e600",
"metadata": {
"ExecuteTime": {
"end_time": "2022-07-28T11:44:47.308320Z",
"start_time": "2022-07-28T11:44:47.080781Z"
},
"scrolled": false
},
"outputs": [],
"source": [
"# Display images and save PNGs\n",
"image_primal = np.array(mi.Bitmap(\"{}/primal.exr\".format(base_dir)))\n",
"image_primal = np.clip(image_primal**(1/2.2), 0.0, 1.0) # Crude gamma correction\n",
"plt.imsave('{}/primal.jpg'.format(base_dir), image_primal)\n",
"\n",
"plt.figure()\n",
"plt.imshow(image_primal)\n",
"plt.axis('off')\n",
"plt.title(\"Primal\")\n",
"plt.show()\n",
"\n",
"image_fd = np.array(mi.Bitmap(\"{}/grad_fd.exr\".format(base_dir)))[:, :, 0]\n",
"plt.imsave('{}/grad_fd.jpg'.format(base_dir), image_fd, vmin=-20, vmax=+20, cmap='diff')\n",
"\n",
"plt.figure()\n",
"plt.imshow(image_fd, cmap='diff', vmin=-20, vmax=+20)\n",
"plt.axis('off')\n",
"plt.title(\"Finite differences\")\n",
"plt.show()\n",
"\n",
"for method, method_name in zip(diff_methods, diff_method_names):\n",
" image_grad = np.array(mi.Bitmap(\"{}/grad_{}.exr\".format(base_dir, method)))[:, :, 0]\n",
" plt.imsave('{}/grad_{}.jpg'.format(base_dir, method), image_grad, vmin=-20, vmax=+20, cmap='diff')\n",
" \n",
" plt.figure()\n",
" plt.imshow(image_grad, cmap='diff', vmin=-20, vmax=+20)\n",
" plt.axis('off')\n",
" plt.title(method_name, size=7)\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a93cc8d2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
16 changes: 16 additions & 0 deletions results/scenes/meshes/veach_floor.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
v -10 -4.14615 -10
v -10 -4.14615 10
v 10 -4.14615 10
v 10 -4.14615 10
v 10 -4.14615 -10
v -10 -4.14615 -10
v -10 -10 -2
v 10 -10 -2
v 10 10 -2
v 10 10 -2
v -10 10 -2
v -10 -10 -2
f 1 2 3
f 4 5 6
f 7 8 9
f 10 11 12
90 changes: 90 additions & 0 deletions results/scenes/meshes/veach_plates_bottom.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Blender v2.82 (sub 7) OBJ File: 'mis_grad.blend'
# www.blender.org
o plates_bottom_plate4
v 3.125000 -3.996151 4.066700
v 3.125000 -3.820690 3.082209
v -3.125000 -3.820690 3.082209
v -3.125000 -3.996151 4.066700
v 3.125000 -3.968363 3.055890
v -3.125000 -3.968363 3.055890
v 3.125000 -4.143824 4.040380
v -3.125000 -4.143824 4.040380
v 3.125000 -2.706510 0.256090
v 3.125000 -2.083750 -0.526323
v -3.125000 -2.083750 -0.526323
v -3.125000 -2.706510 0.256090
v 3.125000 -2.201112 -0.619737
v -3.125000 -2.201112 -0.619737
v 3.125000 -2.823872 0.162676
v -3.125000 -2.823872 0.162676
v 3.125000 -3.288250 1.369720
v 3.125000 -2.838560 0.476536
v -3.125000 -2.838560 0.476536
v -3.125000 -3.288250 1.369720
v 3.125000 -2.972538 0.409082
v -3.125000 -2.972538 0.409082
v 3.125000 -3.422228 1.302266
v -3.125000 -3.422228 1.302266
v 3.125000 -3.730960 2.700459
v 3.125000 -3.433780 1.745639
v -3.125000 -3.433780 1.745639
v -3.125000 -3.730960 2.700459
v 3.125000 -3.577003 1.701062
v -3.125000 -3.577003 1.701062
v 3.125000 -3.874184 2.655882
v -3.125000 -3.874184 2.655882
vn 0.0000 -0.9845 -0.1755
vn 0.0000 -0.1755 0.9845
vn 1.0000 0.0000 0.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.1755 -0.9845
vn 0.0000 -0.7824 -0.6228
vn 0.0000 -0.6228 0.7824
vn 0.0000 0.6228 -0.7824
vn 0.0000 -0.8932 -0.4497
vn 0.0000 -0.4497 0.8932
vn 0.0000 0.4497 -0.8932
vn 0.0000 -0.9548 -0.2972
vn 0.0000 -0.2972 0.9548
vn 0.0000 0.2972 -0.9548
s off
f 7//1 6//1 5//1
f 6//1 7//1 8//1
f 8//2 1//2 4//2
f 7//3 2//3 1//3
f 3//4 8//4 4//4
f 5//5 3//5 2//5
f 8//2 7//2 1//2
f 7//3 5//3 2//3
f 3//4 6//4 8//4
f 5//5 6//5 3//5
f 15//6 14//6 13//6
f 14//6 15//6 16//6
f 16//7 9//7 12//7
f 9//3 13//3 10//3
f 14//4 12//4 11//4
f 13//8 11//8 10//8
f 16//7 15//7 9//7
f 9//3 15//3 13//3
f 14//4 16//4 12//4
f 13//8 14//8 11//8
f 23//9 22//9 21//9
f 22//9 23//9 24//9
f 24//10 17//10 20//10
f 23//3 18//3 17//3
f 22//4 20//4 19//4
f 21//11 19//11 18//11
f 24//10 23//10 17//10
f 23//3 21//3 18//3
f 22//4 24//4 20//4
f 21//11 22//11 19//11
f 31//12 30//12 29//12
f 30//12 31//12 32//12
f 32//13 25//13 28//13
f 31//3 26//3 25//3
f 30//4 28//4 27//4
f 29//14 27//14 26//14
f 32//13 31//13 25//13
f 31//3 29//3 26//3
f 30//4 32//4 28//4
f 29//14 30//14 27//14
Loading

0 comments on commit 037830e

Please sign in to comment.