-
Notifications
You must be signed in to change notification settings - Fork 5
/
denoise_input.py
executable file
·248 lines (176 loc) · 7.38 KB
/
denoise_input.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from PIL import Image
import numpy as np
import tensorflow as tf
from tqdm import tqdm
import os
import re
import argparse
PATCH_SHAPE = (128 ,128, 3)
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def load_patches_as_array(root, TO_YCbCr = True):
"""Helper function to load patches, normalize and convert into numpy arrays
Input:
root: folder path for image patches, with file name in sequence.
Output:
image_array: A numpy array of size (batch_size, 128, 128, 3)
"""
if not os.path.exists(root):
raise IOError(root + " does not exists.")
image_array = []
for r, fo, f in os.walk(root):
f = sorted(f)
with tqdm(total = len(f), desc = "loading " + root) as pbar:
for im in f:
try:
im = Image.open(r + im)
if TO_YCbCr:
im = im.convert('YCbCr')
except IOError:
print ("File Not Found, ", r + im)
break
im = np.asarray(im)
if im.shape[2] == 4:
im = im[:,:,0:3]
if not im.shape == PATCH_SHAPE:
raise ValueError("Wrong Image Patch Size, %s" % str(im.shape))
image_array.append(im)
pbar.update(1)
image_array = np.asarray(image_array)
return image_array
def load_patches(CleanRoot = "./Images/CleanPatches/", NoisyRoot = "./Images/NoisyPatches/"):
"""Helper function to load Clean and Noisy patches for training.
Input:
CleanRoot: Root folder path for Clean patches.
NoisyRoot: Root folder path for Noisy patches.
Return:
clean_patches: size of (N, PATCH_SHAPE), numpy array
noisy_patches: size of (N, PATCH_SHAPE), numpy array
"""
assert type(CleanRoot) is str, "Invalid root for clean patches"
assert type(NoisyRoot) is str, "Invalid root for noisy patches"
clean_patches = load_patches_as_array(CleanRoot)
noisy_patches = load_patches_as_array(NoisyRoot)
"""Sanity Check: clean_patches shape same noisy_patches """
assert clean_patches.shape == noisy_patches.shape, "Shape mismatch of clean_patches(%s) and noisy_patches(%s)" % (str(clean_patches.shape), str(noisy_patches.shape))
return clean_patches, noisy_patches
def convert_patches_to_tfRecords(root, TO_YCbCr = True, output = None):
"""Helper function to convert a folder of patch files into tf records
Input:
root: root folder for Clean and Noisy patches
TO_YCbCr: convert patches into YCbCr space?
Return:
count: number of patches converted
"""
if output == None:
dataset_filename = os.path.join(root, 'dataset.tfrecords')
else:
dataset_filename = os.path.join(output, 'dataset.tfrecords')
CleanRoot = os.path.join(root, "Clean")
NoisyRoot = os.path.join(root, "Noisy")
if not os.path.exists(CleanRoot) or not os.path.exists(NoisyRoot):
raise IOError("%s or %s does not exists." % (CleanRoot, NoisyRoot))
CleanFs = os.listdir(CleanRoot)
NoisyFs = os.listdir(NoisyRoot)
assert len(CleanFs) == len(NoisyFs), "File # mismatch"
CleanFs = sorted(CleanFs)
NoisyFs = sorted(NoisyFs)
total_count = len(CleanFs)
print ('Writing', dataset_filename)
pbar = tqdm(total = total_count, desc = "loading")
writer = tf.python_io.TFRecordWriter(dataset_filename)
for i in range(total_count):
CleanF = CleanFs[i]
NoisyF = NoisyFs[i]
try:
cim = Image.open(os.path.join(CleanRoot, CleanF))
nim = Image.open(os.path.join(NoisyRoot, NoisyF))
if TO_YCbCr:
cim = cim.convert('YCbCr')
nim = nim.convert('YCbCr')
except IOError as e:
print (e)
continue
cim = np.asarray(cim)
nim = np.asarray(nim)
if cim.shape[2] == 4:
cim = cim[:,:,0:3]
if nim.shape[2] == 4:
nim = nim[:,:,0:3]
if not nim.shape == PATCH_SHAPE or not cim.shape == PATCH_SHAPE:
raise ValueError("Wrong Image Patch Size, %s, %s" %
(str(nim.shape), str(cim.shape)))
# Convert to float64 will cause filesize increase by 8 times.
# cim = cim / 255.
# nim = nim / 255.
cim_raw = cim.tostring()
nim_raw = nim.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(PATCH_SHAPE[0]),
'width': _int64_feature(PATCH_SHAPE[1]),
'depth': _int64_feature(PATCH_SHAPE[2]),
'clean_raw': _bytes_feature(cim_raw),
'noisy_raw': _bytes_feature(nim_raw)
}))
writer.write(example.SerializeToString())
pbar.update(1)
writer.close
return total_count
def read_tfRecords(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
# Defaults are not specified since both keys are required.
features={
'clean_raw': tf.FixedLenFeature([], tf.string),
'noisy_raw': tf.FixedLenFeature([], tf.string)
})
cim = tf.decode_raw(features['clean_raw'], tf.uint8)
nim = tf.decode_raw(features['noisy_raw'], tf.uint8)
cim.set_shape([np.prod(list(PATCH_SHAPE))])
nim.set_shape([np.prod(list(PATCH_SHAPE))])
cim = tf.reshape(cim, [*PATCH_SHAPE])
nim = tf.reshape(nim, [*PATCH_SHAPE])
# Floatarize before batching expands total memory usage
# by 8 times!!!
# cim = tf.cast(cim, tf.float32) * (1. / 255)
# nim = tf.cast(nim, tf.float32) * (1. / 255)
# print ("cim", cim)
return cim, nim
def input_tfRecords(fn, batch_size):
with tf.name_scope('input'):
with tf.device('cpu:0'):
filename_queue = tf.train.string_input_producer(
[fn], num_epochs = None #train Forever
)
cim, nim = read_tfRecords(filename_queue)
min_after_dequeue = 100000
cims, nims = tf.train.shuffle_batch(
[cim, nim], batch_size=batch_size, num_threads = 2,
capacity = min_after_dequeue + 3 * batch_size,
min_after_dequeue = min_after_dequeue
)
cims = tf.cast(cims, tf.float32) * (1. / 255)
nims = tf.cast(nims, tf.float32) * (1. / 255)
print ('cims', cims.name)
print ('nims', nims.name)
return cims, nims
def extract_patches_from_image_tensor(clean_imgt, noisy_imgt):
"""
Extract patches from the given image tensor:
"""
def main(args):
root = args.root
ycbcr = args.ycbcr if args.ycbcr else True
output = args.output if args.output else None
convert_patches_to_tfRecords(root, ycbcr, output)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('root', type=str, help = 'root folder for clean and noisy patches')
parser.add_argument('--ycbcr', action = 'store_true', help = 'preprocess patches into ycbcr space')
parser.add_argument('--output', type=str, help = '(Optional) Output folder')
args = parser.parse_args()
main(args)