The link to the models: https://drive.google.com/file/d/1X85QqooE2A4p9Zgi1h7XMMDLzcfc3HE_/view?usp=sharing
Change `model = 'c:\\models\\encoder\\model.xml'` to where ever you extrac the "encoder" model.
'''
Copyright (c) 2018-2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
from __future__ import print_function
from argparse import ArgumentParser, SUPPRESS
import logging as log
import os
import sys
import ngraph as ng
import numpy as np
from openvino.inference_engine import IECore
from transformers import AutoTokenizer
def main():
log.basicConfig(format='[ %(levelname)s ] %(message)s', level=log.INFO, stream=sys.stdout)
log.info('Loading Inference Engine')
ie = IECore()
model = 'c:\\models\\encoder\\model.xml'
log.info(f'Loading network:')
log.info(f' {model}')
net = ie.read_network(model=model)
log.info('Device info:')
versions = ie.get_versions('MYRIAD')
log.info(f' MYRIAD')
log.info(f' MKLDNNPlugin version ......... {versions["MYRIAD"].major}.{versions["MYRIAD"].minor}')
log.info(f' Build ........... {versions["MYRIAD"].build_number}')
log.info(f'Inputs number: {len(net.input_info.keys())}')
assert len(net.input_info.keys()) == 2, 'Sample supports network with two inputs'
assert len(net.outputs.keys()) == 1, 'Sample supports clean one output'
input_name = list(net.input_info.keys())[0]
input_info = net.input_info[input_name]
supported_input_dims = 2
log.info(f' Input name: {input_name}')
log.info(f' Input shape: {str(input_info.input_data.shape)}')
print()
if len(input_info.input_data.layout) == supported_input_dims:
n, c = input_info.input_data.shape
assert n == 1, 'Sample supports topologies with one '
else:
raise AssertionError('Sample supports input with NC shape only')
log.info('Preparing input blobs')
if len(input_info.layout) == supported_input_dims:
input_info.precision = 'U8'
tokenizer = AutoTokenizer.from_pretrained('facebook/mbart-large-50-many-to-many-mmt')
sample_input = "The universe is a dark forest."
model_inputs = tokenizer(sample_input, max_length=92, padding='max_length', truncation=True, return_tensors="np")
input_ids = model_inputs["input_ids"]
attention_mask = model_inputs["attention_mask"]
data = {"input_ids":input_ids ,"attention_mask":attention_mask}
log.info('Loading model to the device')
exec_net = ie.load_network(network=net, device_name='MYRIAD')
log.info('Creating infer request and starting inference')
exec_result = exec_net.infer(inputs=data)
log.info('Processing output blobs')
result = exec_result[output_name]
boxes = {}
detections = result[0][0]
for number, proposal in enumerate(detections):
imid, label, confidence, coords = np.int(proposal[0]), np.int(proposal[1]), proposal[2], proposal[3:]
if confidence > 0.5:
xmin, ymin, xmax, ymax = w_new * coords[0], h_new * coords[1], w_new * coords[2], h_new * coords[3]
log.info(f' [{number},{label}] element, prob = {confidence:.6f}, '
f'bbox = ({xmin:.3f},{ymin:.3f})-({xmax:.3f},{ymax:.3f}), batch id = {imid}')
if not imid in boxes.keys():
boxes[imid] = []
boxes[imid].append([xmin, ymin, xmax, ymax])
imid = 0
tmp_image = cv2.imread(args.input)
for box in boxes[imid]:
cv2.rectangle(
tmp_image,
(np.int(box[0]), np.int(box[1])), (np.int(box[2]), np.int(box[3])),
color=(232, 35, 244), thickness=2)
cv2.imwrite('out.bmp', tmp_image)
log.info('Image out.bmp created!')
log.info('Execution successful\n')
log.info(
'This sample is an API example, for any performance measurements please use the dedicated benchmark_app tool')
if __name__ == '__main__':
sys.exit(main() or 0)