-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathutils.py
182 lines (161 loc) · 5.81 KB
/
utils.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
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 28 15:36:37 2019
@author: wmy
"""
import numpy as np
import matplotlib.pyplot as plt
import os
import random
from PIL import Image
from PIL import ImageFilter
class DataLoader(object):
def __init__(self, scale=4, crop_size=96, name=None):
self.__scale = 4
self.__crop_size = 96
self.scale = scale
self.crop_size = crop_size
self.name = name
pass
@property
def scale(self):
return self.__scale
@scale.setter
def scale(self, value):
if not isinstance(value, int):
raise ValueError("scale must be int")
elif value <= 0:
raise ValueError("scale must > 0")
else:
self.__scale = value
pass
pass
@property
def crop_size(self):
return self.__crop_size
@crop_size.setter
def crop_size(self, value):
if not isinstance(value, int):
raise ValueError("crop size must be int")
elif value <= 0:
raise ValueError("crop size must > 0")
else:
self.__crop_size = value
pass
pass
def imread(self, path):
return Image.open(path)
def resize(self, image, size):
resamples = [Image.NEAREST, Image.BILINEAR, Image.HAMMING, \
Image.BICUBIC, Image.LANCZOS]
resample = random.choice(resamples)
return image.resize(size, resample=resample)
def gaussianblur(self, image, radius=2):
return image.filter(ImageFilter.GaussianBlur(radius=radius))
def medianfilter(self, image, size=3):
return image.filter(ImageFilter.MedianFilter(size=size))
def downsampling(self, image):
resize = (image.size[0]//self.scale, image.size[1]//self.scale)
hidden_scale = random.uniform(1, 3)
hidden_resize = (int(resize[0]/hidden_scale), int(resize[1]/hidden_scale))
radius = random.uniform(1, 3)
image = self.gaussianblur(image, radius)
image = self.resize(image, hidden_resize)
image = self.resize(image, resize)
return image
def search(self, setpath):
results = []
files = os.listdir(setpath)
for file in files:
path = os.path.join(setpath, file)
results.append(path)
pass
return results
def rotate(self, lr, hr):
angle = random.choice([0, 90, 180, 270])
lr = lr.rotate(angle, expand=True)
hr = hr.rotate(angle, expand=True)
return lr, hr
def flip(self, lr, hr):
mode = random.choice([0, 1, 2, 3])
if mode == 0:
pass
elif mode == 1:
lr = lr.transpose(Image.FLIP_LEFT_RIGHT)
hr = hr.transpose(Image.FLIP_LEFT_RIGHT)
pass
elif mode == 2:
lr = lr.transpose(Image.FLIP_TOP_BOTTOM)
hr = hr.transpose(Image.FLIP_TOP_BOTTOM)
pass
elif mode == 3:
lr = lr.transpose(Image.FLIP_LEFT_RIGHT)
hr = hr.transpose(Image.FLIP_LEFT_RIGHT)
lr = lr.transpose(Image.FLIP_TOP_BOTTOM)
hr = hr.transpose(Image.FLIP_TOP_BOTTOM)
pass
return lr, hr
def crop(self, lr, hr):
hr_crop_size = self.crop_size
lr_crop_size = hr_crop_size//self.scale
lr_w = np.random.randint(lr.size[0] - lr_crop_size + 1)
lr_h = np.random.randint(lr.size[1] - lr_crop_size + 1)
hr_w = lr_w * self.scale
hr_h = lr_h * self.scale
lr = lr.crop([lr_w, lr_h, lr_w+lr_crop_size, lr_h+lr_crop_size])
hr = hr.crop([hr_w, hr_h, hr_w+hr_crop_size, hr_h+hr_crop_size])
return lr, hr
def pair(self, fp):
hr = self.imread(fp)
lr = self.downsampling(hr)
lr, hr = self.rotate(lr, hr)
lr, hr = self.flip(lr, hr)
lr, hr = self.crop(lr, hr)
lr = np.asarray(lr)
hr = np.asarray(hr)
return lr, hr
def batches(self, setpath="datasets/train", batch_size=16, complete_batch_only=False):
images = self.search(setpath)
sizes = []
for image in images:
array = plt.imread(image)
sizes.append(array.shape[0])
sizes.append(array.shape[1])
pass
crop_size_max = min(sizes)
crop_size = min(crop_size_max, self.crop_size)
if self.crop_size != crop_size:
self.crop_size = crop_size
print("Info: crop size adjusted to " + str(self.crop_size) + ".")
pass
np.random.shuffle(images)
n_complete_batches = int(len(images)/batch_size)
self.n_batches = int(len(images) / batch_size)
have_res_batch = (len(images)/batch_size) > n_complete_batches
if have_res_batch and complete_batch_only==False:
self.n_batches += 1
pass
for i in range(n_complete_batches):
batch = images[i*batch_size:(i+1)*batch_size]
lrs, hrs = [], []
for image in batch:
lr, hr = self.pair(image)
lrs.append(lr)
hrs.append(hr)
pass
lrs = np.array(lrs)
hrs = np.array(hrs)
yield lrs, hrs
if self.n_batches > n_complete_batches:
batch = images[n_complete_batches*batch_size:]
lrs, hrs = [], []
for image in batch:
lr, hr = self.pair(image)
lrs.append(lr)
hrs.append(hr)
pass
lrs = np.array(lrs)
hrs = np.array(hrs)
yield lrs, hrs
pass
pass