def main(): args = build_argparser().parse_args() assert args.device.split(':')[0] == "HETERO", "This sample supports only Hetero Plugin. " \ "Please specify correct device, e.g. HETERO:FPGA,CPU" model_xml = args.model model_bin = os.path.splitext(model_xml)[0] + ".bin" # Plugin initialization for specified device and load extensions library if specified plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir) if args.cpu_extension and 'CPU' in args.device: plugin.add_cpu_extension(args.cpu_extension) # Read IR net = IENetwork.from_ir(model=model_xml, weights=model_bin) layers = net.get_layers() net_ops = set([l['type'] for l in layers.values()]) if not any(op in net_ops for op in ("Convolution", "Concat")): print("Specified IR doesn't contain any Convolution or Concat operations for which affinity going to be set.\n" "Try to use another topology to make the affinity setting result more visible.") # Configure the plugin to initialize default affinity for network in set_initial_affinity() function. plugin.set_config({"TARGET_FALLBACK": args.device.split(':')[1]}) # Enable graph visualization plugin.set_config({"HETERO_DUMP_GRAPH_DOT": "YES"}) plugin.set_initial_affinity(net) net.set_affinity(types_affinity_map={"Convolution": "GPU", "Concat": "CPU"}) # Affinity setting example based on layer name. # layers_affinity_map has higher priority and will overrides affinity set by layer type. # net.set_affinity(types_affinity_map={"Convolution": "GPU", "Concat": "CPU"}, # layers_affinity_map={"fire4/expand3x3/Conv2D": "CPU"}) assert len(net.inputs.keys()) == 1, "Sample supports only single input topologies" assert len(net.outputs) == 1, "Sample supports only single output topologies" input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) # Read and pre-process input image n, c, h, w = net.inputs[input_blob] image = cv2.imread(args.input) image = cv2.resize(image, (w, h)) image = image.transpose((2, 0, 1)) # Change data layout from HWC to CHW image = image.reshape((n, c, h, w)) # Load network to the plugin exec_net = plugin.load(network=net) del net # Start sync inference res = exec_net.infer(inputs={input_blob: image}) top_ind = np.argsort(res[out_blob], axis=1)[0, -args.number_top:][::-1] for i in top_ind: print("%f #%d" % (res[out_blob][0, i], i)) del exec_net del plugin cwd = os.getcwd() print( "Graphs representing default and resulting affinities dumped to {} and {} files respectively" .format(os.path.join(cwd, 'hetero_affinity.dot'), os.path.join(cwd, 'hetero_subgraphs.dot')) )
def test_set_initial_affinity(): with warnings.catch_warnings(record=True) as w: plugin = IEPlugin("HETERO:CPU", None) net = IENetwork(model=test_net_xml, weights=test_net_bin) plugin.set_initial_affinity(net) for l, params in net.layers.items(): assert params.affinity == "CPU", "Incorrect affinity for {}".format(l) assert len(w) == 1 assert "IEPlugin class is deprecated. " \ "Please use IECore class instead." in str(w[0].message)
def test_set_initial_affinity_wrong_device(device): with pytest.raises(RuntimeError) as e: with warnings.catch_warnings(record=True) as w: plugin = IEPlugin("CPU", None) net = IENetwork(model=test_net_xml, weights=test_net_bin) plugin.set_initial_affinity(net) assert len(w) == 1 assert "IEPlugin class is deprecated. " \ "Please use IECore class instead." in str(w[0].message) assert "set_initial_affinity method applicable only for HETERO device" in str(e.value)
def prepare_model(log, model, weights, cpu_extension, device_list, plugin_dir, thread_num, stream_num): model_xml = model model_bin = weights if len(device_list) == 1: device = device_list[0] elif len(device_list) == 2: device = 'HETERO:{},{}'.format(device_list[0], device_list[1]) else: log.error('Wrong count devices') sys.exit(1) log.info('Plugin initialization.'); plugin = IEPlugin(device = device, plugin_dirs = plugin_dir) if cpu_extension and 'CPU' in device: plugin.add_cpu_extension(cpu_extension) log.info('Loading network files:\n\t {0}\n\t {1}'.format( model_xml, model_bin)) net = IENetwork(model = model_xml, weights = model_bin) if plugin.device == 'CPU': supported_layers = plugin.get_supported_layers(net) not_supported_layers = [ l for l in net.layers.keys() \ if l not in supported_layers ] if len(not_supported_layers) != 0: log.error('Following layers are not supported by the plugin \ for specified device {0}:\n {1}'.format(plugin.device, ', '.join(not_supported_layers))) log.error('Please try to specify cpu extensions library path in \ sample\'s command line parameters using -l or --cpu_extension \ command line argument') sys.exit(1) if thread_num is not None: if 'CPU' in device_list: plugin.set_config({'CPU_THREADS_NUM': str(thread_num)}) else: log.error('Parameter : Number of threads is used only for CPU') sys.exit(1) if stream_num is not None: if 'CPU' in device_list: plugin.set_config({'CPU_THROUGHPUT_STREAMS': str(stream_num)}) else: log.error('Parameter : Number of streams is used only for CPU') sys.exit(1) if len(device_list) == 2: plugin.set_config({'TARGET_FALLBACK': device}) plugin.set_initial_affinity(net) return net, plugin
def get_openvino_plugin(openvino_network, inference_platform, library_path, cpu_libpath): """ Method used to load IEPlugin according to given target platform :param openvino_network: IENetwork object :param inference_platform: Target Device Plugin name (CPU, GPU, HETERO:MYRIAD,GPU,CPU etc. :param library_path: Lib path to Shared Libraries /opt/intel/openvino/deployment_tools/inference_engine/lib/ubuntu.. :return: IEPlugin object """ openvino_plugin = None # If OpenVINO Selected, Check for Hardware (GPU, MYRIAD or CPU) is supported for this example # Load corresponding device library from the indicated paths, this application requires the environment # variables are already set correctly # source /opt/intel/openvino/bin/setupvars.sh if inference_platform == 'GPU': print('Trying to Load OpenVINO GPU Plugin') openvino_plugin = IEPlugin(device=inference_platform, plugin_dirs=library_path) elif inference_platform == 'MYRIAD': print('Trying to Load OpenVINO Myriad Plugin') openvino_plugin = IEPlugin(device=inference_platform, plugin_dirs=library_path) openvino_plugin.set_config({"VPU_FORCE_RESET": "NO"}) elif inference_platform == 'HETERO:CPU,GPU' or inference_platform == 'HETERO:GPU,CPU': openvino_plugin = IEPlugin(device=inference_platform, plugin_dirs=library_path) openvino_plugin.add_cpu_extension(cpu_libpath) openvino_plugin.set_config({"TARGET_FALLBACK": inference_platform.split(':')[1]}) # Enable graph visualization # openvino_plugin.set_config({"HETERO_DUMP_GRAPH_DOT": "YES"}) openvino_plugin.set_initial_affinity(openvino_network) supported_layers = openvino_plugin.get_supported_layers(openvino_network) print('Supported Layers') # [print(layer) for layer in supported_layers] not_supported_layers = [l for l in openvino_network.layers.keys() if l not in supported_layers] print('UnSupported Layers') # [print(layer) for layer in not_supported_layers] elif inference_platform == 'HETERO:MYRIAD,GPU' or inference_platform == 'HETERO:GPU,MYRIAD': openvino_plugin = IEPlugin(device=inference_platform, plugin_dirs=library_path) openvino_plugin.set_config({"TARGET_FALLBACK": inference_platform.split(':')[1]}) # Enable graph visualization # openvino_plugin.set_config({"HETERO_DUMP_GRAPH_DOT": "YES"}) openvino_plugin.set_initial_affinity(openvino_network) supported_layers = openvino_plugin.get_supported_layers(openvino_network) print('Supported Layers') # [print(layer) for layer in supported_layers] not_supported_layers = [l for l in openvino_network.layers.keys() if l not in supported_layers] print('UnSupported Layers') # [print(layer) for layer in not_supported_layers] elif inference_platform == 'HETERO:MYRIAD,CPU' or inference_platform == 'HETERO:CPU,MYRIAD': openvino_plugin = IEPlugin(device=inference_platform, plugin_dirs=library_path) openvino_plugin.add_cpu_extension(cpu_libpath) openvino_plugin.set_config({"TARGET_FALLBACK": inference_platform.split(':')[1]}) # Enable graph visualization # openvino_plugin.set_config({"HETERO_DUMP_GRAPH_DOT": "YES"}) openvino_plugin.set_initial_affinity(openvino_network) supported_layers = openvino_plugin.get_supported_layers(openvino_network) print('Supported Layers') # [print(layer) for layer in supported_layers] not_supported_layers = [l for l in openvino_network.layers.keys() if l not in supported_layers] print('UnSupported Layers') # [print(layer) for layer in not_supported_layers] elif inference_platform == "CPU": # By default try to load CPU library print('Trying to Load OpenVINO CPU Plugin') openvino_plugin = IEPlugin(device=inference_platform, plugin_dirs=library_path) openvino_plugin.add_cpu_extension(cpu_libpath) else: print('Undefined Target Platform for OpenVINO: {}'.format(inference_platform)) help_menu() exit(-2) return openvino_plugin
from openvino.inference_engine import IENetwork, IEPlugin model_xml = '/opt/intel/computer_vision_sdk_2018.4.420/deployment_tools/model_optimizer/10_lrmodels/UNet/FP16/semanticsegmentation_frozen_person_32.xml' model_bin = '/opt/intel/computer_vision_sdk_2018.4.420/deployment_tools/model_optimizer/10_lrmodels/UNet/FP16/semanticsegmentation_frozen_person_32.bin' net = IENetwork.from_ir(model=model_xml, weights=model_bin) seg_image = Image.open("data/input/009649.png") palette = seg_image.getpalette() # Get a color palette index_void = 2 # Define index_void Back Ground camera_width = 320 camera_height = 240 fps = "" elapsedTime = 0 plugin = IEPlugin(device="HETERO:MYRIAD,CPU") plugin.set_config({"TARGET_FALLBACK": "HETERO:MYRIAD,CPU"}) plugin.set_initial_affinity(net) #plugin = IEPlugin(device="CPU") exec_net = plugin.load(network=net) input_blob = next(iter(net.inputs)) #input_blob = 'input' out_blob = next(iter(net.outputs)) #out_blob = 'output/BiasAdd' n, c, h, w = net.inputs[input_blob].shape #n, c, h, w = 1, 3, 256, 256 del net cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FPS, 30) cap.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height) time.sleep(1)
def main(): log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) args = build_argparser().parse_args() assert args.device.split(':')[0] == "HETERO", "This demo supports only Hetero Plugin. " \ "Please specify correct device, e.g. HETERO:FPGA,CPU" model_xml = args.model model_bin = os.path.splitext(model_xml)[0] + ".bin" # Plugin initialization for specified device and load extensions library if specified plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir) if args.cpu_extension and 'CPU' in args.device: plugin.add_cpu_extension(args.cpu_extension) # Read IR net = IENetwork(model=model_xml, weights=model_bin) if plugin.device == "CPU": supported_layers = plugin.get_supported_layers(net) not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers] if len(not_supported_layers) != 0: log.error("Following layers are not supported by the plugin for specified device {}:\n {}". format(plugin.device, ', '.join(not_supported_layers))) log.error("Please try to specify cpu extensions library path in demo's command line parameters using -l " "or --cpu_extension command line argument") sys.exit(1) net_ops = set([l.type for l in net.layers.values()]) if not any([op == "Convolution" for op in net_ops]): log.warning("Specified IR doesn't contain any Convolution operations for which affinity going to be set.\n" "Try to use another topology to make the affinity setting result more visible.") # Configure the plugin to initialize default affinity for network in set_initial_affinity() function. plugin.set_config({"TARGET_FALLBACK": args.device.split(':')[1]}) # Enable graph visualization plugin.set_config({"HETERO_DUMP_GRAPH_DOT": "YES"}) plugin.set_initial_affinity(net) for l in net.layers.values(): if l.type == "Convolution": l.affinity = "GPU" assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies" assert len(net.outputs) == 1, "Demo supports only single output topologies" input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) # Read and pre-process input image n, c, h, w = net.inputs[input_blob].shape image = cv2.imread(args.input) image = cv2.resize(image, (w, h)) image = image.transpose((2, 0, 1)) # Change data layout from HWC to CHW image = image.reshape((n, c, h, w)) # Load network to the plugin exec_net = plugin.load(network=net) del net # Start sync inference res = exec_net.infer(inputs={input_blob: image}) top_ind = np.argsort(res[out_blob], axis=1)[0, -args.number_top:][::-1] for i in top_ind: log.info("%f #%d" % (res[out_blob][0, i], i)) del exec_net del plugin cwd = os.getcwd() log.info( "Graphs representing default and resulting affinities dumped to {} and {} files respectively" .format(os.path.join(cwd, 'hetero_affinity.dot'), os.path.join(cwd, 'hetero_subgraphs.dot')) )