diff --git a/docs/sail_api/1.基础函数.md b/docs/sail_api/1.基础函数.md new file mode 100644 index 0000000..9c11a74 --- /dev/null +++ b/docs/sail_api/1.基础函数.md @@ -0,0 +1,75 @@ +# sail 基础函数 + +## get_available_tpu_num + +获取当前设备中可用的TPU数量 + +**接口形式:** +```python +def get_available_tpu_num() -> int +``` + +**返回值说明**: + +返回当前设备中可用 TPU的数量 + +## set_print_flag + +设置是否打印程序的计算耗时信息 + +**接口形式:** + +```python +def set_print_flag(print_flag: bool) -> None +``` + +**参数说明** + +- print_flag:bool + +print_flag 为 True 时,打印程序的计算主要的耗时信息,否则不打印 + +## set_dump_io_flag + +设置是否存储输入时间和输出时间。 + +**接口形式:** + +```python +def set_dump_io_flag(dump_io_flag: bool) -> None +``` + +**参数说明** + +- dump_io_flag: bool + +dump_io_flag为True时,存储输入数据和输出数据,否则不存储。 + +## set_decoder_env + +设置Decoder的环境变量,必须在Decoder构造前设置,否则使用默认值。 + +**接口形式:** + +```python +def set_decoder_env(env_name: str, env_value: str) -> None +``` + +**参数说明:** + +- env_name: str + + 选择设置Decoder的属性名称,可选的属性名称有: + + - ‘refcounted_frames’ 设置为1时,解码出来的图像需要程序手动释放,为0时由Decoder自动释放。 + - ‘extra_frame_buffer_num’ 设置Decoder的最大缓存帧数 + - ‘rtsp_transport’ 设置RTSP采用的传输协议 + - ‘stimeout’ 设置阻塞超时时间 + - ‘rtsp_flags’ 设置RTSP是否自定义IO + - ‘buffer_size’ 设置缓存大小 + - ‘max_delay’ 设置最大时延 + - ‘probesize’ 解析文件时读取的最大字节数 + - ‘analyzeduration’ 解析文件时读取的最大时长 + +- env_value: str + diff --git a/docs/sail_api/2.sail数据类型.md b/docs/sail_api/2.sail数据类型.md new file mode 100644 index 0000000..6f30f4f --- /dev/null +++ b/docs/sail_api/2.sail数据类型.md @@ -0,0 +1,21 @@ +# sail 数据类型 + +定义sophon环境中常用的数据类型 + +**接口形式** + +```python +sail.Dtype.BM_FLOAT32 +sail.Dtype.BM_INT8 +sail.Dtype.BM_UINT8 +sail.Dtype.BM_INT32 +sail.Dtype.BM_UINT32 +``` + +**参数说明** + +- sail.Dtype.BM_FLOAT32 数据类型为float32 +- sail.Dtype.BM_INT8 数据类型为int8 +- sail.Dtype.BM_UINT8 数据类型为uint8 +- sail.Dtype.BM_INT32 数据类型为int32 +- sail.Dtype.BM_UINT32 数据类型为uint32 diff --git a/docs/sail_api/3.填充属性.md b/docs/sail_api/3.填充属性.md new file mode 100644 index 0000000..0335750 --- /dev/null +++ b/docs/sail_api/3.填充属性.md @@ -0,0 +1,156 @@ +# 填充属性 + +PaddingAtrr中存储了数据padding的各项属性,可通过配置PaddingAtrr进行数据填充 + +## \_\_init\_\_ + +初始化 PaddingAtrr + +**接口形式:** + +```python +def __init__(self) +def __init__(self, stx: int, sty: int, width: int, height: int, r: int, g: int, b: int) +``` + +**参数说明:** + +- stx: int + +原图像相对于目标图像在x方向上的偏移量 + +- sty: int + +原图像相对于目标图像在y方向上的偏移量 + +- width: int + +在 padding 的同时可对原图像进行 resize,width 为原图像 resize 后的宽,若不进行 resize,则 width 为原图像的宽 + +- height: int + +在 padding 的同时对原图像进行 resize,height 为原图像 resize 后的高,若不进行 resize,则 height 为原图像的高 + +- r: int + +padding 时在 R 通道上填充的像素值 + +- g: int + +padding 时在 G 通道上填充的像素值 + +- b: int + +padding 时在B通道上填充的像素值 + +## set_stx + +设置原图像相对于目标图像在 x 方向上的偏移量 + +**接口形式** + +```python +def set_stx(self, stx: int) -> None +``` + +**参数说明** + +- stx: int + +原图像相对于目标图像在x方向上的偏移量 + +## set_sty + +设置原图像相对于目标图像在y方向上的偏移量 + +**接口形式:** + +```python +def set_sty(self, sty: int) -> None +``` + +**参数说明:** + +- sty: int + +原图像相对于目标图像在y方向上的偏移量 + +## set_w + +设置原图像resize后的width + +**接口形式:** + +```python +def set_w(self, width: int) -> None +``` + +**参数说明:** + +- width: int + +在padding的同时可对原图像进行resize,width为原图像resize后的宽,若不进行resize,则width为原图像的宽 + +## set_h + +设置原图像resize后的height + +**接口形式:** + +```python +def set_h(self, height: int) -> None +``` + +**参数说明:** + +- height: int + +在padding的同时可对原图像进行resize,height为原图像resize后的高,若不进行resize,则height为原图像的高 + +## set_r + +设置R通道上的padding值 + +**接口形式:** + +```python +def set_r(self, r: int) -> None +``` + +**参数说明** + +- r: int + +R通道上的padding值 + +## set_g + +设置G通道上的padding值 + +**接口形式:** + +```python +def set_g(self, g: int) -> None +``` + +**参数说明** + +- g: int + +G通道上的padding值 + +## set_b + +设置B通道上的padding值 + +**接口形式:** + +```python +def set_b(self, b: int) -> None +``` + +**参数说明** + +- b: int + +B通道上的padding值 diff --git a/docs/sail_api/4.设备句柄.md b/docs/sail_api/4.设备句柄.md new file mode 100644 index 0000000..d869765 --- /dev/null +++ b/docs/sail_api/4.设备句柄.md @@ -0,0 +1,51 @@ +# 设备句柄 + +Handle是设备句柄的包装类,在程序中用于设备的标识。 + +## \_\_init\_\_ + +初始化 Handle + +**接口形式:** + +```python +def __init__(self, tpu_id: int) +``` + +**参数说明:** + +- tpu_id: int + +创建 Handle 使用的TPU的id号 + +## get_device_id + +获取 Handle 中 TPU的id + +**接口形式:** + +```python +def get_device_id(self) -> int +``` + +**返回值说明:** + +- tpu_id: int + +Handle 中的 TPU 的 id 号 + +## get_sn + +获取 Handle 中标识设备的序列码 + +**接口形式:** + +```python +def get_sn(self) -> str +``` + +**返回值说明** + +- serial_number: str + +返回 Handle 中设备的序列码 diff --git a/docs/sail_api/5.resize插值策略.md b/docs/sail_api/5.resize插值策略.md new file mode 100644 index 0000000..5a0a752 --- /dev/null +++ b/docs/sail_api/5.resize插值策略.md @@ -0,0 +1,17 @@ +# resize 插值策略(sail.bmcv_resize_algorithm) + +定义图像resize中常见的插值策略 + +**接口形式** + +```python +sail.bmcv_resize_algorithm.BMCV_INTER_NEAREST +sail.bmcv_resize_algorithm.BMCV_INTER_LINEAR +sail.bmcv_resize_algorithm.BMCV_INTER_BICUBIC +``` + +**参数说明** + +- sail.bmcv_resize_algorithm.BMCV_INTER_NEAREST 最近邻插值算法 +- sail.bmcv_resize_algorithm.BMCV_INTER_LINEAR 双线性插值算法 +- sail.bmcv_resize_algorithm.BMCV_INTER_BICUBIC 双三次插值算法 \ No newline at end of file diff --git a/docs/sail_api/6.Tensor张量.md b/docs/sail_api/6.Tensor张量.md new file mode 100644 index 0000000..f065c10 --- /dev/null +++ b/docs/sail_api/6.Tensor张量.md @@ -0,0 +1,240 @@ +# Tensor 张量 + +Tensor 是模型推理的输入输出类型,包含了数据信息,实现内容管理 + +## \_\_init\_\_ + +初始化 Tensor,并为 Tensor 分配内存 + +**接口形式1** + +```python +def __init__(self, handle: Handle, data: np.arrat, own_sys_data=True) +``` + +**参数说明1** + +- handle: sail.Handle + +设备标识 Handle + +- array_data: numpy.array + +利用 numpy.array 类型初始化 Tensor,其数据类型可以是 np.float32,np.int8,np.uint8 + +- own_sys_data:bool + +指示该 Tensor 是否拥有 system memory,如果为 False,则直接将数据复制到 device memory + +**接口形式2** + +```python +def __init__(self, handle: Handle, shape: tuple, dtype: Dtype, own_sys_data: bool, own_dev_data: bool) +``` + +**参数说明2** + +- handle: sail.Handle + +设备标识 Handle + +- shape: tuple + +设置 Tensor 的 shape + +- dtype: sail.Dtype + +Tensor 的数据类型 + +- own_sys_data: bool + +指示 Tensor 是否拥有 system memory + +- own_dev_data: bool + +指示 Tensor 是否拥有 device memory + +## shape + +获取 Tensor 的 shape + +**接口形式:** + +```python +def shape(self) -> list : +``` + +**返回值说明:** + +- tensor_shape : list + +返回Tensor的shape的列表。 + +## asnumpy + +获取Tensor中系统内存的数据,返回numpy.array类型。 + +**接口形式:** +```python +def asnumpy(self) -> numpy.array + +def asnumpy(self, shape: tuple) -> numpy.array +``` + +**参数说明:** + +- shape: tuple + +可对Tensor中的数据reshape,返回形状为shape的numpy.array + +**返回值说明** + +返回Tensor中系统内存的数据,返回类型为numpy.array。 + +## update_data + +更新Tensor中系统内存的数据 + +**接口形式:** + +```python +def update_data(self, data: numpy.array) -> None +``` + +**参数说明:** + +- data: numpy.array + +更新的数据,数据size不能超过Tensor的size,Tensor的shape将保持不变。 + +## scale_from + +先对data按比例缩放,再将数据更新到Tensor的系统内存。 + +**接口形式:** + +```python +def scale_from(self, data: numpy.array, scale: float32)->None +``` + +**参数说明:** + +- data: numpy.array + +对data进行scale,再将数据更新到Tensor的系统内存。 + +- scale: float32 + +等比例缩放时的尺度。 + +## scale_to + +先对Tensor进行等比例缩放,再将数据返回到系统内存。 + +**接口形式:** + +```python +def scale_to(self, scale: float32)->numpy.array + +def scale_to(self, scale: float32, shape: tuple)->numpy.array +``` + +**参数说明:** + +- scale: float32 + +等比例缩放时的尺度。 + +- shape: tuple + +数据返回前可进行reshape,返回shape形状的数据。 + +**返回值说明:** + +- data: numpy.array + +将处理后的数据返回至系统内存,返回numpy.array + +## reshape + +对Tensor进行reshape + +**接口形式:** + +```python +def reshape(self, shape: list)->None +``` + +**参数说明:** + +- shape: list + +设置期望得到的新shape。 + +# own_sys_data + +查询该Tensor是否拥有系统内存的数据指针。 + +**接口形式:** + +```python +def own_sys_data(self)->bool +``` + +**返回值说明:** + +- judge_ret: bool + +如果拥有系统内存的数据指针则返回True,否则False。 + +## own_dev_data + +查询该Tensor是否拥有设备内存的数据 + +**接口形式:** + +```python +def own_dev_data(self)->bool +``` + +**返回值说明:** + +- judge_ret : bool + +如果拥有设备内存中的数据则返回True,否则False。 + +## sync_s2d + +将Tensor中的数据从系统内存拷贝到设备内存。 + +**接口形式:** + +```python +def sync_s2d(self)->None + +def sync_s2d(self, size)->None +``` + +**参数说明:** + +- size: int + +将特定size字节的数据从系统内存拷贝到设备内存。 + +## sync_d2s + +将Tensor中的数据从设备内存拷贝到系统内存。 + +**接口形式:** + +```python +def sync_d2s(self)->None + +def sync_d2s(self, size: int)->None +``` + +**参数说明:** + +- size: int + +将特定size字节的数据从设备内存拷贝到系统内存。 diff --git a/main.py b/main.py index 28679cf..739dda1 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,6 @@ from sophon_ai_project.yolov8_bmcv import main from sophon_ai_project.yolov8_bmcv import argsparser -# def main(): -# print("Hello from sophon-ai-project!") - if __name__ == "__main__": args = argsparser() diff --git a/src/sophon_ai_project/postprocess_numpy.py b/src/sophon_ai_project/postprocess_numpy.py index ee82d4b..dcd2f89 100644 --- a/src/sophon_ai_project/postprocess_numpy.py +++ b/src/sophon_ai_project/postprocess_numpy.py @@ -86,7 +86,7 @@ class pseudo_torch_nms: ovr = inter / (areas[i] + areas[order[1:]] - inter) inds = np.where(ovr <= iou_thres)[0] - order = order[inds + 1] + order = order[inds + 1] # type: ignore keep = np.array(keep) return keep diff --git a/src/sophon_ai_project/yolov8_bmcv.py b/src/sophon_ai_project/yolov8_bmcv.py index 3dbda75..1fb4558 100644 --- a/src/sophon_ai_project/yolov8_bmcv.py +++ b/src/sophon_ai_project/yolov8_bmcv.py @@ -20,7 +20,7 @@ logging.basicConfig(level=logging.INFO) class YOLOv8: def __init__(self, args): # load bmodel - self.net = sail.Engine(args.bmodel, args.dev_id, sail.IOMode.SYSO) + self.net = sail.Engine(args.bmodel, args.dev_id, sail.IOMode.SYSO) # type: ignore logging.debug("load {} success!".format(args.bmodel)) self.handle = sail.Handle(args.dev_id) self.bmcv = sail.Bmcv(self.handle) @@ -42,7 +42,7 @@ class YOLOv8: output_shape = self.net.get_output_shape(self.graph_name, output_name) output_dtype = self.net.get_output_dtype(self.graph_name, output_name) output_scale = self.net.get_output_scale(self.graph_name, output_name) - output = sail.Tensor(self.handle, output_shape, output_dtype, True, True) + output = sail.Tensor(self.handle, output_shape, output_dtype, True, True) # type: ignore self.output_tensors[output_name] = output self.output_scales[output_name] = output_scale if(output_shape[1] < output_shape[2]): @@ -87,13 +87,13 @@ class YOLOv8: def preprocess_bmcv(self, input_bmimg): rgb_planar_img = sail.BMImage(self.handle, input_bmimg.height(), input_bmimg.width(), - sail.Format.FORMAT_RGB_PLANAR, sail.DATA_TYPE_EXT_1N_BYTE) - self.bmcv.convert_format(input_bmimg, rgb_planar_img) + sail.Format.FORMAT_RGB_PLANAR, sail.DATA_TYPE_EXT_1N_BYTE) # type: ignore + self.bmcv.convert_format(input_bmimg, rgb_planar_img) # type: ignore resized_img_rgb, ratio, txy = self.resize_bmcv(rgb_planar_img) - preprocessed_bmimg = sail.BMImage(self.handle, self.net_h, self.net_w, sail.Format.FORMAT_RGB_PLANAR, self.img_dtype) + preprocessed_bmimg = sail.BMImage(self.handle, self.net_h, self.net_w, sail.Format.FORMAT_RGB_PLANAR, self.img_dtype) # type: ignore self.bmcv.convert_to(resized_img_rgb, preprocessed_bmimg, ((self.ab[0], self.ab[1]), \ (self.ab[2], self.ab[3]), \ - (self.ab[4], self.ab[5]))) + (self.ab[4], self.ab[5]))) # type: ignore return preprocessed_bmimg, ratio, txy def resize_bmcv(self, bmimg): @@ -117,7 +117,7 @@ class YOLOv8: ratio = (r, r) txy = (tx1, ty1) - attr = sail.PaddingAtrr() + attr = sail.PaddingAtrr() # type: ignore attr.set_stx(int(round(tx1 - 0.1))) attr.set_sty(int(round(ty1 - 0.1))) attr.set_w(tw) @@ -127,7 +127,7 @@ class YOLOv8: attr.set_b(114) preprocess_fn = self.bmcv.vpp_crop_and_resize_padding if self.use_vpp else self.bmcv.crop_and_resize_padding - resized_img_rgb = preprocess_fn(bmimg, 0, 0, img_w, img_h, self.net_w, self.net_h, attr, sail.bmcv_resize_algorithm.BMCV_INTER_LINEAR) + resized_img_rgb = preprocess_fn(bmimg, 0, 0, img_w, img_h, self.net_w, self.net_h, attr, sail.bmcv_resize_algorithm.BMCV_INTER_LINEAR) # type: ignore else: r_w = self.net_w / img_w r_h = self.net_h / img_h @@ -145,7 +145,7 @@ class YOLOv8: Returns: """ input_tensors = {self.input_name: input_tensor} - self.net.process(self.graph_name, input_tensors, self.input_shapes, self.output_tensors) + self.net.process(self.graph_name, input_tensors, self.input_shapes, self.output_tensors) # type: ignore outputs_dict = {} for name in self.output_names: # outputs_dict[name] = self.output_tensors[name].asnumpy()[:img_num] * self.output_scales[name] @@ -175,8 +175,8 @@ class YOLOv8: ratio_list.append(ratio) txy_list.append(txy) - input_tensor = sail.Tensor(self.handle, self.input_shape, self.input_dtype, False, False) - self.bmcv.bm_image_to_tensor(preprocessed_bmimg, input_tensor) + input_tensor = sail.Tensor(self.handle, self.input_shape, self.input_dtype, False, False) # type: ignore + self.bmcv.bm_image_to_tensor(preprocessed_bmimg, input_tensor) # type: ignore else: BMImageArray = eval('sail.BMImageArray{}D'.format(self.batch_size)) @@ -190,8 +190,8 @@ class YOLOv8: ratio_list.append(ratio) txy_list.append(txy) bmimgs[i] = preprocessed_bmimg.data() - input_tensor = sail.Tensor(self.handle, self.input_shape, self.input_dtype, False, False) - self.bmcv.bm_image_to_tensor(bmimgs, input_tensor) + input_tensor = sail.Tensor(self.handle, self.input_shape, self.input_dtype, False, False) # type: ignore + self.bmcv.bm_image_to_tensor(bmimgs, input_tensor) # type: ignore start_time = time.time() outputs = self.predict(input_tensor, img_num) @@ -216,13 +216,13 @@ def draw_bmcv(dev_id, image, boxes, output_img_dir, file_name, cn, masks=None, c if (x2 - x1) <= thickness * 2 or (y2 - y1) <= thickness * 2: logging.info("width or height too small, this rect will not be drawed: (x1={},y1={},w={},h={})".format(x1, y1, x2-x1, y2-y1)) else: - bmcv.rectangle(img_bgr_planar, x1, y1, (x2 - x1), (y2 - y1), color, thickness) + bmcv.rectangle(img_bgr_planar, x1, y1, (x2 - x1), (y2 - y1), color, thickness) # type: ignore # bmcv.putText(image, COCO_CLASSES[int(classes_ids[idx] + 1)], x1, y1, tuple(color),1.0,1) - logging.debug("class id={}, score={}, (x1={},y1={},w={},h={})".format(int(classes_ids[idx]), conf_scores[idx], x1, y1, x2-x1, y2-y1)) + logging.debug("class id={}, score={}, (x1={},y1={},w={},h={})".format(int(classes_ids[idx]), conf_scores[idx], x1, y1, x2-x1, y2-y1)) # type: ignore if isvideo: return img_bgr_planar else: - bmcv.imwrite(os.path.join(output_img_dir, file_name), img_bgr_planar) + bmcv.imwrite(os.path.join(output_img_dir, file_name), img_bgr_planar) # type: ignore def main(args): # check params @@ -266,8 +266,8 @@ def main(args): # decode start_time = time.time() decoder = sail.Decoder(img_file, True, args.dev_id) - bmimg = sail.BMImage() - ret = decoder.read(yolov8_handle, bmimg) + bmimg = sail.BMImage() # type: ignore + ret = decoder.read(yolov8_handle, bmimg) # type: ignore if ret != 0: logging.error("{} decode failure.".format(img_file)) continue @@ -331,9 +331,9 @@ def main(args): yolov8_handle = sail.Handle(args.dev_id) end_flag = False while not end_flag: - frame = sail.BMImage() + frame = sail.BMImage() # type: ignore start_time = time.time() - ret = decoder.read(yolov8_handle, frame) + ret = decoder.read(yolov8_handle, frame) # type: ignore decode_time += time.time() - start_time if ret: #differ from cv. end_flag = True @@ -356,7 +356,7 @@ def main(args): classes_ids=det_draw[:, -1], conf_scores=det_draw[:, -2], isvideo=True) - encoder.video_write(drawed_bmimg) + encoder.video_write(drawed_bmimg) # type: ignore frame_list.clear() decoder.release() encoder.release()