question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

```cupyx.scipy.ndimage.zoom``` returns incorrect values

See original GitHub issue

Description

Hi, I am currently working on a project that I need to scale a 4-d uint8 array by a factor in each axis. I first tried the scipy.ndimage.zoom, but it was slow. I later found out that cupyx supports this function so I changed to cupyx.

However, the array returned is not the same as the scipy one. Around 0.0044% of the values are not the same. My guess is that cupyx set some of the values to 0 directly (maybe due to error, I am not sure).

This is the same cross-section of the two arrays: Cupyx PWHOR190733102S_11Oct2021_CWJKG6YV_3DQ_Apical_LV_short_axis_view_SAXA_Frame0000

Scipy PWHOR190733102S_11Oct2021_CWJKG6YV_3DQ_Apical_LV_short_axis_view_SAXA_Frame0000

You can see there are differences on the top.

To Reproduce

import numpy as np
import cupy as cp
import cupyx.scipy.ndimage
import scipy.ndimage

# Inputs:
# orginal_array: 4d uint8 numpy shape: (208, 208, 224, 21) 
# space_directions: [0.97212501, 1.37501857, 1.15326139, 1]

data_array_cupyx = cupyx.scipy.ndimage.zoom(cp.array(original_array, dtype=cp.uint8), space_directions, output=cp.uint8).get().astype(np.uint8)
data_array_scipy = scipy.ndimage.zoom(original_array, space_directions, output=np.uint8)

diff_array = data_array_cupyx != data_array_scipy
print(np.prod(data_array_cupyx.shape))
# >>> 313008696
print(np.sum(diff_array))
# >>> 13921

Installation

Source (pip install cupy)

Environment

OS                           : Windows-10-10.0.19041-SP0
Python Version               : 3.8.3
CuPy Version                 : 10.2.0
CuPy Platform                : NVIDIA CUDA
NumPy Version                : 1.22.3
SciPy Version                : 1.8.0
Cython Build Version         : 0.29.28
Cython Runtime Version       : None
CUDA Root                    : C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2
nvcc PATH                    : C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin\nvcc.EXE
CUDA Build Version           : 11020
CUDA Driver Version          : 11060
CUDA Runtime Version         : 11020
cuBLAS Version               : (available)
cuFFT Version                : 10401
cuRAND Version               : 10203
cuSOLVER Version             : (11, 1, 0)
cuSPARSE Version             : (available)
NVRTC Version                : (11, 2)
Thrust Version               : 101000
CUB Build Version            : 101000
Jitify Build Version         : <unknown>
cuDNN Build Version          : 8101
cuDNN Version                : 8101
NCCL Build Version           : None
NCCL Runtime Version         : None
cuTENSOR Version             : None
cuSPARSELt Build Version     : None
Device 0 Name                : NVIDIA GeForce GTX 1060 3GB
Device 0 Compute Capability  : 61
Device 0 PCI Bus ID          : 0000:01:00.0

Additional Information

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
PCRedHotcommented, Mar 10, 2022

Sure, here is a subset of the data. part.zip

Script

import numpy as np
import cupy as cp
import cupyx.scipy.ndimage
import scipy.ndimage

numpy_file_path = 'path_to/part.npy'

space_directions = [0.97212501, 1.37501857, 1.15326139]
original_array = np.load(numpy_file_path)

# original_array shape: (208, 208, 224) uint8

data_array_cupyx = cupyx.scipy.ndimage.zoom(cp.array(original_array, dtype=cp.uint8), space_directions, output=cp.uint8).get()
data_array_scipy = scipy.ndimage.zoom(np.array(original_array, dtype=np.uint8), space_directions, output=np.uint8)

# new array shape: (202, 286, 258)

# Differences
diff_array = data_array_cupyx != data_array_scipy
print('Different Values:', np.sum(diff_array), 'out of', np.prod(data_array_cupyx.shape))
# >>> Different Values: 568 out of 14905176
0reactions
grlee77commented, Mar 9, 2022

Okay, that is good that the floating point case followed by casting is consistent. That shouldn’t be necessary as ideally output=cp.uint8 should work the same way as in scipy. If you are able to make a small subset of the data where you see the issue available for us to test with, that could help look into it a bit more.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cupyx.scipy.ndimage.zoom — CuPy 11.4.0 documentation
zoom (float or sequence) – The zoom factor along the axes. If a float, zoom is the same for each axis. If a...
Read more >
scipy.ndimage.zoom — SciPy v1.9.3 Manual
The zoom factor along the axes. If a float, zoom is the same for each axis. If a sequence, zoom should contain one...
Read more >
CuPy Documentation - Read the Docs
Multidimensional Image Processing (cupyx.scipy.ndimage.*) ... Note: The kernel does not have return values. You need to pass both input ...
Read more >
problem with the 2d interpolation method using scipy.ndimage ...
jpg',1) # returns RGB image img_1 = scipy.ndimage.zoom(img,[3,3,1], order=1) # zoom should contain one value for each axis.
Read more >
Source code for monai.transforms.spatial.array
Tensor: """ Small fn to simplify returning data. If `MetaTensor`, update affine. ... [docs]class Zoom(InvertibleTransform): """ Zooms an ND image using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found