-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_2_export_trained_estimator.py
143 lines (118 loc) · 5.19 KB
/
4_2_export_trained_estimator.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
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)
# model_fn with tf.estimator.Estimator function signature
def cnn_model_fn(features, labels, mode, params):
"""
:param features: dictionary with single key 'x' which represents input images
:param labels: ground truth label
:param mode: tensorflow mode - TRAIN, PREDICT, EVAL
:param params: dictionay of additional parameter
:return: tf.estimator.EstimatorSpec
"""
# ================================
# common operations for all modes
# ================================
is_training = mode == tf.estimator.ModeKeys.TRAIN
input_size = 28
n_output_classes = 10
if params is not None:
input_size = params['input_size']
n_output_classes = params['n_output_classes']
inputs = tf.reshape(features['x'], shape=[-1, input_size, input_size, 1])
# Convolutional Layer #1
# [batch_size, 28, 28, 1] => [batch_size, 14, 14, 32]
conv1 = tf.layers.conv2d(inputs, filters=32, kernel_size=5, padding='same', activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(conv1, pool_size=[2, 2], strides=2)
# Convolutional Layer #2
# [batch_size, 14, 14, 32] => [batch_size, 7, 7, 64]
conv2 = tf.layers.conv2d(pool1, filters=64, kernel_size=5, padding='same', activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(conv2, pool_size=[2, 2], strides=2)
# Flatten tensor into a batch of vectors
# [batch_size, 7, 7, 64] => [batch_size, 7 * 7 * 64]
flat3 = tf.layers.flatten(pool2)
# Dense Layer with dropout
# [batch_size, 7 * 7 * 64] => [batch_size, 1024]
dense4 = tf.layers.dense(flat3, units=1024, activation=tf.nn.relu)
dropout4 = tf.layers.dropout(dense4, rate=0.4, training=is_training)
# Logits layer
# [batch_size, 1024] => [batch_size, 10]
logits = tf.layers.dense(dropout4, units=n_output_classes)
# # add items to log
# input_label_copy = tf.identity(labels[0], name='first_label_item')
# ================================
# prediction & serving mode
# mode == tf.estimator.ModeKeys.PREDICT == 'infer'
# ================================
predicted_classes = tf.argmax(logits, axis=1)
predictions = {
'class_id': tf.cast(predicted_classes, dtype=tf.int32),
'probabilities': tf.nn.softmax(logits),
'logits': logits,
}
# export output must be one of tf.estimator.export. ... class NOT a Tensor
export_outputs = {
'exported_output': tf.estimator.export.PredictOutput(
{
'dense4': tf.identity(dense4, name='dense4'),
'output_classes': predictions['class_id'],
}
),
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs)
# compute loss
# labels: integer 0 ~ 9
# logits: score not probability
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
# compute evaluation metric
accuracy = tf.metrics.accuracy(labels=labels, predictions=predicted_classes, name='acc_op')
metrics = {'accuracy': accuracy} # during evaluation
tf.summary.scalar('accuracy', accuracy[1]) # during training
# ================================
# evaluation mode
# ================================
if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=metrics)
# ================================
# training mode
# ================================
assert mode == tf.estimator.ModeKeys.TRAIN
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train_ops = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_ops)
# function used to map input for tensorflow serving system
def serving_input_receiver_fn():
# dictionary used to input when serving
# here 'x' is the serving input name
inputs = {
'x': tf.placeholder(tf.float32, shape=[None, 28, 28, 1], name='serving_input_image_x')
}
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
def create_serving_model(model_dir):
# Load trained Estimator
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn,
model_dir=model_dir,
config=None,
params={
'input_size': 28,
'n_output_classes': 10,
},
warm_start_from=model_dir,
)
# below function will save servable files to 'model_dir'
# which will be named with current time stamp
# you can inspect saved servable model with saved_model_cli
# ex) saved_model_cli show --dir model_dir --all
# default model name: 'serve'
# default signature name: 'serving_default' or the name you specified in model_fn's export_outputs dict
# default output name: 'output'
mnist_classifier.export_savedmodel(model_dir, serving_input_receiver_fn=serving_input_receiver_fn)
return
def main():
# take pretrained model from '4_1_high_level_api.py'
model_dir = './models/high_api'
create_serving_model(model_dir)
return
if __name__ == '__main__':
main()