Skip to content

Commit 27dbca7

Browse files
authored
Merge pull request #3157 from anzhella-pankratova/read_model_unify
Python demos: unify core.read_model()
2 parents ae5c05e + 399b70c commit 27dbca7

File tree

20 files changed

+56
-81
lines changed

20 files changed

+56
-81
lines changed

demos/3d_segmentation_demo/python/3d_segmentation_demo.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ def read_image(test_data_path, data_name, sizes=(128, 128, 128), is_series=True,
245245
def main():
246246
args = parse_arguments()
247247

248-
# --------------------------------- 1. Load Plugin for inference engine ---------------------------------
249248
log.info('OpenVINO Inference Engine')
250249
log.info('\tbuild: {}'.format(get_version()))
251250
core = Core()
@@ -262,7 +261,6 @@ def main():
262261
raise AttributeError("Device {} do not support of 3D convolution. "
263262
"Please use CPU, GPU or HETERO:*CPU*, HETERO:*GPU*")
264263

265-
# --------------------- 2. Read IR Generated by ModelOptimizer (.xml and .bin files) ---------------------
266264
log.info('Reading model {}'.format(args.path_to_model))
267265
model = core.read_model(args.path_to_model)
268266

@@ -279,12 +277,10 @@ def main():
279277

280278
n, c, d, h, w = model.inputs[0].shape
281279

282-
# ------------------------------------ 3. Loading model to the plugin -------------------------------------
283280
compiled_model = core.compile_model(model, args.target_device)
284281
infer_request = compiled_model.create_infer_request()
285282
log.info('The model {} is loaded to {}'.format(args.path_to_model, args.target_device))
286283

287-
# --------------------------------------- 4. Preparing input data -----------------------------------------
288284
start_time = perf_counter()
289285
if not os.path.exists(args.path_to_input_data):
290286
raise AttributeError("Path to input data: '{}' does not exist".format(args.path_to_input_data))
@@ -312,9 +308,7 @@ def main():
312308
original_data = data_crop
313309
original_size = original_data.shape[-3:]
314310

315-
# ---------------------------------------------- 5. Do inference --------------------------------------------
316311
result = infer_request.infer({input_tensor_name: data_crop})
317-
# ---------------------------- 6. Processing of the received inference results ------------------------------
318312
result = next(iter(result.values()))
319313
batch, channels, out_d, out_h, out_w = result.shape
320314

@@ -375,7 +369,6 @@ def main():
375369
total_latency = (perf_counter() - start_time) * 1e3
376370
log.info("Metrics report:")
377371
log.info("\tLatency: {:.1f} ms".format(total_latency))
378-
# --------------------------------------------- 7. Save output -----------------------------------------------
379372
tiff_output_name = os.path.join(args.path_to_output, 'output.tiff')
380373
Image.new('RGB', (original_data.shape[3], original_data.shape[2])).save(tiff_output_name,
381374
append_images=list_img, save_all=True)

demos/colorization_demo/python/colorization_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def main(args):
6767
core = Core()
6868

6969
log.info('Reading model {}'.format(args.model))
70-
model = core.read_model(args.model, args.model.with_suffix(".bin"))
70+
model = core.read_model(args.model)
7171

7272
input_tensor_name = 'data_l'
7373
input_shape = model.input(input_tensor_name).shape

demos/face_recognition_demo/python/ie_module.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020

2121
class Module:
22-
def __init__(self, core, model, model_type):
22+
def __init__(self, core, model_path, model_type):
2323
self.core = core
2424
self.model_type = model_type
25-
log.info('Reading {} model {}'.format(model_type, model))
26-
self.model = core.read_model(model, model.with_suffix('.bin'))
27-
self.model_path = model
25+
log.info('Reading {} model {}'.format(model_type, model_path))
26+
self.model = core.read_model(model_path)
27+
self.model_path = model_path
2828
self.active_requests = 0
2929
self.clear()
3030

demos/formula_recognition_demo/python/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ def prerocess_crop(crop, tgt_shape, preprocess_type='crop'):
120120
return preprocess_image(PREPROCESSING[preprocess_type], bin_crop, tgt_shape)
121121

122122

123-
def read_net(model_xml, ie, model_type):
124-
model_bin = os.path.splitext(model_xml)[0] + ".bin"
125-
126-
log.info('Reading {} model {}'.format(model_type, model_xml))
127-
return ie.read_model(model_xml, model_bin)
123+
def read_net(model_path, ie, model_type):
124+
log.info('Reading {} model {}'.format(model_type, model_path))
125+
return ie.read_model(model_path)
128126

129127

130128
def change_layout(model_input):

demos/gpt2_text_prediction_demo/python/gpt2_text_prediction_demo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ def main():
7979
ie = Core()
8080

8181
# read IR
82-
model_path = args.model
8382
log.info('Reading model {}'.format(args.model))
84-
model = ie.read_model(model_path)
83+
model = ie.read_model(args.model)
8584

8685
# check number inputs and outputs
8786
if len(model.inputs) != 1:

demos/human_pose_estimation_3d_demo/python/modules/inference_engine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818

1919

2020
class InferenceEngine:
21-
def __init__(self, net_model_xml_path, device, stride):
21+
def __init__(self, model_path, device, stride):
2222
self.device = device
2323
self.stride = stride
2424

2525
log.info('OpenVINO Inference Engine')
2626
log.info('\tbuild: {}'.format(get_version()))
2727
self.core = Core()
2828

29-
log.info('Reading model {}'.format(net_model_xml_path))
30-
self.model = self.core.read_model(net_model_xml_path)
29+
log.info('Reading model {}'.format(model_path))
30+
self.model = self.core.read_model(model_path)
3131

3232
required_output_keys = {'features', 'heatmaps', 'pafs'}
3333
for output_tensor_name in required_output_keys:
@@ -40,7 +40,7 @@ def __init__(self, net_model_xml_path, device, stride):
4040
self.input_tensor_name = self.model.inputs[0].get_any_name()
4141
compiled_model = self.core.compile_model(self.model, self.device)
4242
self.infer_request = compiled_model.create_infer_request()
43-
log.info('The model {} is loaded to {}'.format(net_model_xml_path, self.device))
43+
log.info('The model {} is loaded to {}'.format(model_path, self.device))
4444

4545
def infer(self, img):
4646
img = img[0:img.shape[0] - (img.shape[0] % self.stride),

demos/image_inpainting_demo/python/inpainting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class ImageInpainting:
1818
def __init__(self, core, model_path, device='CPU'):
19-
model = core.read_model(model_path, model_path.with_suffix('.bin'))
19+
model = core.read_model(model_path)
2020

2121
if len(model.inputs) != 2:
2222
raise RuntimeError("The model expects 2 input layers")

demos/image_retrieval_demo/python/image_retrieval_demo/image_retrieval.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ def __init__(self, model_path, device, cpu_extension):
3636
if cpu_extension and device == 'CPU':
3737
core.add_extension(cpu_extension, 'CPU')
3838

39-
path = '.'.join(model_path.split('.')[:-1])
4039
log.info('Reading model {}'.format(model_path))
41-
self.model = core.read_model(path + '.xml', path + '.bin')
40+
self.model = core.read_model(model_path)
4241
self.input_tensor_name = "Placeholder"
4342
compiled_model = core.compile_model(self.model, device)
4443
self.infer_request = compiled_model.create_infer_request()

demos/image_translation_demo/python/image_translation_demo/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"""
1313

1414
class CocosnetModel:
15-
def __init__(self, core, model_xml, device='CPU'):
16-
model = core.read_model(model_xml)
15+
def __init__(self, core, model_path, device='CPU'):
16+
model = core.read_model(model_path)
1717
if len(model.inputs) != 3:
1818
raise RuntimeError("The CocosnetModel expects 3 input layers")
1919
if len(model.outputs) != 1:
@@ -38,8 +38,8 @@ def infer(self, input_semantics, reference_image, reference_semantics):
3838

3939

4040
class SegmentationModel:
41-
def __init__(self, core, model_xml, device='CPU'):
42-
model = core.read_model(model_xml)
41+
def __init__(self, core, model_path, device='CPU'):
42+
model = core.read_model(model_path)
4343
if len(model.inputs) != 1:
4444
raise RuntimeError("The SegmentationModel expects 1 input layer")
4545
if len(model.outputs) != 1:

demos/instance_segmentation_demo/python/instance_segmentation_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def main():
115115

116116
# Read IR
117117
log.info('Reading model {}'.format(args.model))
118-
model = core.read_model(args.model, args.model.with_suffix('.bin'))
118+
model = core.read_model(args.model)
119119
image_input, image_info_input, (n, c, h, w), model_type, output_names, postprocessor = check_model(model)
120120
args.no_keep_aspect_ratio = model_type == 'yolact' or args.no_keep_aspect_ratio
121121

0 commit comments

Comments
 (0)