1

I have a set of feature files in numpy format in a folder and would like to vstack them so i can add input_shape in my neural network, input_shape=(21,2048) . The numpy files as as in picture; enter image description here

The size of the np files are different, example: (16, 2048) , (8, 2048) , (5, 2048) . The maximum size is (21, 2048). I would like to pad them with zero to get size 21 and vstack them. How can i do this?

i tried:

#create a function and iterate files in folder
max_pad_len = 21
def padding(file):
    pad_features = np.pad(file_name, pad_width=([(max_pad_len - file_name.shape[0]),0],[0,0]), mode='constant', constant_values=0)
    return pad_features

visual_path = ("C:/Users/Data/9. Visual Feature/Input/")

visual_all = []
for file in os.listdir(visual_path):
    if file.endswith('.npy'):
        file_name = np.load(open(visual_path+file,"rb"))
        data = padding(file_name)
        visual_all.append(data)
vid_ft = np.vstack(visual_all)

vid_ft.shape
(23016, 2048)

it should be (6,21,2048). Where did it wrong?

  • with [np.pad](https://numpy.org/doc/stable/reference/generated/numpy.pad.html) – Nin17 Jul 18 '22 at 11:01
  • i did use it, but i didnt get the correct output. its like it didnt stack properly. i add the code in the question above – Serena Taylor Jul 18 '22 at 11:12
  • You loop is replacing the value of `visual_all` with the padded `item`. You must aggregate padded items within a list that you then feed to `np.vstack`. – Leonard Jul 18 '22 at 11:19
  • 1
    You should consider posting a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). In your case, with a couple of fake arrays of different shapes. In most cases, this helps find out bugs. – Leonard Jul 18 '22 at 11:24
  • i add my codes in the question above. the output is horribly wrong :(. how to do this? – Serena Taylor Jul 18 '22 at 12:26
  • can you add a few small arrays and the desired output for those arrays – Nin17 Jul 18 '22 at 13:54
  • sample file above is 6 feature files, the output should be (6,21,2048). I need to 1. pad the feature[0] to 21, and 2. Vstack them – Serena Taylor Jul 18 '22 at 14:05
  • Debug - make sure the padding function gers it right. Make sure the shapes in `visual_all` list are right. And look at using `np.stack`. Don't just check the last step and throw your hands up in dispair! – hpaulj Jul 18 '22 at 14:22
  • i looked back at the code and run again with np.stack instead of np.vstack and i got the result that i wanted. thank you – Serena Taylor Jul 18 '22 at 15:02

0 Answers0