Skip to content

sitk.GetArrayFromImage is extremely slow #2229

Open
@argman

Description

it takes several seconds to transform sitk image to numpy array, why?

Activity

changed the title resample using sitk.sitkLinear interpolater is extremely slow sitk.GetArrayFromImage is extremely slow on Jan 13, 2025
argman

argman commented on Jan 13, 2025

@argman
Author

when i use GetArrayViewFromImage instead, its pretty fast, so the deep copy is slow?

zivy

zivy commented on Jan 13, 2025

@zivy
Member

Hello @argman,

What you are experiencing is a design decision, a feature, not a bug.

The GetArrayFromImage function creates a copy of the data which can be modified. The runtime depends on the size of the image, so can take a while if dealing with large images. The GetArrayViewFromImage is as the name suggests, a view on the image data. Note that the returned numpy array cannot be modified and that the numpy array is only valid if the SimpleITK image is valid (doesn't go out of scope at which point the SimpleITK image memory is released).

Choosing which method to use depends on your needs.

blowekamp

blowekamp commented on Jan 13, 2025

@blowekamp
Member

To put some timing on things here is a sample:

img = %time sitk.Image([1024, 1024,1024], sitk.sitkUInt8)
a = %time sitk.GetArrayViewFromImage(img)
print(a.shape, memoryview(a).readonly)
a = %time sitk.GetArrayFromImage(img)
print(a.shape, memoryview(a).readonly)

Output:

CPU times: user 16.3 ms, sys: 64.9 ms, total: 81.1 ms
Wall time: 79.9 ms
CPU times: user 35 μs, sys: 0 ns, total: 35 μs
Wall time: 36 μs
(1024, 1024, 1024) True
CPU times: user 31.5 ms, sys: 61.5 ms, total: 93 ms
Wall time: 93.1 ms
(1024, 1024, 1024) False

Do you get similar results? Notice that the time to create a new image is just 13 ms slower that to create a zero filled image. This comparison indicates that the copy is reasonably performant.

argman

argman commented on Jan 16, 2025

@argman
Author

To put some timing on things here is a sample:

img = %time sitk.Image([1024, 1024,1024], sitk.sitkUInt8)
a = %time sitk.GetArrayViewFromImage(img)
print(a.shape, memoryview(a).readonly)
a = %time sitk.GetArrayFromImage(img)
print(a.shape, memoryview(a).readonly)

Output:

CPU times: user 16.3 ms, sys: 64.9 ms, total: 81.1 ms
Wall time: 79.9 ms
CPU times: user 35 μs, sys: 0 ns, total: 35 μs
Wall time: 36 μs
(1024, 1024, 1024) True
CPU times: user 31.5 ms, sys: 61.5 ms, total: 93 ms
Wall time: 93.1 ms
(1024, 1024, 1024) False

Do you get similar results? Notice that the time to create a new image is just 13 ms slower that to create a zero filled image. This comparison indicates that the copy is reasonably performant.

I think your test is normal, but when i encounterd this issue is when the system runs in high load(cpu and memory almost 100%).

what i do is i want to create a pytorch tensor from the image, so i convert sitk.image to numpy array and then to pytorch tensor, now i skip the numpy part and directly create tensor from the array view, the conversion to numpy array can be quite slow.

i find it maybe a system issue or numpy issue, when the system is highly occupied, the getarryfromimage method can take several seconds to finish, but when i change to use pytorch, it just take several ms.

i dont know the reason, maybe its correspond to system memory cache?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      sitk.GetArrayFromImage is extremely slow · Issue #2229 · SimpleITK/SimpleITK