|
| 1 | +import numpy as np |
| 2 | +import os |
| 3 | +from data.base_dataset import BaseDataset |
| 4 | +from .imlib import imlib |
| 5 | +from multiprocessing.dummy import Pool |
| 6 | +from tqdm import tqdm |
| 7 | +from util.util import augment |
| 8 | +import random |
| 9 | + |
| 10 | + |
| 11 | +# GoPro dataset |
| 12 | +class GoProDataset(BaseDataset): |
| 13 | + def __init__(self, opt, split='train', dataset_name='GoPro'): |
| 14 | + super(GoProDataset, self).__init__(opt, split, dataset_name) |
| 15 | + |
| 16 | + if self.root == '': |
| 17 | + rootlist = ['/Data/dataset/GOPRO_Large/'] |
| 18 | + for root in rootlist: |
| 19 | + if os.path.isdir(root): |
| 20 | + self.root = root |
| 21 | + break |
| 22 | + |
| 23 | + self.batch_size = opt.batch_size |
| 24 | + self.patch_size = opt.patch_size |
| 25 | + self.mode = opt.mode # RGB, Y or L= |
| 26 | + self.imio = imlib(self.mode, lib=opt.imlib) |
| 27 | + self.names, self.blur_dirs, self.gt_dirs = self._get_image_dir(self.root, split) |
| 28 | + |
| 29 | + if split == 'train': |
| 30 | + self._getitem = self._getitem_train |
| 31 | + self.len_data = 500 * 16 # 500 * self.batch_size |
| 32 | + elif split == 'val': |
| 33 | + self._getitem = self._getitem_test |
| 34 | + self.len_data = len(self.names) |
| 35 | + elif split == 'test': |
| 36 | + self._getitem = self._getitem_test |
| 37 | + self.len_data = len(self.names) |
| 38 | + else: |
| 39 | + raise ValueError |
| 40 | + |
| 41 | + self.blur_images = [0] * len(self.names) |
| 42 | + self.gt_images = [0] * len(self.names) |
| 43 | + read_images(self) |
| 44 | + |
| 45 | + def __getitem__(self, index): |
| 46 | + return self._getitem(index) |
| 47 | + |
| 48 | + def __len__(self): |
| 49 | + return self.len_data |
| 50 | + |
| 51 | + def _getitem_train(self, idx): |
| 52 | + idx = idx % len(self.names) |
| 53 | + |
| 54 | + blur_img = self.blur_images[idx] |
| 55 | + gt_img = self.gt_images[idx] |
| 56 | + blur_img, gt_img = self._crop_patch(blur_img, gt_img) |
| 57 | + |
| 58 | + blur_img, gt_img = augment(blur_img, gt_img) |
| 59 | + |
| 60 | + blur_img = np.float32(blur_img) / 255 |
| 61 | + gt_img = np.float32(gt_img) / 255 |
| 62 | + |
| 63 | + return {'gt_noise': gt_img, |
| 64 | + 'blur_img': blur_img, |
| 65 | + 'gt_img': gt_img, |
| 66 | + 'fname': self.names[idx]} |
| 67 | + |
| 68 | + def _getitem_test(self, idx): |
| 69 | + |
| 70 | + blur_img = self.blur_images[idx] |
| 71 | + gt_img = self.gt_images[idx] |
| 72 | + |
| 73 | + blur_img = np.float32(blur_img) / 255 |
| 74 | + gt_img = np.float32(gt_img) / 255 |
| 75 | + |
| 76 | + noise_root = self.gt_dirs[idx].replace('sharp', 'npy') |
| 77 | + noise_root = noise_root.replace('test', 'test_noise_' + self.opt.noisetype) |
| 78 | + noise_file = noise_root[:-3] + 'npy' |
| 79 | + gt_noise = np.float32(np.load(noise_file, allow_pickle=True)) |
| 80 | + |
| 81 | + return {'gt_noise': gt_noise, |
| 82 | + 'blur_img': blur_img, |
| 83 | + 'gt_img': gt_img, |
| 84 | + 'fname': self.names[idx]} |
| 85 | + |
| 86 | + def _crop_patch(self, blur, gt): |
| 87 | + ih, iw = blur.shape[-2:] |
| 88 | + p = self.patch_size |
| 89 | + pw = random.randrange(0, iw - p + 1) |
| 90 | + ph = random.randrange(0, ih - p + 1) |
| 91 | + return blur[..., ph:ph+p, pw:pw+p], \ |
| 92 | + gt[..., ph:ph+p, pw:pw+p] |
| 93 | + |
| 94 | + def _get_image_dir(self, dataroot, split=None): |
| 95 | + blur_dirs = [] |
| 96 | + gt_dirs = [] |
| 97 | + image_names = [] |
| 98 | + |
| 99 | + if split == 'train' or split == 'test': |
| 100 | + for scene_file in os.listdir(dataroot + split + '/'): |
| 101 | + for image_file in os.listdir(dataroot + split + '/' + scene_file + '/sharp/'): |
| 102 | + image_names.append(scene_file + '-' + image_file) |
| 103 | + blur_dirs.append(dataroot + split + '/' + scene_file + '/blur_gamma/' + image_file) |
| 104 | + gt_dirs.append(dataroot + split + '/' + scene_file + '/sharp/' + image_file) |
| 105 | + elif split == 'val': |
| 106 | + for scene_file in os.listdir(dataroot + 'test/'): |
| 107 | + for image_file in os.listdir(dataroot + 'test/' + scene_file + '/sharp/'): |
| 108 | + image_names.append(scene_file + '-' + image_file) |
| 109 | + blur_dirs.append(dataroot + 'test/' + scene_file + '/blur_gamma/' + image_file) |
| 110 | + gt_dirs.append(dataroot + 'test/' + scene_file + '/sharp/' + image_file) |
| 111 | + break |
| 112 | + else: |
| 113 | + raise ValueError |
| 114 | + |
| 115 | + image_names = sorted(image_names) |
| 116 | + blur_dirs = sorted(blur_dirs) |
| 117 | + gt_dirs = sorted(gt_dirs) |
| 118 | + |
| 119 | + return image_names, blur_dirs, gt_dirs |
| 120 | + |
| 121 | + |
| 122 | +def iter_obj(num, objs): |
| 123 | + for i in range(num): |
| 124 | + yield (i, objs) |
| 125 | + |
| 126 | +def imreader(arg): |
| 127 | + i, obj = arg |
| 128 | + for _ in range(3): |
| 129 | + try: |
| 130 | + obj.blur_images[i] = obj.imio.read(obj.blur_dirs[i]) |
| 131 | + obj.gt_images[i] = obj.imio.read(obj.gt_dirs[i]) |
| 132 | + failed = False |
| 133 | + break |
| 134 | + except: |
| 135 | + failed = True |
| 136 | + if failed: print('%s fails!' % obj.names[i]) |
| 137 | + |
| 138 | +def read_images(obj): |
| 139 | + # may use `from multiprocessing import Pool` instead, but less efficient and |
| 140 | + # NOTE: `multiprocessing.Pool` will duplicate given object for each process. |
| 141 | + print('Starting to load images via multiple imreaders') |
| 142 | + pool = Pool() # use all threads by default |
| 143 | + for _ in tqdm(pool.imap(imreader, iter_obj(len(obj.names), obj)), total=len(obj.names)): |
| 144 | + pass |
| 145 | + pool.close() |
| 146 | + pool.join() |
| 147 | + |
| 148 | +if __name__ == '__main__': |
| 149 | + pass |
0 commit comments