Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jupyter Kernel dies while trying to run Mitsuba python code #515

Open
jojo96 opened this issue Oct 18, 2021 · 15 comments
Open

Jupyter Kernel dies while trying to run Mitsuba python code #515

jojo96 opened this issue Oct 18, 2021 · 15 comments

Comments

@jojo96
Copy link

jojo96 commented Oct 18, 2021

  • [❔ 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

  1. Open jupyter notebook from Anaconda terminal
  2. Run the given python code
@Speierers
Copy link
Member

Hi @jojo96 ,

Does this also happen if you run the same code in a simple Python script?

@merlinND
Copy link
Member

Hi @jojo96,

Does adding film.prepare(channels) help in that case?

/// Configure the film for rendering a specified set of channels
virtual void prepare(const std::vector<std::string> &channels) = 0;

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.

@jojo96
Copy link
Author

jojo96 commented Oct 19, 2021

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.

@jojo96
Copy link
Author

jojo96 commented Oct 19, 2021

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

@merlinND
Copy link
Member

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:

// Insert default channels and set up the film
for (size_t i = 0; i < 5; ++i)
channels.insert(channels.begin() + i, std::string(1, "XYZAW"[i]));
film->prepare(channels);

@jojo96
Copy link
Author

jojo96 commented Oct 19, 2021

@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.

@merlinND
Copy link
Member

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().

@jojo96
Copy link
Author

jojo96 commented Oct 19, 2021

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.

@Speierers
Copy link
Member

I suppose it is failing at a specific line in the code above?

@jojo96
Copy link
Author

jojo96 commented Nov 3, 2021

@Speierers it fails at scene.integrator().render(scene, sensor)

@Speierers
Copy link
Member

I will need your scene file to test this on my end. Or is this happening with any scenes?

@jojo96
Copy link
Author

jojo96 commented Nov 3, 2021

This is just happening with any scene.

@Speierers
Copy link
Member

Can you try with raw=False in
img = film.bitmap(raw=False).convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8, srgb_gamma=True)?

@jojo96
Copy link
Author

jojo96 commented Nov 4, 2021

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..

@Speierers
Copy link
Member

Not sure to understand how you've got it to work now. So should we keep investigating this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants