MATLAB相關(guān)內(nèi)容官方文檔
Python h5py filter pipline 官方文檔
順著官方文檔讀就行,不懂的敲一敲代碼試試就知道了;
1、用到的函數(shù):
h5create(filename,datasetname,size,Name,value)
h5write(filename,datasetname,data,start,count,stride)
2、參數(shù)說(shuō)明:filename:hdf5/h5文件的文件名,包含擴(kuò)展名,如'LLD-logo.hdf5';
? ? ? ? ? ? ? ? ? ? ? ?datasetname:數(shù)據(jù)集名字,就是在h5文件中創(chuàng)建的dataSet名字,如'data'、‘data_LL’
? ? ? ? ? ? ? ? ? ? ? ?size: 表示數(shù)據(jù)集的大小,也就是數(shù)據(jù)的shape,如果你以后要寫(xiě)入的數(shù)據(jù)的個(gè)數(shù)維度不確定,可以把個(gè)數(shù)維度設(shè)置為INF;
? ? ? ? ? ? ? ? ? ? ?? data: 要寫(xiě)入的數(shù)據(jù),其大小的'size'應(yīng)該與之前創(chuàng)建dataSet的size一致。
? ? ? ? ? ? ? ? ? ? ? ?Name,value:表示名稱(chēng)-值形式的鍵值對(duì),有些參數(shù)是可選的,通過(guò)? ‘參數(shù)名’-‘值’? 的形式進(jìn)行賦值,一般都有默認(rèn)值
start、count表示寫(xiě)入data的一個(gè)子集,start表示開(kāi)始的索引值,指定要寫(xiě)入的第一個(gè)元素,count指沿每個(gè)維度要寫(xiě)入的元素?cái)?shù)。
stride表示間距、步幅。start、count、stride構(gòu)成了data的一個(gè)超切片。
比如我要寫(xiě)入一個(gè)大小(size)為[12,12,3,24]的數(shù)組到‘myfile.h5’文件中的‘ds’數(shù)據(jù)集:可以使用命令:
h5create('myfile.h5','/ds',[12,12,3,24])
如果不存在‘myfile.h5’文件則會(huì)自動(dòng)創(chuàng)建一個(gè);如果有則向已有文件中寫(xiě)入;
這里我們創(chuàng)建一個(gè)形狀為[12,12,3,24],作為寫(xiě)入數(shù)據(jù)集的數(shù)據(jù),實(shí)際情況都是根據(jù)你要寫(xiě)入數(shù)據(jù)的形狀來(lái)定義size。
然后寫(xiě)入一個(gè)與size相同的數(shù)組到ds數(shù)據(jù)集中:
h5write('myfile.h5','/ds',mats)
這樣就可以把一個(gè)數(shù)據(jù)寫(xiě)入ds數(shù)據(jù)集中了。
記錄一下今天寫(xiě)h5文件的經(jīng)驗(yàn):
MATLAB寫(xiě)h5文件遵循:1、先創(chuàng)建一個(gè)空的h5文件,規(guī)定好這個(gè)文件的文件名、包含的數(shù)據(jù)集名、數(shù)據(jù)集的size(其實(shí)就是shape)、數(shù)據(jù)類(lèi)型,如果要規(guī)定壓縮等級(jí)(共有0-9,默認(rèn)0應(yīng)該是壓縮等級(jí)最低的),還必須要指定‘ChunkSize’,最好就規(guī)定為單位數(shù)組,比如我要寫(xiě)入的數(shù)據(jù)的總size是400x400x3x15365,我的size就設(shè)為[400x400x3x15365],ChunkSize就設(shè)為[400,400,3,1],我設(shè)置的最高壓縮等級(jí)是9,但是也會(huì)感覺(jué)文件寫(xiě)入的很慢了。2、創(chuàng)建數(shù)據(jù)集后才能用h5write方法寫(xiě)入數(shù)據(jù)集。這里可以指定向哪個(gè)h5文件的哪個(gè)數(shù)據(jù)集寫(xiě),如果沒(méi)有該數(shù)據(jù)集名會(huì)在該h5文件中創(chuàng)建該數(shù)據(jù)集,指定寫(xiě)入的數(shù)據(jù)(大小要與之前創(chuàng)建的數(shù)據(jù)集一致),以及從數(shù)據(jù)集的什么位置開(kāi)始寫(xiě):這時(shí)候就是需要指定h5write中的start和count參數(shù).你可以把MATLAB中的多維矩陣的存儲(chǔ)想象成一個(gè)個(gè)矩形小格子,比如在size為[10,20]的矩陣中的最后[5,7]塊大小的位置存儲(chǔ)實(shí)際情況是這樣的:
1,1 |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
6,14 |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
? |
10,20 |
從[6,14]開(kāi)始,存這么一塊“ChunkSize”為[5,7]的數(shù)據(jù)塊。如果是3維4維或者更高維的也是同理。
MATLAB中 h5create方法是用來(lái)創(chuàng)建數(shù)據(jù)集的,如果這個(gè)文件不存在才創(chuàng)建相應(yīng)的h5文件,否則是在h5文件中創(chuàng)建數(shù)據(jù)集;用來(lái)創(chuàng)建數(shù)據(jù)集時(shí)需要指定數(shù)據(jù)集的一些屬性,比如數(shù)據(jù)集的名稱(chēng)、size、Datatype、ChunkSize以及壓縮級(jí)別Deflate等;先創(chuàng)建使要寫(xiě)入的數(shù)據(jù)集先存在才能接著使用h5write方法寫(xiě)入數(shù)據(jù),寫(xiě)入數(shù)據(jù)時(shí)就是以創(chuàng)建數(shù)據(jù)集時(shí)的各項(xiàng)參數(shù)寫(xiě)入。最重要的是之前設(shè)置的數(shù)據(jù)類(lèi)型和壓縮級(jí)別,很影響文件大小!!!
常見(jiàn)的hdf5/h5文件的寫(xiě)入過(guò)程是:
>> data_HH = h5read('logo-1.hdf5','/data_HH');
>> h5create('test.h5','/data_HH',[200,200,3,15365],'Datatype','single','ChunkSize',[200,200,3,1],'Deflate',7)
>> h5write('test.h5','/data_HH',data_HH);
對(duì)于python來(lái)說(shuō)我之前一直沒(méi)找到如何在python中使用壓縮文件,準(zhǔn)確來(lái)說(shuō)我看文檔不夠耐心和仔細(xì),導(dǎo)致錯(cuò)過(guò)很多重要細(xì)節(jié);之前用hdf5文件格式讀取別人的文件只有幾百兆或者十幾個(gè)G,我一讀取再寫(xiě)入往往要大了許多,當(dāng)時(shí)以為是存儲(chǔ)的數(shù)據(jù)類(lèi)型引起的,但是其實(shí)是因?yàn)槲覜](méi)有用壓縮算法!!!對(duì)于官方文檔的配置說(shuō)明看的一點(diǎn)也不認(rèn)真,這一點(diǎn)也是要特別吸取教訓(xùn);
在python中只需在創(chuàng)建數(shù)據(jù)集的時(shí)候指定:
>>> dset = f.create_dataset("zipped", (100, 100), compression="gzip")
或:
>>> dset = f.create_dataset("zipped_max", (100, 100), compression="gzip", compression_opts=9)
即可。
python 創(chuàng)建壓縮的hdf5數(shù)據(jù)集:
test = h5py.File('test.h5','r+')
test.create_dataset('data_processed',data=data_processed,compression='gzip',compression_opts=7)
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
