最近在跑一个用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)
封装成功。