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

rectangle_perimeter with NaN params causes fatal memory leak #7114

Open
eyaler opened this issue Sep 2, 2023 · 6 comments
Open

rectangle_perimeter with NaN params causes fatal memory leak #7114

eyaler opened this issue Sep 2, 2023 · 6 comments

Comments

@eyaler
Copy link

eyaler commented Sep 2, 2023

Description:

Following code raises RAM usage to 100% within a few seconds and hangs the (64GB) machine. I had the NaN's due to an invalid bounding box calculation.

Way to reproduce:

import numpy as np
from skimage.draw import rectangle_perimeter
rectangle_perimeter((np.nan, np.nan), (np.nan, np.nan))

Version information:

3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
Windows-10-10.0.22621-SP0 (I am actually on Windows 11)
scikit-image version: 0.21.0
numpy version: 1.24.3
@eyaler eyaler added the 🐛 Bug label Sep 2, 2023
@lagru
Copy link
Member

lagru commented Sep 2, 2023

Hmm, I can't reproduce the RAM bombing on my machine. Instead I get

skimage/draw/draw.py:959: RuntimeWarning: invalid value encountered in cast
  top_left = np.round(top_left).astype(int)
skimage/draw/draw.py:960: RuntimeWarning: invalid value encountered in cast
  bottom_right = np.round(bottom_right).astype(int)
skimage/draw/draw.py:271: RuntimeWarning: invalid value encountered in cast
  r = np.round(r).astype(int)
skimage/draw/draw.py:272: RuntimeWarning: invalid value encountered in cast
  c = np.round(c).astype(int)

and this output

(array([-9223372036854775808, -9223372036854775808, -9223372036854775808,
        -9223372036854775808]),
 array([-9223372036854775808, -9223372036854775808, -9223372036854775808,
        -9223372036854775808]))

I am a bit surprised because from a casual look there isn't much that might be platform specific to the involved code. Maybe

top_left = np.round(top_left).astype(int)
bottom_right = np.round(bottom_right).astype(int)

is behaving different for you? Could you check the output of

np.round((np.nan, np.nan)).astype(int)

on your machine?

In any case, we should probably introduce a guard that checks for NaN, which in this case shouldn't have any noticeable performance implications.

@lagru
Copy link
Member

lagru commented Sep 2, 2023

Oh, and thanks for the report @eyaler!

@lagru lagru changed the title draw with NaN params causes fatal memory leak rectangle_perimeter with NaN params causes fatal memory leak Sep 2, 2023
@eyaler
Copy link
Author

eyaler commented Sep 2, 2023

@lagru

> np.round((np.nan, np.nan)).astype(int)
<ipython-input-3-ea42ef702220>:1: RuntimeWarning: invalid value encountered in cast
  np.round((np.nan, np.nan)).astype(int)
Out[3]: array([-2147483648, -2147483648])

then you have in https://github.com/scikit-image/scikit-image/blob/v0.21.0/skimage/draw/draw.py#L929
top_left -= 1

so let do:
a = np.round((np.nan, np.nan)).astype(int); a -= 1; print(a)
and we get:
array([2147483647, 2147483647])

that's one big rectangle!

@lagru
Copy link
Member

lagru commented Sep 28, 2023

Yes, this doesn't seem to happen on my hardware because np.round((np.nan, np.nan)).astype(int) returns array([-9223372036854775808, -9223372036854775808]) for me. That number is negative and get's treated differently.

Are you interested in making a PR that adds a check for NaN's to address this issue? A simple if np.isnan(...): raise ... + a test should suffice. :D

@lagru
Copy link
Member

lagru commented Sep 28, 2023

Re my comment above, I actually forgot that I already made a PR for this #7115. 🙈 😄

Copy link

Hello scikit-image core devs! There hasn't been any activity on this issue for more than 180 days. I have marked it as "dormant" to make it easy to find.
To our contributors, thank you for your contribution and apologies if this issue fell through the cracks! Hopefully this ping will help bring some fresh attention to the issue. If you need help, you can always reach out on our forum
If you think that this issue is no longer relevant, you may close it, or we may do it at some point (either way, it will be done manually).

@github-actions github-actions bot added the 😴 Dormant no recent activity label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants