Search code examples
pythonimage-processingkerasdatasetdata-augmentation

data Augmentation while preserving existing label


Problem statement

I am working on a project using YOLO model to detect tools form given picture.i.e hammer, screwdrivers, bolt etc. I have only 100 pictures as training dataset and I have labelled them using polygons. I have decided to augment the data with the below given code. I’ve got 500 new images but, the problem is that I don't want to label them again. I am looking for any way out with which label bounding boxes (polygons) adjust (preserved) with news augmented images so that I can get polygons data without doing labelling again. In short, I want to preserver the label during the image augmentation process.

Code used for Augmentation

Brightness=[0.7,0.8,1] # Different brightness Levels
Rotation=[10,-10]    # Different rotation Levels

# Main link source
main_path=r"./Augmentation"
# Directoy from main path
dir=os.listdir(main_path)[1:]
# Read and iterate all images from directory
for name in dir:

    image = Image.open(os.path.join(main_path,name))
# Apply rotation from image

    for j in Rotation:             # Different rotation Levels
        rotated = image.rotate(j)
        ransImageRGBA = rotated.convert("RGB")
        apply_br = ImageEnhance.Brightness(ransImageRGBA)
# Apply values for brightness in rotated images
        for i in Brightness:         # Different rotation Levels
             Lightness =apply_br.enhance(i)
             # below line for output
             Lightness = apply_br.enhance(i).save((os.path.join(main_path, 'augmented_Images',str(i)+str(j))+name))
             print('image has been augmented successfully')


Solution

  • look into imaug. The augmentations from this module also augment the labels. One more thing, what you are doing right now is offline augmentation. You might want to look at online augmentation. Then every epoch the pictures are augmented in a different way and you only train on the augmented pictures. This way you don't have to have a lot of discspace.

    If you are using Yolov4 with darknet, image augmentation is performed automatically.