Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / postprocessor / crop_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
17 from .postprocessor import PostprocessorWithSpecificTargets, PostprocessorWithTargetsConfigValidator
18 from ..representation import BrainTumorSegmentationAnnotation, BrainTumorSegmentationPrediction
19 from ..config import NumberField
20 from ..preprocessor import Crop3D
21 from ..utils import get_size_3d_from_config
22
23
24 class CropSegmentationMask(PostprocessorWithSpecificTargets):
25     __provider__ = 'crop_segmentation_mask'
26
27     annotation_types = (BrainTumorSegmentationAnnotation,)
28     prediction_types = (BrainTumorSegmentationPrediction,)
29
30     def validate_config(self):
31         class _ConfigValidator(PostprocessorWithTargetsConfigValidator):
32             size = NumberField(floats=False, min_value=1)
33             dst_width = NumberField(floats=False, optional=True, min_value=1)
34             dst_height = NumberField(floats=False, optional=True, min_value=1)
35             dst_volume = NumberField(floats=False, optional=True, min_value=1)
36
37         _ConfigValidator(self.name, on_extra_argument=_ConfigValidator.ERROR_ON_EXTRA_ARGUMENT).validate(self.config)
38
39     def configure(self):
40         self.dst_height, self.dst_width, self.dst_volume = get_size_3d_from_config(self.config)
41
42     def process_image(self, annotation, prediction):
43         for target in annotation:
44             target.mask = Crop3D.crop_center(target.mask, self.dst_height, self.dst_width, self.dst_volume)
45
46         for target in prediction:
47             target.mask = Crop3D.crop_center(target.mask, self.dst_height, self.dst_width, self.dst_volume)
48
49         return annotation, prediction