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

[BUG] Non-replicatable memory requirement when using cellpose in a jupyter notebook vs script. [potential memory leak] #1094

Open
MikeLippincott opened this issue Jan 31, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@MikeLippincott
Copy link

Describe the bug
I am segmenting timelapse image datasets using cellpose.
Which has been working verywell for all purposes inside of a jupyter notebook.
When I use the same code in a script (a nbconverted script, i.e. an exact copy of the notebook) I get a large memory scaling usage.
In the notebook to segment (individually) 13 - 1900x1900 pixel images uses less than 5GB of ram and less than 2GB of VRAM.

I tried to segment just one single image (1900 x 1900 pixels) and I got the same error.

When I run the script I max out my machine's memory (128GB).
Without prior knowledge to this code, I am suspecting that this memory usage might not be a leak but rather my system using GPU in a notebook and the CPU in the script despite the code not changing.

I have a minimal example below that will hopefully reproduce on your end.

Thank you for creating and maintaining this software! Thank you for engaging with the scientific community, and thank you for your time and help!

To Reproduce/Run log
code used:

import numpy as np
from cellpose import models
from csbdeep.utils import normalize


nuclei = np.random.randint(0, 255, (1900, 1900))
print(nuclei.shape)
model_name = "nuclei"
diameter = 50
model = models.Cellpose(model_type=model_name, gpu=True)

channels = [[1, 0]]

# # get masks
masks, flows, styles, diams = model.eval(nuclei, channels=channels, diameter=diameter)

Other information
System is running Linux Pop OS 22.04.
python 3.10.16
cellpose 3.1.0
numpy 1.26.4
csbdeep 0.8.1

@MikeLippincott MikeLippincott added the bug Something isn't working label Jan 31, 2025
@MikeLippincott
Copy link
Author

For reasons unknown to me. If I add a model eval line prior to running, the error disappears.

import numpy as np
from cellpose import models
from csbdeep.utils import normalize


nuclei = np.random.randint(0, 255, (1900, 1900))
print(nuclei.shape)
model_name = "nuclei"
diameter = 50
model = models.Cellpose(model_type=model_name, gpu=True)

channels = [[1, 0]]

masks, flows, styles, diams = model.eval(nuclei, channels=channels, diameter=diameter)

# # get masks
masks, flows, styles, diams = model.eval(nuclei, channels=channels, diameter=diameter)

My speculation is that the python interpreter is doing some funky stuff in the script vs the ipykernel in a notebook.

This is likely not an issue in CellPose, but rather uncertainty in the python interpreter.

If anyone else is having this issue, please try my solution of 'tricking' the interpreter.

Thank you to the maintainers of CellPose. Please feel free to close this issue!

@carsen-stringer
Copy link
Member

sorry what exactly was the change that was needed? glad it's resolved!

@MikeLippincott
Copy link
Author

It was wierd...It seemed like I "primed" the python interpreter by running a single image through cellpose's eval prior to looping through a list of images. I could not explain why or what the python interpreter is doing.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants