Giter Site home page Giter Site logo

conradry / copy-paste-aug Goto Github PK

View Code? Open in Web Editor NEW
524.0 5.0 73.0 1.21 MB

Copy-paste augmentation for segmentation and detection tasks

License: MIT License

Python 4.23% Jupyter Notebook 95.77%
instance-segmentation object-detection deep-learning data-augmentation copy-paste

copy-paste-aug's Introduction

Copy-Paste

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.

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_class
class SomeVisionDataset(Dataset):
    def __init__(self, *args):
        super(SomeVisionDataset, self).__init__(*args)

    def __len__(self):
        return length

    def load_example(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 and the load_example method instead of __getitem__.

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!

copy-paste-aug's People

Contributors

conradry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

copy-paste-aug's Issues

Torchvision Dataset Integration

I developed my own dataset class. However, I keep getting a strange error.
This is my code

from copy_paste import copy_paste_class
from torch.utils.data import Dataset
import torchvision.transforms as transforms

@copy_paste_class
class DatasetClass(Dataset):
    def __init__(self, df, transforms = None):
        self.df = df
        self.transforms = transforms
        
    def __len__(self):
        return len(self.df)
    
    def load_example(self,index):
        #locally
#         img_path = self.df['imagePath'][index]
        # on DGX    
        img_path = self.df['imagePath'][index]
        
        img_label = self.df['class'][index]
        img_PIL = Image.open(img_path)
        print('done')
        if img_PIL.mode != "RGB":
            img_PIL = img_PIL.convert('RGB')          
        border = (int(self.df['xmin'][index]),\
                  int(self.df['ymin'][index]),\
                  int(self.df['xmax'][index]),\
                  int(self.df['ymax'][index])) # left, top, right, bottom
        img_PIL = img_PIL.crop(border)
        image = img_PIL
        image = self.transforms(image)
        
        return image, img_label_idx




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


transforms = 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.HorizontalFlip(p=0.5),
      CopyPaste(blend=True, sigma=1, pct_objects_paste=0.5, p=1)
    ], bbox_params=A.BboxParams(format="coco")
)

Finally, this is the error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_16726/2411785438.py in <module>
----> 1 dataset[0]

~/Desktop/nas/vision/Orjuwan/LDR/Experiments/LogoDetection/YOLOv5/copy_paste.py in __getitem__(self, idx)
    298             self._split_transforms()
    299 
--> 300         img_data = self.load_example(idx)
    301         if self.copy_paste is not None:
    302             paste_idx = random.randint(0, self.__len__() - 1)

/tmp/ipykernel_16726/579992425.py in load_example(self, index)
     29         img_PIL = img_PIL.crop(border)
     30         image = img_PIL
---> 31         image = self.transforms(image)
     32 
     33         return image, img_label_idx

~/anaconda3/lib/python3.8/site-packages/albumentations/core/composition.py in __call__(self, force_apply, *args, **data)
    191     def __call__(self, *args, force_apply=False, **data):
    192         if args:
--> 193             raise KeyError("You have to pass data to augmentations as named arguments, for example: aug(image=image)")
    194         if self.is_check_args:
    195             self._check_args(**data)

KeyError: 'You have to pass data to augmentations as named arguments, for example: aug(image=image)'

How To Visualize Augmented Images in As presented in Fig 2.

@conradry Very interesting work done. Thanks. The 'example.ipynb' does not display augmented images as presented in figure 2 of the original paper. Can you advise on how to reproduce and visualize the augmentation effect as observed in the newly created images as presented in Fig 2 of the paper?

How to use copy-paste augmentation for YOLO5 object detection

Hey @conradry,
I'm curios to know how can i use the copy-paste augmentation in YOLO. Im using albumentations to do some augmentation on my custom dataset and I have added the copy-paste aug as you said in the readme file but im not able to make it work! im getting an error that needs the mask params... which I dont have it! any idea how can I use it in yolov5 detection??

transform = A.Compose([
       A.NoOp(p=0.4),
       A.RandomScale(p=0.2)
       CopyPaste(blend=True, sigma=1, pct_objects_paste=0.5, p=1)
], bbox_params=A.BboxParams(format="pascal_voc"))

Thanks!

An error occurred while using Copy-Paste!

ValueError: Expected x_max for bbox (0.65, 0.51, 1.12, 0.64, 3) to be in the range [0.0, 1.0], got 1.1234809015877545
Augmentation I use:
aug_list = [A.Resize(800,800),
CopyPaste(blend=True, sigma=1, pct_objects_paste=0.6, p=0.5) #pct_objects_paste is a guess
]
transform = A.Compose(
aug_list, bbox_params=A.BboxParams(format="coco")
)
Can anyone tell me where is the problem?

How do I use this augment data?

I have successfully run your program. Now I want to know how to use the data, to train the model directly, or to save the data and train the model with the original data?

RandomScale of albumentations drops "bboxes" content in coco annotations

Running the example jupyter , the "bboxes" is [] for both img_data and paste_img_data in code
img_data = self.copy_paste(**img_data, **paste_img_data) . The empty bboxe causes the augmentation failed. I'm sure that "bboxes" extracted from annotations. My data is standard coco format. And the program works when drop other aug before CopyPaste.

The example script is no use for my aerial image

Thank you for your work. But may more situations should be considered. With no changes to your example script , the aug failed for my aerial image. There is no data format problem in the process.

AttributeError: 'CopyPaste' object has no attribute '_to_dict'

/usr/local/lib/python3.6/dist-packages/albumentations/core/composition.py in <listcomp>(.0)
     97             "__class_fullname__": self.get_class_fullname(),
     98             "p": self.p,
---> 99             "transforms": [t._to_dict() for t in self.transforms],  # skipcq: PYL-W0212
    100         }
    101 

AttributeError: 'CopyPaste' object has no attribute '_to_dict'

Mosaic can't be used for Instance Segmentation?

The Mosaic transformation can not be used for Instance Segmentation?

I constructed the instance segmentation config according to the format of objecti detection (add ‘gt_masks’), but get an error.

image

Reproduce results in Paper

Have you been able to reproduce the results achieved in the paper with this unofficial implementation?

How to use copy-paste augmentation for YOLOX object detection

Hey @conradry,Thank you for your work!
Could you tell me how can i use the copy-paste augmentation in YOLOX. The dataset for YOLOX should be in COCO format, and I already have my custom dataset, so I tried do some copy-paste-aug on my data, but it didn't work...any idea how can I use it in yoloX?
And if theres is a way to save augmented data (img and annotations.json) to different folder, I'd be glad to know.

copy paste to background image

Hi @conradry , first of all thanks for your great work,
I want to use copy paste to copy annotation from original image to a background image without annotations. How can I do it ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.