From f4982e364c7a4c0cdca6c948666ac1ff0e821632 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Mon, 5 Aug 2019 13:44:34 +0200 Subject: [PATCH] Made iou threshold actually count Signed-off-by: Jim Martens --- src/twomartens/masterthesis/cli.py | 17 +++++++++-------- src/twomartens/masterthesis/ssd.py | 23 ++++++++++++++++++----- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/twomartens/masterthesis/cli.py b/src/twomartens/masterthesis/cli.py index daa11df..e2330c6 100644 --- a/src/twomartens/masterthesis/cli.py +++ b/src/twomartens/masterthesis/cli.py @@ -171,7 +171,7 @@ def _ssd_train(args: argparse.Namespace) -> None: _init_eager_mode() batch_size, image_size, learning_rate, steps_per_val_epoch, nr_classes, \ - iou_threshold, dropout_rate, top_k, nr_trajectories, \ + dropout_rate, top_k, nr_trajectories, \ coco_path, summary_path, weights_path, train_gt_path, val_gt_path, \ save_train_images, save_summaries = _ssd_train_get_config_values(conf.get_property) @@ -188,7 +188,6 @@ def _ssd_train(args: argparse.Namespace) -> None: image_size, nr_classes, "training", - iou_threshold, dropout_rate, top_k, pre_trained_weights_file) @@ -241,7 +240,8 @@ def _ssd_test(args: argparse.Namespace) -> None: _init_eager_mode() batch_size, image_size, learning_rate, \ - forward_passes_per_image, nr_classes, iou_threshold, dropout_rate, \ + forward_passes_per_image, nr_classes, confidence_threshold, iou_threshold, \ + dropout_rate, \ use_entropy_threshold, entropy_threshold_min, entropy_threshold_max, \ use_coco, \ top_k, nr_trajectories, test_pretrained, \ @@ -260,7 +260,6 @@ def _ssd_test(args: argparse.Namespace) -> None: image_size, nr_classes, "training", - iou_threshold, dropout_rate, top_k, weights_file) @@ -291,6 +290,8 @@ def _ssd_test(args: argparse.Namespace) -> None: use_entropy_threshold, entropy_threshold_min, entropy_threshold_max, + confidence_threshold, + iou_threshold, output_path, coco_path, use_dropout, @@ -532,7 +533,7 @@ def _ssd_evaluate_unbatch_list(glob_string: str) -> List[np.ndarray]: def _ssd_train_get_config_values(config_get: Callable[[str], Union[str, float, int, bool]] - ) -> Tuple[int, int, float, int, int, float, float, int, int, + ) -> Tuple[int, int, float, int, int, float, int, int, str, str, str, str, str, bool, bool]: @@ -541,7 +542,6 @@ def _ssd_train_get_config_values(config_get: Callable[[str], Union[str, float, i learning_rate = config_get("Parameters.learning_rate") steps_per_val_epoch = config_get("Parameters.steps_per_val_epoch") nr_classes = config_get("Parameters.nr_classes") - iou_threshold = config_get("Parameters.ssd_iou_threshold") dropout_rate = config_get("Parameters.ssd_dropout_rate") top_k = config_get("Parameters.ssd_top_k") nr_trajectories = config_get("Parameters.nr_trajectories") @@ -561,7 +561,6 @@ def _ssd_train_get_config_values(config_get: Callable[[str], Union[str, float, i learning_rate, steps_per_val_epoch, nr_classes, - iou_threshold, dropout_rate, top_k, nr_trajectories, @@ -579,7 +578,7 @@ def _ssd_train_get_config_values(config_get: Callable[[str], Union[str, float, i def _ssd_test_get_config_values(args: argparse.Namespace, config_get: Callable[[str], Union[str, float, int, bool]] - ) -> Tuple[int, int, float, int, int, float, float, + ) -> Tuple[int, int, float, int, int, float, float, float, bool, float, float, bool, int, int, bool, @@ -590,6 +589,7 @@ def _ssd_test_get_config_values(args: argparse.Namespace, learning_rate = config_get("Parameters.learning_rate") forward_passes_per_image = config_get("Parameters.ssd_forward_passes_per_image") nr_classes = config_get("Parameters.nr_classes") + confidence_threshold = config_get("Parameters.ssd_confidence_threshold") iou_threshold = config_get("Parameters.ssd_iou_threshold") dropout_rate = config_get("Parameters.ssd_dropout_rate") use_entropy_threshold = config_get("Parameters.ssd_use_entropy_threshold") @@ -614,6 +614,7 @@ def _ssd_test_get_config_values(args: argparse.Namespace, learning_rate, forward_passes_per_image, nr_classes, + confidence_threshold, iou_threshold, dropout_rate, # diff --git a/src/twomartens/masterthesis/ssd.py b/src/twomartens/masterthesis/ssd.py index 08a3ea2..97e47a4 100644 --- a/src/twomartens/masterthesis/ssd.py +++ b/src/twomartens/masterthesis/ssd.py @@ -51,8 +51,9 @@ tfe = tf.contrib.eager def get_model(use_dropout: bool, dropout_model: callable, vanilla_model: callable, image_size: int, nr_classes: int, mode: str, - iou_threshold: float, dropout_rate: float, top_k: int, - pre_trained_weights_file: Optional[str] = None) -> Tuple[tf.keras.models.Model, np.ndarray]: + dropout_rate: float, top_k: int, + pre_trained_weights_file: Optional[str] = None, + iou_threshold: Optional[float] = None) -> Tuple[tf.keras.models.Model, np.ndarray]: """ Returns the correct SSD model and the corresponding predictor sizes. @@ -63,10 +64,11 @@ def get_model(use_dropout: bool, image_size: size of the resized images nr_classes: number of classes mode: one of "training", "inference", "inference_fast" - iou_threshold: all boxes with higher iou to local maximum box are suppressed dropout_rate: rate for dropout layers (only applies if dropout is used) top_k: number of highest scoring predictions kept for each batch item pre_trained_weights_file: path to h5 file with pre-trained weights + iou_threshold: all boxes with higher iou to local maximum box are suppressed + (only relevant for inference modes) Returns: SSD model, predictor_sizes @@ -151,6 +153,8 @@ def predict(generator: callable, use_entropy_threshold: bool, entropy_threshold_min: float, entropy_threshold_max: float, + confidence_threshold: float, + iou_threshold: float, output_path: str, coco_path: str, use_dropout: bool, @@ -171,6 +175,9 @@ def predict(generator: callable, use_entropy_threshold: if True entropy thresholding is applied entropy_threshold_min: specifies the minimum threshold for the entropy entropy_threshold_max: specifies the maximum threshold for the entropy + confidence_threshold: minimum confidence required for box to count as positive + iou_threshold: all boxes with iou overlap larger than threshold to local maximum box + will be suppressed output_path: the path in which the results should be saved coco_path: the path to the COCO data set use_dropout: if True, multiple forward passes and observations will be used @@ -195,6 +202,8 @@ def predict(generator: callable, _decode_predictions, decode_func=ssd_output_decoder.decode_detections_fast, image_size=image_size, + confidence_threshold=confidence_threshold, + iou_threshold=iou_threshold ), transform_func=functools.partial( _transform_predictions, @@ -347,13 +356,17 @@ def _predict_vanilla_step(inputs: np.ndarray, model: tf.keras.models.Model) -> n def _decode_predictions(predictions: np.ndarray, decode_func: callable, image_size: int, - entropy_threshold: float) -> np.ndarray: + entropy_threshold: float, + confidence_threshold: float, + iou_thresholdd: float) -> np.ndarray: return decode_func( y_pred=predictions, img_width=image_size, img_height=image_size, input_coords="corners", - entropy_thresh=entropy_threshold + entropy_thresh=entropy_threshold, + confidence_thresh=confidence_threshold, + iou_threshold=iou_thresholdd )