Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / accuracy_checker / adapters / segmentation.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 ..adapters import Adapter
18 from ..representation import SegmentationPrediction, BrainTumorSegmentationPrediction
19
20
21 class SegmentationAdapter(Adapter):
22     __provider__ = 'segmentation'
23
24     def process(self, raw, identifiers=None, frame_meta=None):
25         result = []
26         frame_meta = frame_meta or [] * len(identifiers)
27         raw_outputs = self._extract_predictions(raw, frame_meta)
28         for identifier, output in zip(identifiers, raw_outputs[self.output_blob]):
29             result.append(SegmentationPrediction(identifier, output))
30
31         return result
32
33     def _extract_predictions(self, outputs_list, meta):
34         if not 'tiles_shape' in (meta[-1] or {}):
35             new_raw = {}
36             for out in outputs_list:
37                 for key, val in out.items():
38                     out_previous = new_raw.get(key, [])
39                     out_previous.append(val)
40                     new_raw[key] = out_previous
41
42             for k in new_raw:
43                 new_raw[k] = [new_raw[k]]
44             return  new_raw
45         tiles_shapes = [meta['tiles_shape'] for meta in meta]
46         restore_output = []
47         offset = 0
48         for _, image_tiles_shape in enumerate(tiles_shapes):
49             next_offset = offset + image_tiles_shape[0] * image_tiles_shape[1]
50             image_tiles = [network_output[self.output_blob] for network_output in outputs_list[offset:next_offset]]
51             tiles_columns = image_tiles[::image_tiles_shape[0]]
52             image = tiles_columns[0]
53             for tile_column in tiles_columns[1:]:
54                 image = np.concatenate((image, tile_column), axis=3)
55             restore_output.append(image.squeeze())
56             offset = next_offset
57
58         return {self.output_blob: restore_output}
59
60
61 class BrainTumorSegmentationAdapter(Adapter):
62     __provider__ = 'brain_tumor_segmentation'
63
64     def process(self, raw, identifiers=None, frame_meta=None):
65         result = []
66         frame_meta = frame_meta or [] * len(identifiers)
67         raw_outputs = self._extract_predictions(raw, frame_meta)
68         for identifier, output in zip(identifiers, raw_outputs[self.output_blob]):
69             result.append(BrainTumorSegmentationPrediction(identifier, output))
70
71         return result
72
73     def _extract_predictions(self, outputs_list, meta):
74         if not (meta[-1] or {}).get('multi_infer', False):
75            return outputs_list[0]
76
77         output_keys = list(outputs_list[0].keys())
78         output_map = {}
79         for output_key in output_keys:
80             output_data = [[output[output_key] for output in outputs_list]]
81             output_map[output_key] = output_data
82
83         return output_map