forked from XPixelGroup/BasicSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_image_dataset.py
63 lines (50 loc) · 1.99 KB
/
single_image_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import mmcv
import numpy as np
from os import path as osp
from torch.utils import data as data
from basicsr.data.transforms import totensor
from basicsr.utils import FileClient
class SingleImageDataset(data.Dataset):
"""Read only lq images in the test phase.
Read LQ (Low Quality, e.g. LR (Low Resolution), blurry, noisy, etc).
There are two modes:
1. 'ann_file': Use annotation file to generate paths.
2. 'folder': Scan folders to generate paths.
Args:
opt (dict): Config for train datasets. It contains the following keys:
dataroot_lq (str): Data root path for lq.
ann_file (str): Path for annotation file.
io_backend (dict): IO backend type and other kwarg.
"""
def __init__(self, opt):
super(SingleImageDataset, self).__init__()
self.opt = opt
# file client (io backend)
self.file_client = None
self.io_backend_opt = opt['io_backend']
self.lq_folder = opt['dataroot_lq']
if 'ann_file' in self.opt:
with open(self.opt['ann_file'], 'r') as fin:
self.paths = [
osp.join(self.lq_folder,
line.split(' ')[0]) for line in fin
]
else:
self.paths = [
osp.join(self.lq_folder, v)
for v in mmcv.scandir(self.lq_folder)
]
def __getitem__(self, index):
if self.file_client is None:
self.file_client = FileClient(
self.io_backend_opt.pop('type'), **self.io_backend_opt)
# load lq image
lq_path = self.paths[index]
img_bytes = self.file_client.get(lq_path)
img_lq = mmcv.imfrombytes(img_bytes).astype(np.float32) / 255.
# TODO: color space transform
# BGR to RGB, HWC to CHW, numpy to tensor
img_lq = totensor(img_lq, bgr2rgb=True, float32=True)
return {'lq': img_lq, 'lq_path': lq_path}
def __len__(self):
return len(self.paths)