Home > other >  Unable to load numpy npz file - instead returns a numpy.lib.npyio.NpzFile
Unable to load numpy npz file - instead returns a numpy.lib.npyio.NpzFile

Time:06-06

When trying to load a npz file with load as described in the numpy documentation, I am getting on object of type numpy.lib.npyio.NpzFile.

This is the code:

imgs = np.ones((3,32,32,3))
masks = np.ones((3,32,32,1))
array_dict = {"imgs" : imgs, "masks": masks}
np.savez_compressed("/path/to/imgs_with_masks.npz", array_dict)
loaded_data = np.load("/path/to/imgs_with_masks.npz")
print(type(loaded_data), " - ", loaded_data)

The output of this is:

<class 'numpy.lib.npyio.NpzFile'>  -  <numpy.lib.npyio.NpzFile object at 0x2b766fa00>

I am expecting a decompressed structure I can work with but instead I am getting an undocumneted object. What is going on here and how do I get my data from this?

CodePudding user response:

You need to pass array_dict with ** to np.savez_compressed. If you pass an arbitrary Python object, numpy will use pickle instead of just compressing the arrays directly.

Once you've passed your arrays as kwargs, when you load the npz file, you can access the arrays using bracket notation:

# Save
>>> np.savez_compressed("imgs_with_masks.npz", **array_dict)

# Now load
>>> loaded = np.load("imgs_with_masks.npz")
>>> loaded
<numpy.lib.npyio.NpzFile at 0x1318ad940>

>>> list(loaded.keys())
['imgs', 'masks']

>>> loaded['img']
array([[[[1., 1., 1.],
         [1., 1., 1.],
         [1., 1., 1.],
         ...
        ]]])

>>> loaded['masks']
array([[[[1.],
         [1.],
         [1.],
         ...,
         [1.],
         [1.],
         [1.]],
       ]])

CodePudding user response:

Always, when I want to create a numpy .npz file, I write like the below:

Save NPZ File:

import numpy as np
imgs  = np.ones((3,32,32,3))
masks = np.ones((3,32,32,1))
np.savez_compressed('imgs_with_masks.npz', imgs=imgs, masks=masks)

Load NPZ File:

npz_dataset = np.load('imgs_with_masks.npz')
print(npz_dataset.files)
imgs  = npz_dataset['imgs']
masks = npz_dataset['masks']
print(imgs.shape)
print(masks.shape)

Output:

['imgs', 'masks']
(3, 32, 32, 3)
(3, 32, 32, 1)
  • Related