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!