-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBSD.py
148 lines (129 loc) · 5.56 KB
/
BSD.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import random
from os.path import join
import cv2
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from .utils import normalize, Crop, Flip, ToTensor
class DeblurDataset(Dataset):
"""
Structure of self_.records:
seq:
frame:
path of images -> {'Blur': <path>, 'Sharp': <path>}
"""
def __init__(self, path, frames, future_frames, past_frames, crop_size=(256, 256), data_format='RGB',
centralize=True, normalize=True):
assert frames - future_frames - past_frames >= 1
self.frames = frames
self.num_ff = future_frames
self.num_pf = past_frames
self.data_format = data_format
self.W = 640
self.H = 480
self.crop_h, self.crop_w = crop_size
self.normalize = normalize
self.centralize = centralize
self.transform = transforms.Compose([Crop(crop_size), Flip(), ToTensor()])
self._seq_length = 100
self._samples = self._generate_samples(path, data_format)
def _generate_samples(self, dataset_path, data_format):
samples = list()
records = dict()
seqs = sorted(os.listdir(dataset_path), key=int)
for seq in seqs:
records[seq] = list()
for frame in range(self._seq_length):
suffix = 'png' if data_format == 'RGB' else 'tiff'
sample = dict()
sample['Blur'] = join(dataset_path, seq, 'Blur', data_format, '{:08d}.{}'.format(frame, suffix))
sample['Sharp'] = join(dataset_path, seq, 'Sharp', data_format, '{:08d}.{}'.format(frame, suffix))
records[seq].append(sample)
for seq_records in records.values():
temp_length = len(seq_records) - (self.frames - 1)
if temp_length <= 0:
raise IndexError('Exceed the maximum length of the video sequence')
for idx in range(temp_length):
samples.append(seq_records[idx:idx + self.frames])
return samples
def __getitem__(self, item):
top = random.randint(0, self.H - self.crop_h)
left = random.randint(0, self.W - self.crop_w)
flip_lr = random.randint(0, 1)
flip_ud = random.randint(0, 1)
sample = {'top': top, 'left': left, 'flip_lr': flip_lr, 'flip_ud': flip_ud}
blur_imgs, sharp_imgs = [], []
for sample_dict in self._samples[item]:
blur_img, sharp_img = self._load_sample(sample_dict, sample)
blur_imgs.append(blur_img)
sharp_imgs.append(sharp_img)
sharp_imgs = sharp_imgs[self.num_pf:self.frames - self.num_ff]
return [torch.cat(item, dim=0) for item in [blur_imgs, sharp_imgs]]
def _load_sample(self, sample_dict, sample):
if self.data_format == 'RGB':
sample['image'] = cv2.imread(sample_dict['Blur'])
sample['label'] = cv2.imread(sample_dict['Sharp'])
elif self.data_format == 'RAW':
sample['image'] = cv2.imread(sample_dict['Blur'], -1)[..., np.newaxis].astype(np.int32)
sample['label'] = cv2.imread(sample_dict['Sharp'], -1)[..., np.newaxis].astype(np.int32)
sample = self.transform(sample)
val_range = 2.0 ** 8 - 1 if self.data_format == 'RGB' else 2.0 ** 16 - 1
blur_img = normalize(sample['image'], centralize=self.centralize, normalize=self.normalize, val_range=val_range)
sharp_img = normalize(sample['label'], centralize=self.centralize, normalize=self.normalize, val_range=val_range)
return blur_img, sharp_img
def __len__(self):
return len(self._samples)
class Dataloader:
def __init__(self, para, device_id, ds_type='train'):
path = join(para.data_root, para.dataset, '{}_{}'.format(para.dataset, para.ds_config), ds_type)
frames = para.frames
dataset = DeblurDataset(path, frames, para.future_frames, para.past_frames, para.patch_size, para.data_format,
para.centralize, para.normalize)
gpus = para.num_gpus
bs = para.batch_size
ds_len = len(dataset)
if para.trainer_mode == 'ddp':
sampler = torch.utils.data.distributed.DistributedSampler(
dataset,
num_replicas=para.num_gpus,
rank=device_id
)
self.loader = DataLoader(
dataset=dataset,
batch_size=para.batch_size,
shuffle=False,
num_workers=para.threads,
pin_memory=True,
sampler=sampler,
drop_last=True
)
loader_len = np.ceil(ds_len / gpus)
self.loader_len = int(np.ceil(loader_len / bs) * bs)
elif para.trainer_mode == 'dp':
self.loader = DataLoader(
dataset=dataset,
batch_size=para.batch_size,
shuffle=True,
num_workers=para.threads,
pin_memory=True,
drop_last=True
)
self.loader_len = int(np.ceil(ds_len / bs) * bs)
def __iter__(self):
return iter(self.loader)
def __len__(self):
return self.loader_len
if __name__ == '__main__':
from para import Parameter
para = Parameter().args
para.data_format = 'RGB'
para.dataset = 'BSD'
dataloader = Dataloader(para, 0)
for x, y in dataloader:
print(x.shape, y.shape)
break
print(x.type(), y.type())
print(np.max(x.numpy()), np.min(x.numpy()))
print(np.max(y.numpy()), np.min(y.numpy()))