MonaLisa - Image recreation using genetic algorithms
Introduction
This project is a simple implementation of a genetic algorithm to recreate an image using polygons.
For more information about genetic algorithms, see Genetic Algorithms.
Install
The installation can be done via:
pip install monailisa
Usage
Once, the package is download via pip, you can use via:
monailisa <imgObjectifPath> <nbSubject> <nbGeneration> [imgTemporaryPath]
with:
- imgObjectifPath (str) : Being the objectif of the generation.
- nbSubject (int: 0-inf) : Being the number of individuals used in each generation.
- nbGeneration (int: 0-inf): Being the total of generation.
- imgTemporaryPath (optional, str): Used to continue a previous launch, the argument is the previous result image.
Alternative:
Also, you can use tox to test the program in a venv via the repository:
$ git clone https://github.com/nathan-hoche/MonaLisa
$ cd MonaLisa
$ tox -e venv -- monailisa <imgObjectifPath> <nbSubject> <nbGeneration> [imgTemporaryPath]
First Version
This version is based on this paper by Sebastian Charmot.
The first version of the algorithme is based on the following steps:
- Generate a random population of polygons
- Compute the fitness of each individual
- Select the best individual
- Apply crossover between the best individual and the population (with stocking the previous best individual)
- Apply mutation to the population (except the previous best individual), which consists in adding a random polygon to all individuals
- Repeat from step 2, x times to upgrade the image
The result of this different steps are shown below.
| image | 485 generations | 1000 generations |
|---|---|---|
| image | 500 generations |
|---|---|
But After the 1000th generation, the algorithm has a low probability to improve the image anymore, due to the size of the polygons. The algorithm is stuck in a local minimum.
Second Version
The main problem of the first version is the precision of the polygons. As the polygons are represented by a list of points, the more points are far from others, less the polygon is precies. To solve this problem, I decide to search on progressively reduce the size of the polygons. So the algorithm is based on the following steps:
- Generate a random population of polygons
- Compute the fitness of each individual
- Select the best individual
- Equilibrate the limit size of the polygons, when the process is stuck (the fitness is not increasing), we will reduce the limit size of the polygons and when the process is not stuck, we will increase the limit size of the polygons.
- Apply crossover between the best individual and the population (with stocking the previous best individuals)
- Apply mutation to the population (except the previous best individuals), which consists in adding a random polygon to all individuals
- Repeat from step 2, x times to upgrade the image
| image | 200 generations | 1000 generations | 2000 generations | 3000 generations | 4000 generations | 5000 generations |
|---|---|---|---|---|---|---|
In comparison with the first version:
| First version | Second version |
|---|---|
And for the result on 5000 generations:
| Objectif | Result |
|---|---|
With this version, the algorithm is able to improve the image without being stuck. Now, the algorithm is able to recreate the image with a precision of 1 pixel.
Gif Result:
| Monalisa | Logo | |
|---|---|---|
| Number of step | 10000 | 10000 |
| Result |
Metadata
Release files for monailisa 0.1.13
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| monailisa-0.1.13.tar.gz | 12.6 MB | Without provenance | Details for monailisa-0.1.13.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| monailisa-0.1.13-py3-none-any.whl | Python 3 | none | any | Details for monailisa-0.1.13-py3-none-any.whl |
Total release size: 12.6 MB
Release files / monailisa-0.1.13.tar.gz
| Download URL | monailisa-0.1.13.tar.gz |
|---|---|
| Size | 12.6 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4cc95e0f1713b009bfda8bb9c8e1a4df1f6f3dfa296baa27fd78548968ec8f21
|
|
BLAKE2b-256 checksum How to use checksums |
f168aa59db3a0cd6fed7fbae1ed0e4e81f1d47335b9e309c3240b7b0933c4dbc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Release files / monailisa-0.1.13-py3-none-any.whl
| Download URL | monailisa-0.1.13-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae09678344dbdaf1bdd2a9471a3bef380c37f86b3995a5bd7da30d78b56ec5be
|
|
BLAKE2b-256 checksum How to use checksums |
9bad7abb19a18134328fc43e7dda816434e9636d65ece6099fd9399f15b98e81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Security for monailisa 0.1.13
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.