Skip to content

Commit

Permalink
Refactor to move accuracy metrics into model
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrplz committed Oct 14, 2017
1 parent d8f32f8 commit 0cf4a4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 2 additions & 7 deletions capstone_traffic_light_classifier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
# Define model
classifier = TrafficLightClassifier(x, targets, p, n_classes, learning_rate=1e-4)

# Define metrics
correct_predictions = tf.equal(tf.argmax(classifier.inference, axis=1),
tf.argmax(tf.one_hot(targets, depth=n_classes), axis=1))
accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))

with tf.Session() as sess:

# Initialize all variables
Expand Down Expand Up @@ -60,8 +55,8 @@
num_test_batches = 500
for _ in range(num_test_batches):
x_batch, y_batch = dataset.load_batch(batch_size)
average_test_accuracy += sess.run(fetches=accuracy,
average_test_accuracy += sess.run(fetches=classifier.accuracy,
feed_dict={x: x_batch, targets: y_batch, p: 1.})
average_test_accuracy /= num_test_batches
print('TEST accuracy: {:.03f}'.format(average_test_accuracy))
print('Training accuracy: {:.03f}'.format(average_test_accuracy))
print('*' * 50)
11 changes: 11 additions & 0 deletions capstone_traffic_light_classifier/traffic_light_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def __init__(self, x, targets, keep_prob, n_classes, learning_rate):
self._inference = None
self._loss = None
self._train_step = None
self._accuracy = None
self._summaries = None

self.inference
self.loss
self.train_step
self.accuracy
# self.summaries # todo add these

@property
Expand Down Expand Up @@ -68,3 +70,12 @@ def train_step(self):
with tf.variable_scope('training'):
self._train_step = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(self.loss)
return self._train_step

@property
def accuracy(self):
if self._accuracy is None:
with tf.variable_scope('accuracy'):
correct_predictions = tf.equal(tf.argmax(self.inference, axis=1),
tf.argmax(tf.one_hot(self.targets, depth=self.n_classes), axis=1))
self._accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
return self._accuracy

0 comments on commit 0cf4a4b

Please sign in to comment.