Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / postprocessor / zoom_segmentation_mask.py
1 """
2 Copyright (C) 2018-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 import numpy as np
18
19 from .postprocessor import Postprocessor, BasePostprocessorConfig
20 from ..representation import SegmentationAnnotation, SegmentationPrediction
21 from ..config import NumberField
22
23
24 class ZoomSegMask(Postprocessor):
25     """
26     Zoom probabilities of segmentation prediction.
27     """
28
29     __provider__ = 'zoom_segmentation_mask'
30
31     annotation_types = (SegmentationAnnotation, )
32     prediction_types = (SegmentationPrediction, )
33
34     def validate_config(self):
35         class _ZoomSegMaskConfigValidator(BasePostprocessorConfig):
36             zoom = NumberField(floats=False, min_value=1)
37
38         zoom_segmentation_mask_config_validator = _ZoomSegMaskConfigValidator(
39             self.__provider__, on_extra_argument=_ZoomSegMaskConfigValidator.ERROR_ON_EXTRA_ARGUMENT
40         )
41         zoom_segmentation_mask_config_validator.validate(self.config)
42
43     def configure(self):
44         self.zoom = self.config['zoom']
45
46     def process_image(self, annotation, prediction):
47         for annotation_, prediction_ in zip(annotation, prediction):
48             height, width = annotation_.mask.shape[:2]
49             prob = prediction_.mask
50             zoom_prob = np.zeros((prob.shape[0], height, width), dtype=np.float32)
51             for c in range(prob.shape[0]):
52                 for h in range(height):
53                     for w in range(width):
54                         r0 = h // self.zoom
55                         r1 = r0 + 1
56                         c0 = w // self.zoom
57                         c1 = c0 + 1
58                         rt = float(h) / self.zoom - r0
59                         ct = float(w) / self.zoom - c0
60                         v0 = rt * prob[c, r1, c0] + (1 - rt) * prob[c, r0, c0]
61                         v1 = rt * prob[c, r1, c1] + (1 - rt) * prob[c, r0, c1]
62                         zoom_prob[c, h, w] = (1 - ct) * v0 + ct * v1
63             prediction_.mask = zoom_prob
64
65         return annotation, prediction