mitsuba-renderer / mitsuba2

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

[🐛 bug report] - getting IrregularContinuousDistribution while inverse rendering scene with rough conductor material #617

Closed jaroslavknotek closed 2 years ago

jaroslavknotek commented 2 years ago

Summary

I have a scene with a chrome ball:

{
    "type":"sphere",
    "material": {
        "type" : "roughconductor",
        "material":"Cr",
        "id":"rods_material"    
    }
}

Using gpu_autodiff_spectral mode, I tried to optimize parameters:

ParameterMap[
  * material.eta.values,
  * material.k.values,
   ...
]

After following your tutorial here. I came up with this code:

opt = Adam(params, lr=.2)
for i in range(100):
    image = render(scene, optimizer=opt, unbiased=True, spp=16)
    ob_val = ek.hsum(ek.sqr(image - image_ref)) / len(image)
    ek.backward(ob_val)
    opt.step()

The reference image image_ref is an external image loaded this way:

Bitmap('<path>').convert(Bitmap.PixelFormat.RGB, Struct.Type.Float32, srgb_gamma=True)

Therefore I compare array of floats to array of floats both with values in the interval [0,1]. Before running the inverse rendering, I tried to render basic chrome ball, to see how it looks and it rendered just fine.

However, when I start the adam optimizer, it prints out this error + stacktrace:

/tmp/ipykernel_216/2200182066.py in <module>
     66 
     67     
---> 68     opt.step()
     69 
     70 

~/mitsuba2/build/dist/python/mitsuba/python/autodiff.py in step(self)
    362             ek.set_requires_gradient(u)
    363             self.params[k] = u
--> 364         self.params.update()
    365 
    366     def _reset(self, key):

~/mitsuba2/build/dist/python/mitsuba/python/util.py in update(self)
    124         work_list = reversed(sorted(work_list, key=lambda x: x[0]))
    125         for depth, node, keys in work_list:
--> 126             node.parameters_changed(keys)
    127         self.update_list.clear()
    128 

RuntimeError: ​[distr_1d.h:608] IrregularContinuousDistribution: entries must be non-negative!

I am using


Is it possible to limit the parameters space of ADAM optimizer so it wouldn't come up with nonsense values? I think that limiting the learning rate will help a bit but it won't prevent program from crashing. Therefore I consider this a bug.

njroussel commented 2 years ago

Hi @jaroslavknotek

I do not think that Mitsuba 2 can do this out of the box.

During the call to opt.step() we update the ParameterMap which triggers all necessary internal state re-computations. In your case, some spectral distribution needs to be recomputed. Right before that line, you could use ek.clamp or whatever code you need to guarantee that your parameters are valid before updating the ParameterMap.

jaroslavknotek commented 2 years ago

Thank you