Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacking numpy arrays with padding

I have a list of 32 numpy arrays, each of which has shape (n, 108, 108, 2), where n is different in each array. I want to stack all of them to create a numpy array of shape (32, m, 108, 108, 2), where m is the maximum among the ns, and the shorter arrays are padded with zeros.

How do I do this?

I asked something similar yesterday, but the answers there seem to break when using deep arrays like in my case.

Concretely, I went with this solution in the end, which produced the cleanest code:

data = np.column_stack(zip_longest(*data, fillvalue=0))

But now it is throwing this error:

ValueError: setting an array element with a sequence.
like image 578
Jsevillamol Avatar asked Jan 02 '23 16:01

Jsevillamol


2 Answers

I have found a godly answer in this webpage.

The pad_sequences function is exactly what I needed.

from tensorflow.python.keras.preprocessing.sequence import pad_sequences
result = pad_sequences(imgs, padding='post')
like image 109
Jsevillamol Avatar answered Jan 13 '23 15:01

Jsevillamol


In my case I needed to stack images with different width and padded with zeros to the left side. for me this works well:

np.random.seed(42)
image_batch = []
for i in np.random.randint(50,500,size=10):
image_batch.append(np.random.randn(32,i))
for im in image_batch:
    print(im.shape)

output: (32, 152) (32, 485) (32, 398) (32, 320) (32, 156) (32, 121) (32, 238) (32, 70) (32, 152) (32, 171)

def stack_images_rows_with_pad(list_of_images):
    func = lambda x: np.array(list(zip_longest(*x, fillvalue=0))) # applied row wise
    return np.array(list(map(func, zip(*list_of_images)))).transpose(2,0,1)

res = stack_images_rows_with_pad(image_batch)

for im in rez:
    print(im.shape)

output: (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485) (32, 485)

like image 39
D.Shaulskii Avatar answered Jan 13 '23 14:01

D.Shaulskii