最近在跑一個用GAN做圖片壓縮的網絡,訓練測試驗證集的信息都需要封裝成HDF5文件,因此記錄一下供自己參考。我需要訓練自己的數據集,因此要寫一個新的HDF5。
工程的指導上寫的是:
In each case, you will need to create a Pandas dataframe containing a single column: path
, which holds the absolute/relative path to the images. This should be saved as a HDF5
file。
打印了一下已有的cityscapes_paths_test.h5,顯示如下:
因爲我加了語義的mask,打算使用cGAN,所以需要再加一列語義的label路徑。(You will need to download the gtFine
dataset of annotation maps and append a separate column semantic_map_paths
to the Pandas dataframe pointing to the corresponding images from the gtFine
dataset.)
代碼如下:
import glob
import os
import pandas as pd
img_path = '' #圖片路徑
map_path = '' #語義label路徑
i_paths = glob.glob(os.path.join(img_path,'*.png'))
i_paths.sort()
map_paths = glob.glob(os.path.join(map_path,'*.png'))
map_paths.sort()
d = {'semantic_map_paths': map_paths}
df = pd.DataFrame(data=d) #生成dataframe
df.insert(0,'path',i_paths) #加入一列
#print(df)
df.to_hdf('kitti_paths_train.h5', key='test') #轉存爲h5文件
然後讀取看看格式:
f = pd.read_hdf('kitti_paths_train.h5')
print(f)
封裝成功。