Skip to main content

A data augmentation package for converting segmentation data to detection data.

Project description

Seg2DetAugment

PyPI License GitHub Workflow Status

中文 | English

Overview

Seg2DetAugment is a Python package used for converting semantic segmentation data into object detection data and providing advanced data augmentation functions. Through operations such as rotation and background replacement, this tool generates a detection dataset with rotational invariance and adaptability to complex backgrounds, effectively enhancing the robustness of the model in complex scenarios.

When performing object detection tasks, the background often affects the accuracy of our recognition. For example, the model sometimes misidentifies the background as an object, or makes recognition errors when two objects partially occlude each other. Moreover, convolutional neural networks have limitations in rotational adaptability and lack an explicit rotational invariance mechanism. That is to say, when objects are placed in a rotated position, they usually become difficult to recognize, and the confidence level is low, etc. If you have tried the rotation augmentation methods available on the market, you will find that they all have the bug that the bounding box (bbox) inexplicably becomes larger. This is inevitable. Only when the contour of the object is known can rotation ensure that the bbox remains the circumscribed rectangle. Therefore, it is necessary to propose a dataset augmentation method to provide the model with the performance of an object under different backgrounds and the state of the object at different rotation angles.

Core Advantages

  1. Enhanced Rotational Invariance: Maintains the accuracy of the bounding box after rotation through contour tracing technology.
  2. Adaptability to Complex Backgrounds: Supports dynamic background replacement and the superposition of multiple objects.
  3. Improved Annotation Efficiency: Only a small amount of semantic segmentation annotation is required to generate a large-scale detection dataset.

Typical Application Scenarios

  • Multi-angle object detection in industrial quality inspection.
  • Adaptation to complex backgrounds in the scenario of garbage classification.
  • Recognition of multi-pose targets in remote sensing images.

Installation Method

pip install Seg2DetAugment

Making Dataset

Install Anylabeling.

conda create -n anylabeling python=3.10 anaconda
conda activate anylabeling

CPU:

pip install anylabeling

GPU:

pip install anylabeling-gpu

After the installation is completed, run it using the command.

anylabeling

When you need to execute it next time, you just need to do this.

conda activate anylabeling
anylabeling

Prepare the folder containing the images you want to annotate, and click here to select your image folder.

image-20250315135502879

Then click on this icon of the brain to start the SAM (Segment Anything Model) annotation.

image-20250315135548616

Select a model you want. The model will be automatically downloaded from the internet.

image-20250315135613385

Then click the "+Point" button and just click on the object.

image-20250315135828523

If the calculated boundary curve meets your requirements, click "finish". If there are any issues, you can click on the wrongly marked area with the "-Point" button, and it will automatically recalculate.image-20250315135848748

Enter the name you want. The names for the same object must be identical.

image-20250315135959313

After completing all the markings, all your labels and images will be saved in the same folder, just like this.

image-20250315140118317

Then, prepare several blank background images like I do. Please try to create some differences among these background images as much as possible.

img-bkg

Quick Start

from Seg2DetAugment import data_augmentation

# Define the category mapping
dics = {
    'battery': 0,
    'bottle': 1,
    # ... Other categories
}

# Run data augmentation
data_augmentation(
    dics=dics,
    output_folder="output",
    path2labels="path/to/labels",
    path2imgs="path/to/images",
    path2bkgs="path/to/backgrounds",
    counts=3,        # Number of objects per image
    threshold=0.5,   # Overlap threshold
    num_images=100   # Total number of generated images
)

The results after visualizing the labels and images:

image-20250320162210013

You can also augment the data with key points.

After annotating the key points in JSON format using LabelMe,

from Seg2DetAugment import data_augmentation

# Define the class mapping
dics = {
    'ruler': 0,
}

pointOrder=["acute_angle","right_angle"] # The order of the key points

# Run data augmentation
data_augmentation(
    dics=dics,
    output_folder="output",
    path2labels="path/to/labels",
    path2imgs="path/to/images",
    path2bkgs="path/to/backgrounds",
    counts=3,        # The number of objects per image
    threshold=0.5,   # Overlap threshold
    num_images=100,   # The total number of generated images
    pointOrder=pointOrder # Array of the order of key points
)

The augmentation effect is as follows

image-20250320162426899

Output Structure

output/
├── label/
│   ├── 0.txt
│   └── ...
└── img/
    ├── 0.jpg
    └── ...

Parameter Explanation

Parameter Name Description Default Value
dics Mapping of category labels Required
output_folder Path of the output directory Required
path2labels Path of the input segmentation labels Required
path2imgs Path of the input original images Required
path2bkgs Path of the background images Required
counts Maximum number of objects in a single image 3
threshold Overlap detection threshold (IOU) 0.5
num_images Total number of generated images 100
pointOrder *keypoint Array of the order of key points []

Detailed Explanation of threshold

The following is an example to explain the function of threshold.

As shown in the figure, there is actually a strip of radish inside the purple box, but it is completely (100%) occluded by a mineral water bottle and a Fanta bottle. If this dataset is used for training an object detection model, the consequences will be disastrous. Therefore, the function of the threshold is to ensure that at least 50% of the area of each object is visible. That is to say, occlusion is allowed, but the maximum occlusion rate is 50%, not 100%.

Set threshold to the occlusion rate you want. For example, when threshold = 0.5, the generated result will be much more satisfactory.

Licence

This project is open source under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seg2detaugment-0.0.4.tar.gz (8.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

seg2detaugment-0.0.4-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

Seg2DetAugment-0.0.4-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file seg2detaugment-0.0.4.tar.gz.

File metadata

  • Download URL: seg2detaugment-0.0.4.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for seg2detaugment-0.0.4.tar.gz
Algorithm Hash digest
SHA256 7f43ca9292856e661017f3f35b1ff64d2a1c27d1853ad05b427752b351872a87
MD5 bb19b799e656fed3375dd2060e4da729
BLAKE2b-256 5bf93ca4aa62be9d524014e13fd27facf2a02feb09806754d1ec1af800d1be75

See more details on using hashes here.

Provenance

The following attestation bundles were made for seg2detaugment-0.0.4.tar.gz:

Publisher: python-publish.yml on Huuuuugh/Seg2DetAugment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file seg2detaugment-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: seg2detaugment-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for seg2detaugment-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 da823098679212e8cd489db0b442b50fae33137187d5774e656c10ede310e0f5
MD5 175da4e2c03caaa7283c3fe335844d33
BLAKE2b-256 f6c7104bfdbd3fb0991f66ed8f93c104554e1cfb4a3604c5492dc6ac4c23f17d

See more details on using hashes here.

Provenance

The following attestation bundles were made for seg2detaugment-0.0.4-py3-none-any.whl:

Publisher: python-publish.yml on Huuuuugh/Seg2DetAugment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file Seg2DetAugment-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: Seg2DetAugment-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for Seg2DetAugment-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b73eb6791d7556eefe66491620624998878fa33af0ae684acb94ae36d720050e
MD5 f4f3d0cbdf1f2036610eabddeece966a
BLAKE2b-256 499a8215a4aed745f22385defa50a6496674e6a9c2161cc71e21ddec68d9962b

See more details on using hashes here.

Provenance

The following attestation bundles were made for Seg2DetAugment-0.0.4-py3-none-any.whl:

Publisher: python-publish.yml on Huuuuugh/Seg2DetAugment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page