Repository navigation
Expand file tree
/
Copy pathtestRT.py
More file actions
265 lines (244 loc) · 9.19 KB
/
Copy pathtestRT.py
File metadata and controls
265 lines (244 loc) · 9.19 KB
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
from __future__ import print_function
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import pycuda.driver as cuda
import pycuda.autoinit
import numpy as np
from random import randint # generate a random test case
from PIL import Image
import time #import system tools
import os
import uff
import tensorrt as trt
from tensorrt.parsers import uffparser
trt.utils.get_uff_version()
parser = uffparser.create_uff_parser()
def get_uff_required_version(parser):
return str(parser.get_uff_required_version_major()) + '.' + str(parser.get_uff_required_version_minor()) + '.' + str(parser.get_uff_required_version_patch())
if trt.utils.get_uff_version() != get_uff_required_version(parser):
raise ImportError("""ERROR: UFF TRT Required version mismatch""")
#
STARTER_LEARNING_RATE = 1e-4
BATCH_SIZE = 10
NUM_CLASSES = 10
MAX_STEPS = 1000
IMAGE_SIZE = 28
IMAGE_PIXELS = IMAGE_SIZE ** 2
OUTPUT_NAMES = ["fc2/Relu"]
#
def WeightsVariable(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.1, name='weights'))
def BiasVariable(shape):
return tf.Variable(tf.constant(0.1, shape=shape, name='biases'))
def Conv2d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
filter_size = W.get_shape().as_list()
pad_size = filter_size[0]//2
pad_mat = np.array([[0,0],
[pad_size,pad_size],
[pad_size,pad_size],
[0,0]])
x = tf.pad(x, pad_mat)
x = tf.nn.conv2d(x,
W,
strides=[1, strides, strides, 1],
padding='VALID')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def MaxPool2x2(x, k=2):
# MaxPool2D wrapper
pad_size = k//2
pad_mat = np.array([[0,0],
[pad_size,pad_size],
[pad_size,pad_size],
[0,0]])
return tf.nn.max_pool(x,
ksize=[1, k, k, 1],
strides=[1, k, k, 1],
padding='VALID')
#
def network(images):
# Convolution 1
with tf.name_scope('conv1'):
weights = WeightsVariable([5,5,1,32])
biases = BiasVariable([32])
conv1 = tf.nn.relu(Conv2d(images, weights, biases))
pool1 = MaxPool2x2(conv1)
# Convolution 2
with tf.name_scope('conv2'):
weights = WeightsVariable([5,5,32,64])
biases = BiasVariable([64])
conv2 = tf.nn.relu(Conv2d(pool1, weights, biases))
pool2 = MaxPool2x2(conv2)
pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])
# Fully Connected 1
with tf.name_scope('fc1'):
weights = WeightsVariable([7 * 7 * 64, 1024])
biases = BiasVariable([1024])
fc1 = tf.nn.relu(tf.matmul(pool2_flat, weights) + biases)
# Fully Connected 2
with tf.name_scope('fc2'):
weights = WeightsVariable([1024, 10])
biases = BiasVariable([10])
fc2 = tf.reshape(tf.matmul(fc1,weights) + biases, shape= [-1,10] , name='Relu')
return fc2
def loss_metrics(logits, labels):
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=labels,
logits=logits,
name='softmax')
return tf.reduce_mean(cross_entropy, name='softmax_mean')
def training(loss):
tf.summary.scalar('loss', loss)
global_step = tf.Variable(0, name='global_step', trainable=False)
learning_rate = tf.train.exponential_decay(STARTER_LEARNING_RATE,
global_step,
100000,
0.75,
staircase=True)
tf.summary.scalar('learning_rate', learning_rate)
optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
def evaluation(logits, labels):
correct = tf.nn.in_top_k(logits, labels, 1)
return tf.reduce_sum(tf.cast(correct, tf.int32))
def do_eval(sess,
eval_correct,
images_placeholder,
labels_placeholder,
data_set,
summary):
true_count = 0
steps_per_epoch = data_set.num_examples // BATCH_SIZE
num_examples = steps_per_epoch * BATCH_SIZE
for step in range(steps_per_epoch):
feed_dict = fill_feed_dict(data_set,
images_placeholder,
labels_placeholder)
log, correctness = sess.run([summary, eval_correct],
feed_dict=feed_dict)
true_count += correctness
precision = float(true_count) / num_examples
tf.summary.scalar('precision', tf.constant(precision))
print('Num examples %d, Num Correct: %d Precision @ 1: %0.04f' % (num_examples, true_count, precision))
return log
def placeholder_inputs(batch_size):
images_placeholder = tf.placeholder(tf.float32,
shape=(None, 28, 28, 1))
labels_placeholder = tf.placeholder(tf.int32, shape=(None))
return images_placeholder, labels_placeholder
def fill_feed_dict(data_set, images_pl, labels_pl):
images_feed, labels_feed = data_set.next_batch(BATCH_SIZE)
feed_dict = {
images_pl: np.reshape(images_feed, (-1,28,28,1)),
labels_pl: labels_feed,
}
return feed_dict
##
def run_training(data_sets):
with tf.Graph().as_default():
#with tf.device("/gpu:0"):
images_placeholder, labels_placeholder = placeholder_inputs(BATCH_SIZE)
logits = network(images_placeholder)
loss = loss_metrics(logits, labels_placeholder)
train_op = training(loss)
eval_correct = evaluation(logits, labels_placeholder)
summary = tf.summary.merge_all()
init = tf.global_variables_initializer()
saver = tf.train.Saver()
#gpu_optionsgpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
#sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
sess = tf.Session()
summary_writer = tf.summary.FileWriter(
"/tmp/tensorflow/mnist/log",
graph=tf.get_default_graph())
test_writer = tf.summary.FileWriter(
"/tmp/tensorflow/mnist/log/validation",
graph=tf.get_default_graph())
sess.run(init)
for step in range(MAX_STEPS):
start_time = time.time()
feed_dict = fill_feed_dict(data_sets.train,
images_placeholder,
labels_placeholder)
_, loss_value = sess.run([train_op, loss],
feed_dict=feed_dict)
duration = time.time() - start_time
if step % 100 == 0:
print('Step %d: loss = %.2f (%.3f sec)' %
(step, loss_value, duration))
summary_str = sess.run(summary, feed_dict=feed_dict)
summary_writer.add_summary(summary_str, step)
summary_writer.flush()
if (step + 1) % 1000 == 0 or (step + 1) == MAX_STEPS:
saver.save(sess, "./tmp/model.ckpt", global_step=step)
print('Validation Data Eval:')
log = do_eval(sess,
eval_correct,
images_placeholder,
labels_placeholder,
data_sets.validation,
summary)
test_writer.add_summary(log, step)
graphdef = tf.get_default_graph().as_graph_def()
frozen_graph = tf.graph_util.convert_variables_to_constants(sess, graphdef, OUTPUT_NAMES)
return tf.graph_util.remove_training_nodes(frozen_graph)
#
MNIST_DATASETS = input_data.read_data_sets(
'/tmp/tensorflow/mnist/input_data')
tf_model = run_training(MNIST_DATASETS)
tf.train.write_graph(tf_model, './', 'model.pb', as_text=False)
#
print(type(tf_model))
uff_model = uff.from_tensorflow(tf_model, ["fc2/Relu"])
print(type(uff_model))
#
G_LOGGER = trt.infer.ConsoleLogger(trt.infer.LogSeverity.ERROR)
parser = uffparser.create_uff_parser()
parser.register_input("Placeholder", (1,28,28),0)
parser.register_output("fc2/Relu")
engine = trt.utils.uff_to_trt_engine(G_LOGGER,
uff_model,
parser,
1,
1 << 20)
#host_mem = parser.hidden_plugin_memory()
parser.destroy()
# input data
img, label = MNIST_DATASETS.test.next_batch(1)
img = img[0]
#convert input data to Float32
img = img.astype(np.float32)
label = label[0]
# runtime context
runtime = trt.infer.create_infer_runtime(G_LOGGER)
context = engine.create_execution_context()
#
output = np.empty(10, dtype = np.float32)
#alocate device memory
d_input = cuda.mem_alloc(1 * img.size * img.dtype.itemsize)
d_output = cuda.mem_alloc(1 * output.size * output.dtype.itemsize)
#
bindings = [int(d_input), int(d_output)]
stream = cuda.Stream()
#transfer input data to device
cuda.memcpy_htod_async(d_input, img, stream)
#execute model
context.enqueue(1, bindings, stream.handle, None)
#transfer predictions back
cuda.memcpy_dtoh_async(output, d_output, stream)
#syncronize threads
stream.synchronize()
#
print("Test Case: " + str(label))
print ("Prediction: " + str(np.argmax(output)))
#
trt.utils.write_engine_to_file("./tf_mnist.engine", engine.serialize())
#
new_engine = trt.utils.load_engine(G_LOGGER, "./tf_mnist.engine")
#
context.destroy()
engine.destroy()
new_engine.destroy()
runtime.destroy()