-
Notifications
You must be signed in to change notification settings - Fork 0
/
project-cpu-estimator.py
157 lines (111 loc) · 6.14 KB
/
project-cpu-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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
import time
tf.logging.set_verbosity(tf.logging.INFO)
def _parse_function(proto):
tfrecord_features = tf.parse_single_example(proto,
features={
'label': tf.FixedLenFeature([], tf.int64),
'shape': tf.FixedLenFeature([], tf.string),
'image': tf.FixedLenFeature([], tf.string),
}, name='features')
# images were saved as uint8, so we have to decode as uint8.
image = tf.decode_raw(tfrecord_features['image'], tf.uint8)
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
shape = tf.decode_raw(tfrecord_features['shape'], tf.int32)
# the image tensor is flattened out, so we have to reconstruct the shape
image = tf.reshape(image, shape)
label = tfrecord_features['label']
label = tf.one_hot(label, 10)
return {'feature': image}, label
def read_dataset(file_names, mode, batch_size=64, prefetch=8):
dataset = tf.data.TFRecordDataset(file_names)
dataset = dataset.map(_parse_function, num_parallel_calls=48)
if mode == tf.estimator.ModeKeys.TRAIN:
dataset = dataset.repeat()
# dataset = dataset.shuffle(512)
dataset = dataset.batch(batch_size)
dataset = dataset.prefetch(buffer_size=prefetch)
return dataset
def model_fn(features, labels, mode, params):
inputs = tf.reshape(features['feature'], [-1, 240, 320, 3])
# we use xavier initializer to initialize variables
xav_init = tf.contrib.layers.xavier_initializer()
conv1 = tf.layers.conv2d(inputs=inputs, filters=64, kernel_size=[5, 5], padding="same", activation=tf.nn.relu, kernel_initializer=xav_init)
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[3, 3], strides=3)
# Convolutional Layer #2 and Pooling Layer #2
conv2 = tf.layers.conv2d(inputs=pool1, filters=128, kernel_size=[5, 5], padding="same", activation=tf.nn.relu, kernel_initializer=xav_init)
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[3, 3], strides=3)
conv3 = tf.layers.conv2d(inputs=pool2, filters=64, kernel_size=[5, 5], padding="same", activation=tf.nn.relu, kernel_initializer=xav_init)
pool3 = tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2], strides=2)
conv4 = tf.layers.conv2d(inputs=pool3, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.relu, kernel_initializer=xav_init)
pool4 = tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=2)
# Dense Layer
flat = tf.layers.flatten(pool4)
print('my_flaaaaatttt::',flat)
training = (mode == tf.estimator.ModeKeys.TRAIN)
dense1 = tf.layers.dense(inputs=flat, units=1024, activation=tf.nn.relu, kernel_initializer=xav_init)
dropout1 = tf.layers.dropout(inputs=dense1, rate=0.4, training=training)
dense2 = tf.layers.dense(inputs=dropout1, units=128, activation=tf.nn.relu, kernel_initializer=xav_init)
dropout2 = tf.layers.dropout(inputs=dense2, rate=0.4, training=training)
# Logits Layer
logits = tf.layers.dense(inputs=dropout2, units=10, kernel_initializer=xav_init)
# Softmax output of the neural network.
y_pred = tf.nn.softmax(logits=logits)
# Classification output of the neural network.
y_pred_cls = tf.argmax(y_pred, axis=1)
if mode == tf.estimator.ModeKeys.PREDICT:
# If the estimator is supposed to be in prediction-mode
# then use the predicted class-number that is output by
# the neural network. Optimization etc. is not needed.
spec = tf.estimator.EstimatorSpec(mode=mode, predictions=y_pred_cls)
else:
# Otherwise the estimator is supposed to be in either
# training or evaluation-mode. Note that the loss-function
# is also required in Evaluation mode.
# Define the loss-function to be optimized, by first
# calculating the cross-entropy between the output of
# the neural network and the true labels for the input data.
# This gives the cross-entropy for each image in the batch.
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=labels, logits=logits)
# Reduce the cross-entropy batch-tensor to a single number
# which can be used in optimization of the neural network.
loss = tf.reduce_mean(cross_entropy)
# Define the optimizer for improving the neural network.
optimizer = tf.train.AdamOptimizer(params["learning_rate"])
# Get the TensorFlow op for doing a single optimization step.
train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
# Define the evaluation metrics,
# in this case the classification accuracy.
accuracy = tf.metrics.accuracy(labels=tf.argmax(labels, axis=1), predictions=y_pred_cls, name='acc_op')
metrics = {"accuracy": accuracy}
tf.summary.scalar('accuracy', accuracy[1])
#tf.summary.scalar('myloss', loss)
#train_hook_list= []
#train_tensors_log = {'accuracy': accuracy[1], 'loss': loss, 'global_step': tf.train.get_global_step()}
#train_hook_list.append(tf.train.LoggingTensorHook(tensors=train_tensors_log, every_n_iter=5))
# Wrap all of this in an EstimatorSpec.
spec = tf.estimator.EstimatorSpec(mode=mode,loss=loss,train_op=train_op,
eval_metric_ops=metrics)
return spec
train_input_fn = lambda: read_dataset(file_names='train.tfrecords',
mode=tf.estimator.ModeKeys.TRAIN, batch_size=32)
test_input_fn = lambda: read_dataset(file_names='test.tfrecords', mode=tf.estimator.ModeKeys.EVAL,
batch_size=32)
params = {"learning_rate": 1e-4}
config = tf.estimator.RunConfig().replace(session_config=
tf.ConfigProto(log_device_placement=True, device_count={'GPU': 0}))
config = config.replace(save_summary_steps=5)
model = tf.estimator.Estimator(model_fn=model_fn, config=config,
params=params, model_dir="checkpoints/model_1/")
model.train(input_fn=train_input_fn, steps=5000)
result = model.evaluate(input_fn=test_input_fn)
print(result)
def serving_input_receiver_fn():
inputs = {
'feature': tf.placeholder(tf.float32, [None,240, 320, 3])}
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
model.export_savedmodel('models/model_1/',
serving_input_receiver_fn=serving_input_receiver_fn)