Samuel Guedj
3 min readMay 14, 2021

--

Introduction to Genetic Algorithm (Unsupervised Learning): generate a Rose

A mon amie la Rose!

The goal is to start from a random image and generate a picture close to the model using AI

Original/random
After a few min running
Model

Clone https://github.com/sgu-fai/y-data or get 4-Unsupersived/week6-GA/rose

Start by importing basic library and load the picture

For simplicity (and running time), we focus on a small part of the image:

And we will define this part as our target image to reconstruct. Then plot a random picture

The goal is to get an image as close as possible to the target. Don’t worry if it’s still noisy… FYI: GA are not the best solution for these types of problems ;) You can use these blog posts to give you ideas: https://towardsdatascience.com/genetic-algorithm-implementation-in-python-5ab67bb124a6

Here are some of the main function. I will detail soon.

This definition of biased_selection keeps an adjustable percentage of the best chromosomes. This is to avoid degeneration. Then uses a binomial distribution based on fitness score to keep parents. I didn’t show the result to simplify the notebook but it didn’t improve that much

Here we are defining randomly the dominant genes and rebuilding the new generation

This is the main loop. We stop when the fitness is close enough to what we want

Starting the main loop with correct arguments

After a few iterations

Thanks. I hope it helps. enjoy the music!

--

--