Tengerye
Tengerye

Reputation: 1964

tensorflow evaluate with confusion matrix

In the tensorflow CNN tutorial, it computes the accuracy, but I want to leverage from that to the confusion matrix.

Immediately, three different approaches hit on my mind:

  1. I tried to directly compute the prediction result instead of top_k_op in tensorflow, then I could utilize sklearn. But I failed, because the it used multiple threads to compute(line 88);

  2. I tried to load the trained Variables and give new placeholder to cifar10.inference, but failed again, because it defined batch_image as input(line 225);

  3. The last approach is to defined a new operation to replace the line 128

    top_k_op = tf.nn.in_top_k(logits, labels, 1)
    

    but I could not find a proper operations could do that.

This has afflicted me for several days. Please help. Thank you in advance.

Upvotes: 1

Views: 2781

Answers (1)

ilblackdragon
ilblackdragon

Reputation: 1835

You can utilize sklearn's confusion_matrix only after running 'inference' on all the dataset. Meaning, if you are modifying eval_only function, you should just accumulate all the scores into some thread-safe container (list). And then after all threads are stopped (line 113) you can run single confusion matrix computation.

Additionally, if you want to do it in the graph, TensorFlow recently got confusion_matrix op you can try using. That said, it only works on the batch so you will need to increase your batch to get any kind of resolution or write a custom aggregator.

Upvotes: 3

Related Questions