DDR爱好者之家 Design By 杰米
1. h5py简单介绍
h5py文件是存放两类对象的容器,数据集(dataset)和组(group),dataset类似数组类的数据集合,和numpy的数组差不多。group是像文件夹一样的容器,它好比python中的字典,有键(key)和值(value)。group中可以存放dataset或者其他的group。”键”就是组成员的名称,”值”就是组成员对象本身(组或者数据集),下面来看下如何创建组和数据集。
1.1 创建一个h5py文件
import h5py #要是读取文件的话,就把w换成r f=h5py.File("myh5py.hdf5","w")
在当前目录下会生成一个myh5py.hdf5文件。
2. 创建dataset数据集
import h5py f=h5py.File("myh5py.hdf5","w") #deset1是数据集的name,(20,)代表数据集的shape,i代表的是数据集的元素类型 d1=f.create_dataset("dset1", (20,), 'i') for key in f.keys(): print(key) print(f[key].name) print(f[key].shape) print(f[key].value)
输出:
dset1 /dset1 (20,) [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] import h5py import numpy as np f=h5py.File("myh5py.hdf5","w") a=np.arange(20) d1=f.create_dataset("dset1",data=a) for key in f.keys(): print(f[key].name) print(f[key].value)
输出:
/dset1 [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] 2. hpf5用于封装训练集和测试集 #============================================================ # This prepare the hdf5 datasets of the DRIVE database #============================================================ import os import h5py import numpy as np from PIL import Image def write_hdf5(arr,outfile): with h5py.File(outfile,"w") as f: f.create_dataset("image", data=arr, dtype=arr.dtype) #------------Path of the images -------------------------------------------------------------- #train original_imgs_train = "./DRIVE/training/images/" groundTruth_imgs_train = "./DRIVE/training/1st_manual/" borderMasks_imgs_train = "./DRIVE/training/mask/" #test original_imgs_test = "./DRIVE/test/images/" groundTruth_imgs_test = "./DRIVE/test/1st_manual/" borderMasks_imgs_test = "./DRIVE/test/mask/" #--------------------------------------------------------------------------------------------- Nimgs = 20 channels = 3 height = 584 width = 565 dataset_path = "./DRIVE_datasets_training_testing/" def get_datasets(imgs_dir,groundTruth_dir,borderMasks_dir,train_test="null"): imgs = np.empty((Nimgs,height,width,channels)) groundTruth = np.empty((Nimgs,height,width)) border_masks = np.empty((Nimgs,height,width)) for path, subdirs, files in os.walk(imgs_dir): #list all files, directories in the path for i in range(len(files)): #original print "original image: " +files[i] img = Image.open(imgs_dir+files[i]) imgs[i] = np.asarray(img) #corresponding ground truth groundTruth_name = files[i][0:2] + "_manual1.gif" print "ground truth name: " + groundTruth_name g_truth = Image.open(groundTruth_dir + groundTruth_name) groundTruth[i] = np.asarray(g_truth) #corresponding border masks border_masks_name = "" if train_test=="train": border_masks_name = files[i][0:2] + "_training_mask.gif" elif train_test=="test": border_masks_name = files[i][0:2] + "_test_mask.gif" else: print "specify if train or test!!" exit() print "border masks name: " + border_masks_name b_mask = Image.open(borderMasks_dir + border_masks_name) border_masks[i] = np.asarray(b_mask) print "imgs max: " +str(np.max(imgs)) print "imgs min: " +str(np.min(imgs)) assert(np.max(groundTruth)==255 and np.max(border_masks)==255) assert(np.min(groundTruth)==0 and np.min(border_masks)==0) print "ground truth and border masks are correctly withih pixel value range 0-255 (black-white)" #reshaping for my standard tensors imgs = np.transpose(imgs,(0,3,1,2)) assert(imgs.shape == (Nimgs,channels,height,width)) groundTruth = np.reshape(groundTruth,(Nimgs,1,height,width)) border_masks = np.reshape(border_masks,(Nimgs,1,height,width)) assert(groundTruth.shape == (Nimgs,1,height,width)) assert(border_masks.shape == (Nimgs,1,height,width)) return imgs, groundTruth, border_masks if not os.path.exists(dataset_path): os.makedirs(dataset_path) #getting the training datasets imgs_train, groundTruth_train, border_masks_train = get_datasets(original_imgs_train,groundTruth_imgs_train,borderMasks_imgs_train,"train") print "saving train datasets" write_hdf5(imgs_train, dataset_path + "DRIVE_dataset_imgs_train.hdf5") write_hdf5(groundTruth_train, dataset_path + "DRIVE_dataset_groundTruth_train.hdf5") write_hdf5(border_masks_train,dataset_path + "DRIVE_dataset_borderMasks_train.hdf5") #getting the testing datasets imgs_test, groundTruth_test, border_masks_test = get_datasets(original_imgs_test,groundTruth_imgs_test,borderMasks_imgs_test,"test") print "saving test datasets" write_hdf5(imgs_test,dataset_path + "DRIVE_dataset_imgs_test.hdf5") write_hdf5(groundTruth_test, dataset_path + "DRIVE_dataset_groundTruth_test.hdf5") write_hdf5(border_masks_test,dataset_path + "DRIVE_dataset_borderMasks_test.hdf5")
遍历文件夹下的所有文件 os.walk( dir )
for parent, dir_names, file_names in os.walk(parent_dir): for i in file_names: print file_name
parent: 父路径
dir_names: 子文件夹
file_names: 文件名
以上这篇基于h5py的使用及数据封装代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]