-
Notifications
You must be signed in to change notification settings - Fork 3
/
data.py
148 lines (117 loc) · 4.37 KB
/
data.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
# data.py
# SPDX-License-Identifier: MIT
# See COPYING file for more details.
import random
import torchvision.transforms as transforms
import torchvision.transforms.functional as F
import os
import torch
import numpy as np
from PIL import Image as Image
from torchvision.transforms import functional as F
from torch.utils.data import Dataset, DataLoader
def train_dataloader(path, batch_size=64, num_workers=0, use_transform=True):
image_dir = os.path.join(path, 'train')
transform = None
if use_transform:
transform = PairCompose(
[
PairRandomCrop(256),
PairRandomHorizontalFilp(),
PairToTensor()
]
)
dataloader = DataLoader(
DeblurDataset(image_dir, transform=transform),
batch_size=batch_size,
shuffle=True,
num_workers=num_workers,
pin_memory=True
)
return dataloader
def test_dataloader(path, batch_size=1, num_workers=0):
image_dir = os.path.join(path, 'test')
dataloader = DataLoader(
DeblurDataset(image_dir, is_test=True),
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True
)
return dataloader
def valid_dataloader(path, batch_size=1, num_workers=0):
dataloader = DataLoader(
DeblurDataset(os.path.join(path, 'test')),
batch_size=batch_size,
shuffle=False,
num_workers=num_workers
)
return dataloader
class DeblurDataset(Dataset):
def __init__(self, image_dir, transform=None, is_test=False):
self.image_dir = image_dir
self.image_list = os.listdir(os.path.join(image_dir, 'hazy'))
self._check_image(self.image_list)
self.image_list.sort()
self.transform = transform
self.is_test = is_test
def __len__(self):
return len(self.image_list)
def __getitem__(self, idx):
image = Image.open(os.path.join(self.image_dir, 'hazy', self.image_list[idx]))
label = Image.open(os.path.join(self.image_dir, 'clear', self.image_list[idx].split('_')[0]+'.png'))
if self.transform:
image, label = self.transform(image, label)
else:
image = F.to_tensor(image)
label = F.to_tensor(label)
if self.is_test:
name = self.image_list[idx]
return image, label, name
return image, label
@staticmethod
def _check_image(lst):
for x in lst:
splits = x.split('.')
if splits[-1] not in ['png', 'jpg', 'jpeg']:
raise ValueError
class PairRandomCrop(transforms.RandomCrop):
def __call__(self, image, label):
if self.padding is not None:
image = F.pad(image, self.padding, self.fill, self.padding_mode)
label = F.pad(label, self.padding, self.fill, self.padding_mode)
# pad the width if needed
if self.pad_if_needed and image.size[0] < self.size[1]:
image = F.pad(image, (self.size[1] - image.size[0], 0), self.fill, self.padding_mode)
label = F.pad(label, (self.size[1] - label.size[0], 0), self.fill, self.padding_mode)
# pad the height if needed
if self.pad_if_needed and image.size[1] < self.size[0]:
image = F.pad(image, (0, self.size[0] - image.size[1]), self.fill, self.padding_mode)
label = F.pad(label, (0, self.size[0] - image.size[1]), self.fill, self.padding_mode)
i, j, h, w = self.get_params(image, self.size)
return F.crop(image, i, j, h, w), F.crop(label, i, j, h, w)
class PairCompose(transforms.Compose):
def __call__(self, image, label):
for t in self.transforms:
image, label = t(image, label)
return image, label
class PairRandomHorizontalFilp(transforms.RandomHorizontalFlip):
def __call__(self, img, label):
"""
Args:
img (PIL Image): Image to be flipped.
Returns:
PIL Image: Randomly flipped image.
"""
if random.random() < self.p:
return F.hflip(img), F.hflip(label)
return img, label
class PairToTensor(transforms.ToTensor):
def __call__(self, pic, label):
"""
Args:
pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
Returns:
Tensor: Converted image.
"""
return F.to_tensor(pic), F.to_tensor(label)