0

I am trying to use the scipy compatible finction of cupy namely the map_coordinates function. However, after a lot of tinkering I am unable to get it to work. So, I do something as follows:

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

# Generate some random data
x = np.random.rand(10, 10, 10)
# Move it to the GPU
x = cp.array(x)

# Generate some coordinates
d = [cp.array(np.random.rand(10, 10, 10)), 
     cp.array(np.random.rand(10, 10, 10)), 
     cp.array(np.random.rand(10, 10, 10))]

d = cp.stack(d)

print(d.shape)   # (3, 10, 10, 10)

cupyx.scipy.ndimage.map_coordinates(x, d, cp.float32, order=1, 
                                    mode='constant', cval=0) 

Now, this returns the error: ValueError: Out shape is mismatched.

I am not sure now why this should be the case as the output should be generated by the function itself. The coordinates are the correct 4D shape, think. I am really at a loss on how to make this work. Unfortunately, I could not come across any working example of this on the net as well.

4

1 回答 1

3

It looks like a bug of CuPy (v6 and v7 beta2). I filed an issue. It only fails when the output has multiple axes, so until the bug is fixed, you can work around this bug by flattening the coordinates array except for the first axis, like:

cupyx.scipy.ndimage.map_coordinates(
    x, d.reshape(len(d), -1), order=1, mode='constant', cval=0
).reshape(d.shape[1:])
于 2019-07-30T01:45:47.470 回答