Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / representation / segmentation_representation.py
1 """
2 Copyright (c) 2019 Intel Corporation
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 from enum import Enum
18
19 import numpy as np
20
21 from .base_representation import BaseRepresentation
22 from ..data_readers import BaseReader
23
24
25 class GTMaskLoader(Enum):
26     PILLOW = 0
27     OPENCV = 1
28     SCIPY = 2
29     NIFTI = 3
30
31
32 class SegmentationRepresentation(BaseRepresentation):
33     pass
34
35
36 class SegmentationAnnotation(SegmentationRepresentation):
37     LOADERS = {
38         GTMaskLoader.PILLOW: 'pillow_imread',
39         GTMaskLoader.OPENCV: 'opencv_imread',
40         GTMaskLoader.SCIPY: 'scipy_imread',
41         GTMaskLoader.NIFTI: 'nifti_reader'
42     }
43
44     def __init__(self, identifier, path_to_mask, mask_loader=GTMaskLoader.PILLOW):
45         """
46         Args:
47             identifier: object identifier (e.g. image name).
48             path_to_mask: path where segmentation mask should be loaded from. The path is relative to data source.
49             mask_loader: back-end, used to load segmentation masks.
50         """
51
52         super().__init__(identifier)
53         self._mask_path = path_to_mask
54         self._mask_loader = mask_loader
55         self._mask = None
56
57     @property
58     def mask(self):
59         return self._mask if self._mask is not None else self._load_mask()
60
61     @mask.setter
62     def mask(self, value):
63         self._mask = value
64
65     def _load_mask(self):
66         loader = BaseReader.provide(self.LOADERS.get(self._mask_loader))
67         if self._mask is None:
68             mask = loader(self._mask_path, self.metadata['data_source'])
69             return mask.astype(np.uint8)
70
71         return self._mask
72
73
74 class SegmentationPrediction(SegmentationRepresentation):
75     def __init__(self, identifiers, mask):
76         """
77         Args:
78             identifiers: object identifier (e.g. image name).
79             mask: array with shape (n_classes, height, width) of probabilities at each location.
80         """
81
82         super().__init__(identifiers)
83         self.mask = mask
84
85
86 class BrainTumorSegmentationAnnotation(SegmentationAnnotation):
87     def __init__(self, identifier, path_to_mask):
88         super().__init__(identifier, path_to_mask, GTMaskLoader.NIFTI)
89
90 class BrainTumorSegmentationPrediction(SegmentationPrediction):
91     pass