toom
toom

Reputation: 13326

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

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?

Upvotes: 2

Views: 5226

Answers (3)

pherb
pherb

Reputation: 33

This also sometimes happens if you save files from pytorch with the '.pt' extension, then try and load them with numpy. If instead you use torch.load, this may resolve your issue.

Upvotes: 0

user17242583
user17242583

Reputation:

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.]],
       ]])

Upvotes: 4

I&#39;mahdi
I&#39;mahdi

Reputation: 24049

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)

Upvotes: 2

Related Questions