Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / postprocessor / resize_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 from functools import singledispatch
17 import scipy.misc
18 import numpy as np
19
20 from ..config import NumberField
21 from ..utils import get_size_from_config
22 from .postprocessor import PostprocessorWithSpecificTargets, PostprocessorWithTargetsConfigValidator
23 from ..representation import SegmentationPrediction, SegmentationAnnotation
24
25
26 class ResizeSegmentationMask(PostprocessorWithSpecificTargets):
27     __provider__ = 'resize_segmentation_mask'
28
29     annotation_types = (SegmentationAnnotation, )
30     prediction_types = (SegmentationPrediction, )
31
32     def validate_config(self):
33         class _ResizeConfigValidator(PostprocessorWithTargetsConfigValidator):
34             size = NumberField(floats=False, optional=True, min_value=1)
35             dst_width = NumberField(floats=False, optional=True, min_value=1)
36             dst_height = NumberField(floats=False, optional=True, min_value=1)
37
38         resize_config_validator = _ResizeConfigValidator(self.__provider__)
39         resize_config_validator.validate(self.config)
40
41     def configure(self):
42         self.dst_height, self.dst_width = get_size_from_config(self.config, allow_none=True)
43
44     def process_image(self, annotation, prediction):
45         target_height = self.dst_height or self.image_size[0]
46         target_width = self.dst_width or self.image_size[1]
47
48         @singledispatch
49         def resize_segmentation_mask(entry, height, width):
50             return entry
51
52         @resize_segmentation_mask.register(SegmentationPrediction)
53         def _(entry, height, width):
54             entry_mask = []
55             for class_mask in entry.mask:
56                 resized_mask = scipy.misc.imresize(class_mask, (height, width), 'nearest')
57                 entry_mask.append(resized_mask)
58             entry.mask = np.array(entry_mask)
59
60             return entry
61
62         @resize_segmentation_mask.register(SegmentationAnnotation)
63         def _(entry, height, width):
64             entry.mask = scipy.misc.imresize(entry.mask, (height, width), 'nearest')
65             return entry
66
67         for target in annotation:
68             resize_segmentation_mask(target, target_height, target_width)
69
70         for target in prediction:
71             resize_segmentation_mask(target, target_height, target_width)
72
73         return annotation, prediction