froc.py 15.5 KB
Newer Older
mibaumgartner's avatar
mibaumgartner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
Copyright 2020 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import time
import numpy as np

from loguru import logger
from typing import Sequence, List, Dict, Optional, Union, Tuple
from pathlib import Path

import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

from nndet.evaluator import DetectionMetric
from sklearn.metrics import roc_curve
from collections import defaultdict

mibaumgartner's avatar
mibaumgartner committed
31
32
from nndet.utils.info import experimental

mibaumgartner's avatar
mibaumgartner committed
33
34
35
36
37
38
39
40
41
42
43

class FROCMetric(DetectionMetric):
    def __init__(self,
                 classes: Sequence[str],
                 iou_thresholds: Sequence[float] = (0.1, 0.5),
                 fpi_thresholds: Sequence[float] = (1/8, 1/4, 1/2, 1, 2, 4, 8),
                 per_class: bool = False, verbose: bool = True,
                 save_dir: Optional[Union[str, Path]] = None,
                 ):
        """
        Class to compute FROC
mibaumgartner's avatar
mibaumgartner committed
44
45
46
47
48
49
50
        
        Multiclass FROC: This implementation performs the FROC over all
        objects regardless of their class which assigns each object the
        same "weight".
        
        Note this implementation is experimental and might change in the
        future. Please prefer the AP metric for now.
mibaumgartner's avatar
mibaumgartner committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

        Args:
            classes: name of each class
                (index needs to correspond to predicted class indices!)
            iou_thresholds: IoU thresholds for which FROC
                is evaluated
            fpi_thresholds: false positive per image
                thresholds (curve is interpolated at these values, score is
                the mean of the computed sens values at these positions)
            per_class: additional FROC curves are computed per class
            verbose: log time needed for evaluation
        """
        self.classes = classes
        self.iou_thresholds = iou_thresholds
        self.fpi_thresholds = fpi_thresholds
        self.per_class = per_class
        self.verbose = verbose

        if save_dir is None:
            self.save_dir = save_dir
        else:
            self.save_dir = Path(save_dir)

    def get_iou_thresholds(self) -> Sequence[float]:
        """
        Return IoU thresholds needed for this metric in an numpy array

        Returns:
            Sequence[float]: IoU thresholds [M], M is the number of thresholds
        """
        return self.iou_thresholds

    def compute(self, results_list: List[Dict[int, Dict[str, np.ndarray]]]) -> Tuple[
            Dict[str, float], Dict[str, np.ndarray]]:
        """
        Compute FROC

        Args:
            results_list: list with result s per image (in list)
                per category (dict). Inner Dict contains multiple results
                    obtained by :func:`box_matching_batch`.
                `dtMatches`: matched detections [T, D], where T = number of
                    thresholds, D = number of detections
                `gtMatches`: matched ground truth boxes [T, G], where
                    T = number of thresholds, G = number of  ground truth
                `dtScores`: prediction scores [D] detection scores
                `gtIgnore`: ground truth boxes which should be ignored
                    [G] indicate whether ground truth should be ignored
                `dtIgnore`: detections which should be ignored [T, D],
                    indicate which detections should be ignored

        Returns:
            Dict[str, float]: FROC score per IoU (key: FROC_score@IoU:{key:2f})
            Dict[str, np.ndarray]: FROC curve computed at specified fps
                thresholds per IoU; [R] R is the number of fps thresholds
                (key: FROC_curve@IoU:{key:2f})
        """
        if self.verbose:
            logger.info('Start FROC metric computation...')
            tic = time.time()

        scores = {}
        curves = {}
        _score, _curve = self.compute_froc_mul_iou(results_list)
        scores.update(_score)
        curves.update(_curve)

        if self.verbose:
            toc = time.time()
            logger.info(f'FROC finished (t={(toc - tic):0.2f}s).')

        if self.per_class:
            _score, _curve = self.compute_froc_mul_iou_per_class(results_list)
            scores.update(_score)
            curves.update(_curve)

            if self.verbose:
                toc = time.time()
                logger.info(f'FROC per class finished (t={(toc - tic):0.2f}s).')

        if self.save_dir is not None:
            self.plot_froc_curves(curves)
        return scores, curves

    def compute_froc_mul_iou(self, results_list: List[Dict[int, Dict[str, np.ndarray]]]) -> Tuple[
            Dict[str, float], Dict[str, np.ndarray]]:
        """
        Compute FROC curve for multiple IoU values

        Args:
            results_list: list with result s per image (in list)
                per category (dict). Inner Dict contains multiple results
                    obtained by :func:`box_matching_batch`.
                `dtMatches`: matched detections [T, G], where T = number of
                    thresholds, G = number of ground truth
                `gtMatches`: matched ground truth boxes [T, D], where
                    T = number of thresholds, D = number of detections
                `dtScores`: prediction scores [D] detection scores
                `gtIgnore`: ground truth boxes which should be ignored
                    [G] indicate whether ground truth should be ignored
                `dtIgnore`: detections which should be ignored [T, D],
                    indicate which detections should be ignored

        Returns:
            Dict[str, float]: FROC score per IoU
            Dict[str,np.ndarray]: FROC curve computed at specified fps 
                thresholds per IoU; [R] R is the number of fps thresholds
        """
        num_images = len(results_list)
        results = [_r for r in results_list for _r in r.values()]

        if len(results) == 0:
            logger.warning(f"WARNING, no results found for froc computation")
            return ({"froc_score": 0},
                    {"froc_curve": np.zeros(len(self.fpi_thresholds))})

        # r['dtMatches'] [T, R], where R = sum(all detections)
        dt_matches = np.concatenate([r['dtMatches'] for r in results], axis=1)
        dt_ignores = np.concatenate([r['dtIgnore'] for r in results], axis=1)
        dt_scores = np.concatenate([r['dtScores'] for r in results])
        gt_ignore = np.concatenate([r['gtIgnore'] for r in results])
        
        self.check_number_of_iou(dt_matches, dt_ignores)
        
        num_gt = np.count_nonzero(gt_ignore == 0)  # number of ground truth boxes (non ignored)
        if num_gt == 0:
            logger.error("No ground truth found! Returning 0 in FROC.")
            return ({"froc_score": 0},
                    {"froc_curve": np.zeros(len(self.fpi_thresholds))})

        # keep shape in case of 1 threshold
        old_shape = dt_matches.shape
        dt_matches = dt_matches[np.logical_not(dt_ignores)].reshape(old_shape)
        
        curves = {}
        for iou_idx, iou_val in enumerate(self.iou_thresholds):
            # filter scores with ignores detections
            _scores = dt_scores[np.logical_not(dt_ignores[iou_idx])]
            assert len(_scores) == len(dt_matches[iou_idx])
            
            _fps, _sens, _th = (self.compute_froc_curve_one_iou(
                dt_matches[iou_idx], _scores, num_images, num_gt))
            
            # interpolate at defined fpr thresholds
            curves[iou_val] = np.interp(self.fpi_thresholds, _fps, _sens)


        # linearly interpolate curves for needed fps values
        scores = {f"FROC_score_IoU_{key:.2f}": np.mean(c) for key, c in curves.items()}
        curves = {f"FROC_curve_IoU_{key:.2f}": c for key, c in curves.items()}
        curves["FROC_fpi_thresholds"] = self.fpi_thresholds
        return scores, curves

    @staticmethod
    def compute_froc_curve_one_iou(dt_matches: np.ndarray, dt_scores: np.ndarray,
                                   num_images: int, num_gt: int):
        """
        Compute FROC curve for a single IoU value

        Args:
            dt_matches (np.ndarray): binary array indicating which bounding
                boxes have a large enough overlap with gt;
                [R] where R is the number of predictions
            dt_scores (np.ndarray): prediction score for each bounding box;
                [R] where R is the number of predictions
            num_images (int): number of images
            num_gt (int): number of ground truth bounding boxes

        Returns:
            np.ndarray: false positives per image
            np.ndarray: sensitivity
            np.ndarray: thresholds
        """
        num_detections = len(dt_matches)
        num_matched = np.sum(dt_matches)
        num_unmatched = num_detections - num_matched

        if dt_matches.size == 0:
            logger.warning("WARNING, no matches found.")
            return np.zeros((2,)), np.zeros((2,)), np.zeros((2,))
        else:
            fpr, tpr, thresholds = roc_curve(dt_matches, dt_scores)

        if num_unmatched == 0:
            logger.warning("WARNING, no false positives found")
            fps = np.zeros(len(fpr))
        else:
            fps = (fpr * num_unmatched) / num_images
        sens = (tpr * num_matched) / num_gt
        return fps, sens, thresholds

    def compute_froc_mul_iou_per_class(
        self, results_list: List[Dict[int, Dict[str, np.ndarray]]]) -> (
            Dict[str, float], Dict[str, np.ndarray]):
        """
        Compute FROC curve for multiple classes

        Args:
            results_list: list with result s per image (in list)
                per category (dict). Inner Dict contains multiple results
                    obtained by :func:`box_matching_batch`.
                `dtMatches`: matched detections [T, G], where T = number of
                    thresholds, G = number of ground truth
                `gtMatches`: matched ground truth boxes [T, D], where
                    T = number of thresholds, D = number of detections
                `dtScores`: prediction scores [D] detection scores
                `gtIgnore`: ground truth boxes which should be ignored
                    [G] indicate whether ground truth should be ignored
                `dtIgnore`: detections which should be ignored [T, D],
                    indicate which detections should be ignored

        Returns:
            Dict[str, float]: FROC score computed  per class per class
            Dict[str, np.ndarray]: FROC curve computed per class per IoU;
                [R] R is the number of fps thresholds
        """
        froc_scores_cls = {}
        froc_curves_cls = {}
        for cls_idx, cls_str in enumerate(self.classes):
            # filter current class from list of results and put them into a dict with a single entry
