日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于h5py的使用及数据封装实例

發(fā)布時間:2025/3/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于h5py的使用及数据封装实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. h5py簡單介紹

h5py文件是存放兩類對象的容器,數(shù)據(jù)集(dataset)和組(group),dataset類似數(shù)組類的數(shù)據(jù)集合,和numpy的數(shù)組差不多。group是像文件夾一樣的容器,它好比python中的字典,有鍵(key)和值(value)。group中可以存放dataset或者其他的group。”鍵”就是組成員的名稱,”值”就是組成員對象本身(組或者數(shù)據(jù)集),下面來看下如何創(chuàng)建組和數(shù)據(jù)集。

1.1 創(chuàng)建一個h5py文件

import h5py #要是讀取文件的話,就把w換成r f=h5py.File("myh5py.hdf5","w")

在當前目錄下會生成一個myh5py.hdf5文件。

2. 創(chuàng)建dataset數(shù)據(jù)集

import h5py f=h5py.File("myh5py.hdf5","w") #deset1是數(shù)據(jù)集的name,(20,)代表數(shù)據(jù)集的shape,i代表的是數(shù)據(jù)集的元素類型 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 Imagedef 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 pathfor i in range(len(files)):#originalprint "original image: " +files[i]img = Image.open(imgs_dir+files[i])imgs[i] = np.asarray(img)#corresponding ground truthgroundTruth_name = files[i][0:2] + "_manual1.gif"print "ground truth name: " + groundTruth_nameg_truth = Image.open(groundTruth_dir + groundTruth_name)groundTruth[i] = np.asarray(g_truth)#corresponding border masksborder_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_nameb_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 tensorsimgs = 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_masksif 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: 文件名

總結(jié)

以上是生活随笔為你收集整理的关于h5py的使用及数据封装实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。