-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c14157f
commit 05716f0
Showing
9 changed files
with
48,391 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ MNIST-data-png | |
|
||
# logdir | ||
logdir | ||
|
||
# cifar10 data | ||
cifar-10-batches-py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import tensorflow as tf | ||
import numpy as np | ||
import time | ||
import argparse | ||
|
||
def load_graph(model_file): | ||
graph = tf.Graph() | ||
graph_def = tf.GraphDef() | ||
|
||
with open(model_file, "rb") as f: | ||
graph_def.ParseFromString(f.read()) | ||
with graph.as_default(): | ||
tf.import_graph_def(graph_def) | ||
|
||
return graph | ||
|
||
def predict(graph, image): | ||
input_operation = graph.get_operation_by_name("import/Placeholder"); | ||
output_operation = graph.get_operation_by_name("import/final_result"); | ||
|
||
with tf.Session(graph=graph) as sess: | ||
start = time.time() | ||
results = sess.run(output_operation.outputs[0], | ||
{input_operation.outputs[0]: image}) | ||
end=time.time() | ||
results = np.squeeze(results) | ||
return results, end-start | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--times", help="number of images to be processed") | ||
parser.add_argument("--graph", help="graph/model to be executed") | ||
args = parser.parse_args() | ||
|
||
mnist = tf.contrib.learn.datasets.load_dataset("mnist") | ||
eval_data = mnist.test.images # Returns np.array | ||
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32) | ||
|
||
images = len(eval_labels) | ||
|
||
if args.times: | ||
images = int(args.times) | ||
|
||
if args.graph: | ||
print('Evaluating model with %d images' % (images)) | ||
|
||
graph = load_graph(args.graph) | ||
|
||
accuracies = [] | ||
times = [] | ||
|
||
for i in range(0, images): | ||
image = eval_data[i].reshape(1,28,28,1) | ||
results, prediction_time = predict(graph, image) | ||
predicted = results.argsort()[::-1][0] | ||
accuracies.append(float(predicted==eval_labels[i])) | ||
times.append(prediction_time) | ||
|
||
print(' Accuracy: {:.9f}'.format(np.mean(accuracies))) | ||
print('Average prediction time: {:.9f}s'.format(np.mean(times))) | ||
else: | ||
parser.print_help() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
|
||
perf stat -r 10 -e cache-references,cache-misses,branches,branch-misses python inference.py "$@" | ||
perf stat -e cache-references,cache-misses,branches,branch-misses python evaluate.py "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import os | ||
import pickle | ||
import numpy as np | ||
import tensorflow as tf | ||
from tensorflow.python.estimator.export.export_output import PredictOutput | ||
|
||
tf.logging.set_verbosity(tf.logging.INFO) | ||
|
||
def unpickle(file): | ||
import cPickle | ||
with open(file, 'rb') as fo: | ||
dict = cPickle.load(fo) | ||
return dict | ||
|
||
train_data = np.empty([0,32,32,3]) | ||
train_labels = np.empty([0]) | ||
|
||
for i in range(1,6): | ||
path = os.path.join(os.getcwd(), 'cifar-10-batches-py/data_batch_' + str(i)) | ||
batch = unpickle(path) | ||
images = batch['data'].reshape(10000,3,32,32) | ||
train_data = np.append(train_data, np.transpose(images, (0, 2, 3, 1)), axis=0) | ||
labels = np.asarray(batch['labels']) | ||
train_labels = np.append(train_labels, labels) | ||
|
||
path = os.path.join(os.getcwd(), 'cifar-10-batches-py/test_batch') | ||
batch = unpickle(path) | ||
images = batch['data'].reshape(10000,3,32,32) | ||
test_data = np.transpose(images, (0, 2, 3, 1)) | ||
test_labels = np.asarray(batch['labels']).astype(dtype=np.int32) | ||
train_labels = train_labels.astype(dtype=np.int32) | ||
|
||
train_data= np.true_divide(train_data,256).astype(dtype=np.float32) | ||
test_data= np.true_divide(test_data,256).astype(dtype=np.float32) | ||
|
||
print('train_data shape: ' + str(train_data.shape) + str(train_data.dtype)) | ||
print('train_labels shape: ' + str(train_labels.shape) + str(train_labels.dtype)) | ||
|
||
print('test_data shape: ' + str(test_data.shape) + str(test_data.dtype)) | ||
print('test_labels shape: ' + str(test_labels.shape) + str(test_labels.dtype)) | ||
|
||
|
||
|
||
def cnn_model_fn(features, labels, mode): | ||
"""Model function for CNN.""" | ||
|
||
# Convolutional Layer #1 | ||
conv1 = tf.layers.conv2d( | ||
inputs=features["x"], | ||
filters=32, | ||
kernel_size=5, | ||
padding="same", | ||
activation=tf.nn.relu) | ||
|
||
# Pooling Layer #1 | ||
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) | ||
|
||
# Convolutional Layer #2 | ||
conv2 = tf.layers.conv2d( | ||
inputs=pool1, | ||
filters=64, | ||
kernel_size=5, | ||
padding="same", | ||
activation=tf.nn.relu) | ||
|
||
# Pooling Layer #2 | ||
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) | ||
|
||
# Dense Layer | ||
pool2_flat = tf.reshape(pool2, [-1, 8 * 8 * 64]) | ||
dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu) | ||
dropout = tf.layers.dropout( | ||
inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN) | ||
|
||
# Logits Layer | ||
logits = tf.layers.dense(inputs=dropout, units=10) | ||
|
||
predictions = { | ||
# Generate predictions (for PREDICT and EVAL mode) | ||
"classes": tf.argmax(input=logits, axis=1), | ||
# Add `softmax_tensor` to the graph. It is used for PREDICT and by the | ||
# `logging_hook`. | ||
"probabilities": tf.nn.softmax(logits, name="softmax_tensor") | ||
} | ||
|
||
export_outputs={"SIGNATURE_NAME": PredictOutput({ | ||
# Generate predictions (for PREDICT and EVAL mode) | ||
"classes": tf.argmax(input=logits, axis=1), | ||
# Add `softmax_tensor` to the graph. It is used for PREDICT and by the | ||
# `logging_hook`. | ||
"probabilities": tf.nn.softmax(logits, name="final_result") | ||
})} | ||
|
||
if mode == tf.estimator.ModeKeys.PREDICT: | ||
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions,export_outputs=export_outputs) | ||
|
||
# Calculate Loss (for both TRAIN and EVAL modes) | ||
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) | ||
|
||
# Configure the Training Op (for TRAIN mode) | ||
if mode == tf.estimator.ModeKeys.TRAIN: | ||
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001) | ||
train_op = optimizer.minimize( | ||
loss=loss, | ||
global_step=tf.train.get_global_step()) | ||
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op) | ||
|
||
# Add evaluation metrics (for EVAL mode) | ||
eval_metric_ops = { | ||
"accuracy": tf.metrics.accuracy( | ||
labels=labels, predictions=predictions["classes"])} | ||
return tf.estimator.EstimatorSpec( | ||
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops) | ||
|
||
|
||
# Create the Estimator | ||
mnist_classifier = tf.estimator.Estimator( | ||
model_fn=cnn_model_fn, model_dir="./logdir") | ||
|
||
# Set up logging for predictions | ||
tensors_to_log = {"probabilities": "final_result"} | ||
logging_hook = tf.train.LoggingTensorHook( | ||
tensors=tensors_to_log, every_n_iter=50) | ||
|
||
# Train the model | ||
train_input_fn = tf.estimator.inputs.numpy_input_fn( | ||
x={"x": train_data}, | ||
y=train_labels, | ||
batch_size=50, | ||
num_epochs=None, | ||
shuffle=True) | ||
|
||
mnist_classifier.train( | ||
input_fn=train_input_fn, | ||
steps=20000, | ||
hooks=[logging_hook]) |