def test_if_values_is_valids(): v = np.array([ [0.2, 0.1, 0.1], [0.3, 0.2, 0.1], [0.3, 0.3, 0.2], [0.2, 0.3, 0.3], [0.1, 0.2, 0.3], ]) valueMax = 1 IC = layerF0(v, valueMax) A = ARTFUZZY(IC) A.train() actual = [0.1, 0.1, 0.1, 0.7, 0.7, 0.7] assert all([a == b for a, b in zip(actual, A.W[0])])
from src.neural_networks.art_fuzzy import ARTFUZZY A = np.array([[0.25, 0.25], [0.25, 0.75], [0.75, 0.25], [0.75, 0.75]]) B = np.array([[0.25], [0.75], [0.75], [0.25]]) AC = layerF0(A, 1) BC = layerF0(B, 1) WAB = np.ones([AC.shape[0], BC.shape[0]]) rhoA = 0.5 rhoB = 0.9 rhoAB = 0.6 ArtA = ARTFUZZY(AC, rho=rhoA) ArtB = ARTFUZZY(BC, rho=rhoB) categoriesA = ArtA.categories() categoriesB = ArtB.categories() for i in range(0, len(ArtB.I)): championB = max(categoriesB) championIndexB = categoriesB.index(championB) print() if ArtB.hadRessonance(ArtB.I[i], ArtB.W[championIndexB]): ArtB.W[championIndexB] = ArtB.learn(ArtB.I[i], ArtB.W[championIndexB])
def test_If_W_isintance_numpy(): A = ARTFUZZY([1.0, 2.0]) assert isinstance(A.I, np.ndarray)
from src.neural_networks.art_fuzzy import ARTFUZZY from src.utils.functions import * import numpy as np v = np.array([ [0.2, 0.1, 0.1], [0.3, 0.2, 0.1], [0.3, 0.3, 0.2], [0.2, 0.3, 0.3], [0.1, 0.2, 0.3], ]) valueMax = 1 IC = layerF0(v, valueMax) r = ARTFUZZY(IC) r.train() print(r.W) print(r.Js)
x = [] for img_path in imgs_paths: for i in imgs: img = Image.open(img_path + str(i)) im = img.convert("L") x.append(list(im.getdata())) arr = np.array(x) valueMax = 255 IC = layerF0(arr, valueMax) print(IC) r = ARTFUZZY(IC, rho=0.95) r.train() print() #print(r.W) print(r.Js) circ_pesos = [] rect_pesos = [] for i in r.Js: if i[0] < 40 and i[1] not in circ_pesos: circ_pesos.append(i[1]) elif i[0] > 40 and i[1] not in rect_pesos: rect_pesos.append(i[1])
class ARTMAPFUZZY(ART): ArtA = [] ArtB = [] rho = 0 WAB = [] championsA = [] Map = [] def __init__(self, INPUT, OUTPUT, rhoINPUT=0.5, rhoOUTPUT=0.5, rho=0.5): self.ArtA = ARTFUZZY(INPUT, rho=rhoINPUT) self.ArtB = ARTFUZZY(OUTPUT, rho=rhoOUTPUT) self.rho = rho self.WAB = np.ones([INPUT.shape[0], OUTPUT.shape[0]]) def train(self): print("Treinando ...") for i in range(0, len(self.WAB)): self.ArtB.match(i) championIndexB = self.ArtB.getIndexOfChampion() #print("-------", i) #print(championIndexB, self.ArtB.getValueOfChampion()) categories = self.ArtA.categories() championA = self.valueOfChampion(categories) championIndexA = self.indexOfChampion(categories) while championIndexA in self.championsA: categories[championIndexA] = 0 championA = self.valueOfChampion(categories) championIndexA = self.indexOfChampion(categories) else: self.championsA.append(championIndexA) #print("Champion A:", championIndexA, championA) while championA != 0: #print("Vigilance test Art A: ", self.vigilanceValue(self.ArtA.I[i], self.ArtA.W[championIndexA]), self.rho) if self.ArtA.hadRessonance(self.ArtA.I[i], self.ArtA.W[championIndexA]): #print("Vigilance test Match tracking: ", self.vigilanceValue(self.ArtB.Y[championIndexB], self.WAB[championIndexA])) if self.hadRessonance(self.ArtB.Y[championIndexB], self.WAB[championIndexA], self.rho): self.ArtA.W[championIndexA] = self.ArtA.learn( self.ArtA.I[i], self.ArtA.W[championIndexA]) self.ArtA.activate(championIndexA) self.ArtA.Js.append([i, championIndexA]) #print("Ressonance!") #print("Ativacao", self.ArtB.Y[championIndexB], self.WAB[championIndexA], championIndexB) self.WAB[championIndexA] = self.activate( self.WAB[championIndexA], championIndexB) #print() break else: categories[championIndexA] = 0 championA = self.valueOfChampion(categories) championIndexA = self.indexOfChampion(categories) x = self.AND(self.ArtA.I[i], self.ArtA.W[championIndexA]) newRho = (sum(x) / sum(self.ArtA.I[i])) #print(newRho, "NOVO", x, self.ArtA.I[i]) self.ArtA._rho = newRho else: #print("NAO PASSOU", championIndexA, championA) categories[championIndexA] = 0 championA = self.valueOfChampion(categories) championIndexA = self.indexOfChampion(categories) def activate(self, W, i): temp = np.zeros(len(W)) temp[i] = 1 return list(temp) def test(self, INPUT, rho): categories = self.ArtA.categories() championA = self.valueOfChampion(categories) championIndexA = self.indexOfChampion(categories) while championA != 0: if self.hadRessonance(INPUT, self.ArtA.I[championIndexA], rho): return self.WAB[championIndexA] else: categories[championIndexA] = 0 championA = self.valueOfChampion(categories) championIndexA = self.indexOfChampion(categories) return -1 def testMapped(self, INPUT, rho): categories = self.ArtA.categories() championA = self.valueOfChampion(categories) championIndexA = self.indexOfChampion(categories) while championA != 0: if self.hadRessonance(INPUT, self.ArtA.I[championIndexA], rho): t = list(self.WAB[championIndexA]) artB = list(self.ArtB.W[t.index(1)]) s = [str(i) for i in artB] return { "index": t.index(1), "value": self.WAB[championIndexA], "ArtB": artB, "id": "".join(s).replace(".", "") } else: categories[championIndexA] = 0 championA = self.valueOfChampion(categories) championIndexA = self.indexOfChampion(categories) return -1
def __init__(self, INPUT, OUTPUT, rhoINPUT=0.5, rhoOUTPUT=0.5, rho=0.5): self.ArtA = ARTFUZZY(INPUT, rho=rhoINPUT) self.ArtB = ARTFUZZY(OUTPUT, rho=rhoOUTPUT) self.rho = rho self.WAB = np.ones([INPUT.shape[0], OUTPUT.shape[0]])
import numpy as np from PIL import Image from src.neural_networks.art_fuzzy import ARTFUZZY img = Image.open( r"C:/Users/ht3000052/Documents/artmap-fuzzy-tcc/samples/circ.png").convert( 'RGBA') im = img.convert("L") arr = np.array(list(im.getdata())) arr = np.divide(arr, 255) r = ARTFUZZY(arr) print() print(arr.size) # arr = np.fromiter(iter(im.getdata()), np.uint8) # arr.resize(im.height, im.width) # arr ^= 0xFF # invert # inverted_im = Image.fromarray(arr, mode='L') # inverted_im.show() quit()