Home > Enterprise >  how to load a .npy dictionary using np.load()?
how to load a .npy dictionary using np.load()?

Time:10-24

I had a dictionary in this format:

{image_name.jpg:[list_of_features], image_name2.jpg:[list of it's features]}

I saved this dictionary as npy file using this code: np.save('encoded_img_dic.npy', encoded_img_dic) and loaded it back using dic = np.load('/content/encoded_img_dic.npy', allow_pickle = True)

After loading it back, it becomes a np.array and look like this:

array({'1000092795.jpg': array([ 0.18229158, -0.17214337, -0.07549195, ..., -0.01746007,
       -0.10297356,  0.35006437], dtype=float32), '10002456.jpg': array([-0.10733618, -0.08182468, -0.1734893 , ..., -0.13148569,
       -0.12901662, -0.09519334], dtype=float32), '99804383.jpg': array([-0.10597093, -0.1605651 , -0.08017335, ...,  0.03384358,
        0.58321196, -0.06605151], dtype=float32), '998845445.jpg': array([-0.13575825,  0.55340654, -0.01252322, ...,  0.37376422,
        0.02249258,  0.09976979], dtype=float32)}, dtype=object)

This array is a array of dictionary.
It has a dictionary stuck inside it. How do I get that dictionary? so it looks something like this:

{'1000092795.jpg': [ 0.18229158, -0.17214337, -0.07549195, ..., -0.01746007,
           -0.10297356,  0.35006437], '10002456.jpg': [-0.10733618, -0.08182468, -0.1734893 , ..., -0.13148569,
           -0.12901662, -0.09519334], '99804383.jpg': [-0.10597093, -0.1605651 , -0.08017335, ...,  0.03384358,
            0.58321196, -0.06605151], '998845445.jpg':[-0.13575825,  0.55340654, -0.01252322, ...,  0.37376422,
            0.02249258,  0.09976979]}

CodePudding user response:

The np.load gives you a zero-dimensional numpy array. You can get the dictionary out of there by calling the item method.

dic = np.load(filename, allow_pickle=True).item()
  • Related