4

I have a numpy array batch of shape (32,5). Each element of the batch consists of a numpy array batch_elem = [s,_,_,_,_] where s = [img,val1,val2] is a 3-dimensional numpy array and _ are simply scalar values. img is an image (numpy array) with dimensions (84,84,3)

I would like to create a numpy array with the shape (32,84,84,3). Basically I want to extract the image information within each batch and transform it into a 4-dimensional array.

I tried the following:

b = np.vstack(batch[:,0]) #this yields a b with shape (32,3), type: <class 'numpy.ndarray'>

Now I would like to access the images (first index in second dimension)

img_batch = b[:,0] # this returns an array of shape (32,), type: <class 'numpy.ndarray'>

How can I best access the image data and get a shape (32,84,84,3)?

Note:

 s = b[0] #first s of the 32 in batch: shape (3,) , type: <class 'numpy.ndarray'>

Edit:

This should be a minimal example:

img = np.zeros([5,5,3])
s = np.array([img,1,1])
batch_elem = np.array([s,1,1,1,1])
batch = np.array([batch_elem for _ in range(32)])
12
  • img_batch=np.array([a[i, 0] for i in range(a.shape[0])]) should work Commented Jul 21, 2017 at 14:37
  • Is a[0,0] a list? Commented Jul 21, 2017 at 14:41
  • np.array([a[i,0][0] for i in range(a.shape[0])]) works. But is there a way without list comprehensions? Commented Jul 21, 2017 at 14:41
  • a[0,0] is <class 'numpy.ndarray'> Commented Jul 21, 2017 at 14:43
  • 1
    It would be nice if you could construct a minimal example of a , preferably with a few less dimensions and provide it for us. At least the process you used to construct the minimal example. Commented Jul 21, 2017 at 14:47

2 Answers 2

3

Assuming I understand the problem correctly, you can stack twice on the last axis.

res = np.stack(np.stack(batch[:,0])[...,0])

Sign up to request clarification or add additional context in comments.

1 Comment

Wow this is pretty amazing. I was stuck for ages on this :). Looks good - it gives a shape of (32, 5, 5, 3)
1
import numpy as np

# fabricate some data
batch = np.array((32, 1), dtype=object)
for i in range(len(batch)):
    batch[i] = [np.random.rand(84, 84, 3), None, None]

# select images
result = np.array([img for img, _, _ in batch])

# double check!
for i in range(len(batch)):
    assert np.all(result[i, :, :, :] == batch[i][0])

1 Comment

Thank you, I found that this works: np.array([batch[i,0][0] for i in range(batch.shape[0])]). I was looking for a solution without list comprehensions/loops. If that is possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.