How to save arrays in a .npz structure compatible with FBK Fairseq for Direct Speech Translation?

Viewed 210

I generated a npz folder with numpy with the code np.savez(outpath + "/data.npz", **keywords) where keywords is a dictionary structured as:

"0" : array
"1" : array

Each array is a 2D array containing MFCC features extracted with speechpy. For instance the array for the key 0 has shape (518, 13).

This is the folder and filename structure of the resulting npz folder:

data.npz
    0.npy
    1.npy

Since I have to feed the npz file to a sequence modeling toolkit for speech translation FBK-Fairseq-ST build on pytorch, the function to read the npz file is as it follows:

def reader_npz(path):
   with open(path, 'rb') as f:
      shape = np.load(f)
      for i in range(int(shape[0])):
         yield torch.from_numpy(np.load(f))

I had to modify the line for i in range(int(shape["0"])) to for i in range(int(shape[0])) as suggested in the comments by @V. Ayrat, to avoid a key error.

The problem is that this results in the TypeError: only size-1 arrays can be converted to Python scalars since I am giving a 2D array to int.

In fact, if my .npz folder contains 100 npy files, shape=np.load(f) will result in a set of a 2D arrays from shape["0"] to shape["99"].

How should I save the .npy files in the .npz file in order to make the .npz folder readable by the function reader_npz(path) in the FAIR's fairseq script above?

Thanks in advance!

1 Answers

I found a way to read the .npz folder containing the arrays in the.npy files by modifying the reader_npz function as it follows:

def reader_npz(path):
    with open(path, 'rb') as f:
        arrays = np.load(f)
        for key in arrays:
            yield torch.from_numpy(arrays[key])

arrays = np.load(f) results in the list of filenames in the folder. Since each file contains a 2D array, arrays[key] is the 2D array itself, as you can test by printing arrays[key]:

print(arrays[key])
[[-1.7862251  -0.3740275   0.5878265  ...  0.56670946  0.23715064
   0.28952855]
 [-2.3202019  -0.3106088  -0.5866199  ... -0.57073885 -1.3251289
  -0.05244343]
 [-0.88320863  0.04667355 -1.7014104  ...  1.3024858   1.3273206
   1.1638638 ]
 ...
 [ 0.4545314  -0.93115485 -1.2533125  ...  0.32433906  0.31202883
   0.11585686]
 [ 0.04456866 -1.161861   -1.6719444  ...  1.4855083   0.38237372
   0.26423842]
 [-0.18203917 -0.2660923  -0.66291505 ... -1.3389368  -2.3973744
  -0.84333473]]
Related