Synthetic Image Generation using Python

Want to keep up your secrecy as well as increase your dataset to train your model? Synthetic image generation is the way to go!.

jayasurya karthikeyan
featurepreneur
4 min readMar 12, 2021

--

In this article let me share my experience of working with synthetic image generation during my tenure as an intern in Tactii and Tactlabs.

What is Synthetic Image Generation?

Synthetic data generation is just artificial generated data in order to overcome a fixed set of data availability by the use of algorithms and programming. While dealing with datasets containing images.

There are several ways out there to create synthetic image datasets, and in this article, we will look into one of the most used methods to generate synthetic images — Image Composition. We will generate new images by composing the object image as foreground on multiple background images.

Then what’s and how’s a generation of Synthetic Image Generation?

Synthetic data is gaining traction within the machine learning domain. Machine learning algorithms are trained using an immense amount of data, and collecting the necessary amount of labeled training data can be cost-prohibitive.

Synthetically generated data can help companies and researchers build data repositories needed to train and even pre-train machine learning models, a technique referred to as transfer learning.

Steps to create Synthetic Images

As discussed above, we will be using the image composition technique to create a new image.

The first step is to find the images that we need to superimpose on the background.

we will be choosing the below image to be separated from the foreground to paste it into the desired background.

Here we will be working with some sample retail store items. The objective is now to separate the above images from their background and create a vector image.

The second step is to choose a background image. We will be choosing a plain background to mimic a floor.

A sample floor background

Now using various Python libraries such as Pillow and GrabCut we will be extracting the foreground and background of the above images.

There were some issues pertaining to the extraction of the images along its boundaries as the algorithm could not find the type of object in the image.

To overcome this we need to train the model where it could identify specific objects and pick its foreground and background.

Training our own YOLO model

We used a pre-trained YOLO V5 model, where it could identify 100+ images and classify them according to their classes. As we will be using images that the YOLO model could not identify, we need to train the YOLO model on our own according to our needs.

We gave the inputs as images and gave its co-ordinates that needed to be cut off. The YOLO model has inbuilt algorithms that takes care of the same. We trained nearly 1000+ images for it to identify the co-ordinates on a regular basis and each training depending upon your machine configuration takes time in doing so.

The cut-out image after training and prediction.

Creation of the final image and the result

Now the task is to affix the obtained foreground and the background in order to create a new image.

As discussed above we collected many samples belonging to various classes such as boxes, water-bottles, carbonated drinks, etc, and extracted their foreground from their background.

We can re-arrange the stitched image according to our needs by using a proprietary python code and below is the obtained result.

The stitched images along with their background.

The outcome

The above project was informative both in technical as well as in knowing the requirements of the real-world scenarios. Thus the final outcome of creating a synthetic image using various python algorithms and modules was achieved.

--

--

jayasurya karthikeyan
featurepreneur

Intern at Tactii and Tactlabs. Aviation geek, Computer Science enthusiast