neuralia.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-"
  3. """
  4. NeuraLiA 2020 - by psy (epsylon@riseup.net)
  5. """
  6. import time
  7. import glob
  8. import os.path
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11. from PIL import Image
  12. from numpy import array
  13. from scipy.ndimage.interpolation import zoom
  14. from sklearn.cluster import KMeans
  15. from skimage import measure
  16. VERSION = "v:0.2beta"
  17. RELEASE = "21052020"
  18. SOURCE1 = "https://code.03c8.net/epsylon/neuralia"
  19. SOURCE2 = "https://github.com/epsylon/neuralia"
  20. CONTACT = "epsylon@riseup.net - (https://03c8.net)"
  21. print(75*"=")
  22. print(" _ _ ____ _ ___ _ ")
  23. print("| \ | | ___ _ _| _ \ __ _| | |_ _| / \ ")
  24. print("| \| |/ _ \ | | | |_) / _` | | | | / _ \ ")
  25. print("| |\ | __/ |_| | _ < (_| | |___ | | / ___ \ ")
  26. print("|_| \_|\___|\__,_|_| \_\__,_|_____|___/_/ \_| by psy")
  27. print(" ")
  28. print(75*"=","\n")
  29. print('"Advanced Recognition {S.Y.A.} Neural Network"\n')
  30. print("\n"+"-"*15+"\n")
  31. print(" * VERSION: ")
  32. print(" + "+VERSION+" - (rev:"+RELEASE+")")
  33. print("\n * SOURCES:")
  34. print(" + "+SOURCE1)
  35. print(" + "+SOURCE2)
  36. print("\n * CONTACT: ")
  37. print(" + "+CONTACT+"\n")
  38. print("-"*15+"\n")
  39. print("="*50)
  40. simulation = input("\nDo you wanna start? (Y/n): ")
  41. if simulation == "n" or simulation == "N":
  42. import sys
  43. sys.exit()
  44. memo_path = "stored/memo.dat"
  45. input_neurons = 3
  46. hidden_neurons = 62
  47. output_neurons = 1
  48. class Questioning_Network(object):
  49. def make_question(self, images_dataset):
  50. concepts_dataset = {}
  51. i = 0
  52. for k, v in images_dataset.items():
  53. i = i + 1
  54. img = Image.fromarray(v, 'RGB')
  55. img.show()
  56. answer = input("\n[AI] -> Asking: 'What do you think is this? (ex: building)' -> inputs/"+str(k)+"\n")
  57. print("\n[You]-> Replied: "+str(answer))
  58. concepts_dataset[i] = str(answer)
  59. with open(memo_path, 'w') as data:
  60. data.write(str(concepts_dataset))
  61. print("[AI] -> Memorizing ...")
  62. time.sleep(1)
  63. print("")
  64. class Conceptual_Network(object):
  65. def extract_concepts(self):
  66. concepts_dataset = {}
  67. f = open(memo_path, "r")
  68. concepts_dataset = f.read()
  69. f.close()
  70. return concepts_dataset
  71. class Visual_Network(object):
  72. def __init__(self):
  73. print("\n"+"="*75+"\n")
  74. images = glob.glob("inputs/*")
  75. for image in images:
  76. image = image.replace("inputs/","")
  77. if not os.path.isfile('outputs/'+image):
  78. im = plt.imread("inputs/"+image)
  79. print("[AI] -> Visualizing: "+str(image))
  80. im_small = zoom(im, (1,0,1))
  81. h,w = im.shape[:2]
  82. im_small_long = im.reshape((h * w, 3))
  83. im_small_wide = im.reshape((h,w,3))
  84. km = KMeans(n_clusters=3)
  85. km.fit(im_small_long)
  86. cc = km.cluster_centers_.astype(np.uint8)
  87. out = np.asarray([cc[i] for i in km.labels_]).reshape((h,w,3))
  88. seg = np.asarray([(1 if i == 1 else 0)
  89. for i in km.labels_]).reshape((h,w))
  90. contours = measure.find_contours(seg, 0.4, fully_connected="high")
  91. simplified_contours = [measure.approximate_polygon(c, tolerance=4) for c in contours]
  92. plt.figure(figsize=(5,10))
  93. for n, contour in enumerate(simplified_contours):
  94. plt.plot(contour[:, 1], contour[:, 0], linewidth=2)
  95. plt.ylim(h,0)
  96. print("[AI] -> Analyzing: "+str(image))
  97. plt.axis('off')
  98. plt.savefig('outputs/'+image, bbox_inches='tight', transparent=True, pad_inches=0)
  99. else:
  100. print("[AI] -> Recognizing: "+str(image))
  101. def extract_dataset(self):
  102. images_dataset = {}
  103. images = glob.glob("inputs/*")
  104. for image in images:
  105. image = image.replace("inputs/","")
  106. print("[AI] -> Remembering: "+str(image))
  107. img = Image.open('outputs/'+image)
  108. dataset = np.array(img)
  109. images_dataset[image] = dataset
  110. return images_dataset
  111. class Neural_Network(object):
  112. def __init__(self):
  113. self.inputSize = input_neurons
  114. self.hiddenSize = hidden_neurons
  115. self.outputSize = output_neurons
  116. self.W1 = np.random.randn(self.inputSize, self.hiddenSize)
  117. self.W2 = np.random.randn(self.hiddenSize, self.outputSize)
  118. def forward(self, X):
  119. self.z = np.dot(X, self.W1)
  120. self.z2 = self.sigmoid(self.z)
  121. self.z3 = np.dot(self.z2, self.W2)
  122. o = self.sigmoid(self.z3)
  123. return o
  124. def sigmoid(self, s):
  125. return 1/(1+np.exp(-s))
  126. def sigmoidPrime(self, s):
  127. return s*(1-s)
  128. def backward(self, X, y, o):
  129. self.o_error = y - o
  130. self.o_delta = self.o_error*self.sigmoidPrime(o)
  131. self.z2_error = self.o_delta.dot(self.W2.T)
  132. self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2)
  133. self.W1 += X.T.dot(self.z2_delta)
  134. self.W2 += self.z2.T.dot(self.o_delta)
  135. def train(self, X, y):
  136. o = self.forward(X)
  137. self.backward(X, y, o)
  138. def predict(self, dataset, results):
  139. score = self.forward(xPredicted)
  140. self.final_reply(score, dataset, results)
  141. def final_reply(self, score, image, results):
  142. concepts_dataset = { line.split()[0] : line.split()[1] for line in open(memo_path) }
  143. for k, v in concepts_dataset.items():
  144. if "{" in k:
  145. k = k.replace("{", "")
  146. if ":" in k:
  147. k = k.replace(":", "")
  148. if int(k) == int(score[0]):
  149. if "," in v:
  150. v = v.replace(",", "")
  151. results[str(image)] = str(v)
  152. else:
  153. results[str(image)] = "UNKNOWN ..."
  154. # procedural (high) sub-class
  155. NN = Neural_Network()
  156. VN = Visual_Network()
  157. CN = Conceptual_Network()
  158. QN = Questioning_Network()
  159. # procedural (low) sub-class
  160. images_dataset = VN.extract_dataset()
  161. concepts_dataset = CN.extract_concepts()
  162. if not concepts_dataset:
  163. QN.make_question(images_dataset)
  164. else:
  165. t = 0
  166. prev_loss = None
  167. results = {}
  168. for k, v in images_dataset.items():
  169. dataset = v/np.amax(v, axis=0)
  170. for data in dataset:
  171. X = data
  172. xPredicted = X
  173. y_factor = 0.0001 # reply condition factor
  174. i = 0
  175. for concept in concepts_dataset:
  176. i = i + 1
  177. y = np.array([i], dtype=int)
  178. while True:
  179. loss = np.mean(np.square(y - NN.forward(X)))
  180. if prev_loss == loss:
  181. break
  182. else:
  183. if loss > y_factor:
  184. t = t + 1
  185. print("\n"+"="*75)
  186. print("-> ROUNDS (Learning): "+str(t))
  187. print("="*75)
  188. print("\n + Question (image): inputs/" + str(k))
  189. print(" + Current Answer: " + str(y))
  190. print(" + Loss: [" + str(loss)+"]")
  191. print(" + YFactor: ["+ str(y_factor)+"]")
  192. NN.train(X, y)
  193. prev_loss = loss
  194. y_factor = y_factor + 0.0010
  195. else:
  196. break
  197. NN.predict(k, results)
  198. print("\n"+"="*75)
  199. total_neurons = input_neurons + hidden_neurons + output_neurons
  200. print("-> NEURONS: ["+str(total_neurons)+"] (Input: ["+str(input_neurons)+"] | Hidden: ["+str(hidden_neurons)+"] | Output: ["+str(output_neurons)+"])")
  201. print("="*75)
  202. print("\n"+"[AI] -> Replying ...\n")
  203. for k, v in results.items():
  204. v = v.replace("}", "")
  205. print(" + Image: inputs/"+str(k)+" -> "+str(v))
  206. print("\n"+"="*75)