user785099
user785099

Reputation: 5563

regarding loading the npy file and investigating the contents inside it

I have been trying to output the contents of npy file, when print(np.load('/home/ugwz/fcn/vgg16.npy', encoding='latin1')), part of the output looks like as follows, which is sort of hard to read.

enter image description here

Then I try to output the metadata of this array

print(np.load('/home/vgg16.npy', encoding='latin1').size)
print(np.load('/home/vgg16.npy', encoding='latin1').shape)
print(np.load('/home/vgg16.npy', encoding='latin1').ndim)

the output is as follows:

1
()
0

What's the best way to read and understand the npy file?

The additional information is as follows:

print(np.load('/home/vgg16.npy',encoding='latin1').dtype)

object

print(np.load('/home/vgg16.npy',encoding='latin1').item().type)

AttributeError: 'dict' object has no attribute 'type'


print(np.load('/home/vgg16.npy',encoding='latin1').item().shape)

AttributeError: 'dict' object has no attribute 'shape'

Upvotes: 2

Views: 2480

Answers (1)

hpaulj
hpaulj

Reputation: 231385

Based on the end of your screen shot

....], dtype=float)]}

I'd expect the start to be {akey: [array(..... In other words, a dictionary (one or more items), a list (of at least one item), and 1d array.

Though your size, shape, ndim values indicate that this is a single item, 0 dimensional array. What is its dtype. I'm guessing dtype=object.

It looks like there's a 1d array embedded in a list and/or dictionary and/or an object dtype array.

I haven't used the encoding parameter. Its doc is:

encoding : str, optional

What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. Values other than 'latin1', 'ASCII', and 'bytes' are not allowed, as they can corrupt numerical data. Default: 'ASCII'

That's consistent with this file containing a pickled object. pickling is the general Python tool for saving lists, dictionaries, etc. np.save/load can handle pickled objects, but save numpy arrays in its special format, in effect an array pickle.

I wonder if this file can be loaded with pickle (load?), and if that's any clearer?

I'd be tempted to try this load with allow_pickle=False just to verify whether it is trying to handle pickled objects, including dtype=object arrays.

Another to try is

 data = load...
 print(data.dtype)   # object?
 d1 = data[()]       # or
 d1 = data.item()

Either of those to statements should extract the single element from a 0d array. Then try to identify d1 (type, shape, dtype, etc).

Upvotes: 2

Related Questions