-
Notifications
You must be signed in to change notification settings - Fork 19
/
vdsr.py
executable file
·195 lines (158 loc) · 6.51 KB
/
vdsr.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
#!/usr/bin/env python
from __future__ import print_function
import keras
from keras import backend as K
from keras.models import Sequential, Model
from keras.layers import Dense, Activation
from keras.layers import Conv2D, MaxPooling2D, Input, Merge, ZeroPadding2D, merge, add
import tensorflow as tf
from keras.models import load_model
from keras import optimizers
from keras import losses
from keras.optimizers import SGD, Adam
from keras.callbacks import ModelCheckpoint
import os, glob, sys, threading
import scipy.io
from scipy import ndimage, misc
import numpy as np
import re
import math
DATA_PATH = "./data/train/"
IMG_SIZE = (41, 41, 1)
BATCH_SIZE = 64
EPOCHS = 100
TRAIN_SCALES = [2, 3, 4]
VALID_SCALES = [4]
def tf_log10(x):
numerator = tf.log(x)
denominator = tf.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
def load_images(directory):
images = []
for root, dirnames, filenames in os.walk(directory):
for filename in filenames:
if re.search("\.(jpg|jpeg|png|bmp|tiff)$", filename):
filepath = os.path.join(root, filename)
image = ndimage.imread(filepath, mode="L")
images.append(image)
images = np.array(images)
array_shape = np.append(images.shape[0:3], 1)
images = np.reshape(images, (array_shape))
return images
def get_image_list(data_path, scales=[2, 3, 4]):
l = glob.glob(os.path.join(data_path,"*"))
# print(len(l))
l = [f for f in l if re.search("^\d+.mat$", os.path.basename(f))]
# print(len(l))
train_list = []
for f in l:
if os.path.exists(f):
for i in range(len(scales)):
scale = scales[i]
string_scale = "_" + str(scale) + ".mat"
if os.path.exists(f[:-4]+string_scale): train_list.append([f, f[:-4]+string_scale])
return train_list
def get_image_batch(train_list, offset):
target_list = train_list[offset:offset+BATCH_SIZE]
input_list = []
gt_list = []
cbcr_list = []
for pair in target_list:
input_img = scipy.io.loadmat(pair[1])['patch']
gt_img = scipy.io.loadmat(pair[0])['patch']
input_list.append(input_img)
gt_list.append(gt_img)
input_list = np.array(input_list)
input_list.resize([BATCH_SIZE, IMG_SIZE[0], IMG_SIZE[1], 1])
gt_list = np.array(gt_list)
gt_list.resize([BATCH_SIZE, IMG_SIZE[0], IMG_SIZE[1], 1])
return input_list, gt_list
class threadsafe_iter:
"""Takes an iterator/generator and makes it thread-safe by
serializing call to the `next` method of given iterator/generator.
"""
def __init__(self, it):
self.it = it
self.lock = threading.Lock()
def __iter__(self):
return self
def next(self):
with self.lock:
return self.it.next()
def threadsafe_generator(f):
"""A decorator that takes a generator function and makes it thread-safe.
"""
def g(*a, **kw):
return threadsafe_iter(f(*a, **kw))
return g
@threadsafe_generator
def image_gen(target_list):
while True:
for step in range(len(target_list)//BATCH_SIZE):
offset = step*BATCH_SIZE
batch_x, batch_y = get_image_batch(target_list, offset)
yield (batch_x, batch_y)
def PSNR(y_true, y_pred):
max_pixel = 1.0
return 10.0 * tf_log10((max_pixel ** 2) / (K.mean(K.square(y_pred - y_true))))
# Get the training and testing data
train_list = get_image_list("./data/train/", scales=TRAIN_SCALES)
test_list = get_image_list("./data/test/Set5/", scales=VALID_SCALES)
input_img = Input(shape=IMG_SIZE)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(input_img)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(64, (3, 3), padding='same', kernel_initializer='he_normal')(model)
model = Activation('relu')(model)
model = Conv2D(1, (3, 3), padding='same', kernel_initializer='he_normal')(model)
res_img = model
output_img = add([res_img, input_img])
model = Model(input_img, output_img)
# model.load_weights('./checkpoints/weights-improvement-20-26.93.hdf5')
adam = Adam(lr=0.00001)
sgd = SGD(lr=1e-5, momentum=0.9, decay=1e-4, nesterov=False)
model.compile(adam, loss='mse', metrics=[PSNR, "accuracy"])
model.summary()
filepath="./checkpoints/weights-improvement-{epoch:02d}-{PSNR:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor=PSNR, verbose=1, mode='max')
callbacks_list = [checkpoint]
model.fit_generator(image_gen(train_list), steps_per_epoch=len(train_list) // BATCH_SIZE, \
validation_data=image_gen(test_list), validation_steps=len(train_list) // BATCH_SIZE, \
epochs=EPOCHS, workers=8, callbacks=callbacks_list)
print("Done training!!!")
print("Saving the final model ...")
model.save('vdsr_model.h5') # creates a HDF5 file
del model # deletes the existing model