def test_opencv_frame(): # Create InferServer infer_server = cnis.InferServer(dev_id=0) session_desc = cnis.SessionDesc() session_desc.name = "test_session" session_desc.model = infer_server.load_model(utils.model_dir) # Set OpencvPreproc session_desc.preproc = cnis.PreprocessorHost() session_desc.set_preproc_func(cnis.OpencvPreproc(dst_fmt=cnis.VideoPixelFmt.BGRA, keep_aspect_ratio=False).execute) session_desc.show_perf = False # create synchronous session session = infer_server.create_sync_session(session_desc) # Request opencv frame input_pak = prepare_input() output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS # Request opencv frame with roi input_pak = prepare_input() cv_frame = input_pak.data[0].get_cv_frame() cv_frame.roi = cnis.BoundingBox(x=0, y=0, w=0.5, h=0.5) output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS # Destroy session assert infer_server.destroy_session(session)
def test_python_preprocess(): """Test custom preprocess function (written in python)""" # Create InferServer infer_server = cnis.InferServer(dev_id=0) session_desc = cnis.SessionDesc() session_desc.name = "test_session" # Load model session_desc.model = infer_server.load_model(utils.model_dir) # Create PreprocessorHost and set custom preprocess function to description session_desc.preproc = cnis.PreprocessorHost() custom_python_preproc = CustomPreprocess() session_desc.set_preproc_func(custom_python_preproc.execute) session_desc.show_perf = False # Create synchronous session session = infer_server.create_sync_session(session_desc) # Create a package input_pak = cnis.Package(1, utils.tag) output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS # Request assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS assert custom_python_preproc.called # Destroy session assert infer_server.destroy_session(session)
def test_video_infer_server_request_sync(): # Create VideoInferServer object infer_server = cnis.VideoInferServer(dev_id=0) session_desc = cnis.SessionDesc() session_desc.name = "test_session" # Load model session_desc.model = infer_server.load_model(utils.model_dir) # Create VideoPreprocessorMLU and set parameters. Use CNCV preproc. session_desc.preproc = cnis.VideoPreprocessorMLU() if cnis.get_device_core_version(dev_id=0) == cnis.CoreVersion.MLU270 or \ cnis.get_device_core_version(dev_id=0) == cnis.CoreVersion.MLU220: session_desc.set_preproc_params( cnis.VideoPixelFmt.BGRA, cnis.VideoPreprocessType.CNCV_PREPROC, keep_aspect_ratio=False) else: session_desc.set_preproc_params( cnis.VideoPixelFmt.RGB24, cnis.VideoPreprocessType.CNCV_PREPROC, keep_aspect_ratio=True) session_desc.show_perf = False # Create synchronous session session = infer_server.create_sync_session(session_desc) # Prepare input video_frame video_frame = utils.prepare_video_frame() output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS # Request VideoFrame assert infer_server.request_sync(session, video_frame, utils.tag, status, output) assert status == cnis.Status.SUCCESS # Prepare input video_frame video_frame = utils.prepare_video_frame() input_pak = cnis.Package(1, utils.tag) input_pak.data[0].set(video_frame) # Request input package assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS # Prepare input with bounding boxes video_frame = utils.prepare_video_frame() bbox = [cnis.BoundingBox(x=0, y=0, w=0.5, h=0.5)] * 4 output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS # Request VideoFrame and bounding boxes of it assert infer_server.request_sync(session, video_frame, bbox, utils.tag, status, output) assert status == cnis.Status.SUCCESS # Destroy session infer_server.destroy_session(session)
def request_package(infer_server, session): """Request InferServer to process a package""" # Test request sync video_frame = utils.prepare_video_frame() # create package and set video_frame to it input_pak = cnis.Package(1, utils.tag) input_pak.data[0].set(video_frame) output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS
def prepare_input_and_preproc(self, width, height): """Read image from file. Convert color and resize the image to satisfy the model input""" input_pak = cnis.Package(1, self.tag) img = cv2.imread(cur_file_dir + "/../test/data/test.jpg") if self.core_ver == cnis.CoreVersion.MLU220 or self.core_ver == cnis.CoreVersion.MLU270: resized_img = cv2.resize(img, (width, height)) result_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2BGRA) else: ratio = min(width / img.shape[1], height / img.shape[0]) resized_w = int(ratio * img.shape[1]) resized_h = int(ratio * img.shape[0]) resized_img = cv2.resize(img, (resized_w, resized_h)) delta_w = width - resized_w delta_h = height - resized_h top, bottom = delta_h // 2, delta_h - (delta_h // 2) left, right = delta_w // 2, delta_w - (delta_w // 2) color = [0, 0, 0] new_img = cv2.copyMakeBorder(resized_img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) result_img = cv2.cvtColor(new_img, cv2.COLOR_BGR2RGB) input_pak.data[0].set_user_data({"image_width": img.shape[1], "image_height": img.shape[0]}) input_pak.data[0].set(result_img) return input_pak
def execute(self): """Execyte Inference only demo""" # Create InferServer cnis.bind_device(self.dev_id) infer_server = cnis.InferServer(self.dev_id) # Create session. Sync API session_desc = cnis.SessionDesc() session_desc.name = "test_session_sync" session_desc.engine_num = 1 session_desc.strategy = cnis.BatchStrategy.STATIC session_desc.model = infer_server.load_model(self.model_dir) session = infer_server.create_sync_session(session_desc) width, height = self.get_model_input_wh(session_desc.model) for _ in range(4): # Prepare input and output input_pak = self.prepare_input_and_preproc(width, height) output_pak = cnis.Package(1) # Request status = cnis.Status.SUCCESS ret = infer_server.request_sync(session, input_pak, status, output_pak, timeout=20000) if not ret: print("[EasyDK PythonAPISamples] [InferOnlyDemo] RequestSync failed, ret: {}, status: {}".format( ret, status)) # Postproc and print results if status == cnis.Status.SUCCESS: self.postproc_and_print(output_pak, session_desc.model, 0.6) # Destroy Session infer_server.destroy_session(session)
def test_create_sync_session(): # First of all we need to create an InferServer object infer_server = cnis.InferServer(dev_id=0) # Secondly, to create a synchronous session, a session description is needed. session_desc = cnis.SessionDesc() session_desc.name = "test_session" # Load model and set it to session description session_desc.model = infer_server.load_model(utils.model_dir) session_desc.show_perf = False # create a synchronous session session = infer_server.create_sync_session(session_desc) assert session # Test request sync input_pak = prepare_input(session_desc.model) output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS assert output.perf assert len(output.data) == 1 and output.data[0].get_model_io() # Test get model and unload model assert infer_server.get_model(session) == session_desc.model assert cnis.InferServer.unload_model(session_desc.model) assert not cnis.InferServer.unload_model(session_desc.model) model = infer_server.load_model(utils.model_dir) cnis.InferServer.clear_model_cache() assert not cnis.InferServer.unload_model(model) # destroy session assert infer_server.destroy_session(session)
def request_package(session_desc, infer_server, session): """Create input package and send it to InferServer.""" # Create a package with random data input_pak = cnis.Package(1, utils.tag) input_shape = session_desc.model.input_shape(0) data_size = [input_shape[1], input_shape[2], input_shape[3]] if session_desc.model.input_layout(0).order == cnis.DimOrder.NCHW: data_size = [input_shape[2], input_shape[3], input_shape[1]] input_data = np.random.randint(0, 255, size=data_size, dtype=np.dtype(np.uint8)) input_pak.data[0].set(input_data) output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS # Request assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS
def test_observer(): infer_server = cnis.InferServer(dev_id=0) session_desc = cnis.SessionDesc() session_desc.model = infer_server.load_model(utils.model_dir) session_desc.show_perf = False class TestPostproc(cnis.Postprocess): """To use custom postprocess, we define a class TestPostproc which inherits from cnis.Postprocess. The execute_func API will be called by InferServer to do postprocess. """ def __init__(self): super().__init__() def execute_func(self, result, model_output, model_info): result.set({"key1": "result1", "key2": "result2"}) return True session_desc.postproc = cnis.Postprocessor() session_desc.set_postproc_func(TestPostproc().execute) class TestObserver(cnis.Observer): """To receive results from InferServer, we define a class TestObserver which inherits from cnis.Observer. After a request is sent to InferServer and is processed by InferServer, the response_func API will be called with status, results and user data. """ def __init__(self): super().__init__() self.called = False def response_func(self, status, data, user_data): # Check user data assert "user_data" in user_data assert user_data["user_data"] == "cnis" assert len(data.data) == 1 # Check data result = data.data[0].get_dict() assert "key1" in result assert "key2" in result assert result["key1"] == "result1" assert result["key2"] == "result2" self.called = True obs = TestObserver() session = infer_server.create_session(session_desc, obs) input_pak = cnis.Package(1, utils.tag) if cnis.get_device_core_version(dev_id=0) == cnis.CoreVersion.MLU270 or \ cnis.get_device_core_version(dev_id=0) == cnis.CoreVersion.MLU220: input_pak.data[0].set( np.random.randint(0, 255, size=(300, 300, 4), dtype=np.uint8)) else: input_pak.data[0].set( np.random.randint(0, 255, size=(416, 416, 3), dtype=np.uint8)) infer_server.request(session, input_pak, {"user_data": "cnis"}) infer_server.wait_task_done(session, utils.tag) assert obs.called infer_server.destroy_session(session)
def prepare_input(): """Read image from file. Set OpenCV mat to OpencvFrame.""" cv_frame = cnis.OpencvFrame() img = cv2.imread(cur_file_dir + "/../test/data/test.jpg") cv_frame.img = img cv_frame.fmt = cnis.VideoPixelFmt.BGR24 # Create package with one frame and set cv_frame to it input_pak = cnis.Package(1, utils.tag) input_pak.data[0].set(cv_frame) return input_pak
def prepare_input(model): """Read image from file. Set OpenCV mat to input package.""" img = cv2.imread(cur_file_dir + "/data/test.jpg") # resize to model input shape resized_img = cv2.resize(img, (get_model_input_wh(model))) # convert color to model input pixel format if cnis.get_device_core_version(dev_id=0) == cnis.CoreVersion.MLU270 or \ cnis.get_device_core_version(dev_id=0) == cnis.CoreVersion.MLU220: result_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2BGRA) else: result_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2RGB) # Create input package and set OpenCV mat to input package input_pak = cnis.Package(1, utils.tag) input_pak.data[0].set(result_img) return input_pak
def prepare_opencv_data(self): """Read image from file. Set OpenCV mat to OpencvFrame.""" # Create a video_frame cv_frame = cnis.OpencvFrame() img = cv2.imread(cur_file_dir + "/../test/data/test.jpg") cv_frame.img = img cv_frame.fmt = cnis.VideoPixelFmt.BGR24 # Create package with one frame and set cv_frame to it input_pak = cnis.Package(1, self.tag) input_pak.data[0].set(cv_frame) input_pak.data[0].set_user_data({ "image_width": img.shape[1], "image_height": img.shape[0] }) return input_pak
def prepare_mlu_data(self): """Read image from file. Convert OpenCV mat to VideoFrame (convert color and copy data from cpu to mlu)""" # Prepare MLU data cv_image = cv2.imread(cur_file_dir + "/../test/data/test.jpg") img_width = cv_image.shape[1] img_height = cv_image.shape[0] # Create a video_frame video_frame = cnis.VideoFrame() video_frame.plane_num = 2 video_frame.format = cnis.VideoPixelFmt.NV12 video_frame.width = img_width video_frame.height = img_height video_frame.stride = [video_frame.width, video_frame.width] # Convert image from BGR24 TO YUV NV12 i420_img = cv2.cvtColor(cv_image, cv2.COLOR_BGR2YUV_I420) i420_img = i420_img.reshape(int(img_width * img_height * 3 / 2)) img_y = i420_img[:img_width * img_height] img_uv = i420_img[img_width * img_height:] img_uv.reshape((int(img_width * img_height / 4), 2), order="F").reshape(int(img_width * img_height / 2)) # Create mlu buffer mlu_buffer_y = cnis.Buffer(img_width * img_height, self.dev_id) mlu_buffer_uv = cnis.Buffer(int(img_width * img_height / 2), self.dev_id) # Copy to mlu buffer mlu_buffer_y.copy_from(img_y) mlu_buffer_uv.copy_from(img_uv) # Set buffer to video_frame video_frame.set_plane(0, mlu_buffer_y) video_frame.set_plane(1, mlu_buffer_uv) # Create package and set video_frame to it input_pak = cnis.Package(1, self.tag) input_pak.data[0].set(video_frame) # Set user data to input data, as PostprocYolov3 need to known the image width and height input_pak.data[0].set_user_data({ "image_width": img_width, "image_height": img_height }) return input_pak
def execute(self): """Execyte synchronous demo""" # Create InferServer cnis.bind_device(self.dev_id) infer_server = cnis.InferServer(self.dev_id) # Create session. Sync API session_desc = cnis.SessionDesc() session_desc.name = "test_session_sync" session_desc.engine_num = 1 session_desc.strategy = cnis.BatchStrategy.STATIC session_desc.model = infer_server.load_model(self.model_dir) self.opencv_preproc(session_desc) self.python_postproc(session_desc) session = infer_server.create_sync_session(session_desc) for _ in range(4): # Prepare input and output input_pak = self.prepare_opencv_data() output_pak = cnis.Package(1) # Request status = cnis.Status.SUCCESS ret = infer_server.request_sync(session, input_pak, status, output_pak, timeout=20000) if not ret: print( "[EasyDK PythonAPISamples] [SyncDemo] RequestSync failed, ret: {}, status: {}" .format(ret, status)) if status == cnis.Status.SUCCESS: self.print_result(output_pak) # Destroy Session infer_server.destroy_session(session)
def test_video_infer_server_request_async(): # Create VideoInferServer object infer_server = cnis.VideoInferServer(dev_id=0) session_desc = cnis.SessionDesc() session_desc.name = "test_session" # Load model session_desc.model = infer_server.load_model(utils.model_dir) # Create VideoPreprocessorMLU and set parameters. Use CNCV preproc. session_desc.preproc = cnis.VideoPreprocessorMLU() if cnis.get_device_core_version(dev_id=0) == cnis.CoreVersion.MLU270 or \ cnis.get_device_core_version(dev_id=0) == cnis.CoreVersion.MLU220: session_desc.set_preproc_params( cnis.VideoPixelFmt.BGRA, cnis.VideoPreprocessType.CNCV_PREPROC, keep_aspect_ratio=False) else: session_desc.set_preproc_params( cnis.VideoPixelFmt.RGB24, cnis.VideoPreprocessType.CNCV_PREPROC, keep_aspect_ratio=True) session_desc.show_perf = False # Define a TestObserver class to receive results class TestObserver(cnis.Observer): """To receive results from InferServer, we define a class MyObserver which inherits from cnis.Observer. After a request is sent to InferServer and is processed by InferServer, the response_func API will be called with status, results and user data. """ def __init__(self): super().__init__() self.called = False def response_func(self, status, data, user_data): assert status == cnis.Status.SUCCESS assert "user_data" in user_data assert user_data["user_data"] == "cnis" self.called = True # Create observer obs = TestObserver() # Create asynchronous session session = infer_server.create_session(session_desc, obs) # Prepare input video_frame video_frame = utils.prepare_video_frame() # Create user data user_data = {"user_data": "cnis"} # Request VideoFrame assert infer_server.request(session, video_frame, utils.tag, user_data) # Prepare input package video_frame = utils.prepare_video_frame() input_pak = cnis.Package(1, utils.tag) input_pak.data[0].set(video_frame) # Request package assert infer_server.request(session, input_pak, user_data) # Prepare input with bounding box video_frame = utils.prepare_video_frame() bbox = [cnis.BoundingBox(x=0, y=0, w=0.5, h=0.5)] * 4 # Request VideoFrame and bounding boxes of it assert infer_server.request(session, video_frame, bbox, utils.tag, user_data) infer_server.wait_task_done(session, utils.tag) assert obs.called # Destroy session infer_server.destroy_session(session)
def test_package(): data_num = 4 # Create a Package with 4 data input_pak = cnis.Package(data_num, utils.tag) assert len(input_pak.data) == data_num assert input_pak.tag == utils.tag