mitsuba2
mitsuba2 copied to clipboard
Jupyter Kernel dies while trying to run Mitsuba python code
- [❔ other question]
Summary
I was trying to run Mitsuba in Jupyter notebook. I tried the following code:
import mitsuba import os mitsuba.set_variant('scalar_rgb') from mitsuba.core import * from mitsuba.core.xml import load_file filename = r'D:\mitsuba2\resources\data\scenes\cbox\shapeTest2.xml' Thread.thread().file_resolver().append(os.path.dirname(filename)) scene = load_file(filename) sensor = scene.sensors()[0]
film = sensor.film() film.set_destination_file(r'C:\Users\u0148719\ou.exr')
film.develop()
Whenever I use the command film.develop(), my kernel dies! It was working before in my other laptop.
System configuration
For bug report, please enter information regarding your system configuration<- [remove this]
- Platform: Windows
- Python version: 3.7
- Mitsuba 2 version: latest from github repo
- Compiled variants:
-
scalar_rgb
-
Description
Jupyter Kernel dies when trying to compile Mitsuba code. The whole code is given above.
The error log from Jupyter terminal:
[I 18:45:17.359 NotebookApp] Creating new notebook in [I 18:45:21.818 NotebookApp] Kernel started: 367aef11-1840-4f81-bf08-b03766ca876e, name: python3 [I 18:47:22.696 NotebookApp] Saving file at /Untitled.ipynb [I 18:49:22.655 NotebookApp] Saving file at /Untitled.ipynb [I 18:51:12.811 NotebookApp] KernelRestarter: restarting kernel (1/5), keep random ports kernel 367aef11-1840-4f81-bf08-b03766ca876e restarted [I 18:51:21.799 NotebookApp] Saving file at /Untitled.ipynb [I 18:51:32.172 NotebookApp] Saving file at /Untitled.ipynb [I 18:52:21.832 NotebookApp] KernelRestarter: restarting kernel (1/5), keep random ports kernel 367aef11-1840-4f81-bf08-b03766ca876e restarted [I 18:52:32.060 NotebookApp] Starting buffering for 367aef11-1840-4f81-bf08-b03766ca876e:4d794398fdfd471c8bfebe02d2174901 [I 18:52:32.407 NotebookApp] Kernel restarted: 367aef11-1840-4f81-bf08-b03766ca876e [I 18:52:32.442 NotebookApp] Restoring connection for 367aef11-1840-4f81-bf08-b03766ca876e:4d794398fdfd471c8bfebe02d2174901 [I 18:52:34.015 NotebookApp] Replaying 3 buffered messages [I 18:52:44.409 NotebookApp] KernelRestarter: restarting kernel (1/5), keep random ports kernel 367aef11-1840-4f81-bf08-b03766ca876e restarted [I 18:53:22.682 NotebookApp] Saving file at /Untitled.ipynb [I 18:58:14.439 NotebookApp] KernelRestarter: restarting kernel (1/5), keep random ports kernel 367aef11-1840-4f81-bf08-b03766ca876e restarted
Steps to reproduce
- Open jupyter notebook from Anaconda terminal
- Run the given python code
Hi @jojo96 ,
Does this also happen if you run the same code in a simple Python script?
Hi @jojo96,
Does adding film.prepare(channels)
help in that case?
https://github.com/mitsuba-renderer/mitsuba2/blob/4e7628c6eed365904ca2ba536b795d1b03410344/include/mitsuba/render/film.h#L25-L26
I believe it's required before calls to develop()
, otherwise the film's storage is not allocated (since it doesn't know yet how many channels to allocate). In your code it looks like it's missing because there's no integrator.render()
call either, which would have taken care of it.
Hi @Speierers I copied the whole code in a simple python script and it runs fine.
"" import mitsuba import os mitsuba.set_variant('scalar_rgb') from mitsuba.core import Thread from mitsuba.core.xml import load_file filename = r'D:\mitsuba2\resources\data\scenes\cbox\shapeTest2.xml'
Thread.thread().file_resolver().append(os.path.dirname(filename)) scene = load_file(filename) sensor = scene.sensors()[0]
film = sensor.film() film.set_destination_file(r'C:\Users\Jojo\Downloads\ou.exr') film.develop()
from mitsuba.core import Bitmap, Struct img = film.bitmap(raw=True).convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8, srgb_gamma=True) img.write(r'C:\Users\Jojo\Downloads\final_image.jpg') ""
But the thing is that there is no image called final _image saved at my location 'C:\Users\Jojo\Downloads\final_image.jpg'. This did not happen when the Jupyter notebook was running earlier.
Hi @merlinND
I am not sure how to use film.prepare(channels). Tried this but getting an error:
" channels = 3 film = sensor.film() film.set_destination_file(r'C:\Users\Jojo\Downloads\ou2.exr') film.prepare(channels) film.develop() "
Originally, I followed the documentation: https://mitsuba2.readthedocs.io/en/latest/src/python_interface/rendering_scene.html and prepared the script. There was no mention to film.prepare(channels) there
Hi @jojo96,
If you check out the signature of film.prepare
, it takes a list of strings (channel names). For example film.prepare(['R', 'G', 'B'])
.
In the documentation you linked, before film.develop()
is called, there's a call to:
# Call the scene's integrator to render the loaded scene with the desired sensor
scene.integrator().render(scene, sensor)
which prepares the film:
https://github.com/mitsuba-renderer/mitsuba2/blob/4e7628c6eed365904ca2ba536b795d1b03410344/src/librender/integrator.cpp#L70-L73
@merlinND I tried to put the line: scene.integrator().render(scene, sensor)
But the notebook goes on running indefinitely. Earlier, I could just render fine without this line.
If it goes on running "indefinitely", first try with low resolution and spp. As @Speierers suggested, it's good to run these tests in simple Python scripts.
I could just render fine without this line.
How were you rendering before? From the code you posted at the top, I don't see any rendering taking place:
import mitsuba
import os
mitsuba.set_variant('scalar_rgb')
from mitsuba.core import *
from mitsuba.core.xml import load_file
filename = r'D:\mitsuba2\resources\data\scenes\cbox\shapeTest2.xml'
Thread.thread().file_resolver().append(os.path.dirname(filename))
scene = load_file(filename)
sensor = scene.sensors()[0]
film = sensor.film()
film.set_destination_file(r'C:\Users\u0148719\ou.exr')
film.develop()
The film.prepare()
call I suggested should fix the crash you reported, but indeed it won't render anything unless you call integrator.render()
.
You were right. I missed the integrator.render(). Now, I prepared a simple Python file:
import mitsuba
import os
mitsuba.set_variant('scalar_rgb')
from mitsuba.core import Thread
from mitsuba.core.xml import load_file
filename = r'D:\mitsuba2\resources\data\scenes\cbox\shapeTest2.xml'
Thread.thread().file_resolver().append(os.path.dirname(filename))
scene = load_file(filename)
sensor = scene.sensors()[0]
scene.integrator().render(scene, sensor)
film = sensor.film()
film.set_destination_file(r'C:\Users\Jojo\Downloads\ou.exr')
film.develop()
from mitsuba.core import Bitmap, Struct
img = film.bitmap(raw=True).convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8, srgb_gamma=True)
img.write(r'C:\Users\Jojo\Downloads\test.jpg')
I ran it from the command line and the rgb image 'test.jpg' is produced. But still can't make it run from Jupyter notebook :( Sorry for all the trouble.
I suppose it is failing at a specific line in the code above?
@Speierers it fails at scene.integrator().render(scene, sensor)
I will need your scene file to test this on my end. Or is this happening with any scenes?
This is just happening with any scene.
Can you try with raw=False
in
img = film.bitmap(raw=False).convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8, srgb_gamma=True)
?
Hi, it's now working with both raw = True and raw = False. I have no idea why Jupyter notebook keeps breaking from time to time..
Not sure to understand how you've got it to work now. So should we keep investigating this?