我正在嘗試創建一個 h5 檔案來存盤用于訓練超解析度 GAN 的資料集。每個訓練對將是低解析度和高解析度影像。資料集將以下列方式包含資料:[[LR1,HR1],[LR2,HR2],...[LRn,HRn]]。我有 256x256 RGB 影像用于 HR 和 128x128 RGB 用于 LR。我對將其存盤在 h5 檔案中的最佳方式持懷疑態度,我是否應該將影像縮放 255 倍,然后再將它們存盤在 h5 檔案中?
為此,我撰寫了以下代碼。任何幫助/建議將不勝感激。
import h5py
import numpy as np
import os
import cv2
import glob
def store_super_resolution_dataset_in_h5_file(path_to_LR,path_to_HR):
'''This function takes the files with the same name from LR and HR folders and stores the new dataset in h5 format'''
#create LR and HR image lists
LR_images = glob.glob(path_to_LR '*.jpg')
HR_images = glob.glob(path_to_HR '*.jpg')
#sort the lists
LR_images.sort()
HR_images.sort()
print('LR_images: ',LR_images)
print('HR_images: ',HR_images)
#create a h5 file
h5_file = h5py.File('super_resolution_dataset.h5','w')
#create a dataset in the h5 file
dataset = h5_file.create_dataset('super_resolution_dataset',(len(LR_images),2,256,256),dtype='f')
#store the images in the dataset
for i in range(len(LR_images)):
LR_image = cv2.imread(LR_images[i])
HR_image = cv2.imread(HR_images[i])
dataset[i,0,:,:] = LR_image
dataset[i,1,:,:] = HR_image
#close the h5 file
h5_file.close()
uj5u.com熱心網友回復:
下面有2個代碼段。第一個代碼段顯示了我推薦的方法:將高解析度和低解析度影像加載到單獨的資料集以減小 HDF5 檔案大小。第二個只是更正代碼中的錯誤(修改為使用with/as:背景關系管理器)。兩個代碼段都在#create a h5 file注釋之后開始。
我對 43 張影像進行了測驗,以比較生成的檔案大小。結果是:
- 1 個資料集大小 = 66.0 MB
- 2 資料集大小 = 41.3 MB(減少 37%)
使用 2 個資料集的推薦方法:
#create a h5 file
with h5py.File('low_hi_resolution_dataset.h5','w') as h5_file:
#create 2 datasets for LR and HR images in the h5 file
lr_ds = h5_file.create_dataset('low_res_dataset',(len(LR_images),128,128,3),dtype='f')
hr_ds = h5_file.create_dataset('hi_res_dataset',(len(LR_images),256,256,3),dtype='f')
#store the images in the dataset
for i in range(len(LR_images)):
LR_image = cv2.imread(LR_images[i])
HR_image = cv2.imread(HR_images[i])
lr_ds[i] = LR_image
hr_ds[i] = HR_image
修改您的方法:
#create a h5 file
with h5py.File('super_resolution_dataset.h5','w') as h5_file:
#create a dataset in the h5 file
dataset = h5_file.create_dataset('super_resolution_dataset',(len(LR_images),2,256,256,3),dtype='f')
#store the images in the dataset
for i in range(len(LR_images)):
LR_image = cv2.imread(LR_images[i])
HR_image = cv2.imread(HR_images[i])
dataset[i,0,0:128,0:128,:] = LR_image
dataset[i,1,:,:,:] = HR_image
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510782.html
