1

I spin a loop on two sub table of my original table.

When I start the loop, and that I check the shape, I get (1008,) while the shape must be (1008,168,252,3). Is there a problem in my loop?

train_images2 = []
for i in range(len(train_2)):
  im = process_image(Image.open(train_2['Path'][i]))
  train_images2.append(im)
train_images2 = np.asarray(train_images2)
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Is this shape(1008,) of train_2 or train_images2? – Jaja Apr 13 '19 at 21:12
  • 1
    What's the dtype of the array? – hpaulj Apr 13 '19 at 21:17
  • 1
    Check that all the images that you put into the list have the same shape. If they don't, then when `train_images2` is converted to an array, it will be a one-dimensional array with data type `object`. – Warren Weckesser Apr 13 '19 at 21:48
  • 1
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Machavity Oct 21 '20 at 19:23

1 Answers1

0

The problem is that your process_image() function is returning a scalar instead of the processed image (i.e. a 3D array of shape (168,252,3)). So, the variable im is just a scalar. Because of this, you get the array train_images2 to be 1D array. Below is a contrived example which illustrates this:

In [59]: train_2 = range(1008)
In [65]: train_images2 = []

In [66]: for i in range(len(train_2)):
    ...:     im = np.random.random_sample()
    ...:     train_images2.append(im)
    ...: train_images2 = np.asarray(train_images2)
    ...: 

In [67]: train_images2.shape
Out[67]: (1008,)

So, the fix is that you should make sure that process_image() function returns a 3D array as in the below contrived example:

In [58]: train_images2 = []
In [59]: train_2 = range(1008)

In [60]: for i in range(len(train_2)):
    ...:     im = np.random.random_sample((168,252,3))
    ...:     train_images2.append(im)
    ...: train_images2 = np.asarray(train_images2)
    ...: 

# indeed a 4D array as you expected
In [61]: train_images2.shape
Out[61]: (1008, 168, 252, 3)
kmario23
  • 57,311
  • 13
  • 161
  • 150