Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / postprocessor / clip_segmentation_mask.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 import numpy as np
17 from .postprocessor import PostprocessorWithSpecificTargets, PostprocessorWithTargetsConfigValidator
18 from ..representation import BrainTumorSegmentationAnnotation, BrainTumorSegmentationPrediction
19 from ..config import NumberField, ConfigError
20
21
22 class ClipSegmentationMask(PostprocessorWithSpecificTargets):
23     __provider__ = 'clip_segmentation_mask'
24
25     annotation_types = (BrainTumorSegmentationAnnotation,)
26     prediction_types = (BrainTumorSegmentationPrediction,)
27
28     def validate_config(self):
29         class _ConfigValidator(PostprocessorWithTargetsConfigValidator):
30             min_value = NumberField(floats=False, min_value=0, optional=True)
31             max_value = NumberField(floats=False)
32
33         _ConfigValidator(self.name, on_extra_argument=_ConfigValidator.ERROR_ON_EXTRA_ARGUMENT).validate(self.config)
34
35     def configure(self):
36         self.min_value = self.config.get('min_value', 0)
37         self.max_value = self.config['max_value']
38         if self.max_value < self.min_value:
39             raise ConfigError('max_value should be greater than min_value')
40
41     def process_image(self, annotation, prediction):
42         for target in annotation:
43             target.mask = np.clip(target.mask, a_min=self.min_value, a_max=self.max_value)
44
45         for target in prediction:
46             target.mask = np.clip(target.mask, a_min=self.min_value, a_max=self.max_value)
47
48         return annotation, prediction