2017-09-05 348 views
0

一本字典我正在写一个加载NPZ文件的程序,做了一些变更,再次的值,并将其存储。 .npz文件的形状(尺寸)有所不同。如何正确保存.npz格式

我得到的地步,我所能做的一切,与不同的是,个人numpy的阵列发生故障。

码流:

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) 

然而,问题是这样的,订货会丢失。

打印代码:

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

数据读取格式:

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) 

数据输出格式:

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) 

正如你所看到的,不会丢失任何数据,但它有发生故障。

+0

显示打印代码。名字的来源不清楚。 – hpaulj

+0

哦,对不起。将更新。 –

+0

'data_to_store'是一个列表,而不是一个字典。这些名称然后自动生成 – hpaulj

回答

1

这是因为在开始的时候你的数据转换为词典:

data = dict(np.load(my_npz_file)) 

字典没有在Python(at least in your Python version)维持秩序,但你可以使用OrderedDict


更新:确切的问题是在这里...

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

您在数据中的所有图层的列表,然后遍历他们在一个随机的顺序,并将其写入到文件中。那么,以前被称为arr_0现在将例如arr_23,因为这是它如何能在数据的随机遍历结束,并np.savez只会分配新的,连续的名字。

但你也可以提供自己的名字np.savez这将简化代码很多:

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

这将节省每一层具有相同的名称,因为它原本就有。

+0

因此,如果我使用有序的字典,那么它应该全部保存? 有什么奇怪的是,我打印出我的字典中的订购,它似乎保存在那里? –

+0

你的Python版本是什么? – Kos

+0

嗯,不只是丢失的顺序:自动由savez()函数生成的输出表明,不同的键映射到不同的值... – Kos