Skip to content Skip to sidebar Skip to footer

How To Load And Evaluate A CNN Using A Test Set In Tensorflow?

I'm trying to train a CNN on a set of images. There are 2 folders: training_set and test_set, each containing 2 classes. They look like this: training_set/ classA/ img1

Solution 1:

You need to iterate over the data, then you can collect predictions and true classes.

predicted_probs = np.array([])
true_classes =  np.array([])

for images, labels in test_images:
  predicted_probs = np.concatenate([predicted_probs,
                       model(images)])
  true_classes = np.concatenate([true_classes, labels.numpy()])

Since they are sigmoid outputs, you need to transform them into classes with a threshold, i.e 0.5 here:

predicted_classes = [1 * (x[0]>=0.5) for x in predicted_probs]

After that you can get the confusion matrix etc:

conf_matrix = tf.math.confusion_matrix(true_classes, predicted_classes)

Post a Comment for "How To Load And Evaluate A CNN Using A Test Set In Tensorflow?"