-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
176 lines (116 loc) · 5.55 KB
/
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import torch
from torch.utils.data import Dataset
import os
from PIL import Image
import numpy as np
import random
class mydata(Dataset):
def __init__(self, LR_path, GT_path, in_memory = True, transform = None):
self.LR_path = LR_path
self.GT_path = GT_path
self.in_memory = in_memory
self.transform = transform
self.LR_img = sorted(os.listdir(LR_path))
self.GT_img = sorted(os.listdir(GT_path))
if in_memory:
self.LR_img = [np.array(Image.open(os.path.join(self.LR_path, lr)).convert("RGB")).astype(np.uint8) for lr in self.LR_img]
self.GT_img = [np.array(Image.open(os.path.join(self.GT_path, gt)).convert("RGB")).astype(np.uint8) for gt in self.GT_img]
def __len__(self):
return len(self.LR_img)
def __getitem__(self, i):
img_item = {}
if self.in_memory:
GT = self.GT_img[i].astype(np.float32)
LR = self.LR_img[i].astype(np.float32)
else:
GT = np.array(Image.open(os.path.join(self.GT_path, self.GT_img[i])).convert("RGB"))
LR = np.array(Image.open(os.path.join(self.LR_path, self.LR_img[i])).convert("RGB"))
img_item['GT'] = (GT / 127.5) - 1.0
img_item['LR'] = (LR / 127.5) - 1.0
if self.transform is not None:
img_item = self.transform(img_item)
img_item['GT'] = img_item['GT'].transpose(2, 0, 1).astype(np.float32)
img_item['LR'] = img_item['LR'].transpose(2, 0, 1).astype(np.float32)
return img_item
class mydataT(Dataset):
def __init__(self, LR_path, GT_path, in_memory = True, transform = None):
self.LR_path = LR_path
self.GT_path = GT_path
self.in_memory = in_memory
self.transform = transform
self.LR_img = sorted(os.listdir(LR_path))
self.GT_img = sorted(os.listdir(GT_path))
if in_memory:
self.LR_img = [np.array(Image.open(os.path.join(self.LR_path, lr)).convert("RGB")).astype(np.uint8) for lr in self.LR_img]
self.GT_img = [np.array(Image.open(os.path.join(self.GT_path, gt)).convert("RGB")).astype(np.uint8) for gt in self.GT_img]
def __len__(self):
return len(self.LR_img)
def __getitem__(self, i):
img_item = {}
if self.in_memory:
GT = self.GT_img[i].astype(np.float32)
LR = self.LR_img[i].astype(np.float32)
else:
GT = np.array(Image.open(os.path.join(self.GT_path, self.GT_img[i])).convert("RGB"))
LR = np.array(Image.open(os.path.join(self.LR_path, self.LR_img[i])).convert("RGB"))
img_item['GT'] = (GT / 127.5) - 1.0
img_item['LR'] = (LR / 127.5) - 1.0
if self.transform is not None:
img_item = self.transform(img_item)
img_item['GT'] = img_item['GT'].transpose(2, 0, 1).astype(np.float32)
img_item['LR'] = img_item['LR'].transpose(2, 0, 1).astype(np.float32)
return img_item
class testOnly_data(Dataset):
def __init__(self, LR_path, in_memory = True, transform = None):
self.LR_path = LR_path
self.LR_img = sorted(os.listdir(LR_path))
self.in_memory = in_memory
if in_memory:
self.LR_img = [np.array(Image.open(os.path.join(self.LR_path, lr))) for lr in self.LR_img]
def __len__(self):
return len(self.LR_img)
def __getitem__(self, i):
img_item = {}
if self.in_memory:
LR = self.LR_img[i]
else:
LR = np.array(Image.open(os.path.join(self.LR_path, self.LR_img[i])))
img_item['LR'] = (LR / 127.5) - 1.0
img_item['LR'] = img_item['LR'].transpose(2, 0, 1).astype(np.float32)
return img_item
class crop(object):
def __init__(self, scale, patch_size):
self.scale = scale
self.patch_size = patch_size
def __call__(self, sample):
LR_img, GT_img = sample['LR'], sample['GT']
ih, iw = LR_img.shape[:2]
ix = random.randrange(0, iw - self.patch_size +1)
iy = random.randrange(0, ih - self.patch_size +1)
tx = ix * self.scale
ty = iy * self.scale
LR_patch = LR_img[iy : iy + self.patch_size, ix : ix + self.patch_size]
GT_patch = GT_img[ty : ty + (self.scale * self.patch_size), tx : tx + (self.scale * self.patch_size)]
return {'LR' : LR_patch, 'GT' : GT_patch}
class augmentation(object):
def __call__(self, sample):
LR_img, GT_img = sample['LR'], sample['GT']
hor_flip = random.randrange(0,2)
ver_flip = random.randrange(0,2)
rot = random.randrange(0,2)
if hor_flip:
temp_LR = np.fliplr(LR_img)
LR_img = temp_LR.copy()
temp_GT = np.fliplr(GT_img)
GT_img = temp_GT.copy()
del temp_LR, temp_GT
if ver_flip:
temp_LR = np.flipud(LR_img)
LR_img = temp_LR.copy()
temp_GT = np.flipud(GT_img)
GT_img = temp_GT.copy()
del temp_LR, temp_GT
if rot:
LR_img = LR_img.transpose(1, 0, 2)
GT_img = GT_img.transpose(1, 0, 2)
return {'LR' : LR_img, 'GT' : GT_img}