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 ..adapters import Adapter
18 from ..representation import SegmentationPrediction, BrainTumorSegmentationPrediction
21 class SegmentationAdapter(Adapter):
22 __provider__ = 'segmentation'
24 def process(self, raw, identifiers=None, frame_meta=None):
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))
33 def _extract_predictions(self, outputs_list, meta):
34 if not 'tiles_shape' in (meta[-1] or {}):
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
43 new_raw[k] = [new_raw[k]]
45 tiles_shapes = [meta['tiles_shape'] for meta in meta]
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())
58 return {self.output_blob: restore_output}
61 class BrainTumorSegmentationAdapter(Adapter):
62 __provider__ = 'brain_tumor_segmentation'
64 def process(self, raw, identifiers=None, frame_meta=None):
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))
73 def _extract_predictions(self, outputs_list, meta):
74 if not (meta[-1] or {}).get('multi_infer', False):
75 return outputs_list[0]
77 output_keys = list(outputs_list[0].keys())
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