Skip to content

Commit

Permalink
Change loading function
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele-falzone committed May 22, 2018
1 parent c14157f commit 05716f0
Show file tree
Hide file tree
Showing 9 changed files with 48,391 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ MNIST-data-png

# logdir
logdir

# cifar10 data
cifar-10-batches-py
15 changes: 9 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ done
echo "\n--------------- Freezing graph -----------------------------\n";
freeze_graph --input_saved_model_dir=${m} --output_graph=${m}/frozen.pb --output_node_names=final_result;

echo "\n--------------- Optimizing graph ---------------------------\n";
python -m tensorflow.python.tools.optimize_for_inference --input=${m}/frozen.pb --output=${m}/optimized.pb --input_names="Placeholder" --output_names="final_result"

echo "\n--------------- Adding graph to Tensorboard ----------------\n"
python -m scripts.graph_pb2tb ${m} ${m}/frozen.pb;
python -m scripts.graph_pb2tb ${m} ${m}/optimized.pb;

echo "\n--------------- Generating quantized graphs ----------------\n"
#python -m scripts.quantize_graph --input=${m}/frozen.pb --output=${m}/round.pb --output_node_names=final_result --mode=round;
#python -m scripts.quantize_graph --input=${m}/optimized.pb --output=${m}/round.pb --output_node_names=final_result --mode=round;

#python -m scripts.quantize_graph --input=${m}/frozen.pb --output=${m}/quantize.pb --output_node_names=final_result --mode=quantize;
#python -m scripts.quantize_graph --input=${m}/optimized.pb --output=${m}/quantize.pb --output_node_names=final_result --mode=quantize;

python -m scripts.quantize_graph --input=${m}/frozen.pb --output=${m}/eightbit.pb --output_node_names=final_result --mode=eightbit;
python -m scripts.quantize_graph --input=${m}/optimized.pb --output=${m}/eightbit.pb --output_node_names=final_result --mode=eightbit;

python -m scripts.quantize_graph --input=${m}/frozen.pb --output=${m}/weights.pb --output_node_names=final_result --mode=weights;
python -m scripts.quantize_graph --input=${m}/optimized.pb --output=${m}/weights.pb --output_node_names=final_result --mode=weights;

python -m scripts.quantize_graph --input=${m}/frozen.pb --output=${m}/weights_rounded.pb --output_node_names=final_result --mode=weights_rounded;
python -m scripts.quantize_graph --input=${m}/optimized.pb --output=${m}/weights_rounded.pb --output_node_names=final_result --mode=weights_rounded;

echo "\n--------------- DONE ---------------------------------------\n"
echo "Directory path: ${m}"
1,190 changes: 1,190 additions & 0 deletions cnn-cifar10-small.ipynb

Large diffs are not rendered by default.

41,989 changes: 41,989 additions & 0 deletions cnn-mnist-small.ipynb

Large diffs are not rendered by default.

5,010 changes: 4,993 additions & 17 deletions cnn-mnist.ipynb

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions evaluate.py
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()

12 changes: 7 additions & 5 deletions inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ def predict(graph, image):
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()
for i in range(0,10):
start = time.time()
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: image})
end=time.time()
print('{:.9f}s '.format(end-start))
results = np.squeeze(results)
return results, end-start

Expand All @@ -52,7 +54,7 @@ def predict(graph, image):

results, prediction_time = predict(graph, image)

print('Evaluation time: {:.9f}s\n'.format(prediction_time))
#print('Evaluation time: {:.9f}s\n'.format(prediction_time))

top_k = results.argsort()[-5:][::-1]

Expand Down
2 changes: 1 addition & 1 deletion performance.sh
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 "$@"
136 changes: 136 additions & 0 deletions prova.py
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])

0 comments on commit 05716f0

Please sign in to comment.