Now Reading
Hands-on Guide To Synthetic Image Generation With Flip

Hands-on Guide To Synthetic Image Generation With Flip

Your neural network is only as good as the data you feed it because it trains on millions of parameters to produce an effective performance but in case you are working on something new and and you’re stuck with a limited dataset and online available datasets are limited you can use augmentation techniques in to train your neural network with diversity in relevant data.

Often deep learning engineers have to deal with insufficient data that can create problems like increased variance in their models that can lead to overfitting and limit the experimentation with the dataset. Synthetic data generation is just artificial generated data in order to overcome a fixed set of data availability by use of algorithms and programming.While dealing with datasets containing images. Flip-data which is an open source python library can help us overcome the issue of limited image datasets and help us generate synthetic images by taking images and separating them into objects and backgrounds and repositioning the object on top of background through data augmentation techniques and have some flexibility in the dataset.

How To Start Your Career In Data Science?

Dependencies

Flip requires:

Python (>= 3.7), Opencv (>= 4.3.0), Numpy (>= 1.19.1)

What is data augmentation?

Data augmentation uses simple strategic techniques like flipping,resizing, rotating etc. on image objects with respect to the background to produce diversity in the available images.It is most commonly used to train and improve neural networks by using artificial data produced from existing data.

DATA AUGMENTATION TECHNIQUES USED

Below, are the basic operations performed on images to produce variations.

FLIP

We can flip the images with respect to the axis randomly or in the x and y plane.

flip.transformers.data_augmentation.Flip(mode=’random’)

ROTATE

We can rotate the images by setting the min-max angles and get desired augmented results. 

flip.transformers.data_augmentation.Rotate(mode=’random’, min=60, max=180)

RANDOM SIZE

We can resize the images by setting the min-max width of the image according to our desired results.

flip.transformers.data_augmentation.RandomResize(mode='symmetric_w',relation='parent',  w_percentage_min=0.3, w_percentage_max=0.5,)

TRANSFORM

After completing the above steps we can determine the min-max values of height and width to place the object respectively with the background and apply random composition or setting the percentage value.

transform = flip.transformers.Compose(
        [
            flip.transformers.ApplyToObjects(transform_objects),
            flip.transformers.domain_randomization.ObjectsRandomPosition(
                x_min=0.1,
                y_min=0.5,
                x_max=0.8,
                y_max=0.8,
                mode='percentage'
            ),            
            flip.transformers.domain_randomization.Draw(),
            flip.transformers.io.SaveImage(OUTPUT_DIR, name)
        ]
    )

The main transformers are:

See Also

Transformer, Compose and ApplyToObjects

By the way, all Transformers will be executed over objects of class Element and will return a new transformed Element.

At last, we can set a number of samples, objects and define input output paths for respective backgrounds and objects and save them to result images.

 generate_data(n_samples=1,
                  n_objects=4,
                  backgrounds_pattern="backgrounds/*",
                  objects_pattern="objects/*",
                  output_dir="synthetic_images",
                  show=False)

BACKGROUND

You can choose the desired background or use a single photo to crop the object from the background (crop images github link), we will use a jpg format image for background.

OBJECT 

The object image format we are using is png.

 RESULTS

CONCLUSION

We have successfully generated synthetic 2D images from a single background and object image and we can also generate thousands of new 2D images from a small batch of objects and backgrounds as per our requirements.

Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.
Join our Telegram Group. Be part of an engaging community

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top