-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_dataset.py
148 lines (107 loc) · 5.02 KB
/
load_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
import os
import tensorflow as tf
from tensorflow.python.data.experimental import AUTOTUNE
class MedicalDataSet:
def __init__(self,
scale=4,
subset='train',
downgrade='bicubic',
images_dir='Dataset/Final/Chest X Ray - Copy/images',
caches_dir='Dataset/Final/Chest X Ray - Copy/caches'):
_scales = [2, 3, 4, 8]
if scale in _scales:
self.scale = scale
else:
raise ValueError(f'scale must be in ${_scales}')
if subset == 'train':
self.image_ids = range(1, 801)
elif subset == 'valid':
self.image_ids = range(801, 901)
else:
raise ValueError("subset must be 'train' or 'valid'")
self.downgrade = downgrade
self.subset = subset
self.images_dir = images_dir
self.caches_dir = caches_dir
os.makedirs(images_dir, exist_ok=True)
os.makedirs(caches_dir, exist_ok=True)
def dataset(self, batch_size=16, repeat_count=None, random_transform=True):
ds = tf.data.Dataset.zip((self.lr_dataset(), self.hr_dataset()))
if random_transform:
ds = ds.map(lambda lr, hr: random_crop(lr, hr, scale=self.scale), num_parallel_calls=AUTOTUNE)
ds = ds.map(random_rotate, num_parallel_calls=AUTOTUNE)
ds = ds.map(random_flip, num_parallel_calls=AUTOTUNE)
ds = ds.batch(batch_size, drop_remainder=True)
ds = ds.repeat(repeat_count)
ds = ds.prefetch(buffer_size=AUTOTUNE)
return ds
def hr_dataset(self):
if not os.path.exists(self._hr_images_dir()):
raise ValueError("HR dataset directory not found")
ds = self._images_dataset(self._hr_image_files()).cache(self._hr_cache_file())
if not os.path.exists(self._hr_cache_index()):
self._populate_cache(ds, self._hr_cache_file())
return ds
def lr_dataset(self):
if not os.path.exists(self._lr_images_dir()):
raise ValueError("LR dataset directory not found")
ds = self._images_dataset(self._lr_image_files()).cache(self._lr_cache_file())
if not os.path.exists(self._lr_cache_index()):
self._populate_cache(ds, self._lr_cache_file())
return ds
def _hr_cache_file(self):
return os.path.join(self.caches_dir, f'{self.subset}_HR.cache')
def _lr_cache_file(self):
return os.path.join(self.caches_dir, f'{self.subset}_LR_{self.downgrade}_X{self.scale}.cache')
def _hr_cache_index(self):
return f'{self._hr_cache_file()}.index'
def _lr_cache_index(self):
return f'{self._lr_cache_file()}.index'
def _hr_image_files(self):
images_dir = self._hr_images_dir()
return [os.path.join(images_dir, filename) for filename in os.listdir(images_dir)]
def _lr_image_files(self):
images_dir = self._lr_images_dir()
return [os.path.join(images_dir, filename) for filename in os.listdir(images_dir)]
def _hr_images_dir(self):
return os.path.join(self.images_dir, f'{self.subset}_HR')
def _lr_images_dir(self):
return os.path.join(self.images_dir, f'{self.subset}_LR_{self.downgrade}', f'X{self.scale}')
def _hr_images_archive(self):
return f'DIV2K_{self.subset}_HR.zip'
def _lr_images_archive(self):
return f'{self.subset}_LR_{self.downgrade}_X{self.scale}.zip'
@staticmethod
def _images_dataset(image_files):
ds = tf.data.Dataset.from_tensor_slices(image_files)
ds = ds.map(tf.io.read_file)
ds = ds.map(lambda x: tf.image.decode_png(x, channels=1), num_parallel_calls=AUTOTUNE)
return ds
@staticmethod
def _populate_cache(ds, cache_file):
print(f'Caching decoded images in {cache_file} ...')
for _ in ds:
pass
print(f'Cached decoded images in {cache_file}.')
# -----------------------------------------------------------
# Transformations
# -----------------------------------------------------------
def random_crop(lr_img, hr_img, hr_crop_size=96, scale=2):
lr_crop_size = hr_crop_size // scale
lr_img_shape = tf.shape(lr_img)[:2]
lr_w = tf.random.uniform(shape=(), maxval=lr_img_shape[1] - lr_crop_size + 1, dtype=tf.int32)
lr_h = tf.random.uniform(shape=(), maxval=lr_img_shape[0] - lr_crop_size + 1, dtype=tf.int32)
hr_w = lr_w * scale
hr_h = lr_h * scale
lr_img_cropped = lr_img[lr_h:lr_h + lr_crop_size, lr_w:lr_w + lr_crop_size]
hr_img_cropped = hr_img[hr_h:hr_h + hr_crop_size, hr_w:hr_w + hr_crop_size]
return lr_img_cropped, hr_img_cropped
def random_flip(lr_img, hr_img):
rn = tf.random.uniform(shape=(), maxval=1)
return tf.cond(rn < 0.5,
lambda: (lr_img, hr_img),
lambda: (tf.image.flip_left_right(lr_img),
tf.image.flip_left_right(hr_img)))
def random_rotate(lr_img, hr_img):
rn = tf.random.uniform(shape=(), maxval=4, dtype=tf.int32)
return tf.image.rot90(lr_img, rn), tf.image.rot90(hr_img, rn)