How to create your own Synthetic Data- for computer vision applications

September 7, 2021

| Juan Felipe Montenegro Torres

Nowadays, we are in the Big Data era where thanks to the internet, it is possible to have thousands of free accessible data, nonetheless, this data usually doesn’t come with its respective annotations. Making the process of training Deep Learning or Machine Learning models very difficult, and even harder when the data is a customized group of images, where there are few possibilities to obtain large amounts of data.

For these specific cases, the creation of synthetic images becomes a suitable solution, since, in this way, we can increase the amount of data that can be used to train our models. However, when we create synthetic images we still have to label our data, solving just the tip of the iceberg. This is where “Flip” has a huge impact as an all-in-one solution.

Flip is an open-source Python library that allows generating synthetic images from a group of backgrounds and objects (objects: images that are located over the background image). In addition, it allows you to apply different transformations to objects, such as flipping them about an axis, rotate them at different angles, resize them, and specify the area of ​​the background where you want to place the objects. Finally, Flip has the option of saving these image compositions as a .jpg format and creates masks of the segmented image, at the end, you will have a set of synthetic images and their respective annotations (bounding boxes or segmentation masks) ready to feed your algorithms.

For instance, in the following use-case, open-source data of butterflies and nature backgrounds was used as recommended in the library repository. An example of each of these databases is shown below:

Example of background used.

Example of images used.

For each one of these butterfly images, the Crop images from mask file was used, this takes the image and its corresponding segmentation and crops it, so that we can remove the background of each one of our objects, as shown in the following figure. Each image was placed in different folders as indicated in the library repository.

Final objects used.

Subsequently, locate the file examples/ and change the parameters according to how you want the new images to be arranged. Here is an example of the parameters used to create the final images:

In this example, the parameters were set-up so that the objects could be rotated at any angle with the Rotate() function, which also allows setting a range between a minimum and maximum angle as desired by the user. Then objects were rotated about the X-axis with the Flip() function. This function also allows to flip them about the Y-axis. And finally, their size was changed in a range between 30% and 70% about the original size with the RandomResize() function, which also allows resizing the objects asymmetrically or symmetrically with the height according to the percentages indicated.

Additionally, parameters were set to randomly place the objects at any point on the X-axis and between the middle and the end of the Y-axis with the ObjectsRandomPosition() function which also allows using the “random” mode that places them at any position randomly. The Draw() function places these objects in the positions determined by the previous function. It also allows creating the respective segmentation mask for all the objects located in the image with the CreateBoundingBoxes() and CreateMasks() functions, which asks for the name of all the classes to maintain the same annotation for each class in all the images even if a class does not appear in the image. And finally, SaveImage() and SaveMask() saves the images in the specified path generating the following figures:

Finally, this process can be also done for Bounding Boxes annotation as shown in the article: “Creating Synthetic Images with Flip.

You can also read an application from this library in the Anomaly Detection project in the article: “Anomaly Detection with Computer Vision.

And take a look at the full code in the library’s GitHub repository.

Exploring Generative AI: Use Cases in Computer Vision and Beyond
Thursday, February 22, and Friday, February 23
Register Free

Follow Us

Copyright © 2024 Linked AI, Inc. All rights reserved.