Skip to content

Commit

Permalink
chore: update documentation
Browse files Browse the repository at this point in the history
Signed-off-by: Barış Zeren <bzeren1819@gmail.com>
  • Loading branch information
StepTurtle committed May 20, 2024
1 parent 9a90d0d commit db33155
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 22 deletions.
95 changes: 91 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,88 @@ You can add your prompts as dictionaries under the `prompts` key. Each dictionar
}
```

Also, you should set your configuration in the configuration files under `config` folder according to the usage.
You should set your configuration in the configuration files under `config` folder according to the usage.
Following instructions will guide you to set each configuration file.

**`config/anonymize_with_unified_model.yaml`**

```yaml
rosbag:
input_bag_path: "path/to/input.bag" # Path to the input ROS2 bag file with 'mcap' or 'sqlite3' extension
output_bag_path: "path/to/output/folder" # Path to the output ROS2 bag folder
output_save_compressed_image: True # Save images as compressed images (True or False)
output_storage_id: "sqlite3" # Storage id for the output bag file (`sqlite3` or `mcap`)

grounding_dino:
box_threshold: 0.1 # Threshold for the bounding box (float)
text_threshold: 0.1 # Threshold for the text (float)
nms_threshold: 0.1 # Threshold for the non-maximum suppression (float)

open_clip:
score_threshold: 0.7 # Validity threshold for the OpenCLIP model (float

yolo:
confidence: 0.15 # Confidence threshold for the YOLOv8 model (float)

bbox_validation:
iou_threshold: 0.9 # Threshold for the intersection over union (float), if the intersection over union is greater than this threshold, the object will be selected as inside the validation prompt

blur:
kernel_size: 31 # Kernel size for the Gaussian blur (int)
sigma_x: 11 # Sigma x for the Gaussian blur (int)
```

**`config/yolo_create_dataset.yaml`**

```yaml
rosbag:
input_bags_folder: "path/to/input/folder" # Path to the input ROS2 bag files folder

dataset:
output_dataset_folder: "path/to/output/folder" # Path to the output dataset folder
output_dataset_subsample_coefficient: 25 # Subsample coefficient for the dataset (int)

grounding_dino:
box_threshold: 0.1 # Threshold for the bounding box (float)
text_threshold: 0.1 # Threshold for the text (float)
nms_threshold: 0.1 # Threshold for the non-maximum suppression (float)

open_clip:
score_threshold: 0.7 # Validity threshold for the OpenCLIP model (float

bbox_validation:
iou_threshold: 0.9 # Threshold for the intersection over union (float), if the intersection over union is greater than this threshold, the object will be selected as inside the validation prompt
```

**`config/yolo_train.yaml`**

```yaml
dataset:
input_dataset_yaml: "path/to/input/data.yaml" # Path to the config file of the dataset, which is created in the previous step

yolo:
epochs: 100 # Number of epochs for the YOLOv8 model (int)
model: 'yolov8x.pt' # Select the base model for YOLOv8 ('yolov8x.pt' 'yolov8l.pt', 'yolov8m.pt', 'yolov8n.pt')
```

**`config/yolo_anonymize.yaml`**

```yaml
rosbag:
input_bag_path: "path/to/input.bag" # Path to the input ROS2 bag file with 'mcap' or 'sqlite3' extension
output_bag_path: "path/to/output/folder" # Path to the output ROS2 bag folder
output_save_compressed_image: True # Save images as compressed images (True or False)
output_storage_id: "sqlite3" # Storage id for the output bag file (`sqlite3` or `mcap`)

yolo:
model: "path/to/yolo/model" # Path to the trained YOLOv8 model file (`.pt` extension) (you can download the pre-trained model from releases)
config_path: "path/to/input/data.yaml" # Path to the config file of the dataset, which is created in the previous step
confidence: 0.15 # Confidence threshold for the YOLOv8 model (float)

