All Projects → ahmedfgad → GARI

ahmedfgad / GARI

Licence: MIT license
GARI (Genetic Algorithm for Reproducing Images) reproduces a single image using Genetic Algorithm (GA) by evolving pixel values.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to GARI

zoofs
zoofs is a python library for performing feature selection using a variety of nature-inspired wrapper algorithms. The algorithms range from swarm-intelligence to physics-based to Evolutionary. It's easy to use , flexible and powerful tool to reduce your feature size.
Stars: ✭ 142 (+246.34%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms, optimization-algorithms
biteopt
Derivative-Free Optimization Method for Global Optimization (C++)
Stars: ✭ 91 (+121.95%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms, optimization-algorithms
Pagmo2
A C++ platform to perform parallel computations of optimisation tasks (global and local) via the asynchronous generalized island model.
Stars: ✭ 540 (+1217.07%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms, optimization-algorithms
Mindseye
Neural Networks in Java 8 with CuDNN and Aparapi
Stars: ✭ 8 (-80.49%)
Mutual labels:  image-manipulation, image-analysis, optimization-algorithms
geneticalgorithm2
Supported highly optimized and flexible genetic algorithm package for python
Stars: ✭ 36 (-12.2%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms, optimization-algorithms
Bluepyopt
Blue Brain Python Optimisation Library
Stars: ✭ 143 (+248.78%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Evolutionary Computation Course
Jupyter/IPython notebooks about evolutionary computation.
Stars: ✭ 173 (+321.95%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Circle Evolution
Evolutionary Art Using Circles in Python
Stars: ✭ 237 (+478.05%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Harris-Hawks-Optimization-Algorithm-and-Applications
Source codes for HHO paper: Harris hawks optimization: Algorithm and applications: https://www.sciencedirect.com/science/article/pii/S0167739X18313530. In this paper, a novel population-based, nature-inspired optimization paradigm is proposed, which is called Harris Hawks Optimizer (HHO).
Stars: ✭ 31 (-24.39%)
Mutual labels:  evolutionary-algorithms, optimization-algorithms
Mealpy
A collection of the state-of-the-art MEta-heuristics ALgorithms in PYthon (mealpy)
Stars: ✭ 72 (+75.61%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
datafsm
Machine Learning Finite State Machine Models from Data with Genetic Algorithms
Stars: ✭ 14 (-65.85%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
BusterNet
No description or website provided.
Stars: ✭ 49 (+19.51%)
Mutual labels:  image-manipulation, image-analysis
Evolutionsimulator
Evolution Simulator with Box2D
Stars: ✭ 143 (+248.78%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Watchmaker
The Watchmaker Framework for Evolutionary Computation
Stars: ✭ 189 (+360.98%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Evolutionary.jl
Evolutionary & genetic algorithms for Julia
Stars: ✭ 142 (+246.34%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
machine-learning-blackjack-solution
Finding an optimal Blackjack strategy using AI
Stars: ✭ 40 (-2.44%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
EvOLuTIoN
A simple simulation in Unity, which uses genetic algorithm to optimize forces applied to cubes
Stars: ✭ 44 (+7.32%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Python Computer Vision from Scratch
This repository explores the variety of techniques commonly used to analyze and interpret images. It also describes challenging real-world applications where vision is being successfully used, both for specialized applications such as medical imaging, and for fun, consumer-level tasks such as image editing and stitching, which students can apply…
Stars: ✭ 219 (+434.15%)
Mutual labels:  image-manipulation, image-analysis
triangula
Generate high-quality triangulated and polygonal art from images.
Stars: ✭ 3,775 (+9107.32%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms
Eaopt
🍀 Evolutionary optimization library for Go (genetic algorithm, partical swarm optimization, differential evolution)
Stars: ✭ 718 (+1651.22%)
Mutual labels:  genetic-algorithm, evolutionary-algorithms

GARI

GARI (Genetic Algorithm for Reproducing Images) is a Python project that uses the PyGAD library for reproducing images using the genetic algorithm. GARI reproduces a single image using Genetic Algorithm (GA) by evolving pixel values. This project works with both color and gray images.

For implementing the genetic algorithm, the PyGAD library is used. Check its documentation here: https://pygad.readthedocs.io

IMPORTANT If you are coming for the code of the tutorial Reproducing Images using a Genetic Algorithm with Python, then it has been moved to the TutorialProject directory on 18 May 2020.

PyGAD Installation

To install PyGAD, simply use pip to download and install the library from PyPI (Python Package Index). The library lives a PyPI at this page https://pypi.org/project/pygad.

For Windows, issue the following command:

pip install pygad

For Linux and Mac, replace pip by use pip3 because the library only supports Python 3.

pip3 install pygad

PyGAD is developed in Python 3.7.3 and depends on NumPy for creating and manipulating arrays and Matplotlib for creating figures. The exact NumPy version used in developing PyGAD is 1.16.4. For Matplotlib, the version is 3.1.0.

Project Steps

The steps to follow in order to reproduce an image are as follows:

  • Read an image
  • Prepare the fitness function
  • Create an instance of the pygad.GA class with the appropriate parameters
  • Run PyGAD
  • Plot results
  • Calculate some statistics

The next sections discusses the code of each of these steps.

Read an Image

There is an image named fruit.jpg in the project which is read according to the next code.

import imageio
import numpy

target_im = imageio.imread('fruit.jpg')
target_im = numpy.asarray(target_im/255, dtype=numpy.float)

Here is the read image.

fruit

Based on the chromosome representation used in the example, the pixel values can be either in the 0-255, 0-1, or any other ranges.

Note that the range of pixel values affect other parameters like the range from which the random values are selected during mutation and also the range of the values used in the initial population. So, be consistent.

Prepare the Fitness Function

The next code creates a function that will be used as a fitness function for calculating the fitness value for each solution in the population. This function must be a maximization function that accepts 2 parameters representing a solution and its index. It returns a value representing the fitness value.

The fitness value is calculated using the sum of absolute difference between genes values in the original and reproduced chromosomes. The gari.img2chromosome() function is called before the fitness function to represent the image as a vector because the genetic algorithm can work with 1D chromosomes.

For more information about preparing the fitness function in PyGAD, please read the PyGAD's documentation.

target_chromosome = gari.img2chromosome(target_im)

def fitness_fun(solution, solution_idx):
    fitness = numpy.sum(numpy.abs(target_chromosome-solution))

    # Negating the fitness value to make it increasing rather than decreasing.
    fitness = numpy.sum(target_chromosome) - fitness
    return fitness

Create an Instance of the pygad.GA Class

It is very important to use random mutation and set the mutation_by_replacement to True. Based on the range of pixel values, the values assigned to the init_range_low, init_range_high, random_mutation_min_val, and random_mutation_max_val parameters should be changed.

If the image pixel values range from 0 to 255, then set init_range_low and random_mutation_min_val to 0 as they are but change init_range_high and random_mutation_max_val to 255.

Feel free to change the other parameters or add other parameters. Please check the PyGAD's documentation for the full list of parameters.

import pygad

ga_instance = pygad.GA(num_generations=20000,
                       num_parents_mating=10,
                       fitness_func=fitness_fun,
                       sol_per_pop=20,
                       num_genes=target_im.size,
                       init_range_low=0.0,
                       init_range_high=1.0,
                       mutation_percent_genes=0.01,
                       mutation_type="random",
                       mutation_by_replacement=True,
                       random_mutation_min_val=0.0,
                       random_mutation_max_val=1.0)

Run PyGAD

Simply, call the run() method to run PyGAD.

ga_instance.run()

Plot Results

After the run() method completes, the fitness values of all generations can be viewed in a plot using the plot_result() method.

ga_instance.plot_result()

Here is the plot after 20,000 generations.

Fitness Values

Calculate Some Statistics

Here is some information about the best solution.

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

if ga_instance.best_solution_generation != -1:
    print("Best fitness value reached after {best_solution_generation} generations.".format(best_solution_generation=ga_instance.best_solution_generation))

result = gari.chromosome2img(solution, target_im.shape)
matplotlib.pyplot.imshow(result)
matplotlib.pyplot.title("PyGAD & GARI for Reproducing Images")
matplotlib.pyplot.show()

Evolution by Generation

The solution reached after the 20,000 generations is shown below.

solution

After more generations, the result can be enhanced like what shown below.

solution

The results can also be enhanced by changing the parameters passed to the constructor of the pygad.GA class.

Here is an example of input image and how it is evolved after some iterations.

Generation 0

solution_0

Generation 1,000

solution_1000

Generation 2,500

solution_2500

Generation 4,500

solution_4500

Generation 7,000

solution_7000

Generation 8,500

solution_8500

Generation 20,000

solution

For More Information

There are different resources that can be used to get started with the building CNN and its Python implementation.

Tutorial: Reproduce Images with Genetic Algorithm

In 1 May 2019, I wrote a tutorial discussing this project. The tutorial is titled Reproducing Images using a Genetic Algorithm with Python which is published at Heartbeat. Check it at these links:

Tutorial Cover Image

Book: Practical Computer Vision Applications Using Deep Learning with CNNs

You can also check my book cited as Ahmed Fawzy Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Dec. 2018, Apress, 978-1-4842-4167-7 which discusses neural networks, convolutional neural networks, deep learning, genetic algorithm, and more.

Find the book at these links:

Fig04

Citing PyGAD - Bibtex Formatted Citation

If you used PyGAD, please consider adding a citation to the following paper about PyGAD:

@misc{gad2021pygad,
      title={PyGAD: An Intuitive Genetic Algorithm Python Library}, 
      author={Ahmed Fawzy Gad},
      year={2021},
      eprint={2106.06158},
      archivePrefix={arXiv},
      primaryClass={cs.NE}
}

Contact Us

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].