1

I have seen in a program on github which contains

img_out = np.zeros(((4,)+(512,1024,3)+(3,)))

I am trying to understand the structure of the numpy array formed from this. The documentation doesn't give any details of such complex shapes. Can someone explain me how should I interpret the structure of this array.

Sree
  • 973
  • 2
  • 14
  • 32
  • What does `((4,)+(512,1024,3)+(3,))` give you? Then read the docs. – Divakar Sep 15 '19 at 07:30
  • 1
    `>>> np.zeros(((4,)+(512,1024,3)+(3,))).shape` Does that answer your question? – PiRocks Sep 15 '19 at 07:32
  • `512` is width of the image ,`1024` is the height ,3 is channel in the image like `rbg`. Other must be some different parameter. – Hayat Sep 15 '19 at 07:33
  • @PiRocks, So, it is a 5D array. I see it has the shape (4, 512, 1024,3,3).I am assuming it as a 5D array. Is that correct – Sree Sep 15 '19 at 07:36
  • 1
    Yes. It is a 5D array, the shape is also correct. – PiRocks Sep 15 '19 at 07:37
  • The complexity of a 5d array isn't really any different from that of a 2d array, or 3d. The way arrays are defined, using `shape` and `strides` allows us to create n-d arrays (for any `n` 0 to 32). The structure doesn't really change, just values of these attributes. – hpaulj Sep 15 '19 at 07:46

3 Answers3

0

A mention by @Sree.

It's a 5D image where.

import numpy as np
a = np.zeros(((4,)+(512,1024,3)+(3,)))
a.shape

(4, 512, 1024, 3, 3) #output

512 is width of the image

1024 is the height

3 is channel in the image like rbg

so look closly on the code you will know what first and last element represent.

Hayat
  • 1,539
  • 4
  • 18
  • 32
0

you can try to understand a more simple 5 dimensions example:

print(np.zeros( (1, 2, 1, 2, 2)))

output:

[[[[[0. 0.]
    [0. 0.]]]


  [[[0. 0.]
    [0. 0.]]]]]

in this example, you can see that you have 1 element of 5th dimension, which contains 2 elements of 4th dimension which each of them contains a 1 element of 3rd dimension which contains 2 elements of 2nd dimension which each of them contains 2 elements of 1st dimension:

[<== your array 
    5th.1[<=== your 5th dimention, only 1 element of 5th dimention

        5th.1.4th.1[<=== your 4th dimention, 1st element of 4th dimention

            5th.1.4th.1.3th.1[<=== your 3rd dimention, only 1 element of  3rd dimention

                5th.1.4th.1.3th.1.2ed.1[<=== your 2ed dimention, 1st element of 2ed dimention,
                                             inside of this element are those elements of 1st dimention,
                                             there are 2 elements of 1st dimention:
                                                0.0, 0.0],

                 5th.1.4th.1.3th.1.2ed.2[<=== your 2ed dimention, 2end element of 2ed dimention,
                                              inside of this element are those elements of 1st dimention,
                                              there are 2 elements of 1st dimention:
                                                 0.0, 0.0]]], 



        5th.1.4th.2[<=== your 4th dimention, 2end element of 4th dimention

            5th.1.4th.2.3th.1[<=== your 3rd dimention, only 1 element of 3rd dimention

                5th.1.4th.2.3th.1.2ed.1[<=== your 2ed dimention, 1st element of  2ed dimention,
                                             inside of this element are those elements of 1st dimention,
                                             there are 2 element of 1st dimention:
                                                0.0, 0.0],

                 5th.1.4th.2.3th.1.2ed.2[<=== your 2ed dimention, 2end element of 2ed dimention,
                                              inside of this element are those elements of 1st dimention,
                                              there are 2 elements of 1st dimention:
                                                0.0, 0.0]]]]

for simplicity:

[1 X [2 x [1 x [[0.0, 0.0], 
                [0.0., 0.0]]]]]

you have to keep in mind that (4,)+(512,1024,3)+(3,) = (4, 512, 1024, 3, 3), they used 3 tuples to evidentiate:

  1. the number of images (4),
  2. the hight (512), the width(1024) and depth(3),
  3. the number of channels(3)

similar in your example for np.zeros(((4,)+(512,1024,3)+(3,))) you can simplify as:

 [4 X [512 X [1024 X [[0.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0]]]]]
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • thank you for such a clear explanation. However, I have a small doubt, does the depth of an image equal to number of channels? Then what is the use of 3,3 as the last two dimensions. – Sree Sep 15 '19 at 19:37
  • Just happen that here the depth is 3, can be 8 for example https://stackoverflow.com/questions/10108541/difference-between-image-depth-and-channels/10108614#10108614 – kederrac Sep 15 '19 at 19:53
0

The code above can be broken down like

shape = ((4,)+(512,1024,3)+(3,))
## above line is similar to joining list using +, and will result tuple (4,512,1024,3,3)


img_out = np.zeros(shape)
## so shape of img_out = (4,512,1024,3,3)

Dev Khadka
  • 5,142
  • 4
  • 19
  • 33