I trained a convolutional neural network with a training_batch_size
of 17000 images and a validation_batch_size
of 3400 images. My dataset (images) are divided in 6 classes :
Once i’ve trained the model, I had something looking like this
Then, I used my model to verify if the accuracy is consistent on 6000 new testing images, 1000 images per classes.
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import numpy as np
import tensorflow.compat.v1 as tf
from tensorflow.compat.v1.keras import backend as K
from sklearn.metrics import confusion_matrix, roc_curve , auc
from keras.models import load_model
from keras import Model
# ==========================================
# ===============GPU SETUP==================
# ==========================================
config = tf.ConfigProto(device_count={'GPU': 2, 'CPU': 4})
sess = tf.Session(config=config)
K.set_session(sess)
# ==========================================
# ==================MODeLE==================
# ==========================================
model_path = "Model.hdf5"
Classifier: Model = load_model(model_path)
# ==========================================
# ================VARIABLES=================
# ==========================================
mainDataPath = "donnees/"
testPath = mainDataPath + "test"
number_images = 6000
number_images_class_0 = 1000
number_images_class_1 = 1000
number_images_class_2 = 1000
number_images_class_3 = 1000
number_images_class_4 = 1000
number_images_class_5 = 1000
image_scale = 200
images_color_mode = "rgb" # grayscale or rgb
test_data_generator = ImageDataGenerator(rescale=1. / 255)
test_itr = test_data_generator.flow_from_directory(
testPath,# place des images
target_size=(image_scale, image_scale), # taille des images
class_mode="categorical",# Type de classification
shuffle=False,# pas besoin de les boulverser
batch_size=1,# on classe les images une e la fois
color_mode=images_color_mode)# couleur des images
(x, y_true) = test_itr.next()
# Normalize Data
max_value = float(x.max())
x = x.astype('float32') / max_value
# ==========================================
# ===============eVALUATION=================
# ==========================================
# Les classes correctes des images (1000 pour chaque classe) -- the ground truth
y_true = np.array([0] * number_images_class_0+
[1] * number_images_class_1+
[2] * number_images_class_2+
[3] * number_images_class_3+
[4] * number_images_class_4+
[5] * number_images_class_5)
test_eval = Classifier.evaluate_generator(test_itr, verbose=1)
print('>Test loss (Erreur):', test_eval[0])
print('>Test precision:', test_eval[1])
predicted_classes = Classifier.predict_generator(test_itr, verbose=1)
predicted_classes_perc = np.round(predicted_classes.copy(), 4)
predicted_classes = np.round(predicted_classes) # on arrondie le output
My first question is : am i doing the test evaluation right?
If so, how can i
- Print the number of images correctly and incorrectly classed?
- Print the heat map of the classification
- Extract a misclassified image for each species combination
See examples of output below…