-
Notifications
You must be signed in to change notification settings - Fork 7
/
dataset.py
51 lines (39 loc) · 1.58 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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import random
import glob
import numpy as np
import PIL.Image as pil_image
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.enable_eager_execution(config=config)
class Dataset(object):
def __init__(self, images_dir, patch_size, scale, use_fast_loader=False):
self.image_files = sorted(glob.glob(images_dir + '/*'))
self.patch_size = patch_size
self.scale = scale
self.use_fast_loader = use_fast_loader
def __getitem__(self, idx):
if self.use_fast_loader:
hr = tf.read_file(self.image_files[idx])
hr = tf.image.decode_jpeg(hr, channels=3)
hr = pil_image.fromarray(hr.numpy())
else:
hr = pil_image.open(self.image_files[idx]).convert('RGB')
# randomly crop patch from training set
crop_x = random.randint(0, hr.width - self.patch_size * self.scale)
crop_y = random.randint(0, hr.height - self.patch_size * self.scale)
hr = hr.crop((crop_x, crop_y, crop_x + self.patch_size * self.scale, crop_y + self.patch_size * self.scale))
# degrade lr with Bicubic
lr = hr.resize((self.patch_size, self.patch_size), resample=pil_image.BICUBIC)
hr = np.array(hr).astype(np.float32)
lr = np.array(lr).astype(np.float32)
hr = np.transpose(hr, axes=[2, 0, 1])
lr = np.transpose(lr, axes=[2, 0, 1])
# normalization
hr /= 255.0
lr /= 255.0
return lr, hr
def __len__(self):
return len(self.image_files)