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.
16 from functools import singledispatch
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
26 class ResizeSegmentationMask(PostprocessorWithSpecificTargets):
27 __provider__ = 'resize_segmentation_mask'
29 annotation_types = (SegmentationAnnotation, )
30 prediction_types = (SegmentationPrediction, )
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)
38 resize_config_validator = _ResizeConfigValidator(self.__provider__)
39 resize_config_validator.validate(self.config)
42 self.dst_height, self.dst_width = get_size_from_config(self.config, allow_none=True)
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]
49 def resize_segmentation_mask(entry, height, width):
52 @resize_segmentation_mask.register(SegmentationPrediction)
53 def _(entry, height, width):
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)
62 @resize_segmentation_mask.register(SegmentationAnnotation)
63 def _(entry, height, width):
64 entry.mask = scipy.misc.imresize(entry.mask, (height, width), 'nearest')
67 for target in annotation:
68 resize_segmentation_mask(target, target_height, target_width)
70 for target in prediction:
71 resize_segmentation_mask(target, target_height, target_width)
73 return annotation, prediction