Facebook
From Social Monkey, 1 Year ago, written in Python.
This paste is a reply to Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: xx from Walloping Bird - view diff
Embed
Download Paste or View Raw
Hits: 131
  1. pip install sewar
  2. from numpy.random import randint
  3. from numpy.random import rand
  4. import numpy as np
  5. from PIL import Image
  6. import cv2
  7. import os
  8. import random
  9. from skimage import metrics
  10. import argparse
  11. from sewar.full_ref import mse
  12. import pandas as pd
  13. import glob
  14. import time
  15. from google.colab import drive
  16. drive.mount('/content/drive')
  17. !unzip "/content/drive/My Drive/zbiór_danych/tiny-imagenet-200-gray.zip"
  18.  
  19. def fitness(x, path):
  20.     im_pil = Image.open(path)
  21.     im_pil.save("picture.jpeg", qtables = {0: list(map(int, x))} )
  22.     im_pil2 = Image.open("picture.jpeg")
  23.  
  24.     imageA = np.array(im_pil)
  25.     if(len(imageA.shape)==3):
  26.         imageA = imageA[:, :, ::-1]
  27.         grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
  28.     else:
  29.         grayA = imageA
  30.  
  31.     imageB = np.array(im_pil2)
  32.     if(len(imageB.shape)==3):
  33.         imageB = imageB[:, :, ::-1]
  34.         grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
  35.     else:
  36.         grayB = imageB
  37.    
  38.     score = mse(grayA,grayB)
  39.     compression = os.path.getsize("picture.jpeg")/os.path.getsize(path)
  40.     return score + 30 * compression
  41.  
  42. def decode(bounds, n_bits, bitstring):
  43.     decoded = list()
  44.     largest = 2**n_bits
  45.     for i in range(len(bounds)):
  46.         start, end = i * n_bits, (i * n_bits)+n_bits
  47.         substring = bitstring[start:end]
  48.         chars = ''.join([str(s) for s in substring])
  49.         integer = int(chars, 2)
  50.         value = bounds[i][0] + int((integer/largest) * (bounds[i][1] - bounds[i][0]))
  51.         decoded.append(value)
  52.     return decoded
  53.  
  54. def selection_ranking(pop, scores, k):
  55.     pop = [y for _,y in sorted(zip(scores,pop),key=lambda x: x[0])]
  56.     pop[0:k] = pop[len(pop)-k:len(pop)]  
  57.     return pop
  58.  
  59. def crossover(p1, p2, r_cross):
  60.     c1, c2 = p1.copy(), p2.copy()
  61.     if rand() < r_cross:
  62.         pt = randint(1, len(p1)-2)
  63.         c1 = p1[:pt] + p2[pt:]
  64.         c2 = p2[:pt] + p1[pt:]
  65.     return [c1, c2]
  66.  
  67. def mutation(bitstring, r_mut):
  68.     for i in range(len(bitstring)):
  69.         if rand() < r_mut:
  70.             bitstring[i] = 1 - bitstring[i]
  71.      
  72. def genetic_algorithm(fitness, bounds, n_bits, n_iter, n_pop, r_cross, r_mut, start_value, path):
  73.     new_x = [int(example * 2**n_bits / bounds[0][1]) for example in start_value]
  74.     newList = list()
  75.     for j in new_x:
  76.         newList.extend([int(x) for x in bin(j)[2:].zfill(n_bits)])
  77.     pop = [newList for _ in range(n_pop)]
  78.     best, best_eval = pop[0], fitness(decode(bounds, n_bits, pop[0]),path)
  79.     for gen in range(n_iter):
  80.         decoded = [decode(bounds, n_bits, p) for p in pop]
  81.         scores = [fitness(d,path) for d in decoded]
  82.         for i in range(n_pop):
  83.             if scores[i] < best_eval:
  84.                 best, best_eval = pop[i], scores[i]
  85.                 print(">%d, new best f(%s) = %f" % (gen,  decoded[i], scores[i]))
  86.         selected = selection_ranking(pop, scores, 5)
  87.         children = list()
  88.         for i in range(0, n_pop, 2):
  89.             p1, p2 = selected[i], selected[i+1]
  90.             for c in crossover(p1, p2, r_cross):
  91.                 mutation(c, r_mut)
  92.                 children.append(c)
  93.         pop = children
  94.     return [best, best_eval]