def __initReidentification(self): """ Initialisierung aller Variablen, die für die Gesichtsreidentifikation benötigt werden. Vorallem wird hier die Inference Engine von OpenVINO initialisiert. """ if self.__path_to_camera == '0': self.__cap = cv2.VideoCapture(0) else: self.__cap = cv2.VideoCapture(self.__path_to_camera) net = IENetwork(model=self.__model_xml, weights=self.__model_bin) net_reid = IENetwork(model=self.__model_reid_xml, weights=self.__model_reid_bin) plugin = IEPlugin(device="CPU") plugin.add_cpu_extension(self.__path_to_cpuextension) plugin_reid = IEPlugin(device="CPU") plugin_reid.add_cpu_extension(self.__path_to_cpuextension) # plugin_reid.set_config(net_reid) self.__exec_net = plugin.load(network=net, num_requests=1) self.__exec_net_reid = plugin_reid.load(network=net_reid, num_requests=1) self.__input_blob = next(iter(net.inputs)) self.__out_blob = next(iter(net.outputs)) # print('network.inputs = ' + str(list(net.inputs))) # print('network.outputs = ' + str(list(net.outputs))) self.__model_n, self.__model_c, self.__model_h, self.__model_w = net.inputs[ self.__input_blob].shape self.__input_blob_reid = next(iter(net_reid.inputs)) self.__out_blob_reid = next(iter(net_reid.outputs)) self.__model_reid_n, self.__model_reid_c, self.__model_reid_h, self.__model_reid_w = net_reid.inputs[ self.__input_blob_reid].shape
def load_network(model_full_path, device, extensions, batch=1, new_shape=None): from openvino.inference_engine import IENetwork, IEPlugin # Read in Graph file (IR) net = IENetwork.from_ir(model=model_full_path + ".xml", weights=model_full_path + ".bin") input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) # Plugin initialization for specified device and load extensions library if needed plugin = IEPlugin(device=device.upper()) if 'extension library' in extensions[device]: plugin.add_cpu_extension(extensions[device]['extension library']) if device.upper() == 'MYRIAD': # TODO: set to true if you want to use multiple NCS devices # plugin.set_config({"VPU_FORCE_RESET": "YES"}) exec_net = plugin.load(network=net) else: net.batch_size = batch exec_net = plugin.load(network=net) # exec_net = plugin.load(network=net, config={'DYN_BATCH_ENABLED': 'YES'}) del net return plugin, exec_net, input_blob, out_blob
class BaseDetection(object): def __init__(self, device, model_xml, detection_of): """MYRIAD device's plugin should be initialized only once, MYRIAD plugin would be failed when creating exec_net RuntimeError: Can not init Myriad device: NC_ERROR """ self.ie = IECore() model_bin = os.path.splitext(model_xml)[0] + ".bin" net = self.ie.read_network(model=model_xml, weights=model_bin) # Load IR model to the plugin logger.info("Reading IR for {}...".format(detection_of)) self._load_ir_to_plugin(device, net, detection_of) def _load_ir_to_plugin(self, device, net, detection_of): global is_myriad_plugin_initialized global myriad_plugin if detection_of == "Person Detection": logger.info("Checking Person Detection network inputs") assert (len(net.input_info.keys()) == 1 ), "Person Detection network should have only one input" assert (len(net.outputs) == 1 ), "Person Detection network should have only one output" self.input_blob = next(iter(net.input_info)) self.out_blob = next(iter(net.outputs)) # Loading model to the plugin logger.info( f"Loading {device} model to the {detection_of} plugin... version:{get_version()}" ) if device == "MYRIAD" and not is_myriad_plugin_initialized: # To prevent MYRIAD Plugin from initializing failed, use IEPlugin Class which is deprecated # "RuntimeError: Can not init Myriad device: NC_ERROR" self.plugin = IEPlugin(device=device, plugin_dirs=None) self.exec_net = self.plugin.load(network=net, num_requests=2) is_myriad_plugin_initialized = True myriad_plugin = self.plugin elif device == "MYRIAD" and is_myriad_plugin_initialized: logger.info(f"device plugin for {device} already initialized") self.plugin = myriad_plugin self.exec_net = self.plugin.load(network=net, num_requests=2) else: self.exec_net = self.ie.load_network(network=net, device_name=device, num_requests=2) self.input_dims = net.input_info[self.input_blob].input_data.shape self.output_dims = net.outputs[self.out_blob].shape logger.info( f"{detection_of} input dims:{self.input_dims} output dims:{self.output_dims}" )
def get_executable_network(model_xml, model_bin): """ Generate executable network and retrieve network IO parameters needed for inference. :param model_xml: Intermediate representation of the network :param model_bin: Intermediate representation of the network weights :return: Network IO parameters and network with its executable """ # Initialize plugin log.info("Initializing plugin for MYRIAD X VPU.") plugin = IEPlugin(device="MYRIAD") # Initialize network log.info("Reading Intermediate Representation.") net = IENetwork(model=model_xml, weights=model_bin) # Initialize IO blobs input_blob = next(iter(net.inputs)) # Load network into IE plugin log.info("Loading Intermediate Representation of {0}.".format(net.name)) exec_net = plugin.load(network=net, num_requests=2) # Get input parameters of the network n, c, h, w = net.inputs[input_blob].shape return (n, c, h, w), input_blob, exec_net, net
def main(): args = build_argparser().parse_args() 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) 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
def __init__(self, model, weights): self._model = model self._weights = weights IE_PLUGINS_PATH = os.getenv("IE_PLUGINS_PATH") if not IE_PLUGINS_PATH: raise OSError("Inference engine plugin path env not found in the system.") plugin = IEPlugin(device="CPU", plugin_dirs=[IE_PLUGINS_PATH]) if (self._check_instruction("avx2")): plugin.add_cpu_extension(os.path.join(IE_PLUGINS_PATH, "libcpu_extension_avx2.so")) elif (self._check_instruction("sse4")): plugin.add_cpu_extension(os.path.join(IE_PLUGINS_PATH, "libcpu_extension_sse4.so")) else: raise Exception("Inference engine requires a support of avx2 or sse4.") network = IENetwork.from_ir(model=self._model, weights=self._weights) supported_layers = plugin.get_supported_layers(network) not_supported_layers = [l for l in network.layers.keys() if l not in supported_layers] if len(not_supported_layers) != 0: raise Exception("Following layers are not supported by the plugin for specified device {}:\n {}". format(plugin.device, ", ".join(not_supported_layers))) self._input_blob_name = next(iter(network.inputs)) self._output_blob_name = next(iter(network.outputs)) self._net = plugin.load(network=network, num_requests=2) input_type = network.inputs[self._input_blob_name] self._input_layout = input_type if isinstance(input_type, list) else input_type.shape
def init_openvino(): CNN_xml = CNN_ov_file_name CNN_bin = os.path.splitext(CNN_xml)[0] + ".bin" plugin = IEPlugin(device=device) if plugin.device == "CPU": for ext in cpu_extensions: plugin.add_cpu_extension(ext) CNN_net = IENetwork(model=CNN_xml, weights=CNN_bin) if plugin.device == "CPU": supported_layers = plugin.get_supported_layers(CNN_net) layers = CNN_net.layers.keys() not_supported_layers = [l for l in layers if l not in supported_layers] if len(not_supported_layers) != 0: print( "Following layers are not supported by the plugin for specified device {}:\n {}" .format(plugin.device, ', '.join(not_supported_layers)), file=sys.stderr) print( "Please try to specify cpu extensions library path in demo's command line parameters using -l " "or --cpu_extension command line argument", file=sys.stderr) sys.exit(1) input_CNN = next(iter(CNN_net.inputs)) output_CNN = next(iter(CNN_net.outputs)) exec_net = plugin.load(network=CNN_net, num_requests=1) del CNN_net return exec_net, input_CNN, output_CNN
def main(args): pp = pprint.PrettyPrinter(indent=4) model = args.model device = args.device image_path = args.image # Loading model model_weights = model + '.bin' model_structure = model + '.xml' # TODO: Load the model model = IENetwork(model_structure, model_weights) plugin = IEPlugin(device=device) net = plugin.load(network=model, num_requests=1) input_name = next(iter(model.inputs)) # Reading and Preprocessing Image input_img = np.load(image_path) input_img = input_img.reshape(1, 28, 28) input_dict = {input_name: input_img} # TODO: Run Inference and print the layerwise performance net.requests[0].infer(input_dict) pp.pprint(net.requests[0].get_perf_counts())
def __init__( self, yolo_model_xml, yolo_model_bin, cpu_extension_file, ): self.num_cls = 20 self.thr = 0.5 yolo_net = IENetwork(model=yolo_model_xml, weights=yolo_model_bin) self.yolo_input_blob = next(iter(yolo_net.inputs)) self.yolo_out_blob = next(iter(yolo_net.outputs)) plugin = IEPlugin(device='CPU') plugin.add_cpu_extension(cpu_extension_file) # else: # plugin = IEPlugin(device='GPU') # plugin = IEPlugin(device='GPU', # plugin_dirs=r'C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\inference_engine\bin\intel64\Release/') exec_yolo_net = plugin.load(network=yolo_net) del yolo_net self.exec_yolo_net = exec_yolo_net
def build_executors(xml_file, bin_file, devices): threads_list = [] image = cv2.imread("images/road.jpeg") dst_shape = (896, 512) input_images = cv2.dnn.blobFromImages([image], 1, dst_shape, swapRB=True) input_images_dict = {"data": input_images} for device in devices: if device == "CPU": plugin = IEPlugin(device) requests_num = benchmark_config["cpu_request_num"] config = {"CPU_THREADS_NUM": "0", "CPU_THROUGHPUT_STREAMS": str(requests_num)} plugin.add_cpu_extension(path_to_cpu_extention) elif device == "MYRIAD": plugin = IEPlugin("HDDL") config = {"LOG_LEVEL": "LOG_INFO", "VPU_LOG_LEVEL": "LOG_INFO"} requests_num = benchmark_config["myriad_request_num"] else: raise ValueError('Unidentified device "{}"!'.format(device)) sys.exit(-1) plugin.set_config(config) ie_network = IENetwork(xml_file, bin_file) exe_network = plugin.load(ie_network, requests_num) infer_executor = InferExecutor(exe_network, input_images_dict) executor_thread = InferExecutorThread(device, infer_executor, running_time) threads_list.append(executor_thread) return threads_list
def build(cls, model_name, model_version, model_xml, model_bin, mapping_config, batch_size_param, shape_param, num_ireq, target_device, plugin_config): plugin = IEPlugin(device=target_device, plugin_dirs=GLOBAL_CONFIG['plugin_dir']) if GLOBAL_CONFIG['cpu_extension'] is not None \ and 'CPU' in target_device: plugin.add_cpu_extension(GLOBAL_CONFIG['cpu_extension']) net = IENetwork(model=model_xml, weights=model_bin) batching_info = BatchingInfo(batch_size_param) shape_info = ShapeInfo(shape_param, net.inputs) if batching_info.mode == BatchingMode.FIXED: net.batch_size = batching_info.batch_size else: batching_info.batch_size = net.batch_size effective_batch_size = batching_info.get_effective_batch_size() logger.debug( "[Model: {}, version: {}] --- effective batch size - {}".format( model_name, model_version, effective_batch_size)) ############################### # Initial shape setup if shape_info.mode == ShapeMode.FIXED: logger.debug("[Model: {}, version: {}] --- Setting shape to " "fixed value: {}".format(model_name, model_version, shape_info.shape)) net.reshape(shape_info.shape) elif shape_info.mode == ShapeMode.AUTO: logger.debug("[Model: {}, version: {}] --- Setting shape to " "automatic".format(model_name, model_version)) net.reshape({}) elif shape_info.mode == ShapeMode.DEFAULT: logger.debug("[Model: {}, version: {}] --- Setting shape to " "default".format(model_name, model_version)) ############################### # Creating free infer requests indexes queue free_ireq_index_queue = queue.Queue(maxsize=num_ireq) for ireq_index in range(num_ireq): free_ireq_index_queue.put(ireq_index) ############################### requests_queue = queue.Queue( maxsize=GLOBAL_CONFIG['engine_requests_queue_size']) exec_net = plugin.load(network=net, num_requests=num_ireq, config=plugin_config) ir_engine = cls(model_name=model_name, model_version=model_version, mapping_config=mapping_config, net=net, plugin=plugin, exec_net=exec_net, batching_info=batching_info, shape_info=shape_info, free_ireq_index_queue=free_ireq_index_queue, num_ireq=num_ireq, requests_queue=requests_queue, target_device=target_device, plugin_config=plugin_config) return ir_engine
def main(path_to_objxml, path_to_objbin, dev, ext, input, labels_map=None): # Load network obj_net = IENetwork(model=path_to_objxml, weights=path_to_objbin) log.info("Loaded network") input_layer = next(iter(obj_net.inputs)) output_layer = next(iter(obj_net.outputs)) # Pre-process image n, c, h, w = obj_net.inputs[input_layer].shape obj_frame = cv2.imread(input) obj_in_frame = cv2.resize(obj_frame, (w, h)) obj_in_frame = obj_in_frame.transpose((2, 0, 1)) obj_in_frame = obj_in_frame.reshape((n, c, h, w)) log.info("Pre-processed image") obj_plugin = IEPlugin(device=dev) if dev == 'CPU': obj_plugin.add_cpu_extension(ext) obj_exec_net = obj_plugin.load(network=obj_net, num_requests=1) log.info("Loaded network into plugin") # Do inference obj_res = obj_exec_net.infer({input_layer: obj_in_frame}) log.info("Inference successful!") obj_det = obj_res[output_layer] initial_w = obj_frame.shape[1] initial_h = obj_frame.shape[0] for obj in obj_det[0][0]: # Draw only objects when probability more than specified threshold if obj[2] > 0.5: xmin = int(obj[3] * initial_w) ymin = int(obj[4] * initial_h) xmax = int(obj[5] * initial_w) ymax = int(obj[6] * initial_h) class_id = int(obj[1]) # Draw box and label\class_id color = (min(class_id * 12.5, 255), min(class_id * 7, 255), min(class_id * 5, 255)) det_label = labels_map[class_id - 1] if labels_map else str(class_id) cv2.rectangle(obj_frame, (xmin, ymin), (xmax, ymax), color, 2) label_and_prob = det_label + ", " + str(obj[2] * 100) + "%" log.info('Detection: ' + label_and_prob) #cv2.putText(obj_frame, label_and_prob, (xmin, ymin - 7), cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1) log.info("Hit q to close the window") # Resizing to maintainable window inHeight = 368 aspect_ratio = initial_w / initial_h inWidth = int(((aspect_ratio*inHeight)*8)//8) obj_frame = cv2.resize(obj_frame, (inWidth, inHeight)) while True: cv2.imshow('Detections', obj_frame) key = cv2.waitKey(1) if (key & 0xFF) == ord('q'): break cv2.destroyAllWindows()
def __init__(self, cpu_lib, detector_xml, detection_threshold): """ Initialize openvino detector, load configuration network and weights :param cpu_lib: :param detector_xml: :param detection_threshold: """ # Plugin initialization for specified device and load extensions library if specified plugin = IEPlugin(device="CPU") plugin.add_cpu_extension(cpu_lib) # Read detector IR detector_bin = os.path.splitext(detector_xml)[0] + ".bin" detector_net = IENetwork.from_ir(model=detector_xml, weights=detector_bin) self.d_in = next(iter(detector_net.inputs)) self.d_out = next(iter(detector_net.outputs)) detector_net.batch_size = 1 # Read and pre-process input images self.d_n, self.d_c, self.d_h, self.d_w = detector_net.inputs[ self.d_in].shape self.d_images = np.ndarray(shape=(self.d_n, self.d_c, self.d_h, self.d_w)) # Loading models to the plugin self.d_exec_net = plugin.load(network=detector_net) self.detection_threshold = detection_threshold
def load_ir_model(model_xml, device, plugin_dir, cpu_extension): model_bin = os.path.splitext(model_xml)[0] + ".bin" # initialize plugin log.info("Initializing plugin for %s device...", device) plugin = IEPlugin(device=device, plugin_dirs=plugin_dir) if cpu_extension and 'CPU' in device: plugin.add_cpu_extension(cpu_extension) # read IR net = IENetwork(model=model_xml, weights=model_bin) if "CPU" in device: supported_layers = plugin.get_supported_layers(net) not_supported_layers = [ l for l in net.layers.keys() if l not in supported_layers ] if not_supported_layers: log.error( "Following layers are not supported by the plugin for specified device %s:\n %s", plugin.device, ', '.join(not_supported_layers)) log.error( "Please try to specify cpu extensions library path in sample's command line parameters using " "--cpu_extension command line argument") sys.exit(1) input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) exec_net = plugin.load(network=net, num_requests=2) shape = net.inputs[input_blob].shape # pylint: disable=E1136 del net return exec_net, plugin, input_blob, out_blob, shape
def main(image, model): model_xml = cnn_models.pose_face_rec_model_xml(model) model_bin = cnn_models.pose_face_rec_model_bin(model) # Plugin initialization for specified device and load extensions library if specified plugin = IEPlugin(device='CPU', plugin_dirs=None) # Read IR net = IENetwork(model=model_xml, weights=model_bin) input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) exec_net = plugin.load(network=net, num_requests=2) # Read and pre-process input image n, c, h, w = net.inputs[input_blob].shape del net in_frame = cv2.resize(image, (w, h)) in_frame = in_frame.transpose( (2, 0, 1)) # Change data layout from HWC to CHW in_frame = in_frame.reshape((n, c, h, w)) exec_net.start_async(request_id=0, inputs={input_blob: in_frame}) # Parse detection results of the current request if exec_net.requests[0].wait(-1) == 0: res = exec_net.requests[0].outputs # print('r:%f\ty:%f\tp%f' % (res['angle_r_fc'][0][0], res['angle_y_fc'][0][0], res['angle_p_fc'][0][0])) return res['angle_r_fc'][0][0], res['angle_y_fc'][0][0], res['angle_p_fc'][ 0][0]
class InferenceEngine(object): def __init__(self, model_bin, model_xml, device): log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) # Plugin initialization for specified device and load extensions library if specified log.info("Initializing plugin for {} device...".format(device)) self.plugin = IEPlugin(device=device) # Read IR log.info("Reading IR...") net = IENetwork(model=model_xml, weights=model_bin) cpu_extension = "/usr/local/lib/libcpu_extension.so" if cpu_extension and 'CPU' in device: self.plugin.add_cpu_extension(cpu_extension) assert len(net.inputs.keys( )) == 1, "This application supports only single input topologies" assert len( net.outputs) == 1, "This application supports only single output topologies" self.input_blob = next(iter(net.inputs)) self.out_blob = next(iter(net.outputs)) log.info("Loading IR to the plugin...") self.exec_net = self.plugin.load(network=net, num_requests=2) self.n, self.c, self.h, self.w = net.inputs[self.input_blob].shape del net self.asynchronous = False self.cur_request_id = 0 self.next_request_id = 1 def submit_request(self, frame, wait=False): in_frame = cv2.resize(frame, (self.w, self.h)) # Change data layout from HWC to CHW in_frame = in_frame.transpose((2, 0, 1)) in_frame = in_frame.reshape((self.n, self.c, self.h, self.w)) if self.asynchronous: self.cur_request_id, self.next_request_id = self.next_request_id, self.cur_request_id self.exec_net.start_async(request_id=self.next_request_id, inputs={ self.input_blob: in_frame}) else: self.exec_net.start_async(request_id=self.cur_request_id, inputs={ self.input_blob: in_frame}) if wait: return self.wait() return True def wait(self): return (self.exec_net.requests[self.cur_request_id].wait(-1) == 0) def fetch_result(self): return self.exec_net.requests[self.cur_request_id].outputs[self.out_blob]
def __init__(self, device, getFrameFunc): self.getFrameFunc = getFrameFunc self.originFrame = None self.newDataAvailable = False if device == 'CPU': model_xml = './models/FP32/mobilenet-ssd.xml' else: model_xml = './models/FP16/mobilenet-ssd.xml' model_bin = os.path.splitext(model_xml)[0] + ".bin" cpu_extension = '/opt/intel/openvino/inference_engine/lib/intel64/libcpu_extension_sse4.so' self.labels = ["None", "plane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "table", "dog", "horse", "motorcycle", "person", "plant", "sheep", "sofa", "train", "monitor"] net = IENetwork(model=model_xml, weights=model_bin) plugin = IEPlugin(device=device) if cpu_extension and 'CPU' in device: plugin.add_cpu_extension(cpu_extension) self.exec_net = plugin.load(net) self.input_blob = next(iter(net.inputs)) self.out_blob = next(iter(net.outputs)) n, c, self.h, self.w = net.inputs[self.input_blob].shape self.detectedObjects = [] self.infer_time = 0 self.inferFPS = 15 processThread = threading.Thread(target=self.inferenceThread) processThread.daemon = True processThread.start()
def main(args): model = args.model device = args.device image_path = args.image # Loading model model_weights = model + '.bin' model_structure = model + '.xml' model = IENetwork(model_structure, model_weights) plugin = IEPlugin(device=device) # Loading network to device net = plugin.load(network=model, num_requests=1) # Get the name of the input node input_name = next(iter(model.inputs)) # Reading and Preprocessing Image image = cv2.imread(image_path) resized_img = cv2.resize(image, (544, 320)) input_img = np.moveaxis(resized_img, -1, 0) # Running Inference in a loop on the same image for _ in range(int(args.iterations)): net.infer({input_name: input_img})
def __init__(self): try: xml_path = os.environ["XML_PATH"] bin_path = os.environ["BIN_PATH"] except KeyError: print("Please set the environment variables XML_PATH, BIN_PATH") sys.exit(1) xml_local_path = GetLocalPath(xml_path) bin_local_path = GetLocalPath(bin_path) print('path object', xml_local_path) CPU_EXTENSION = os.getenv('CPU_EXTENSION', "/usr/local/lib/libcpu_extension.so") plugin = IEPlugin(device='CPU', plugin_dirs=None) if CPU_EXTENSION: plugin.add_cpu_extension(CPU_EXTENSION) net = IENetwork(model=xml_local_path, weights=bin_local_path) self.input_blob = next(iter(net.inputs)) self.out_blob = next(iter(net.outputs)) self.batch_size = net.inputs[self.input_blob].shape[0] self.inputs = net.inputs self.outputs = net.outputs self.exec_net = plugin.load(network=net, num_requests=self.batch_size)
def main(): global start_time image_queue = queue.Queue(maxsize=4) model_dir = os.environ[ 'HOME'] + "/model_downloader/object_detection/common/mobilenet-ssd/caffe/FP16/" model_xml = model_dir + "mobilenet-ssd.xml" model_bin = model_dir + "mobilenet-ssd.bin" plugin = IEPlugin(device="MYRIAD") net = IENetwork(model=model_xml, weights=model_bin) input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) n, c, h, w = net.inputs[input_blob].shape exec_net = plugin.load(network=net, num_requests=2) start_time = time.time() preprocess_thread = None preprocess_thread = threading.Thread(target=image_process_worker, args=(image_queue, n, c, h, w)) preprocess_thread.start() async_infer_worker(exec_net, image_queue, input_blob, out_blob) preprocess_thread.join() print() del exec_net del net del plugin
def init_model(xml, bins): model_xml = xml model_bin = bins # Plugin initialization for specified device and load extensions library if specified plugin = IEPlugin(device='CPU') plugin.add_cpu_extension( 'utils/libcpu_extension_sse4.so') log.info("Reading 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) input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) log.info("Loading IR to the plugin...") exec_nets = plugin.load(network=net, num_requests=2) n, c, h, w = net.inputs[input_blob].shape del net return exec_nets, n, c, w, h, input_blob, out_blob, plugin
def prepare_inference_engine(): """Takes and reads IR(.xml+.bin) from command line,loads device to plugin, initializes input and output blobs, and loads the network to the plugin. Returns: pointers to the loaded network, input of the network, and output of the network """ plugin = IEPlugin(device=FLAGS.d, plugin_dirs=FLAGS.plugin_dirs) model_xml = FLAGS.m model_bin = os.path.splitext(model_xml)[0] + ".bin" net = IENetwork(model=model_xml, weights=model_bin) input_blob = next(iter(net.inputs)) # grap inputs shape and dimensions output_blob = next(iter(net.outputs)) # grab output shape and dimension plugin = IEPlugin(device=FLAGS.d, plugin_dirs=None) net.batch_size = 1 #hardcoding to 1 for now exec_net = plugin.load(network=net) return exec_net, input_blob, output_blob
def main(args): pp = pprint.PrettyPrinter(indent=4) model = args.model device = args.device image_path = args.image # Loading model model_weights = model + '.bin' model_structure = model + '.xml' model = IENetwork(model_structure, model_weights) plugin = IEPlugin(device=device) # Loading network to device net = plugin.load(network=model, num_requests=1) # Get the name of the input node input_name = next(iter(model.inputs)) # Reading and Preprocessing Image input_img = np.load(image_path) input_img = input_img.reshape(1, 28, 28) # Running Inference net.requests[0].infer({input_name: input_img}) pp.pprint(net.requests[0].get_perf_counts())
class YoloNCS(object): _ANCHORS = anchors_for_yolov3() def __init__(self, model_name=None): if model_name is None: model_name = 'ir/frozen_yolo' self.model = model_name + '.xml' self.weights = model_name + '.bin' self.plugin = IEPlugin(device='MYRIAD') log.info('Loading network files:\n\t{}\n\t{}'.format( self.model, self.weights)) self.net = IENetwork(model=self.model, weights=self.weights) log.info('Preparing inputs') self.input_blob = next(iter(self.net.inputs)) self.net.batch_size = 1 log.info('Loading model to the plugin') self.exec_net = self.plugin.load(network=self.net) def predict(self, input_list, confidence_theshold=.6, iou_theshould=.5): batch_predictions = [] get_from = 0 input_size = input_list.shape[2] input_dict = {self.input_blob: input_list} pred_dict = self.exec_net.infer(inputs=input_dict) for preds in pred_dict.values(): preds = np.transpose(preds, [0, 2, 3, 1]) get_to = get_from + 3 batch_predictions.append( region_np(preds, self._ANCHORS[get_from:get_to], input_size)) get_from = get_to batch_predictions = np.concatenate(batch_predictions, axis=1) return predict(batch_predictions, confidence_theshold, iou_theshould)
def load_model(device, model_xml, model_bin): plugin = IEPlugin(device=device, plugin_dirs=None) net = IENetwork(model=model_xml, weights=model_bin) exec_net = plugin.load(network=net) input_blob = next(iter(net.inputs)) output_blob = next(iter(net.outputs)) return exec_net, input_blob, output_blob
def ArrangeNetwork(self): log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) # Plugin initialization for specified device and load extensions library if specified plugin = IEPlugin(device=self.device) if self.cpu_extension and 'CPU' in self.device: plugin.add_cpu_extension(self.cpu_extension) # Read IR net = IENetwork.from_ir(model=self.model_xml, weights=self.model_bin) assert len(net.inputs.keys() ) == 1, "Sample supports only single input topologies" assert len( net.outputs) == 1, "Sample supports only single output topologies" self.input_blob = next(iter(net.inputs)) # Set input size self.input_size = net.inputs[self.input_blob].shape[2:] # Load network to the plugin self.exec_net = plugin.load(network=net) del net # Warmup with last image to avoid caching self.exec_net.infer( inputs={ self.input_blob: np.zeros((1, 3, self.input_size[0], self.input_size[1])) })
def load_ie_model(model_xml, device, plugin_dir, cpu_extension=''): """Loads a model in the Inference Engine format""" model_bin = os.path.splitext(model_xml)[0] + ".bin" # Plugin initialization for specified device and load extensions library if specified plugin = IEPlugin(device=device, plugin_dirs=plugin_dir) if cpu_extension and 'CPU' in device: plugin.add_cpu_extension(cpu_extension) # Read IR log.info("Loading network files:\n\t%s\n\t%s", model_xml, model_bin) net = IENetwork(model=model_xml, weights=model_bin) if "CPU" in plugin.device: supported_layers = plugin.get_supported_layers(net) not_supported_layers = [l for l in net.layers.keys() if l not in supported_layers] if not_supported_layers: log.error("Following layers are not supported by the plugin for specified device %s:\n %s", 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) assert len(net.inputs.keys()) == 1, "Checker supports only single input topologies" assert len(net.outputs) == 1, "Checker supports only single output topologies" log.info("Preparing input blobs") input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) net.batch_size = 1 # Loading model to the plugin log.info("Loading model to the plugin") exec_net = plugin.load(network=net) model = IEModel(exec_net, net.inputs, input_blob, out_blob) del net return model
def IEsetup(model_xml, model_bin, device, verbose=False): start = time() plugin = IEPlugin(device=device, plugin_dirs=None) libcpu = "inference_engine_samples/intel64/Release/lib/libcpu_extension.so" libcpu = os.environ['HOME'] + "/" + libcpu if device == "CPU": plugin.add_cpu_extension(libcpu) net = IENetwork(model=model_xml, weights=model_bin) if verbose: print("* IEsetup", model_bin, "on", device) exec_net = plugin.load(network=net, num_requests=1) input_blob = next(iter(net.inputs)) #input_blob = 'data' model_n, model_c, model_h, model_w = net.inputs[input_blob].shape if verbose: print("network in shape n/c/h/w (from xml)= %d %d %d %d" % (model_n, model_c, model_h, model_w)) if verbose: print("input_blob =", input_blob) out_blobs = [] for out_blob in net.outputs: if verbose: print(" net.outputs[", out_blob, "].shape", net.outputs[out_blob].shape) out_blobs.append(out_blob) if verbose: print("* IEsetup done %.2fmsec" % (1000. * (time() - start))) del net return exec_net, plugin, input_blob, out_blobs
def load (modelTopo, modelWeights): try: net = IENetwork(modelTopo, modelWeights) plugin = IEPlugin(device='MYRIAD') excNet = plugin.load(network=net, num_requests=2) return net, excNet except Exception as e: raise e
def run(): #使用两个线程处理 plugin = IEPlugin(device="MYRIAD") net = IENetwork(model=model_xml, weights=model_bin) input_blob = next(iter(net.inputs)) out_blob = next(iter(net.outputs)) n, c, h, w = net.inputs[input_blob].shape exec_net = plugin.load(network=net, num_requests=1) del net imgDetect = ImgDetect() vid = cv2.VideoCapture(0) # Get video information fps = int(vid.get(cv2.CAP_PROP_FPS)) print( "input fps is : *****************************" + str(fps)) width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) command.append('ffmpeg') #command.append("-rtsp_transport") #command.append("tcp") command.append('-y') command.append('-f') command.append('rawvideo') command.append('-vcodec') command.append('rawvideo') command.append('-pix_fmt') command.append('bgr24') command.append('-s') command.append("{}x{}".format(width, height)) command.append('-r') command.append(str(fps)) command.append('-i') command.append('-') command.append('-c:v') command.append('libx264') command.append('-pix_fmt') command.append('yuv420p') command.append('-preset') command.append('ultrafast') command.append('-f') command.append('flv') command.append(rtmpUrl) thread1 = Thread(target=Video, args=(vid, imgDetect, n, c, h, w, exec_net, input_blob, out_blob)) thread1.start() thread2 = Thread(target=push_frame, args=(imgDetect, n, c, h, w, exec_net, input_blob, out_blob)) thread2.start() del exec_net del plugin
def run_ie_on_dataset(model_xml, model_bin, cpu_extension_path, images_dir, prob_threshold=0.01): plugin = IEPlugin(device='CPU') plugin.add_cpu_extension(cpu_extension_path) net = IENetwork.from_ir(model=model_xml, weights=model_bin) 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)) exec_net = plugin.load(network=net, num_requests=2) num, chs, height, width = net.inputs[input_blob] del net cur_request_id = 0 detection_data = [] for image in os.listdir(images_dir): im_path = os.path.join(images_dir, image) frame = cv2.imread(im_path) initial_h, initial_w, _ = frame.shape in_frame = cv2.resize(frame, (width, height)) in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW in_frame = in_frame.reshape((num, chs, height, width)) objects_per_image = [] exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame}) if exec_net.requests[cur_request_id].wait(-1) == 0: res = exec_net.requests[cur_request_id].outputs[out_blob] for obj in res[0][0]: if obj[2] > prob_threshold: xmin = int(obj[3] * initial_w) ymin = int(obj[4] * initial_h) xmax = int(obj[5] * initial_w) ymax = int(obj[6] * initial_h) class_id = int(obj[1]) conf = obj[2] objects_per_image.append({'bbox': [xmin, ymin, xmax, ymax], 'class_id': class_id, 'score': conf}) det_item = {'image': im_path, 'objects': objects_per_image} detection_data.append(det_item) del exec_net del plugin return detection_data
def greengrass_object_detection_sample_ssd_run(): client.publish(topic=PARAM_TOPIC_NAME, payload="OpenVINO: Initializing...") model_bin = os.path.splitext(PARAM_MODEL_XML)[0] + ".bin" # Plugin initialization for specified device and load extensions library if specified plugin = IEPlugin(device=PARAM_DEVICE, plugin_dirs="") if "CPU" in PARAM_DEVICE: plugin.add_cpu_extension(PARAM_CPU_EXTENSION_PATH) # Read IR net = IENetwork(model=PARAM_MODEL_XML, weights=model_bin) client.publish(topic=PARAM_TOPIC_NAME, payload="OpenVINO: Read IR...") 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].shape cap = cv2.VideoCapture(PARAM_INPUT_SOURCE) exec_net = plugin.load(network=net) del net client.publish(topic=PARAM_TOPIC_NAME, payload="Starting inference on %s" % PARAM_INPUT_SOURCE) start_time = timeit.default_timer() inf_seconds = 0.0 frame_count = 0 labeldata = None if PARAM_LABELMAP_FILE is not None: with open(PARAM_LABELMAP_FILE) as labelmap_file: labeldata = json.load(labelmap_file) while (cap.isOpened()): ret, frame = cap.read() if not ret: break frameid = cap.get(cv2.CAP_PROP_POS_FRAMES) initial_w = cap.get(3) initial_h = cap.get(4) in_frame = cv2.resize(frame, (w, h)) in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW in_frame = in_frame.reshape((n, c, h, w)) # Start synchronous inference inf_start_time = timeit.default_timer() res = exec_net.infer(inputs={input_blob: in_frame}) inf_seconds += timeit.default_timer() - inf_start_time # Parse detection results of the current request res_json = OrderedDict() frame_timestamp = datetime.datetime.now() object_id = 0 for obj in res[out_blob][0][0]: if obj[2] > 0.5: xmin = int(obj[3] * initial_w) ymin = int(obj[4] * initial_h) xmax = int(obj[5] * initial_w) ymax = int(obj[6] * initial_h) cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (255, 165, 20), 4) obj_id = "Object" + str(object_id) classlabel = labeldata[str(int(obj[1]))] if labeldata else "" res_json[obj_id] = {"label": int(obj[1]), "class": classlabel, "confidence": round(obj[2], 2), "xmin": round( obj[3], 2), "ymin": round(obj[4], 2), "xmax": round(obj[5], 2), "ymax": round(obj[6], 2)} object_id += 1 frame_count += 1 # Measure elapsed seconds since the last report seconds_elapsed = timeit.default_timer() - start_time if seconds_elapsed >= reporting_interval: res_json["timestamp"] = frame_timestamp.isoformat() res_json["frame_id"] = int(frameid) res_json["inference_fps"] = frame_count / inf_seconds start_time = timeit.default_timer() report(res_json, frame) frame_count = 0 inf_seconds = 0.0 client.publish(topic=PARAM_TOPIC_NAME, payload="End of the input, exiting...") del exec_net del plugin
def main(): log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) args = build_argparser().parse_args() model_xml = args.model model_bin = os.path.splitext(model_xml)[0] + ".bin" # Plugin initialization for specified device and load extensions library if specified log.info("Initializing plugin for {} device...".format(args.device)) 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 log.info("Reading IR...") net = IENetwork.from_ir(model=model_xml, weights=model_bin) if "CPU" in plugin.device: 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 sample's command line parameters using -l " "or --cpu_extension command line argument") sys.exit(1) 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)) log.info("Loading IR to the plugin...") exec_net = plugin.load(network=net, num_requests=2) # Read and pre-process input image n, c, h, w = net.inputs[input_blob] del net predictions = [] data = Input(args.input_type, args.input) cur_request_id = 0 fps = 25 out_width = 640 out_height = 480 if args.dump_output_video: fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(args.path_to_output_video, fourcc, fps, (int(out_width), int(out_height))) while not data.is_finished(): frame, img_id = data.get_next_item() initial_h, initial_w, channels = frame.shape in_frame = cv2.resize(frame, (w, h)) in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW in_frame = in_frame.reshape((n, c, h, w)) exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame}) if exec_net.requests[cur_request_id].wait(-1) == 0: # Parse detection results of the current request res = exec_net.requests[cur_request_id].outputs[out_blob] coco_detections = [] for obj in res[0][0]: # Draw only objects when probability more than specified threshold if obj[2] > args.prob_threshold: x1 = float(obj[3] * initial_w) y1 = float(obj[4] * initial_h) x2 = float(obj[5] * initial_w) y2 = float(obj[6] * initial_h) x_, y_ = round(x1, 1), round(y1, 1) w_ = round(x2 - x1, 1) h_ = round(y2 - y1, 1) class_id = int(obj[1]) coco_det = {} coco_det['image_id'] = img_id coco_det['category_id'] = class_id coco_det['bbox'] = [x_, y_, w_, h_] coco_det['score'] = round(float(obj[2]), 1) coco_detections.append(coco_det) # Draw box and label\class_id cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 0), 2) cv2.putText(frame, str(class_id) + ' ' + str(round(obj[2] * 100, 1)) + ' %', (int(x1), int(y1) - 7), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) predictions.extend(coco_detections) if args.dump_output_video: img_resized = cv2.resize(frame, (out_width, out_height)) out.write(img_resized) if args.show: cv2.imshow("Detection Results", frame) key = cv2.waitKey(10) if key == 27: break if args.dump_predictions_to_json: with open(args.output_json_path, 'w') as output_file: json.dump(predictions, output_file, sort_keys=True, indent=4) cv2.destroyAllWindows() del exec_net del plugin