2 Copyright (c) 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.
17 from .postprocessor import PostprocessorWithSpecificTargets, PostprocessorWithTargetsConfigValidator
18 from ..representation import BrainTumorSegmentationAnnotation, BrainTumorSegmentationPrediction
19 from ..config import NumberField, ConfigError
22 class ClipSegmentationMask(PostprocessorWithSpecificTargets):
23 __provider__ = 'clip_segmentation_mask'
25 annotation_types = (BrainTumorSegmentationAnnotation,)
26 prediction_types = (BrainTumorSegmentationPrediction,)
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)
33 _ConfigValidator(self.name, on_extra_argument=_ConfigValidator.ERROR_ON_EXTRA_ARGUMENT).validate(self.config)
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')
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)
45 for target in prediction:
46 target.mask = np.clip(target.mask, a_min=self.min_value, a_max=self.max_value)
48 return annotation, prediction