def test_mlu_buffer_copy(): """Test copy an array or a buffer to a mlu buffer Test copy a mlu buffer to a buffer """ buf_size = 4 buf = cnis.Buffer(size=buf_size, dev_id=0) # copy from array dtype = np.dtype(np.uint8) src_data = np.random.randint(0, 255, size=buf_size, dtype=dtype) buf.copy_from(src_data) dst_data = np.random.randint(0, 255, size=buf_size, dtype=dtype) buf.copy_to(dst=dst_data) assert dst_data.any() == src_data.any() # copy from buffer dst_buf = cnis.Buffer(buf_size) dst_buf.copy_from(src=buf) dst_buf_data = np.random.randint(0, 255, size=buf_size, dtype=dtype) dst_buf.copy_to(dst=dst_buf_data) assert src_data.any() == dst_buf_data.any() # copy to buffer buf.copy_to(dst=dst_buf) dst_buf.copy_to(dst=dst_buf_data) assert dst_data.any() == dst_buf_data.any()
def test_cpu_buffer_copy_to(): """Test copy cpu buffer to an array or a buffer""" buf_size = 4 buf = cnis.Buffer(size=buf_size) dtype = np.dtype(np.uint8) src_data = np.random.randint(0, 255, size=buf_size, dtype=dtype) buf.copy_from(src_data) # copy to array dst_data = np.random.randint(0, 255, size=buf_size, dtype=dtype) buf.copy_to(dst=dst_data) assert src_data.any() == dst_data.any() # copy to buffer dst_buf = cnis.Buffer(buf_size) buf.copy_to(dst=dst_buf) assert buf.data().any() == dst_buf.data().any()
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 test_buffer(): """Test cpu buffer and mlu buffer""" buf_size = 4 # cpu memory cpu_buf = cnis.Buffer(size=buf_size) assert cpu_buf.memory_size() == buf_size assert cpu_buf.dev_id() < 0 assert cpu_buf.type() == cnis.MemoryType.CPU assert not cpu_buf.on_mlu() assert not cpu_buf.own_memory() # malloc when get data cpu_buf.data() assert cpu_buf.own_memory() # mlu memory mlu_buf = cnis.Buffer(size=buf_size, dev_id=0) assert mlu_buf.memory_size() == buf_size assert mlu_buf.dev_id() == 0 assert mlu_buf.type() == cnis.MemoryType.MLU assert mlu_buf.on_mlu() assert not mlu_buf.own_memory() # malloc when get data mlu_buf.data() assert mlu_buf.own_memory()
def test_video_frame(): # Create a VideoFrame object with a yuv420sp nv12 1920x1080 image. video_frame = cnis.VideoFrame() video_frame.plane_num = 2 video_frame.format = cnis.VideoPixelFmt.NV12 video_frame.width = 1920 video_frame.height = 1080 video_frame.stride = [video_frame.width, video_frame.width] y_size = int(video_frame.stride[0] * video_frame.height) uv_size = int(video_frame.stride[1] * video_frame.height // 2) assert video_frame.get_plane_size(0) == y_size assert video_frame.get_plane_size(1) == uv_size assert video_frame.get_total_size() == y_size + uv_size # Create Buffers to store y and uv data mlu_buffer_y = cnis.Buffer(y_size, 0) mlu_buffer_uv = cnis.Buffer(uv_size, 0) dtype = np.dtype(np.uint8) # Use random numbers as y and uv plane data y_data = np.random.randint(0, 255, size=y_size, dtype=dtype) uv_data = np.random.randint(0, 255, size=uv_size, dtype=dtype) mlu_buffer_y.copy_from(y_data) mlu_buffer_uv.copy_from(uv_data) # Set buffers to VideoFrame object video_frame.set_plane(0, mlu_buffer_y) video_frame.set_plane(1, mlu_buffer_uv) plane_0 = video_frame.get_plane(0) plane_1 = video_frame.get_plane(1) # Modify the y and uv data dst_y_data = np.random.randint(0, 255, size=y_size, dtype=dtype) dst_uv_data = np.random.randint(0, 255, size=uv_size, dtype=dtype) plane_0.copy_to(dst=dst_y_data) plane_1.copy_to(dst=dst_uv_data) assert dst_y_data.any() == y_data.any() assert dst_uv_data.any() == uv_data.any() # set roi video_frame.roi = cnis.BoundingBox(x=0, y=0, w=0.5, h=0.5)
def test_cpu_buffer_copy_from(): """Test copy an array or a buffer to a cpu buffer""" buf_size = 4 buf = cnis.Buffer(size=buf_size) # copy from array dtype = np.dtype(np.uint8) src_data = np.random.randint(0, 255, size=buf_size, dtype=dtype) buf.copy_from(src_data) dst_data = buf.data(dtype=dtype) assert dst_data.any() == src_data.any() # modify data dst_data[0] = 0 assert buf.data(dtype=dtype).any() == dst_data.any() # get reshaped data dst_data = buf.data(shape=[2, buf_size // 2], dtype=dtype) assert dst_data.shape == (2, buf_size // 2) assert buf.data(dtype=dtype).any() == dst_data.any() # copy from buffer dst_buf = cnis.Buffer(buf_size) dst_buf.copy_from(src=buf) assert buf.data(dtype=dtype).any() == dst_buf.data(dtype=dtype).any()
def test_model_io(): # Create ModelIO model_io = cnis.ModelIO() assert len(model_io.buffers) == 0 assert len(model_io.shapes) == 0 data_size = 100 * 100 dtype = np.dtype(np.uint8) buffers = [cnis.Buffer(size=data_size)] * 4 src_data = np.random.randint(0, 255, size=data_size, dtype=dtype) for buffer in buffers: buffer.copy_from(src_data) # Set buffers and shapes to ModelIO model_io.buffers = buffers shapes = [cnis.Shape([1, 1, 100, 100])] * 4 model_io.shapes = shapes assert len(model_io.buffers) == 4 assert len(model_io.shapes) == 4 for buffer in model_io.buffers: dst_data = np.random.randint(0, 255, size=data_size, dtype=dtype) buffer.copy_to(dst_data) assert dst_data.any() == src_data.any() for i, shape in enumerate(model_io.shapes): assert shape == shapes[i]