|
1 | 1 | """
|
2 |
| - Copyright (c) 2019-2020 Intel Corporation |
| 2 | + Copyright (c) 2019-2022 Intel Corporation |
3 | 3 | Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | you may not use this file except in compliance with the License.
|
5 | 5 | You may obtain a copy of the License at
|
|
15 | 15 |
|
16 | 16 |
|
17 | 17 | class ImageInpainting:
|
18 |
| - def __init__(self, ie, model_path, device='CPU'): |
19 |
| - model = ie.read_network(model_path, model_path.with_suffix('.bin')) |
| 18 | + def __init__(self, core, model_path, device='CPU'): |
| 19 | + model = core.read_model(model_path, model_path.with_suffix('.bin')) |
20 | 20 |
|
21 |
| - assert len(model.input_info) == 2, "Expected 2 input blob" |
22 |
| - assert len(model.outputs) == 1, "Expected 1 output blobs" |
| 21 | + if len(model.inputs) != 2: |
| 22 | + raise RuntimeError("The model expects 2 input layers") |
| 23 | + if len(model.outputs) != 1: |
| 24 | + raise RuntimeError("The model expects 1 output layer") |
23 | 25 |
|
24 |
| - self._input_layer_names = sorted(model.input_info) |
25 |
| - self._output_layer_name = next(iter(model.outputs)) |
| 26 | + self.image_input_layer, self.mask_input_layer = sorted([node.get_any_name() for node in model.inputs]) |
26 | 27 |
|
27 |
| - self._ie = ie |
28 |
| - self._exec_model = self._ie.load_network(model, device) |
| 28 | + compiled_model = core.compile_model(model, device) |
| 29 | + self.infer_request = compiled_model.create_infer_request() |
29 | 30 |
|
30 |
| - _, channels, input_height, input_width = model.input_info[self._input_layer_names[0]].input_data.shape |
31 |
| - assert channels == 3, "Expected 3-channel input" |
| 31 | + self.nchw_layout = model.input(self.image_input_layer).shape[1] == 3 |
| 32 | + if self.nchw_layout: |
| 33 | + _, _, input_height, input_width = model.input(self.image_input_layer).shape |
| 34 | + _, mask_channels, mask_height, mask_width = model.input(self.mask_input_layer).shape |
| 35 | + else: |
| 36 | + _, input_height, input_width, _ = model.input(self.image_input_layer).shape |
| 37 | + _, mask_height, mask_width, mask_channels = model.input(self.mask_input_layer).shape |
32 | 38 |
|
33 |
| - _, channels, mask_height, mask_width = model.input_info[self._input_layer_names[1]].input_data.shape |
34 |
| - assert channels == 1, "Expected 1-channel input" |
| 39 | + if mask_channels != 1: |
| 40 | + raise RuntimeError("The model expects 1 channel for {} input layer".format(self.mask_input_layer)) |
35 | 41 |
|
36 |
| - assert mask_height == input_height and mask_width == input_width, "Mask size expected to be equal to image size" |
| 42 | + if mask_height != input_height or mask_width != input_width: |
| 43 | + raise RuntimeError("Mask size is expected to be equal to image size") |
37 | 44 | self.input_height = input_height
|
38 | 45 | self.input_width = input_width
|
39 | 46 |
|
40 | 47 |
|
41 | 48 | def infer(self, image, mask):
|
42 |
| - output = self._exec_model.infer(inputs={self._input_layer_names[0]: image, self._input_layer_names[1]: mask}) |
43 |
| - return output[self._output_layer_name] |
| 49 | + output = self.infer_request.infer(inputs={self.image_input_layer: image, self.mask_input_layer: mask}) |
| 50 | + return next(iter(output.values())) |
44 | 51 |
|
45 | 52 |
|
46 |
| - def process(self, src_image, mask): |
47 |
| - image = np.transpose(src_image, (2, 0, 1)) |
48 |
| - mask = np.transpose(mask, (2, 0, 1)) |
| 53 | + def process(self, image, mask): |
| 54 | + if self.nchw_layout: |
| 55 | + image = np.transpose(image, (2, 0, 1)) |
| 56 | + mask = np.transpose(mask, (2, 0, 1)) |
49 | 57 | image = np.expand_dims(image, axis=0)
|
50 | 58 | mask = np.expand_dims(mask, axis=0)
|
51 | 59 | output = self.infer(image, mask)
|
52 | 60 |
|
53 |
| - output = np.transpose(output, (0, 2, 3, 1)).astype(np.uint8) |
54 |
| - return output[0] |
| 61 | + if self.nchw_layout: |
| 62 | + output = np.transpose(output, (0, 2, 3, 1)) |
| 63 | + return output.astype(np.uint8)[0] |
0 commit comments