You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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?
Activity
argman commentedon Jan 13, 2025
when i use GetArrayViewFromImage instead, its pretty fast, so the deep copy is slow?
zivy commentedon Jan 13, 2025
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. TheGetArrayViewFromImage
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 commentedon Jan 13, 2025
To put some timing on things here is a sample:
Output:
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 commentedon Jan 16, 2025
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?