-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdataset.py
230 lines (179 loc) · 8.52 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Copyright 2021 Dakewe Biotech Corporation. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Realize the function of dataset preparation."""
import os
import queue
import threading
import cv2
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
import imgproc
__all__ = [
"TrainValidImageDataset", "TestImageDataset",
"PrefetchGenerator", "PrefetchDataLoader", "CPUPrefetcher", "CUDAPrefetcher",
]
class TrainValidImageDataset(Dataset):
"""Customize the data set loading function and prepare low/high resolution image data in advance.
Args:
image_dir (str): Train/Valid dataset address.
image_size (int): High resolution image size.
upscale_factor (int): Image up scale factor.
mode (str): Data set loading method, the training data set is for data enhancement, and the verification data set is not for data enhancement.
"""
def __init__(self, image_dir: str, image_size: int, upscale_factor: int, mode: str) -> None:
super(TrainValidImageDataset, self).__init__()
# Get all image file names in folder
self.image_file_names = [os.path.join(image_dir, image_file_name) for image_file_name in os.listdir(image_dir)]
# Specify the high-resolution image size, with equal length and width
self.image_size = image_size
# How many times the high-resolution image is the low-resolution image
self.upscale_factor = upscale_factor
# Load training dataset or test dataset
self.mode = mode
def __getitem__(self, batch_index: int) -> [torch.Tensor, torch.Tensor]:
# Read a batch of image data
hr_image = cv2.imread(self.image_file_names[batch_index], cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.
# Use high-resolution image to make low-resolution image
lr_image = imgproc.imresize(hr_image, 1 / self.upscale_factor)
if self.mode == "Train":
# Data augment
lr_image, hr_image = imgproc.random_crop(lr_image, hr_image, self.image_size, self.upscale_factor)
lr_image, hr_image = imgproc.random_rotate(lr_image, hr_image, angles=[0, 90, 180, 270])
elif self.mode == "Valid":
lr_image, hr_image = imgproc.center_crop(lr_image, hr_image, self.image_size, self.upscale_factor)
else:
raise ValueError("Unsupported data processing model, please use `Train` or `Valid`.")
# Only extract the image data of the Y channel
lr_y_image = imgproc.bgr2ycbcr(lr_image, use_y_channel=True)
hr_y_image = imgproc.bgr2ycbcr(hr_image, use_y_channel=True)
# Convert image data into Tensor stream format (PyTorch).
# Note: The range of input and output is between [0, 1]
lr_y_tensor = imgproc.image2tensor(lr_y_image, range_norm=False, half=False)
hr_y_tensor = imgproc.image2tensor(hr_y_image, range_norm=False, half=False)
return {"lr": lr_y_tensor, "hr": hr_y_tensor}
def __len__(self) -> int:
return len(self.image_file_names)
class TestImageDataset(Dataset):
"""Define Test dataset loading methods.
Args:
test_lr_image_dir (str): Test dataset address for low resolution image dir.
test_hr_image_dir (str): Test dataset address for high resolution image dir.
upscale_factor (int): Image up scale factor.
"""
def __init__(self, test_lr_image_dir: str, test_hr_image_dir: str, upscale_factor: int) -> None:
super(TestImageDataset, self).__init__()
# Get all image file names in folder
self.lr_image_file_names = [os.path.join(test_lr_image_dir, x) for x in os.listdir(test_lr_image_dir)]
self.hr_image_file_names = [os.path.join(test_hr_image_dir, x) for x in os.listdir(test_hr_image_dir)]
# How many times the high-resolution image is the low-resolution image
self.upscale_factor = upscale_factor
def __getitem__(self, batch_index: int) -> [torch.Tensor, torch.Tensor]:
# Read a batch of image data
lr_image = cv2.imread(self.lr_image_file_names[batch_index], cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.
hr_image = cv2.imread(self.hr_image_file_names[batch_index], cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.
# Only extract the image data of the Y channel
lr_y_image = imgproc.bgr2ycbcr(lr_image, use_y_channel=True)
hr_y_image = imgproc.bgr2ycbcr(hr_image, use_y_channel=True)
# Convert image data into Tensor stream format (PyTorch).
# Note: The range of input and output is between [0, 1]
lr_y_tensor = imgproc.image2tensor(lr_y_image, range_norm=False, half=False)
hr_y_tensor = imgproc.image2tensor(hr_y_image, range_norm=False, half=False)
return {"lr": lr_y_tensor, "hr": hr_y_tensor}
def __len__(self) -> int:
return len(self.lr_image_file_names)
class PrefetchGenerator(threading.Thread):
"""A fast data prefetch generator.
Args:
generator: Data generator.
num_data_prefetch_queue (int): How many early data load queues.
"""
def __init__(self, generator, num_data_prefetch_queue: int) -> None:
threading.Thread.__init__(self)
self.queue = queue.Queue(num_data_prefetch_queue)
self.generator = generator
self.daemon = True
self.start()
def run(self) -> None:
for item in self.generator:
self.queue.put(item)
self.queue.put(None)
def __next__(self):
next_item = self.queue.get()
if next_item is None:
raise StopIteration
return next_item
def __iter__(self):
return self
class PrefetchDataLoader(DataLoader):
"""A fast data prefetch dataloader.
Args:
num_data_prefetch_queue (int): How many early data load queues.
kwargs (dict): Other extended parameters.
"""
def __init__(self, num_data_prefetch_queue: int, **kwargs) -> None:
self.num_data_prefetch_queue = num_data_prefetch_queue
super(PrefetchDataLoader, self).__init__(**kwargs)
def __iter__(self):
return PrefetchGenerator(super().__iter__(), self.num_data_prefetch_queue)
class CPUPrefetcher:
"""Use the CPU side to accelerate data reading.
Args:
dataloader (DataLoader): Data loader. Combines a dataset and a sampler, and provides an iterable over the given dataset.
"""
def __init__(self, dataloader) -> None:
self.original_dataloader = dataloader
self.data = iter(dataloader)
def next(self):
try:
return next(self.data)
except StopIteration:
return None
def reset(self):
self.data = iter(self.original_dataloader)
def __len__(self) -> int:
return len(self.original_dataloader)
class CUDAPrefetcher:
"""Use the CUDA side to accelerate data reading.
Args:
dataloader (DataLoader): Data loader. Combines a dataset and a sampler, and provides an iterable over the given dataset.
device (torch.device): Specify running device.
"""
def __init__(self, dataloader, device: torch.device):
self.batch_data = None
self.original_dataloader = dataloader
self.device = device
self.data = iter(dataloader)
self.stream = torch.cuda.Stream()
self.preload()
def preload(self):
try:
self.batch_data = next(self.data)
except StopIteration:
self.batch_data = None
return None
with torch.cuda.stream(self.stream):
for k, v in self.batch_data.items():
if torch.is_tensor(v):
self.batch_data[k] = self.batch_data[k].to(self.device, non_blocking=True)
def next(self):
torch.cuda.current_stream().wait_stream(self.stream)
batch_data = self.batch_data
self.preload()
return batch_data
def reset(self):
self.data = iter(self.original_dataloader)
self.preload()
def __len__(self) -> int:
return len(self.original_dataloader)