In [1]:
import matplotlib.pyplot as plt
try:
xrange = xrange
except:
xrange = range
from PIL import ImageGrab
import cv2
import time
import math
import datetime
import random
import io
from lib.getkeys import key_check
from lib.reinforcement import Qnetwork,updateTarget,updateTargetGraph
from lib.SQL import SQLCalls
from sys import stdout
import sqlite3
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np
import os
from PIL import Image
from keras import backend as K
from keras.models import load_model
from keras.utils import plot_model
from keras.models import Model,Sequential
from keras.layers import Input, LSTM, Dense, Dropout, Conv2D, MaxPooling2D,concatenate, Flatten, GlobalAveragePooling2D
from keras.utils import to_categorical
SQL=SQLCalls()
In [6]:
def make_model():
image_model_inputs = Input(shape=(580,580,3),dtype='float32',name='main_image')
image_model=Conv2D(16, (2, 2), padding='valid', activation='relu')(image_model_inputs)
image_model=Conv2D(16, (2, 2), padding='valid', activation='relu')(image_model)
image_model=Conv2D(32, (1,1),strides=2, padding='valid', activation='relu')(image_model)
image_model=Conv2D(32, (1,1),strides=2, padding='valid', activation='relu')(image_model)
image_model=Conv2D(8, (1,1),strides=3, padding='same', activation='relu')(image_model)
image_model=Conv2D(8, (1,1),strides=2, padding='same', activation='relu')(image_model)
image_model=Flatten()(image_model)
gene_model_inputs = Input(shape=(13,13,12),dtype='float32',name='gene_image')
gene_model=Conv2D(16, (2, 2), padding='valid', activation='relu')(gene_model_inputs)
gene_model=Conv2D(16, (2, 2), padding='valid', activation='relu')(gene_model)
# gene_model=Conv2D(8, (1,1),strides=3, padding='same', activation='relu')(gene_model)
#gene_model=Conv2D(8, (1,1),strides=2, padding='same', activation='relu')(gene_model)
gene_model=Flatten()(gene_model)
combined_model=concatenate([image_model,gene_model])
combined_model=Dense(32, activation='relu')(combined_model)
combined_model=Dense(8, activation='relu')(combined_model)
combined_model_preditions=Dense(2, activation='softmax')(combined_model)
model=Model(inputs=[image_model_inputs,gene_model_inputs],outputs=combined_model_preditions)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
return model
model=make_model()
In [25]:
model = load_model('dqn_frozen_model.h5')
In [3]:
def setup_genomes():
BoxRadius=6
BoxLength=BoxRadius*2+1
BoxArea=(BoxLength)*(BoxLength)
gene_image=np.empty([len(Genomes),BoxLength,BoxLength,12])
gene_image.fill(0)
BUTTON_AMOUNT=6
for Genome_Num,Genome in enumerate(Genomes):
for gene in Genome:
genome_type=0
#print(gene[0],Genome_Num,BoxArea*2)
if gene[0]<BoxArea:
pass
#print("Normal Input")
elif gene[0]>BoxArea*2:
#print("bias")
continue
else:
pass
# print("Inverse Input")
genome_type+=BUTTON_AMOUNT
genome_type+=int(gene[1]-1000001)
if genome_type>=0:
# print ,Y,Type(Type of Input,Button Pressed)
gene_image[Genome_Num][int(gene[0]%(BoxArea)//BoxLength)][int(gene[0]%(BoxArea)%13)][genome_type]=gene[2]
return gene_image
Genomes=SQL.GatherGenomes()
gene_images=setup_genomes()
In [9]:
def adapt_array(arr):
"""
http://stackoverflow.com/a/31312102/190597 (SoulNibbler)
"""
out = io.BytesIO()
np.save(out, arr)
out.seek(0)
return sqlite3.Binary(out.read())
def convert_array(text):
out = io.BytesIO(text)
out.seek(0)
return np.load(out)
sqlite3.register_adapter(np.ndarray, adapt_array)
# Converts TEXT to np.array when selecting
sqlite3.register_converter("array", convert_array)
con=sqlite3.connect('DQN.db', detect_types=sqlite3.PARSE_DECLTYPES,isolation_level=None)
cur = con.cursor()
cur.execute("PRAGMA synchronous = OFF;")
cur.execute("PRAGMA journal_mode=WAL;")
cur.execute("PRAGMA read_uncommitted = true;")
cur.execute("SELECT GenomeNum,Gene,GeneContent FROM Genes ORDER BY Genome,Gene")
sql = '''Select image
from example_images
WHERE score=0 and GenomeKey like '%90%' LIMIT 1'''
cur.execute(sql)
results=np.array(cur.fetchone())[0]
cur.execute("PRAGMA synchronous = OFF;")
cur.execute("PRAGMA journal_mode=WAL;")
cur.execute("PRAGMA read_uncommitted = true;")
cur.execute("SELECT GenomeNum,Gene,GeneContent FROM Genes ORDER BY Genome,Gene")
sql = '''Select geneimage
from example_genes where GenomeKey like '%05:43:07.450%' LIMIT 32'''
cur.execute(sql)
results2=np.array(cur.fetchall()).reshape([32,13,13,12])
Image.fromarray(results,'RGB')
Image.fromarray(Images[0],'RGB')
Out[9]:
In [8]:
image_duplicated=np.tile(Images[0], (32,1,1,1))
print(image_duplicated.shape)
print(results2.shape)
history=model.predict([image_duplicated,results2],batch_size=16)
print(np.sum(history[:,0]))
print(np.argmax((history[:,0])))
print(np.sum(history[:,1]))
print(max(history[:,0]))
print(history)
In [24]:
results=(history[:,0])
UsedGenomes=np.ones(Genomes.shape[0])
results2=results*UsedGenomes
for i in range(10):
results2=results*UsedGenomes
results3=np.argmax(results2)
print(results3)
UsedGenomes[results3]=0
In [5]:
Images=[]
for i in range(150):
sql = '''Select image
from example_images
WHERE id=? LIMIT 1'''
cur.execute(sql,(i+1,))
results=np.array(cur.fetchone())[0]
image_duplicated=np.tile(results, (32,1,1,1))
history=model.predict([image_duplicated,results2],batch_size=16)
if np.sum(history[:,0])>2 and np.sum(history[:,0])<30:
print(len(Images),np.sum(history[:,0]))
Images.append(results)
In [111]:
Image.fromarray(Images[2],'RGB')
Out[111]:
In [ ]: