Image Generator | Genetic Algorithms | Genetic Programming | #GeneticAlgorithms #GeneticProgramming

preview_player
Показать описание

Here we have a small binary image, and we want to generate that using a genetic algorithm.
So we generated 1000 random images initially. We calculated fitness of all of them. Then we created a pool, in which we have a lot more images (copies from 1000 images) and we selected randomly 2 images and merged them randomly. New image is called baby image. We generated 1000 baby images the same way (but randomly). We removed initial 1000 images and placed newly generated baby images there.

Now baby images are more fit compared to initial ones, as we generated them from some of the fit images. And we mutated some of the genes of some of the baby images (for the sake of variety).
Then we calculated fitness again.
Best fit image was more fit then the previous best fit image.
We keep doing this until we found the perfect fit image (exact same as original image).

And this took a lot of generations and a lot of processing power.

Feel free to ask questions and don't forget to subscribe.

Some more genetic algorithms are coming soon!!!

#nirmites #ImageGenerator #GeneticAlgorithms #GeneticProgramming
Рекомендации по теме
Комментарии
Автор

Here is the code.

import random
from time import sleep
import cv2
import numpy as np


class DNA:
genes = []
fitness_score = 0

def __init__(self):
self.genes = np.array(
[random.choice([0, 255]) for i in range(11 * 12)]
)

def fitness(self):
score = 0
for i in range(len(target)):
if self.genes[i] == target[i]:
score += 1

self.fitness_score = score / len(target)

def crossover(self, obj):
child = DNA()
breakpoint = random.randrange(0, len(target))

for i in range(len(target)):
if i > breakpoint:
child.genes[i] = self.genes[i]
else:
child.genes[i] = obj.genes[i]

return child

def mutate(self):
for i in range(len(target)):
if random.random() <= mutation_rate:
self.genes[i] = random.choice([0, 255])

def get_phrase(self):
return "".join(self.genes)


population_size = int(input("Population: "))

target = cv2.imread("image.png", 0).reshape(11 * 12)
cv2.imshow("Original image", target.reshape(11, 12))

population = [DNA() for i in range(population_size)]
mutation_rate = 0.0001


iterations = 0
while True:
iterations += 1
for i in population:
i.fitness()

best = 0
for i in population:
if best < i.fitness_score:
best = i.fitness_score
phrase = np.array(i.genes.reshape(11, 12), dtype=np.uint8)
print(
"Generation:",
iterations,
" Score:",
str(best * 100)[:5],
)
cv2.imshow("Generated image", phrase)
cv2.waitKey(1)

found = True
for i in range(11 * 12):
if not phrase.reshape(11 * 12)[i] == target[i]:
found = False

if found:
cv2.waitKey(0)
break

matingPool = []

for i in range(len(population)):
n = * 100) + 1

for j in range(n):


for i in range(len(population)):
a = random.randrange(0, len(matingPool))
b = random.randrange(0, len(matingPool))

parent_a = matingPool[a]
parent_b = matingPool[b]

child = parent_a.crossover(parent_b)
child.mutate()

population[i] = child

agentNirmites
Автор

Any reason you chose to replace the entire population instead of having survivor selection? I've always had luck with (μ+λ) GAs rather than (μ, μ).

rebeccarivers
Автор

Can you generate text using this algorithm 🤔

freepythoncode
Автор

So u compared each pixel of original image with generated image and given score.and top scored 3-4 generated images with mutation added for new best score.

Everu
Автор

similar type of code available in c or c++?

keensoft
welcome to shbcf.ru