forked from arahusky/Tensorflow-Segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolutional_autoencoder.py
342 lines (258 loc) · 14.8 KB
/
convolutional_autoencoder.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import math
import os
import time
from math import ceil
import cv2
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.ops import gen_nn_ops
from imgaug import augmenters as iaa
from imgaug import imgaug
from conv2d import Conv2d
from max_pool_2d import MaxPool2d
import datetime
import io
np.set_printoptions(threshold=np.nan)
# @ops.RegisterGradient("MaxPoolWithArgmax")
# def _MaxPoolWithArgmaxGrad(op, grad, unused_argmax_grad):
# return gen_nn_ops._max_pool_grad(op.inputs[0],
# op.outputs[0],
# grad,
# op.get_attr("ksize"),
# op.get_attr("strides"),
# padding=op.get_attr("padding"),
# data_format='NHWC')
class Network:
IMAGE_HEIGHT = 128
IMAGE_WIDTH = 128
IMAGE_CHANNELS = 1
def __init__(self, layers=None, per_image_standardization=True, batch_norm=True, skip_connections=True):
# Define network - ENCODER (decoder will be symmetric).
if layers == None:
layers = []
layers.append(Conv2d(kernel_size=7, strides=[1, 2, 2, 1], output_channels=64, name='conv_1_1'))
layers.append(Conv2d(kernel_size=7, strides=[1, 1, 1, 1], output_channels=64, name='conv_1_2'))
layers.append(MaxPool2d(kernel_size=2, name='max_1', skip_connection=skip_connections))
layers.append(Conv2d(kernel_size=7, strides=[1, 2, 2, 1], output_channels=64, name='conv_2_1'))
layers.append(Conv2d(kernel_size=7, strides=[1, 1, 1, 1], output_channels=64, name='conv_2_2'))
layers.append(MaxPool2d(kernel_size=2, name='max_2', skip_connection=skip_connections))
layers.append(Conv2d(kernel_size=7, strides=[1, 2, 2, 1], output_channels=64, name='conv_3_1'))
layers.append(Conv2d(kernel_size=7, strides=[1, 1, 1, 1], output_channels=64, name='conv_3_2'))
layers.append(MaxPool2d(kernel_size=2, name='max_3'))
self.inputs = tf.placeholder(tf.float32, [None, self.IMAGE_HEIGHT, self.IMAGE_WIDTH, self.IMAGE_CHANNELS],
name='inputs')
self.targets = tf.placeholder(tf.float32, [None, self.IMAGE_HEIGHT, self.IMAGE_WIDTH, 1], name='targets')
self.is_training = tf.placeholder_with_default(False, [], name='is_training')
self.description = ""
self.layers = {}
if per_image_standardization:
list_of_images_norm = tf.map_fn(tf.image.per_image_standardization, self.inputs)
net = tf.stack(list_of_images_norm)
else:
net = self.inputs
# ENCODER
for layer in layers:
self.layers[layer.name] = net = layer.create_layer(net)
self.description += "{}".format(layer.get_description())
print("Current input shape: ", net.get_shape())
layers.reverse()
Conv2d.reverse_global_variables()
# DECODER
for layer in layers:
net = layer.create_layer_reversed(net, prev_layer=self.layers[layer.name])
self.segmentation_result = tf.sigmoid(net)
# segmentation_as_classes = tf.reshape(self.y, [50 * self.IMAGE_HEIGHT * self.IMAGE_WIDTH, 1])
# targets_as_classes = tf.reshape(self.targets, [50 * self.IMAGE_HEIGHT * self.IMAGE_WIDTH])
# print(self.y.get_shape())
# self.cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(segmentation_as_classes, targets_as_classes))
print('segmentation_result.shape: {}, targets.shape: {}'.format(self.segmentation_result.get_shape(),
self.targets.get_shape()))
# MSE loss
self.cost = tf.sqrt(tf.reduce_mean(tf.square(self.segmentation_result - self.targets)))
self.train_op = tf.train.AdamOptimizer().minimize(self.cost)
with tf.name_scope('accuracy'):
argmax_probs = tf.round(self.segmentation_result) # 0x1
correct_pred = tf.cast(tf.equal(argmax_probs, self.targets), tf.float32)
self.accuracy = tf.reduce_mean(correct_pred)
tf.summary.scalar('accuracy', self.accuracy)
self.summaries = tf.summary.merge_all()
class Dataset:
def __init__(self, batch_size, folder='data128_128', include_hair=False):
self.batch_size = batch_size
self.include_hair = include_hair
train_files, validation_files, test_files = self.train_valid_test_split(
os.listdir(os.path.join(folder, 'inputs')))
self.train_inputs, self.train_targets = self.file_paths_to_images(folder, train_files)
self.test_inputs, self.test_targets = self.file_paths_to_images(folder, test_files, True)
self.pointer = 0
def file_paths_to_images(self, folder, files_list, verbose=False):
inputs = []
targets = []
for file in files_list:
input_image = os.path.join(folder, 'inputs', file)
target_image = os.path.join(folder, 'targets' if self.include_hair else 'targets_face_only', file)
test_image = np.array(cv2.imread(input_image, 0)) # load grayscale
# test_image = np.multiply(test_image, 1.0 / 255)
inputs.append(test_image)
target_image = cv2.imread(target_image, 0)
target_image = cv2.threshold(target_image, 127, 1, cv2.THRESH_BINARY)[1]
targets.append(target_image)
return inputs, targets
def train_valid_test_split(self, X, ratio=None):
if ratio is None:
ratio = (0.7, .15, .15)
N = len(X)
return (
X[:int(ceil(N * ratio[0]))],
X[int(ceil(N * ratio[0])): int(ceil(N * ratio[0] + N * ratio[1]))],
X[int(ceil(N * ratio[0] + N * ratio[1])):]
)
def num_batches_in_epoch(self):
return int(math.floor(len(self.train_inputs) / self.batch_size))
def reset_batch_pointer(self):
permutation = np.random.permutation(len(self.train_inputs))
self.train_inputs = [self.train_inputs[i] for i in permutation]
self.train_targets = [self.train_targets[i] for i in permutation]
self.pointer = 0
def next_batch(self):
inputs = []
targets = []
# print(self.batch_size, self.pointer, self.train_inputs.shape, self.train_targets.shape)
for i in range(self.batch_size):
inputs.append(np.array(self.train_inputs[self.pointer + i]))
targets.append(np.array(self.train_targets[self.pointer + i]))
self.pointer += self.batch_size
return np.array(inputs, dtype=np.uint8), np.array(targets, dtype=np.uint8)
@property
def test_set(self):
return np.array(self.test_inputs, dtype=np.uint8), np.array(self.test_targets, dtype=np.uint8)
def draw_results(test_inputs, test_targets, test_segmentation, test_accuracy, network, batch_num):
n_examples_to_plot = 12
fig, axs = plt.subplots(4, n_examples_to_plot, figsize=(n_examples_to_plot * 3, 10))
fig.suptitle("Accuracy: {}, {}".format(test_accuracy, network.description), fontsize=20)
for example_i in range(n_examples_to_plot):
axs[0][example_i].imshow(test_inputs[example_i], cmap='gray')
axs[1][example_i].imshow(test_targets[example_i].astype(np.float32), cmap='gray')
axs[2][example_i].imshow(
np.reshape(test_segmentation[example_i], [network.IMAGE_HEIGHT, network.IMAGE_WIDTH]),
cmap='gray')
test_image_thresholded = np.array(
[0 if x < 0.5 else 255 for x in test_segmentation[example_i].flatten()])
axs[3][example_i].imshow(
np.reshape(test_image_thresholded, [network.IMAGE_HEIGHT, network.IMAGE_WIDTH]),
cmap='gray')
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
IMAGE_PLOT_DIR = 'image_plots/'
if not os.path.exists(IMAGE_PLOT_DIR):
os.makedirs(IMAGE_PLOT_DIR)
plt.savefig('{}/figure{}.jpg'.format(IMAGE_PLOT_DIR, batch_num))
return buf
def train():
BATCH_SIZE = 100
network = Network()
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")
# create directory for saving models
os.makedirs(os.path.join('save', network.description, timestamp))
dataset = Dataset(folder='data{}_{}'.format(network.IMAGE_HEIGHT, network.IMAGE_WIDTH), include_hair=False,
batch_size=BATCH_SIZE)
inputs, targets = dataset.next_batch()
print(inputs.shape, targets.shape)
# augmentation_seq = iaa.Sequential([
# iaa.Crop(px=(0, 16)), # crop images from each side by 0 to 16px (randomly chosen)
# iaa.Fliplr(0.5), # horizontally flip 50% of the images
# iaa.GaussianBlur(sigma=(0, 2.0)) # blur images with a sigma of 0 to 3.0
# ])
augmentation_seq = iaa.Sequential([
iaa.Crop(px=(0, 16), name="Cropper"), # crop images from each side by 0 to 16px (randomly chosen)
iaa.Fliplr(0.5, name="Flipper"),
iaa.GaussianBlur((0, 3.0), name="GaussianBlur"),
iaa.Dropout(0.02, name="Dropout"),
iaa.AdditiveGaussianNoise(scale=0.01 * 255, name="GaussianNoise"),
iaa.Affine(translate_px={"x": (-network.IMAGE_HEIGHT // 3, network.IMAGE_WIDTH // 3)}, name="Affine")
])
# change the activated augmenters for binary masks,
# we only want to execute horizontal crop, flip and affine transformation
def activator_binmasks(images, augmenter, parents, default):
if augmenter.name in ["GaussianBlur", "Dropout", "GaussianNoise"]:
return False
else:
# default value for all other augmenters
return default
hooks_binmasks = imgaug.HooksImages(activator=activator_binmasks)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
summary_writer = tf.summary.FileWriter('{}/{}-{}'.format('logs', network.description, timestamp),
graph=tf.get_default_graph())
saver = tf.train.Saver(tf.all_variables(), max_to_keep=None)
test_accuracies = []
# Fit all training data
n_epochs = 500
global_start = time.time()
for epoch_i in range(n_epochs):
dataset.reset_batch_pointer()
for batch_i in range(dataset.num_batches_in_epoch()):
batch_num = epoch_i * dataset.num_batches_in_epoch() + batch_i + 1
augmentation_seq_deterministic = augmentation_seq.to_deterministic()
start = time.time()
batch_inputs, batch_targets = dataset.next_batch()
batch_inputs = np.reshape(batch_inputs,
(dataset.batch_size, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1))
batch_targets = np.reshape(batch_targets,
(dataset.batch_size, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1))
batch_inputs = augmentation_seq_deterministic.augment_images(batch_inputs)
batch_inputs = np.multiply(batch_inputs, 1.0 / 255)
batch_targets = augmentation_seq_deterministic.augment_images(batch_targets, hooks=hooks_binmasks)
cost, _ = sess.run([network.cost, network.train_op],
feed_dict={network.inputs: batch_inputs, network.targets: batch_targets,
network.is_training: True})
end = time.time()
print('{}/{}, epoch: {}, cost: {}, batch time: {}'.format(batch_num,
n_epochs * dataset.num_batches_in_epoch(),
epoch_i, cost, end - start))
if batch_num % 100 == 0 or batch_num == n_epochs * dataset.num_batches_in_epoch():
test_inputs, test_targets = dataset.test_set
# test_inputs, test_targets = test_inputs[:100], test_targets[:100]
test_inputs = np.reshape(test_inputs, (-1, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1))
test_targets = np.reshape(test_targets, (-1, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1))
test_inputs = np.multiply(test_inputs, 1.0 / 255)
print(test_inputs.shape)
summary, test_accuracy = sess.run([network.summaries, network.accuracy],
feed_dict={network.inputs: test_inputs,
network.targets: test_targets,
network.is_training: False})
summary_writer.add_summary(summary, batch_num)
print('Step {}, test accuracy: {}'.format(batch_num, test_accuracy))
test_accuracies.append((test_accuracy, batch_num))
print("Accuracies in time: ", [test_accuracies[x][0] for x in range(len(test_accuracies))])
max_acc = max(test_accuracies)
print("Best accuracy: {} in batch {}".format(max_acc[0], max_acc[1]))
print("Total time: {}".format(time.time() - global_start))
# Plot example reconstructions
n_examples = 12
test_inputs, test_targets = dataset.test_inputs[:n_examples], dataset.test_targets[:n_examples]
test_inputs = np.multiply(test_inputs, 1.0 / 255)
test_segmentation = sess.run(network.segmentation_result, feed_dict={
network.inputs: np.reshape(test_inputs,
[n_examples, network.IMAGE_HEIGHT, network.IMAGE_WIDTH, 1])})
# Prepare the plot
test_plot_buf = draw_results(test_inputs, test_targets, test_segmentation, test_accuracy, network,
batch_num)
# Convert PNG buffer to TF image
image = tf.image.decode_png(test_plot_buf.getvalue(), channels=4)
# Add the batch dimension
image = tf.expand_dims(image, 0)
# Add image summary
image_summary_op = tf.summary.image("plot", image)
image_summary = sess.run(image_summary_op)
summary_writer.add_summary(image_summary)
if test_accuracy >= max_acc[0]:
checkpoint_path = os.path.join('save', network.description, timestamp, 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=batch_num)
if __name__ == '__main__':
train()