-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_encoder.py
180 lines (144 loc) · 5.38 KB
/
auto_encoder.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
"""
Definition of Auto Encoder model
Author: Venkat Rebba <rebba498@gmail.com>
"""
import tensorflow as tf
from tensorflow.python.keras.initializers import he_normal
from tensorflow.image import ResizeMethod
from tensorflow.train import AdamOptimizer as Adam
def conv_layer(input_layer, size_in, size_out, name="conv"):
"""
This function is to define convloution layer with
following default parmameters
Filter size: 5*5
Padding: SAME
Inititalization: HE Normal
Activation: LeakyRelu(Alpha=0.5)
"""
with tf.name_scope(name):
# Defining fliter weights
weights = tf.get_variable(
name + "/" + "weights",
dtype=tf.float32,
shape=[5, 5, size_in, size_out],
initializer=he_normal(seed=8)
)
# Defining biases
biases = tf.get_variable(
name + "/" + "biases",
dtype=tf.float32,
shape=[size_out],
initializer=tf.constant_initializer(0.01)
)
# Convolving the input with weights
conv = tf.nn.conv2d(input_layer, weights,
strides=[1, 1, 1, 1], padding="SAME")
# Applying leakey relu activation function
act = tf.nn.leaky_relu(conv + biases, alpha=0.5, name="leakyrelu")
# Adding historgrams to visualize in tensorboard
tf.summary.histogram("weights", weights)
tf.summary.histogram("biases", biases)
tf.summary.histogram("activations", act)
return act
def max_pool_layer(input_layer, filter_size, stride_size, name):
"""
In this function max pool layer is defined
"""
max_pool = tf.nn.max_pool(input_layer,
ksize=[1, filter_size, filter_size, 1],
strides=[1, stride_size, stride_size, 1],
padding="SAME", name=name)
return max_pool
def upsample_layer(input_layer, output_size, name):
"""
This function does upsampling of given input
"""
upsample = tf.image.resize_images(input_layer, size=output_size,
method=ResizeMethod.NEAREST_NEIGHBOR)
return upsample
def get_optimizer_op(logits, labels, learning_rate):
"""
Function computes loss value and minimize it through optimizer
"""
with tf.name_scope("loss"):
loss = tf.reduce_mean(
tf.losses.mean_squared_error(labels, logits),
name="loss"
)
tf.summary.scalar("loss", loss)
with tf.name_scope("train"):
train_step = Adam(learning_rate, name='Adam').minimize(
loss,
global_step=tf.train.get_global_step()
)
return loss, train_step
def auto_encoder_model(features, labels, params, mode):
"""
Auto Encoder model definition
"""
# Converting input to float
features = tf.to_float(features)
x = tf.reshape(features, [-1, 224, 224, 3], name="x")
tf.summary.image('input', x)
y = tf.identity(x)
# Encoder
# input size: 224*224*3
conv1 = conv_layer(x, 3, 32, "conv1")
# 224*224*32
pool1 = max_pool_layer(conv1, 2, 2, "pool1")
# 112*112*32
conv2 = conv_layer(pool1, 32, 64, "conv2")
# 112*112*64
pool2 = max_pool_layer(conv2, 2, 2, "pool2")
# 56*56*64
conv3 = conv_layer(pool2, 64, 128, "conv3")
# 56*56*128
pool3 = max_pool_layer(conv3, 2, 2, "pool3")
# Decoder
# 28*28*128
upsampl1 = upsample_layer(pool3, (56, 56), "upsample1")
# 56*56*128
conv4 = conv_layer(upsampl1, 128, 64, "conv4")
# 56*56*64
upsampl2 = upsample_layer(conv4, (112, 112), "upsample2")
# 112*112*64
conv5 = conv_layer(upsampl2, 64, 32, "conv5")
# 112*112*32
upsampl3 = upsample_layer(conv5, (224, 224), "upsample3")
# 224*224*32
logits = conv_layer(upsampl3, 32, 3, "logits")
tf.summary.image('logit_image', logits, 3)
learning_rate = params['learning_rate']
loss, train_step = get_optimizer_op(logits, y, learning_rate)
# Predictions, predict the loss of input image
predictions = {
"loss": loss,
"logits": logits
}
# when mode == 'infer'
if mode == tf.estimator.ModeKeys.PREDICT:
export_outputs = {
'frame_pred': tf.estimator.export.PredictOutput(predictions)
}
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
export_outputs=export_outputs
)
elif mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(
mode=mode,
loss=loss,
eval_metric_ops={
'eval_loss': tf.metrics.mean_squared_error(y, logits)
}
)
# Logging hook
logging_hook = tf.train.LoggingTensorHook({"loss": loss}, every_n_iter=5)
# default train mode
return tf.estimator.EstimatorSpec(
mode=mode,
loss=loss,
train_op=train_step,
training_hooks=[logging_hook]
)