2
$\begingroup$

I'm not using ImageDataGenerator because i'm using hdf5 files. I used DataGenerator class (1) to feed data to the model.fit_generator. How can I get the confusion matrix given that I have batches of random data? I know how to get it with model.predict using sklearn.

(1) https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly#

$\endgroup$
0
$\begingroup$
loaded_model.predict_generator(generator=test_generator)

will give us a set of probabilities

y_true = test_generator.classes

will give us true labels

Because this is a binary classification problem, you have to find predicted labels. To do that you can use:

y_pred = probabilities > 0.5

Then we have true labels and predicted labels on the test dataset. So, the confusion matrix is given by:

font = {
'family': 'Times New Roman',
'size': 12
}
matplotlib.rc('font', **font)
mat = confusion_matrix(y_true, y_pred)
plot_confusion_matrix(conf_mat=mat, figsize=(8, 8), show_normed=False)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.