blur:
kernel_size: 31 # Kernel size for the Gaussian blur (int)
sigma_x: 11 # Sigma x for the Gaussian blur (int)
```

### Usage

Expand All @@ -71,7 +152,9 @@ The tool provides two options to anonymize images in rosbags.
**Option 1: Anonymize with Unified Model**

You should provide a single rosbag and tool anonymize images in rosbag with a unified model.
The model is a combination of GroundingDINO, OpenCLIP and SegmentAnything.
The model is a combination of GroundingDINO, OpenCLIP, YOLOv8 and SegmentAnything.
If you don't want to use pre-trained YOLOv8 model, you can follow the instructions in the second option to train your
own YOLOv8 model.

You should set your configuration in `config/anonymize_with_unified_model.yaml` file.

Expand All @@ -83,7 +166,7 @@ python3 main.py config/anonymize_with_unified_model.yaml --anonymize_with_unifie

<ins>Step 1:</ins> Create an initial dataset with the unified model.
You can provide multiple rosbags to create a dataset.
You should set your configuration in `config/create_dataset_with_unified_model.yaml` file.
You should set your configuration in `config/yolo_create_dataset.yaml` file.
After running the following command, the tool will create a dataset in YOLO format.

``` shell
Expand All @@ -108,7 +191,11 @@ python3 main.py config/yolo_train.yaml --yolo_train
```

<ins>Step 5:</ins> Anonymize images in rosbags with the trained YOLOv8 model.
You should set your configuration in `config/yolo_anonymize.yaml` file.
You should set your configuration in `config/yolo_anonymize.yaml` file.
If you want to anonymize your ROS2 bag file with only YOLOv8 model,
you should use following command.
But we recommend to use the unified model for better results.
You can follow the `Option 1` for the unified model with the YOLOv8 model trained by you.

``` shell
python3 main.py config/yolo_anonymize.yaml --yolo_anonymize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
def anonymize_with_unified_model(config_data, json_data, device) -> None:
reader = RosbagReader(config_data["rosbag"]["input_bag_path"], 1)
writer = RosbagWriter(
config_data["rosbag"]["output_bag_paht"],
config_data["rosbag"]["output_bag_path"],
config_data["rosbag"]["output_save_compressed_image"],
config_data["rosbag"]["output_storage_id"],
)
Expand Down
6 changes: 3 additions & 3 deletions autoware_rosbag2_anonymizer/tools/yolo_anonymize.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def yolo_anonymize(config_data, json_data, device) -> None:

yolo_model_path = config_data["yolo"]["model"]
yolo_confidence = config_data["yolo"]["confidence"]
yolo_config_path = config_data["yolo"]["config_path"]
Expand All @@ -35,7 +35,7 @@ def yolo_anonymize(config_data, json_data, device) -> None:
# Create rosbag reader and rosbag writer
reader = RosbagReader(config_data["rosbag"]["input_bag_path"], 1)
writer = RosbagWriter(
config_data["rosbag"]["output_bag_paht"],
config_data["rosbag"]["output_bag_path"],
config_data["rosbag"]["output_save_compressed_image"],
config_data["rosbag"]["output_storage_id"],
)
Expand All @@ -58,7 +58,7 @@ def yolo_anonymize(config_data, json_data, device) -> None:
config_data["blur"]["kernel_size"],
config_data["blur"]["sigma_x"],
)

# Debug ------------------
# bounding_box_annotator = sv.BoundingBoxAnnotator()
# annotated_image = bounding_box_annotator.annotate(
Expand Down
22 changes: 12 additions & 10 deletions autoware_rosbag2_anonymizer/tools/yolo_create_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,25 @@ def yolo_create_dataset(config_data, json_data, device) -> None:
ANNOTATIONS_DIRECTORY_PATH = os.path.join(DATASET_DIR_PATH, "annotations")
IMAGES_DIRECTORY_PATH = os.path.join(DATASET_DIR_PATH, "images")
DATA_YAML_PATH = os.path.join(DATASET_DIR_PATH, "data.yaml")

