Skip to main content

Copy-Paste-Augmentation

Unofficial implementation of the copy-paste augmentation from Simple Copy-Paste is a Strong Data Augmentation Method for Instance Segmentation.

The augmentation function is built to integrate easily with albumentations. An example for creating a compatible torchvision dataset is given for COCO. Core functionality for image, masks, and bounding boxes is finished; keypoints are not yet supported. In general, you can use the CopyPaste augmentation just as you would any other albumentations augmentation function. There are a few usage limitations of note.

The image shows a composition of the main image (people in an airport) and extra images (train). Since the extra images are also affected by the pre-processing transformations, they can show a very weird aspect ratio and can be out of scale compared to the rest of the picture.

Usage Notes

  1. BboxParams cannot have label_fields. To attach class labels to a bounding box, directly append it to the bounding box coordinates. (I.e. (x1, y1, x2, y2, class_id)).
  2. Bounding boxes passed to the CopyPaste augmentation must also include the index of the corresponding mask in the 'masks' list. (I.e. the bounding box looks like (x1, y1, x2, y2, class_id, mask_index)). An example is given for COCO.
  3. The CopyPaste augmentation expects 6 keyword arguments instead of three:
# typical albumentations transform
output = transforms(image=image, masks=masks, bboxes=bboxes)

# for the copy paste transform
output = transforms(
    image=image, masks=masks, bboxes=bboxes,
    paste_image=paste_image, paste_masks=paste_masks, paste_bboxes=paste_bboxes
  )
  1. After pasting objects, the original bounding boxes may be occluded. To make things easier, the original bounding boxes are just extracted from the updated masks.

Integration with Torchvision datasets

The idea is to have a standard torchvision dataset that's decorated for copy-paste functionality.

The dataset class looks like:

from copy_paste import copy_paste_class
from torch.utils.data import Dataset

@copy_paste_annotation
class SomeVisionDataset(Dataset):
    def __init__(self, *args):
        super(SomeVisionDataset, self).__init__(*args)

    def __len__(self):
        return length

    def __getitem__(self, idx):
        image_data_dict = load_some_data(idx)
        transformed_data_dict = self.transforms(**image_data_dict)
        return transformed_data_dict

The only difference from a regular torchvision dataset is the decorator.

To compose transforms with copy-paste augmentation:

import albumentations as A
from albumentations.pytorch.transforms import ToTensorV2
from copy_paste import CopyPaste

transform = A.Compose([
      A.RandomScale(scale_limit=(-0.9, 1), p=1), #LargeScaleJitter from scale of 0.1 to 2
      A.PadIfNeeded(256, 256, border_mode=0), #constant 0 border
      A.RandomCrop(256, 256),
      A.HorizontalFlip(p=0.5),
      CopyPaste(blend=True, sigma=1, pct_objects_paste=0.5, p=1)
    ], bbox_params=A.BboxParams(format="coco")
)

Note: bbox params are NOT optional!

Metadata

Release files for copy-paste-augmentation 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for copy-paste-augmentation 0.1.0
File Size Uploaded
copy_paste_augmentation-0.1.0.tar.gz 611.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for copy-paste-augmentation 0.1.0
File Interpreter ABI Platform
copy_paste_augmentation-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.2 MB

Release files / copy_paste_augmentation-0.1.0.tar.gz

Download URL copy_paste_augmentation-0.1.0.tar.gz
Size 611.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6e9732be887ec60fb4ae0fc8eba73dc8b9c97074caf3937f6682f0cba5e4381c
BLAKE2b-256 checksum
How to use checksums
1f05313e76d1ad1fb096cafa62cf5232d4b629fbe3f52845b34a51a8b3266eaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

Release files / copy_paste_augmentation-0.1.0-py3-none-any.whl

Download URL copy_paste_augmentation-0.1.0-py3-none-any.whl
Size 611.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e6e12e4cf20c823b2c3744b64bd041ad18f312d3afd19729e070e66c4decce75
BLAKE2b-256 checksum
How to use checksums
69cca4b7cb4efdd957e155b6d80bd79474f79cb5eb0eb03d5f9523b3e4638c8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page