Karl Taht
Karl Taht

Reputation: 125

How to correctly save a dictionary in .npz format

I am writing a program that loads an npz file, makes some changes to the values and stores it again. The .npz file varies in shape (dimensions).

I have gotten to the point where I can do everything, with exception that the individual numpy arrays are out of order.

Code flow:

data = dict(np.load(my_npz_file))

#Do some changes to dictionary

data_to_store = [data[layer] for layer in data]
np.savez(output_npz_file, *data_to_store)

However the problem is this, ordering is lost.

Print code:

keys=data.keys()
    for i,h in enumerate(keys):
        print "i="+str(i)+"h="+str(h)+"shape="+str(np.shape(data[h]))

Data read in format:

i=0h=arr_24shape=(128, 32, 3, 3)
i=1h=arr_25shape=(128,)
i=2h=arr_26shape=(48, 256, 1, 1)
i=3h=arr_27shape=(48,)
i=4h=arr_20shape=(32, 256, 1, 1)
i=5h=arr_21shape=(32,)
i=6h=arr_22shape=(128, 32, 1, 1)
i=7h=arr_23shape=(128,)
i=8h=arr_28shape=(192, 48, 1, 1)
i=9h=arr_29shape=(192,)
i=10h=arr_46shape=(256, 64, 1, 1)
i=11h=arr_47shape=(256,)
i=12h=arr_44shape=(64, 512, 1, 1)
i=13h=arr_45shape=(64,)
i=14h=arr_42shape=(256, 64, 3, 3)
i=15h=arr_43shape=(256,)
i=16h=arr_40shape=(256, 64, 1, 1)
i=17h=arr_41shape=(256,)
i=18h=arr_48shape=(256, 64, 3, 3)
i=19h=arr_49shape=(256,)
i=20h=arr_33shape=(48,)
i=21h=arr_32shape=(48, 384, 1, 1)
i=22h=arr_31shape=(192,)
i=23h=arr_30shape=(192, 48, 3, 3)
i=24h=arr_37shape=(192,)
i=25h=arr_36shape=(192, 48, 3, 3)
i=26h=arr_35shape=(192,)
i=27h=arr_34shape=(192, 48, 1, 1)
i=28h=arr_39shape=(64,)
i=29h=arr_38shape=(64, 384, 1, 1)
i=30h=arr_19shape=(128,)
i=31h=arr_18shape=(128, 32, 3, 3)
i=32h=arr_51shape=(1000,)
i=33h=arr_50shape=(1000, 512, 1, 1)
i=34h=arr_11shape=(64,)
i=35h=arr_10shape=(64, 16, 1, 1)
i=36h=arr_13shape=(64,)
i=37h=arr_12shape=(64, 16, 3, 3)
i=38h=arr_15shape=(32,)
i=39h=arr_14shape=(32, 128, 1, 1)
i=40h=arr_17shape=(128,)
i=41h=arr_16shape=(128, 32, 1, 1)
i=42h=arr_1shape=(96,)
i=43h=arr_0shape=(96, 3, 7, 7)
i=44h=arr_3shape=(16,)
i=45h=arr_2shape=(16, 96, 1, 1)
i=46h=arr_5shape=(64,)
i=47h=arr_4shape=(64, 16, 1, 1)
i=48h=arr_7shape=(64,)
i=49h=arr_6shape=(64, 16, 3, 3)
i=50h=arr_9shape=(16,)
i=51h=arr_8shape=(16, 128, 1, 1)

Data output format:

i=0h=arr_24shape=(192,)
i=1h=arr_25shape=(192, 48, 3, 3)
i=2h=arr_26shape=(192,)
i=3h=arr_27shape=(192, 48, 1, 1)
i=4h=arr_20shape=(48,)
i=5h=arr_21shape=(48, 384, 1, 1)
i=6h=arr_22shape=(192,)
i=7h=arr_23shape=(192, 48, 3, 3)
i=8h=arr_28shape=(64,)
i=9h=arr_29shape=(64, 384, 1, 1)
i=10h=arr_46shape=(64,)
i=11h=arr_47shape=(64, 16, 1, 1)
i=12h=arr_44shape=(16,)
i=13h=arr_45shape=(16, 96, 1, 1)
i=14h=arr_42shape=(96,)
i=15h=arr_43shape=(96, 3, 7, 7)
i=16h=arr_40shape=(128,)
i=17h=arr_41shape=(128, 32, 1, 1)
i=18h=arr_48shape=(64,)
i=19h=arr_49shape=(64, 16, 3, 3)
i=20h=arr_33shape=(1000, 512, 1, 1)
i=21h=arr_32shape=(1000,)
i=22h=arr_31shape=(128, 32, 3, 3)
i=23h=arr_30shape=(128,)
i=24h=arr_37shape=(64, 16, 3, 3)
i=25h=arr_36shape=(64,)
i=26h=arr_35shape=(64, 16, 1, 1)
i=27h=arr_34shape=(64,)
i=28h=arr_39shape=(32, 128, 1, 1)
i=29h=arr_38shape=(32,)
i=30h=arr_19shape=(256,)
i=31h=arr_18shape=(256, 64, 3, 3)
i=32h=arr_51shape=(16, 128, 1, 1)
i=33h=arr_50shape=(16,)
i=34h=arr_11shape=(256,)
i=35h=arr_10shape=(256, 64, 1, 1)
i=36h=arr_13shape=(64,)
i=37h=arr_12shape=(64, 512, 1, 1)
i=38h=arr_15shape=(256,)
i=39h=arr_14shape=(256, 64, 3, 3)
i=40h=arr_17shape=(256,)
i=41h=arr_16shape=(256, 64, 1, 1)
i=42h=arr_1shape=(128,)
i=43h=arr_0shape=(128, 32, 3, 3)
i=44h=arr_3shape=(48,)
i=45h=arr_2shape=(48, 256, 1, 1)
i=46h=arr_5shape=(32,)
i=47h=arr_4shape=(32, 256, 1, 1)
i=48h=arr_7shape=(128,)
i=49h=arr_6shape=(128, 32, 1, 1)
i=50h=arr_9shape=(192,)
i=51h=arr_8shape=(192, 48, 1, 1)

As you can see, no data is lost, but it has gotten out of order.

Upvotes: 6

Views: 14786

Answers (1)

Kos
Kos

Reputation: 72281

That's because at the beginning you convert the data to a dictionary:

data = dict(np.load(my_npz_file))

Dictionaries don't preserve order in Python (at least in your Python version), but you can use an OrderedDict.


Update: the exact problem is here...

data_to_store = [data[layer] for layer in data]
np.savez(output_npz_file, *data_to_store)

you make a list of all the layers in data, then iterate over them in a random order and write them into the file. So what was previously called arr_0 will now be for example arr_23 because that's how it can end up in a random traversal of data, and np.savez will just assign new, sequential names.

But you can also provide your own names to np.savez which will simplify your code a lot:

np.savez(output_npz_file, **data)  # data is a dict here

This will save each layer with the same name as it had originally.

Upvotes: 12

Related Questions