|
| 1 | +""" |
| 2 | +Copyright 2017-2018 Fizyr (https://fizyr.com) |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import keras |
| 18 | +from ..utils.eval import evaluate |
| 19 | + |
| 20 | + |
| 21 | +class Evaluate(keras.callbacks.Callback): |
| 22 | + def __init__(self, generator, iou_threshold=0.5, score_threshold=0.05, max_detections=100, save_path=None, tensorboard=None, verbose=1): |
| 23 | + """ Evaluate a given dataset using a given model at the end of every epoch during training. |
| 24 | +
|
| 25 | + # Arguments |
| 26 | + generator : The generator that represents the dataset to evaluate. |
| 27 | + iou_threshold : The threshold used to consider when a detection is positive or negative. |
| 28 | + score_threshold : The score confidence threshold to use for detections. |
| 29 | + max_detections : The maximum number of detections to use per image. |
| 30 | + save_path : The path to save images with visualized detections to. |
| 31 | + tensorboard : Instance of keras.callbacks.TensorBoard used to log the mAP value. |
| 32 | + verbose : Set the verbosity level, by default this is set to 1. |
| 33 | + """ |
| 34 | + self.generator = generator |
| 35 | + self.iou_threshold = iou_threshold |
| 36 | + self.score_threshold = score_threshold |
| 37 | + self.max_detections = max_detections |
| 38 | + self.save_path = save_path |
| 39 | + self.tensorboard = tensorboard |
| 40 | + self.verbose = verbose |
| 41 | + |
| 42 | + super(Evaluate, self).__init__() |
| 43 | + |
| 44 | + def on_epoch_end(self, epoch, logs={}): |
| 45 | + # run evaluation |
| 46 | + average_precisions = evaluate( |
| 47 | + self.generator, |
| 48 | + self.model, |
| 49 | + iou_threshold=self.iou_threshold, |
| 50 | + score_threshold=self.score_threshold, |
| 51 | + max_detections=self.max_detections, |
| 52 | + save_path=self.save_path |
| 53 | + ) |
| 54 | + |
| 55 | + self.mean_ap = sum(average_precisions.values()) / len(average_precisions) |
| 56 | + |
| 57 | + if self.tensorboard is not None and self.tensorboard.writer is not None: |
| 58 | + import tensorflow as tf |
| 59 | + summary = tf.Summary() |
| 60 | + summary_value = summary.value.add() |
| 61 | + summary_value.simple_value = self.mean_ap |
| 62 | + summary_value.tag = "mAP" |
| 63 | + self.tensorboard.writer.add_summary(summary, epoch) |
| 64 | + |
| 65 | + if self.verbose == 1: |
| 66 | + for label, average_precision in average_precisions.items(): |
| 67 | + print(self.generator.label_to_name(label), '{:.4f}'.format(average_precision)) |
| 68 | + print('mAP: {:.4f}'.format(self.mean_ap)) |
0 commit comments