2017-09-05 58 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