2 Copyright (C) 2018-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
19 from .postprocessor import Postprocessor, BasePostprocessorConfig
20 from ..representation import SegmentationAnnotation, SegmentationPrediction
21 from ..config import NumberField
24 class ZoomSegMask(Postprocessor):
26 Zoom probabilities of segmentation prediction.
29 __provider__ = 'zoom_segmentation_mask'
31 annotation_types = (SegmentationAnnotation, )
32 prediction_types = (SegmentationPrediction, )
34 def validate_config(self):
35 class _ZoomSegMaskConfigValidator(BasePostprocessorConfig):
36 zoom = NumberField(floats=False, min_value=1)
38 zoom_segmentation_mask_config_validator = _ZoomSegMaskConfigValidator(
39 self.__provider__, on_extra_argument=_ZoomSegMaskConfigValidator.ERROR_ON_EXTRA_ARGUMENT
41 zoom_segmentation_mask_config_validator.validate(self.config)
44 self.zoom = self.config['zoom']
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):
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
65 return annotation, prediction