# Create folders if it is not exist
os.makedirs(DATASET_DIR_PATH, exist_ok=True)
os.makedirs(ANNOTATIONS_DIRECTORY_PATH, exist_ok=True)
os.makedirs(IMAGES_DIRECTORY_PATH, exist_ok=True)

# Declare counter variable for naming
IMAGE_COUNTER = 0
SUBSAMPLE_COEFFICIENT = config_data["dataset"]["output_dataset_subsample_coefficint"]

SUBSAMPLE_COEFFICIENT = config_data["dataset"][
"output_dataset_subsample_coefficient"
]

for rosbag2_path in rosbag2_paths:
reader = RosbagReader(rosbag2_path, SUBSAMPLE_COEFFICIENT)

for i, (msg, is_image) in enumerate(reader):
if not is_image:
continue

# Convert image msg to cv.Mat
image = image = cv_bridge.CvBridge().compressed_imgmsg_to_cv2(msg.data)

Expand All @@ -76,9 +78,9 @@ def yolo_create_dataset(config_data, json_data, device) -> None:
lines=lines,
file_path=f"{ANNOTATIONS_DIRECTORY_PATH}/image{IMAGE_COUNTER}.txt",
)

IMAGE_COUNTER += 1

# box_annotator = sv.BoxAnnotator()
# labels = [
# f"{DETECTION_CLASSES[class_id]} {confidence:0.2f}"
Expand All @@ -93,7 +95,7 @@ def yolo_create_dataset(config_data, json_data, device) -> None:

# cv2.imshow("debug", image)
# cv2.waitKey(1)

data = {"names": DETECTION_CLASSES, "nc": len(DETECTION_CLASSES)}
with open(DATA_YAML_PATH, "w") as yaml_file:
yaml.dump(data, yaml_file, default_flow_style=False)
yaml.dump(data, yaml_file, default_flow_style=False)
2 changes: 1 addition & 1 deletion config/anonymize_with_unified_model.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rosbag:
input_bag_path: '/home/bzeren/projects/labs/data/rosbags/rosbag2_2024_03_22-18_27_21/rosbag2_2024_03_22-18_27_21_0-001.mcap'
output_bag_paht: '/home/bzeren/projects/labs/data/rosbags/rosbag2_2024_03_22-18_27_21/output'
output_bag_path: '/home/bzeren/projects/labs/data/rosbags/rosbag2_2024_03_22-18_27_21/output'
output_save_compressed_image: True
output_storage_id: 'sqlite3' # 'sqlite3' or 'mcap'
grounding_dino:
Expand Down
2 changes: 1 addition & 1 deletion config/yolo_anonymize.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rosbag:
input_bag_path: '/media/bzeren/PortableSSD/YTU-DATA/rosbag2-anonymizer/rosbag2_2024_05_13-15_36_03/rosbag2_2024_05_13-15_36_03_0.db3'
output_bag_paht: '/home/bzeren/projects/labs/data/rosbags/rosbag2_2024_03_22-18_27_21/output'
output_bag_path: '/home/bzeren/projects/labs/data/rosbags/rosbag2_2024_03_22-18_27_21/output'
output_save_compressed_image: True
output_storage_id: 'sqlite3' # 'sqlite3' or 'mcap'
yolo:
Expand Down
2 changes: 1 addition & 1 deletion config/yolo_create_dataset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ rosbag:
input_bags_folder: '/media/bzeren/PortableSSD/YTU-DATA/rosbag2-anonymizer'
dataset:
output_dataset_folder: '/media/bzeren/PortableSSD/YTU-DATA/rosbag2-anonymizer/dataset'
output_dataset_subsample_coefficint: 25
output_dataset_subsample_coefficient: 25
grounding_dino:
config_path: './GroundingDINO_SwinB.cfg.py'
checkpoint_path: './groundingdino_swinb_cogcoor.pth'
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "autoware_rosbag2_anonymizer"
version = "1.0.0"
version = "0.0.0"
dependencies = [
"cv_bridge",
"huggingface_hub",
Expand Down

0 comments on commit db33155

Please sign in to comment.