【问题标题】:Creating a custom image dataset for super-resolution为超分辨率创建自定义图像数据集
【发布时间】:2022-11-05 04:59:01
【问题描述】:

我正在为超分辨率深度学习网络开发自定义图像数据集。我将图像保存到磁盘并可以创建 HDF5 数据集文件。这是我正在使用的代码:

import os, cv2, h5py, glob
import numpy as np
from glob import glob

# define the paths to the dataset
BASE_DATA_PATH = '/usr/local/home/.../esrgan_data'
HR_TRAIN_PATH = os.path.join(BASE_DATA_PATH, 'train_HR')
LR_TRAIN_PATH = os.path.join(BASE_DATA_PATH, 'train_LR')

# create LR and HR image lists
LR_images = glob(LR_TRAIN_PATH + '**/*.png')
HR_images = glob(HR_TRAIN_PATH + '**/*.png')

# sort the lists
LR_images.sort()
HR_images.sort()

# create an h5 file
with h5py.File('datasets/esrgan_trainDS.h5', 'w') as h5_file:
    # create 2 datasets for LR and HR images in the h5 file
    lr_ds = h5_file.create_dataset('trainLR', (len(LR_images), 150, 150, 3), dtype='f')
    hr_ds = h5_file.create_dataset('trainHR', (len(HR_images), 600, 600, 3), dtype='f')
    
    for i in range(len(LR_images)):
        LR_image = cv2.imread(LR_images[i])
        HR_image = cv2.imread(HR_images[i])
        lr_trainDS[i] = LR_image
        hr_trainDS[i] = HR_image

# load the h5 dataset
trainDS = h5py.File('datasets/esrgan_trainDS.h5', 'r')
print('Files in the training dataset: ', list(trainDS.keys()))

训练数据集中的文件:['trainER', 'trainOR']

LRset = trainDS['trainLR']
HRset = trainDS['trainHR']

print('LR dataset shape: ', LRset.shape)
print('HR dataset shape: ', HRset.shape)

LR 数据集形状:(450, 150, 150, 3) HR 数据集形状:(450, 600, 600, 3)

我的问题是,当我尝试从数据集中查看单个图像时,我看到一个黑框,告诉我图像没有保存或没有正确加载。

cv2_imshow('', HRset[100])


我基于此post 编写代码。代码运行没有错误——我可以编写 f5 文件,读取它们并打印文件属性。我只是看不到图像,而且没有错误消息,我不确定我哪里出错了。

我猜这是一个我没有看到的简单错误,但我很感激你能提供的任何帮助。谢谢!

【问题讨论】:

    标签: opencv dataset hdf5 h5py


    【解决方案1】:

    您是引用帖子中一个小错误的受害者。 (我很抱歉;我是那篇文章的作者。显然没有人注意到使用错误 dtype 创建数据集的错误。我更正了该答案以从图像中获取 dtype,然后在创建数据集时使用它。)

    此外,您需要在代码中添加一些行来查看图像。修复 HDF5 文件后,将这两行添加到代码中以查看图像。 (另外,更正imshow() 上的小错字)。修改后的代码如下:

    cv2.imshow('', HRset[100]) 
    cv2.waitKey(0) # waits until a key is pressed in the image window
    cv2.destroyAllWindows() # destroys the window showing the image
    

    这些线有什么作用?
    cv2.waitKey(0) 暂停程序的执行。因此,图像窗口将保持可见。如果不包含此语句,cv2.imshow() 会在眨眼间执行,然后程序会关闭它打开的所有窗口。这使得您不太可能在窗口中看到图像。 (您发布的黑色图像是图像窗口的残留物。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-11
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多