evaluate topk as correct when classes less than or equal to k
@@ -181,22 +181,29 @@ class Base(BaseNetwork):
[tf.shape(softmax), tf.argmax(softmax, 1)], message="softmax:", summarize=200)
argmax_labels = tf.cast(tf.argmax(labels, 1), tf.int32)
- top_5, _ = tf.nn.top_k(softmax, k=5)
- current_accuracy = tf.cast(tf.nn.in_top_k(softmax, argmax_labels, k=1), tf.float32)
- count_border_values = tf.reduce_sum(tf.cast(tf.equal(softmax,
- tf.expand_dims(top_5[:, 0], 1)), tf.float32), axis=-1)
- accuracy, accuracy_update = tf.metrics.mean(tf.div(current_accuracy, count_border_values))
+ def calc_top_k(softmax, labels, k):
- current_accuracy = tf.cast(tf.nn.in_top_k(softmax, argmax_labels, k=3), tf.float32)
+ truth_top_k, _ = tf.nn.top_k(softmax, k)
+ inclusive_accuracy = tf.cast(tf.nn.in_top_k(softmax, argmax_labels, k), tf.float32)
count_border_values = tf.reduce_sum(tf.cast(tf.equal(softmax,
- tf.expand_dims(top_5[:, 2], 1)), tf.float32), axis=-1)
- accuracy_top3, accuracy_top3_update = tf.metrics.mean(tf.div(current_accuracy, count_border_values))
+ tf.expand_dims(truth_top_k[:, k-1], 1)), tf.float32), axis=-1)
+ return tf.div(inclusive_accuracy, count_border_values)
- current_accuracy = tf.cast(tf.nn.in_top_k(softmax, argmax_labels, k=5), tf.float32)
- count_border_values = tf.reduce_sum(tf.cast(tf.equal(softmax,
- tf.expand_dims(top_5[:, 4], 1)), tf.float32), axis=-1)
- accuracy_top5, accuracy_top5_update = tf.metrics.mean(tf.div(current_accuracy, count_border_values))
+ total_classes = labels.get_shape().as_list()[1]
+ batch_size = labels.get_shape().as_list()[0]
+
+ accuracy, accuracy_update = tf.metrics.mean(calc_top_k(softmax, labels, 1))
+
+ if(total_classes > 3):
+ accuracy_top3, accuracy_top3_update = tf.metrics.mean(calc_top_k(softmax, labels, 3))
+ else:
+ accuracy_top3, accuracy_top3_update = tf.metrics.mean(tf.ones(batch_size))
+
+ if(total_classes > 5):
+ accuracy_top5, accuracy_top5_update = tf.metrics.mean(calc_top_k(softmax, labels, 5))
+ else:
+ accuracy_top5, accuracy_top5_update = tf.metrics.mean(tf.ones(batch_size))
updates = tf.group(accuracy_update, accuracy_top3_update, accuracy_top5_update)
