From e72444a18c18376f07c0fdec64daf1c5e6fe2803 Mon Sep 17 00:00:00 2001 From: cvhub Date: Fri, 6 Sep 2024 23:18:58 +0800 Subject: [PATCH] [Refactor] Reorganize directory structure for better project management --- docs/en/custom_model.md | 10 +- docs/zh_cn/custom_model.md | 10 +- .../export_grounding_dino_onnx.py | 0 .../export_internimage_model_onnx.py | 0 .../export_pulc_attribute_model_onnx.py | 0 .../export_recognize_anything_model_onnx.py | 0 .../export_yolov10_onnx.py | 0 .../export_yolov8_obb_onnx.py | 0 .../{ => onnx_exporter}/export_yolow_onnx.py | 0 tools/polygon_mask_conversion.py | 181 ------------------ 10 files changed, 10 insertions(+), 191 deletions(-) rename tools/{ => onnx_exporter}/export_grounding_dino_onnx.py (100%) rename tools/{ => onnx_exporter}/export_internimage_model_onnx.py (100%) rename tools/{ => onnx_exporter}/export_pulc_attribute_model_onnx.py (100%) rename tools/{ => onnx_exporter}/export_recognize_anything_model_onnx.py (100%) rename tools/{ => onnx_exporter}/export_yolov10_onnx.py (100%) rename tools/{ => onnx_exporter}/export_yolov8_obb_onnx.py (100%) rename tools/{ => onnx_exporter}/export_yolow_onnx.py (100%) delete mode 100644 tools/polygon_mask_conversion.py diff --git a/docs/en/custom_model.md b/docs/en/custom_model.md index d1709ebc..bb02f6ec 100644 --- a/docs/en/custom_model.md +++ b/docs/en/custom_model.md @@ -390,19 +390,19 @@ InternImage introduces a large-scale convolutional neural network (CNN) model, l | Affiliation | Shanghai AI Laboratory, Tsinghua University, Nanjing University, etc. | | Published | CVPR 2023 | -Refer to this [tutorial](../../tools/export_internimage_model_onnx.py). +Refer to this [tutorial](../../tools/onnx_exporter/export_internimage_model_onnx.py). ### [PersonAttribute](https://github.com/PaddlePaddle/PaddleClas/blob/release/2.5/docs/zh_CN/models/PULC/PULC_person_attribute.md) This tutorial provides a way for users to quickly build a lightweight, high-precision, and practical classification model of person attributes using PaddleClas PULC (Practical Ultra Lightweight image Classification). The model can be widely used in pedestrian analysis scenarios, pedestrian tracking scenarios, etc. -Refer to this [tutorial](../../tools/export_pulc_attribute_model_onnx.py). +Refer to this [tutorial](../../tools/onnx_exporter/export_pulc_attribute_model_onnx.py). ### [VehicleAttribute](https://github.com/PaddlePaddle/PaddleClas/blob/release/2.5/docs/zh_CN/models/PULC/PULC_vehicle_attribute.md) This tutorial provides a way for users to quickly build a lightweight, high-precision, and practical classification model of vehicle attributes using PaddleClas PULC (Practical Ultra Lightweight image Classification). The model can be widely used in vehicle identification, road monitoring, and other scenarios. -Refer to this [tutorial](../../tools/export_pulc_attribute_model_onnx.py). +Refer to this [tutorial](../../tools/onnx_exporter/export_pulc_attribute_model_onnx.py). ## Object Detection @@ -547,7 +547,7 @@ Refer to this [tutorial](https://github.com/chongzhou96/EdgeSAM/blob/master/scri | Affiliation | IDEA-CVR, IDEA-Research | | Published | Arxiv23 | -Refer to this [tutorial](../../tools/export_grounding_dino_onnx.py). +Refer to this [tutorial](../../tools/onnx_exporter/export_grounding_dino_onnx.py). ### [YOLO-World](https://github.com/AILab-CVC/YOLO-World) @@ -577,4 +577,4 @@ $ yolo export model=yolov8s-worldv2.pt format=onnx opset=13 simplify | Affiliation | OPPO Research Institute, IDEA-Research, AI Robotics | | Published | Arxiv23 | -Refer to this [tutorial](../../tools/export_recognize_anything_model_onnx.py). +Refer to this [tutorial](../../tools/onnx_exporter/export_recognize_anything_model_onnx.py). diff --git a/docs/zh_cn/custom_model.md b/docs/zh_cn/custom_model.md index 59606d34..0e9ea110 100644 --- a/docs/zh_cn/custom_model.md +++ b/docs/zh_cn/custom_model.md @@ -388,19 +388,19 @@ InternImage 引入了一个大规模卷积神经网络 (CNN) 模型,利用可 | 发表单位 | 上海人工智能实验室,清华大学,南京大学等 | | 发表时间 | CVPR 2023 | -请参考此 [教程](../../tools/export_internimage_model_onnx.py)。 +请参考此 [教程](../../tools/onnx_exporter/export_internimage_model_onnx.py)。 ### [PersonAttribute](https://github.com/PaddlePaddle/PaddleClas/blob/release/2.5/docs/zh_CN/models/PULC/PULC_person_attribute.md) 本教程为用户提供了一种使用 PaddleClas PULC (实用超轻量图像分类) 快速构建轻量、高精度和实用的人员属性分类模型的方法。该模型可广泛用于行人分析场景、行人跟踪场景等。 -请参考此 [教程](../../tools/export_pulc_attribute_model_onnx.py)。 +请参考此 [教程](../../tools/onnx_exporter/export_pulc_attribute_model_onnx.py)。 ### [VehicleAttribute](https://github.com/PaddlePaddle/PaddleClas/blob/release/2.5/docs/zh_CN/models/PULC/PULC_vehicle_attribute.md) 本教程为用户提供了一种使用 PaddleClas PULC (实用超轻量图像分类) 快速构建轻量、高精度和实用的车辆属性分类模型的方法。该模型可广泛用于车辆识别、道路监控等场景。 -请参考此 [教程](../../tools/export_pulc_attribute_model_onnx.py)。 +请参考此 [教程](../../tools/onnx_exporter/export_pulc_attribute_model_onnx.py)。 ## Object Detection @@ -544,7 +544,7 @@ $ python tools/converter.py -f configs/damoyolo_tinynasL25_S.py -c damoyolo_tiny | 发表单位 | IDEA-CVR,IDEA-Research | | 发表时间 | Arxiv23 | -请参考此 [教程](../../tools/export_grounding_dino_onnx.py)。 +请参考此 [教程](../../tools/onnx_exporter/export_grounding_dino_onnx.py)。 ### [YOLO-World](https://github.com/AILab-CVC/YOLO-World) @@ -574,6 +574,6 @@ $ yolo export model=yolov8s-worldv2.pt format=onnx opset=13 simplify | 发表单位 | OPPO 研究院,IDEA-Research,AI Robotics | | 发表时间 | Arxiv23 | -请参考此 [教程](../../tools/export_recognize_anything_model_onnx.py)。 +请参考此 [教程](../../tools/onnx_exporter/export_recognize_anything_model_onnx.py)。 diff --git a/tools/export_grounding_dino_onnx.py b/tools/onnx_exporter/export_grounding_dino_onnx.py similarity index 100% rename from tools/export_grounding_dino_onnx.py rename to tools/onnx_exporter/export_grounding_dino_onnx.py diff --git a/tools/export_internimage_model_onnx.py b/tools/onnx_exporter/export_internimage_model_onnx.py similarity index 100% rename from tools/export_internimage_model_onnx.py rename to tools/onnx_exporter/export_internimage_model_onnx.py diff --git a/tools/export_pulc_attribute_model_onnx.py b/tools/onnx_exporter/export_pulc_attribute_model_onnx.py similarity index 100% rename from tools/export_pulc_attribute_model_onnx.py rename to tools/onnx_exporter/export_pulc_attribute_model_onnx.py diff --git a/tools/export_recognize_anything_model_onnx.py b/tools/onnx_exporter/export_recognize_anything_model_onnx.py similarity index 100% rename from tools/export_recognize_anything_model_onnx.py rename to tools/onnx_exporter/export_recognize_anything_model_onnx.py diff --git a/tools/export_yolov10_onnx.py b/tools/onnx_exporter/export_yolov10_onnx.py similarity index 100% rename from tools/export_yolov10_onnx.py rename to tools/onnx_exporter/export_yolov10_onnx.py diff --git a/tools/export_yolov8_obb_onnx.py b/tools/onnx_exporter/export_yolov8_obb_onnx.py similarity index 100% rename from tools/export_yolov8_obb_onnx.py rename to tools/onnx_exporter/export_yolov8_obb_onnx.py diff --git a/tools/export_yolow_onnx.py b/tools/onnx_exporter/export_yolow_onnx.py similarity index 100% rename from tools/export_yolow_onnx.py rename to tools/onnx_exporter/export_yolow_onnx.py diff --git a/tools/polygon_mask_conversion.py b/tools/polygon_mask_conversion.py deleted file mode 100644 index e90a3520..00000000 --- a/tools/polygon_mask_conversion.py +++ /dev/null @@ -1,181 +0,0 @@ -import argparse -import json -import os -import time -import cv2 - -from PIL import Image -from tqdm import tqdm -from datetime import date - -import numpy as np -import matplotlib as plt - -import sys - -sys.path.append(".") -from anylabeling.app_info import __version__ - -# ======================================================================= Usage ========================================================================# -# # -# -------------------------------------------------------------------- mask2poly ----------------------------------------------------------------------# -# python tools/polygon_mask_conversion.py --img_path xxx_folder --mask_path xxx_folder --mode mask2poly # -# # -# -------------------------------------------------------------------- poly2mask ----------------------------------------------------------------------# -# [option1] python tools/polygon_mask_conversion.py --img_path xxx_folder --mask_path xxx_folder --mode poly2mask # -# [option2] python tools/polygon_mask_conversion.py --img_path xxx_folder --mask_path xxx_folder --json_path xxx_folder --mode poly2mask # -# # -# ======================================================================= Usage ========================================================================# - -VERSION = __version__ -IMG_FORMATS = [ - ".bmp", - ".dng", - ".jpeg", - ".jpg", - ".mpo", - ".png", - ".tif", - ".tiff", - ".webp", - ".pfm", -] - - -class PolygonMaskConversion: - def __init__(self, epsilon_factor=0.001): - self.epsilon_factor = epsilon_factor - - def reset(self): - self.custom_data = dict( - version=VERSION, - flags={}, - shapes=[], - imagePath="", - imageData=None, - imageHeight=-1, - imageWidth=-1, - ) - - def get_image_size(self, image_file): - with Image.open(image_file) as img: - width, height = img.size - return width, height - - def mask_to_polygon(self, img_file, mask_file, json_file): - self.reset() - binary_mask = cv2.imread(mask_file, cv2.IMREAD_GRAYSCALE) - contours, _ = cv2.findContours( - binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE - ) - for contour in contours: - epsilon = self.epsilon_factor * cv2.arcLength(contour, True) - approx = cv2.approxPolyDP(contour, epsilon, True) - if len(approx) < 5: - continue - shape = { - "label": "object", - "text": "", - "points": [], - "group_id": None, - "shape_type": "polygon", - "flags": {}, - } - for point in approx: - x, y = point[0].tolist() - shape["points"].append([x, y]) - self.custom_data["shapes"].append(shape) - - image_width, image_height = self.get_image_size(img_file) - self.custom_data["imagePath"] = os.path.basename(img_file) - self.custom_data["imageHeight"] = image_height - self.custom_data["imageWidth"] = image_width - - with open(json_file, "w", encoding="utf-8") as f: - json.dump(self.custom_data, f, indent=2, ensure_ascii=False) - - def polygon_to_mask(self, img_file, mask_file, json_file): - with open(json_file, "r") as f: - data = json.load(f) - polygons = [] - for shape in data["shapes"]: - points = shape["points"] - polygon = [] - for point in points: - x, y = point - polygon.append((x, y)) - polygons.append(polygon) - - image_width, image_height = self.get_image_size(img_file) - image_shape = (image_height, image_width) - binary_mask = np.zeros(image_shape, dtype=np.uint8) - for polygon_points in polygons: - np_polygon = np.array(polygon_points, np.int32) - np_polygon = np_polygon.reshape((-1, 1, 2)) - cv2.fillPoly(binary_mask, [np_polygon], color=255) - cv2.imwrite(mask_file, binary_mask) - - -def main(): - parser = argparse.ArgumentParser(description="Polygon Mask Conversion") - - parser.add_argument("--img_path", help="Path to image directory") - parser.add_argument("--mask_path", help="Path to mask directory") - parser.add_argument( - "--json_path", default="", help="Path to json directory" - ) - parser.add_argument( - "--epsilon_factor", - default=0.001, - type=float, - help="Control the level of simplification when converting a polygon contour to a simplified version", - ) - parser.add_argument( - "--mode", - choices=["mask2poly", "poly2mask"], - required=True, - help="Choose the conversion mode what you need", - ) - args = parser.parse_args() - - print(f"Starting conversion to {args.mode}...") - start_time = time.time() - - converter = PolygonMaskConversion(args.epsilon_factor) - - if args.mode == "mask2poly": - file_list = os.listdir(args.mask_path) - for file_name in tqdm( - file_list, desc="Converting files", unit="file", colour="blue" - ): - img_file = os.path.join(args.img_path, file_name) - mask_file = os.path.join(args.mask_path, file_name) - json_file = os.path.join( - args.img_path, os.path.splitext(file_name)[0] + ".json" - ) - converter.mask_to_polygon(img_file, mask_file, json_file) - elif args.mode == "poly2mask": - # Only binary mask transformations are supported. - os.makedirs(args.mask_path, exist_ok=True) - file_list = os.listdir(args.img_path) - for file_name in tqdm( - file_list, desc="Converting files", unit="file", colour="blue" - ): - base_name, suffix = os.path.splitext(file_name) - if suffix.lower() not in IMG_FORMATS: - continue - img_file = os.path.join(args.img_path, file_name) - if not args.json_path: - json_file = os.path.join(args.img_path, base_name + ".json") - else: - json_file = os.path.join(args.json_path, base_name + ".json") - mask_file = os.path.join(args.mask_path, base_name + ".png") - converter.polygon_to_mask(img_file, mask_file, json_file) - - end_time = time.time() - print(f"Conversion completed successfully!") - print(f"Conversion time: {end_time - start_time:.2f} seconds") - - -if __name__ == "__main__": - main()