mitsuba-renderer / mitsuba2

Mitsuba 2: A Retargetable Forward and Inverse Renderer
Other
2.05k stars 266 forks source link

Why always (out of memory, key error, segment error, runtime error)?! #508

Closed HsiangYangChu closed 3 years ago

HsiangYangChu commented 3 years ago

Summary

I have encountered out of memory, key error, segment error, runtime error every time, I am gonna be mad! I doubt that I lost some key details in the process of compilation or in coding. Could someone provide some insights on that, I will be really appreciated it. Some cases are following.

System configuration

Description

I run the original code invert_pose.py in docs and change some parameters to avoid out of memory, but I get the runtime error. The code is:

import os
import time
import enoki as ek

import mitsuba
mitsuba.set_variant('gpu_autodiff_rgb')

from mitsuba.core import xml, Thread, Transform4f, Bitmap, Float, Vector3f, UInt32
from mitsuba.python.util import traverse
from mitsuba.python.autodiff import render, write_bitmap, Adam

# Convert flat array into a vector of arrays (will be included in next enoki release)
def ravel(buf, dim = 3):
    idx = dim * UInt32.arange(ek.slices(buf) // dim)
    return Vector3f(ek.gather(buf, idx), ek.gather(buf, idx + 1), ek.gather(buf, idx + 2))

# Return contiguous flattened array (will be included in next enoki release)
def unravel(source, target, dim = 3):
    idx = UInt32.arange(ek.slices(source))
    for i in range(dim):
        ek.scatter(target, source[i], dim * idx + i)

# Prepare output folder
output_path = "output/invert_pose/"
if not os.path.isdir(output_path):
    os.makedirs(output_path)

# Load example scene
# scene_folder = '../../../resources/data/docs/examples/invert_pose/'
scene_folder = './bunny/'
Thread.thread().file_resolver().append(scene_folder)
scene = xml.load_file(scene_folder + 'bunny.xml')

params = traverse(scene)
positions_buf = params['PLYMesh.vertex_positions_buf']
positions_initial = ravel(positions_buf)

# Create differential parameter to be optimized
translate_ref = Vector3f(0.0)

# Create a new ParameterMap (or dict)
params_optim = {
    "translate" : translate_ref,
}

# Construct an Adam optimizer that will adjust the translation parameters
opt = Adam(params_optim, lr=0.02)

# Apply the transformation to mesh vertex position and update scene (e.g. Optix BVH)
def apply_transformation():
    trasfo = Transform4f.translate(params_optim["translate"])
    new_positions = trasfo.transform_point(positions_initial)
    unravel(new_positions, params['PLYMesh.vertex_positions_buf'])
    params.set_dirty('PLYMesh.vertex_positions_buf')
    params.update()

# Render a reference image (no derivatives used yet)
apply_transformation()
print("in!")
image_ref = render(scene, spp=2)
print("out!")
crop_size = scene.sensors()[0].film().crop_size()
write_bitmap(output_path + 'out_ref.png', image_ref, crop_size)
print("Write " + output_path + "out_ref.png")

# Move object before starting the optimization process
params_optim["translate"] = Vector3f(0.5, 0.2, -0.2)

time_a = time.time()

iterations = 100
for it in range(iterations):
    # Perform a differentiable rendering of the scene
    image = render(scene,
                   optimizer=opt,
                   spp=2,
                   unbiased=True,
                   pre_render_callback=apply_transformation)

    write_bitmap(output_path + 'out_%03i.png' % it, image, crop_size)

    # Objective: MSE between 'image' and 'image_ref'
    ob_val = ek.hsum(ek.sqr(image - image_ref)) / len(image)

    # Back-propagate errors to input parameters
    ek.backward(ob_val)

    # Optimizer: take a gradient step -> update displacement map
    opt.step()

    print('Iteration %03i: error=%g' % (it, ob_val[0]), end='\r')

time_b = time.time()

print()
print('%f ms per iteration' % (((time_b - time_a) * 1000) / iterations))
print('Why?!')

The result is:

(mt) hsiangyangchu@zhuxiangyang-System-Product-Name:~/work/mitsuba-test/testgeo$ python invert_pose.py 
2021-10-13 10:15:43 INFO  main  [optix_api.cpp:56] Dynamic loading of the Optix library ..
2021-10-13 10:15:43 INFO  main  [xml.cpp:1182] Loading XML file "./bunny/bunny.xml" ..
2021-10-13 10:15:43 INFO  main  [xml.cpp:1183] Using variant "gpu_autodiff_rgb"
2021-10-13 10:15:43 INFO  main  [xml.cpp:353] "./bunny/bunny.xml": in-memory version upgrade (v2.0.0 -> v2.1.0) ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/pathreparam.so" ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/envmap.so" ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/uniform.so" ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/bitmap.so" ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/diffuse.so" ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/ply.so" ..
2021-10-13 10:15:43 WARN  main  [PLYMesh] "bunny.ply": performance warning -- this file uses the ASCII PLY format, which is slow to parse. Consider converting it to the binary PLY format.
2021-10-13 10:15:43 WARN  main  [PLYMesh] Attribute without postifx are not handled for now: attribute "confidence" ignored.
2021-10-13 10:15:43 WARN  main  [PLYMesh] Attribute without postifx are not handled for now: attribute "intensity" ignored.
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/hdrfilm.so" ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/gaussian.so" ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/independent.so" ..
2021-10-13 10:15:43 INFO  main  [PluginManager] Loading plugin "plugins/perspective.so" ..
2021-10-13 10:15:43 INFO  main  [Scene] Building scene in OptiX ..
in!
out!
Write output/invert_pose/out_ref.png
cuda_check(): runtime API error = 0700 "cudaErrorIllegalAddress" in ../ext/enoki/src/cuda/jit.cu:1680.
cuda_check(): runtime API error = 0700 "cudaErrorIllegalAddress" in ../ext/enoki/src/cuda/jit.cu:126.
Speierers commented 3 years ago

Hi @HsiangYangChu ,

Could you try to reduce the number of sample per pixel or the rendering resolution?

We are well aware of the heavy memory usage of those integrators and are working hard to improve this. The upcoming release will likely solve those issues.

HsiangYangChu commented 3 years ago

Hi @HsiangYangChu ,

Could you try to reduce the number of sample per pixel or the rendering resolution?

We are well aware of the heavy memory usage of those integrators and are working hard to improve this. The upcoming release will likely solve those issues.

Thanks for your reply and useful advice, it works when I reduce the number of sample per pixel or the rendering resolution! What bothers me is that the low resolution will limit my application:(!