mibaumgartner's avatar
mibaumgartner committed
271
            results_by_cls = [{0: r[cls_idx]} if cls_idx in r else {} for r in results_list]
mibaumgartner's avatar
mibaumgartner committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
            if results_by_cls:
                cls_scores, cls_curves = self.compute_froc_mul_iou(results_by_cls)

                froc_scores_cls.update({f"{cls_str}_{key}": item for key, item in cls_scores.items()})
                froc_curves_cls.update({f"{cls_str}_{key}": item for key, item in cls_curves.items()})
        return froc_scores_cls, froc_curves_cls

    def plot_froc_curves(self, curves: Dict[str, Sequence[float]]) -> None:
        """
        Plot frocs

        Args:
            curves: dict with froc curves (as obtained by :method:`compute`)
                FROC_score_IoU_{key:.2f} for class "normal" FROC
                {cls_name}_FROC_score_IoU_{key:.2f}: for class specific froc
        """
        # plot normal froc curves
        selection = select_froc_curves(curves)
        fig, ax = get_froc_ax(self.fpi_thresholds)
        for _, froc, iou in zip(*selection):
            ax.plot(self.fpi_thresholds, froc, 'o-', label=f"IoU:{iou:.2f}")
        ax.set_title("FROC")
        ax.legend(loc='lower right')
        fig.savefig(self.save_dir / "FROC.png")
        plt.close(fig)

        # plot cls frocs
        selection = select_froc_curves_cls(curves)
        reordered = defaultdict(list)
        for class_name, (names, frocs, ious) in selection.items():
            for froc, iou in zip(frocs, ious):
                reordered[iou].append((class_name, froc))
        for iou, frocs in reordered.items():
            fig, ax = get_froc_ax(self.fpi_thresholds)
            for class_name, froc in frocs:
                ax.plot(self.fpi_thresholds, froc, 'o-', label=f"{class_name}")
            title = f"FROC_cls_IoU_{iou:.2f}"
            ax.set_title(title)
            ax.legend(loc='lower right')
            fig.savefig(self.save_dir / f"{title.replace('.', '_')}.png")
            plt.close(fig)


