Issue
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?
Solution
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.]],
]])
Answered By - user17242583
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.