Assume that you have a function get_model()
that builds a your exact same model you have trained and a path weights_path
pointing to your HDF5 file containing your model weights:
model = get_model()
model.load_weights(weights_path)
This should load your model properly. Then you just have to define a ImageDataGenerator
of your test data and fit the model to obtain predictions:
# Path to your folder testing data
testing_folder = ""
# Image size (set up the image size used for training)
img_size = 256
# Batch size (you should tune it based on your memory)
batch_size = 16
val_datagen = ImageDataGenerator(
rescale=1. / 255)
validation_generator = val_datagen.flow_from_directory(
testing_folder,
target_size=(img_size, img_size),
batch_size=batch_size,
shuffle=False,
class_mode='categorical')
Then you can make the model generate all predictions over your entire dataset using the model.predict_generator()
method:
# Number of steps corresponding to an epoch
steps = 100
predictions = model.predict_generator(validation_generator, steps=steps)
And finally create a confussion matrix using the metrics.confusion_matrix()
method from sklearn
package:
val_preds = np.argmax(predictions, axis=-1)
val_trues = validation_generator.classes
cm = metrics.confusion_matrix(val_trues, val_preds)
Or get all precisions, recalls and f1-scores for all classes using metrics.precision_recall_fscore_support()
method from sklearn
(argument average=None
outputs metrics for all classes):
# label names
labels = validation_generator.class_indices.keys()
precisions, recall, f1_score, _ = metrics.precision_recall_fscore_support(val_trues, val_preds, labels=labels)
I haven't tested it, but I guess this will help you.