def get_froc_ax(fpi_values: Optional[Sequence[float]] = None) -> Tuple[plt.Figure, plt.Axes]:
    """
    Create preconfigured figure and axes object for froc curves

    Args:
        fpi_values: x values to use for froc

    Returns:
        plt.Figure: figure object
        plt.Axes: configured axes object
    """
    fig, ax = plt.subplots()
    ax.set_xscale("log", base=2)
    
    if fpi_values is not None:
        ax.set_xlim(min(fpi_values), max(fpi_values))
        ax.set_xticks(fpi_values)
    ax.set_ylim(0, 1)
    ax.set_xlabel('Avg number of false positives per scan')
    ax.set_ylabel('Sensitivity')
    ax.grid(True)

    formatter = FuncFormatter(lambda y, _: '{:.3f}'.format(y))
    ax.xaxis.set_major_formatter(formatter)
    return fig, ax


def select_froc_curves(curves: Dict[str, np.ndarray], prefix: Optional[str] = None) -> \
        Tuple[List[str], List[np.ndarray], List[float]]:
    """
    Select froc curves

    Args:
        curves: dict to select frocs from. Class specific frocs need to
            follow FROC_score_IoU_{key:.2f} pattern

    Returns:
        Dict[str, Tuple[List[str], List[np.ndarray], List[float]]]:
            dict defines the classes, tuple is output from
            :method:`select_froc_curves_cls`
    """
    if prefix is None:
        prefix = ""
    froc_keys = [str(c) for c in curves.keys()
                 if str(c).startswith(f"{prefix}FROC_") and
                 not str(c).endswith("_thresholds")]
    frocs = [curves[c] for c in froc_keys]
    ious = [float(c.rsplit('_', 1)[1]) for c in froc_keys]
    return froc_keys, frocs, ious


def select_froc_curves_cls(curves: Dict[str, np.ndarray]) -> \
        Dict[str, Tuple[List[str], List[np.ndarray], List[float]]]:
    """
    Select class specific froc curves

    Args:
        curves: dict to select frocs from. Class specific frocs need to follow
            {cls_name}_FROC_score_IoU_{key:.2f} pattern

    Returns:
        Dict[str, Tuple[List[str], List[np.ndarray], List[float]]]:
            dict defines the classes, tuple is output from
            :method:`select_froc_curves_cls`
    """
    all_classes = [str(c).split('_', 1)[0] for c in curves.keys()
                   if not str(c).startswith("FROC_") and
                   not str(c).endswith("_thresholds")]
    all_classes = list(set(all_classes))
    output = {}
    for cls_name in all_classes:
        output[cls_name] = select_froc_curves(curves, prefix=f"{cls_name}_")
    return output