我正在编写一个加载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 :(得分: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
这将使用与原始名称相同的名称保存每个图层。