def evaluate_learning(SOM, data_type, neurons_nbr): inputs = Parameters({"alpha": Variable(start=0.6, end=0.05, nb_steps=nb_epochs), "sigma": Variable(start=0.5, end=0.001, nb_steps=nb_epochs), "data": data_type, "neurons_nbr": (neurons_nbr, neurons_nbr), "epochs_nbr": nb_epochs}) som = SOM(inputs) som.run() nb_comp = np.mean(som.nbr_quad_dist) return {"nbr_comparisons": nb_comp, "percentage_calculated": nb_comp/(neurons_nbr**2)*100}
def runSOM(self): if self.data is None: self.load_dataset() nb_epochs = self.model["nb_epochs"] if "alpha_start" not in self.model: self.model["alpha_start"] = 0.2 if "alpha_end" not in self.model: self.model["alpha_end"] = 0.05 if "sigma_start" not in self.model: self.model["sigma_start"] = 0.7 if "sigma_end" not in self.model: self.model["sigma_end"] = 0.015 if "nb_images_evals" not in self.dataset: self.dataset["nb_images_evals"] = 75 parameters = Parameters({ "alpha": Variable(start=self.model["alpha_start"], end=self.model["alpha_end"], nb_steps=nb_epochs), "sigma": Variable(start=self.model["sigma_start"], end=self.model["sigma_end"], nb_steps=nb_epochs), "data": self.data.get_data(), "neurons_nbr": (self.model["width"], self.model["height"]), "epochs_nbr": nb_epochs }) if self.model["model"] == "standard": self.map = SOM(parameters) elif self.model["model"] == "fast": self.map = FastSOM(parameters) elif self.model["model"] == "recursive": self.map = RecursiveSOM(parameters) else: print("Error : Unknown model !") # if "initialisation" not in self.codebooks: # self.codebooks["initialisation"] = self.som.neurons.tolist() if "final" in self.codebooks: self.map.neurons = np.asarray(self.codebooks["final"]) else: self.map.run() #for i in range(nb_epochs): # self.map.run_epoch() #self.codebooks["final"] = copy.deepcopy(self.map.neurons.tolist()) # for i in range(nb_epochs): # print("Epoch "+str(i+1)) # if "Epoch "+str(i + 1) not in self.codebooks: # if self.training_data is not None: # self.som.data = self.training_data.get_data(self.som.data.shape[0]) # self.som.run_epoch() # # self.codebooks["Epoch " + str(i + 1)] = copy.deepcopy(self.som.neurons.tolist()) # self.som.run_epoch() self.map.data = self.data.get_data()
def evaluate_reconstruction(SOM, data_type): inputs = Parameters({"alpha": Variable(start=0.6, end=0.05, nb_steps=nb_epochs), "sigma": Variable(start=0.5, end=0.001, nb_steps=nb_epochs), "data": data_type, "neurons_nbr": n_nbr, "epochs_nbr": nb_epochs}) st_som = StandardSOM(inputs) st_som.run() som = SOM(inputs) som.neurons = st_som.neurons return {"MSQE_R": som.mean_square_quantization_error()}
def learning(self, bkg_image): # PARAMETERS self.image_parameters = Parameters({"pictures_dim": self.pictures_dim}) data = MosaicImage(bkg_image, self.image_parameters) inputs_SOM = Parameters({"alpha": Variable(start=self.alpha_start, end=self.alpha_end, nb_steps=self.nb_epochs), "sigma": Variable(start=self.sigma_start, end=self.sigma_end, nb_steps=self.nb_epochs), "data": data.get_data(), "neurons_nbr": self.neurons_nb, "epochs_nbr": self.nb_epochs}) self.som = SOM(inputs_SOM) # RUN for i in range(self.nb_epochs): # print('Epoch', i) self.som.run_epoch() self.initial_map = self.som.get_all_winners()
def evaluate_learning(SOM, data_type): inputs = Parameters({ "alpha": Variable(start=0.6, end=0.05, nb_steps=nb_epochs), "sigma": Variable(start=0.5, end=0.001, nb_steps=nb_epochs), "data": data_type, "neurons_nbr": n_nbr, "epochs_nbr": nb_epochs }) som = SOM(inputs) som.run() msqe_r, nb_errors = som.mean_square_quantization_error_secondary() return { "MSQE_L": som.mean_square_quantization_error(), "MSDtN": som.mean_square_distance_to_neighbour(), "MSQE_R": msqe_r, "Error Nbr": nb_errors, "Error %:": nb_errors / data_type.shape[0] * 100 }
for j in np.ndindex(self.neurons_nbr): if hexagonal_distance(np.asarray(i), np.asarray(j)) <= 1: error[i] += np.mean((self.neurons[i] - self.neurons[j])**2) nb_neighbours += 1 error[i] /= nb_neighbours return np.mean(error) if __name__ == '__main__': for n in range(10, 61, 2): start = time.time() nb_epochs = 10 inputs = Parameters({ "alpha": Variable(start=0.6, end=0.05, nb_steps=nb_epochs), "sigma": Variable(start=0.5, end=0.2, nb_steps=nb_epochs), "data": MosaicImage( Image.open( "/users/yabernar/workspace/watSOM/Code/fast_som/Elijah.png" ), Parameters({"pictures_dim": [10, 10]})).get_data(), "neurons_nbr": (n, n), "epochs_nbr": nb_epochs }) som = FastSOM(inputs) som.run()
temporal_ROI = (1, 200) plot = None bkg = Image.open(chosen_path + "/input/" + 'bkg.jpg') # bkg = Image.open(path2 + video + '{0:05d}.png'.format(1)) # bkg = Image.open(path3 + "example_base.png") ############ # LEARNING # ############ pictures_dim = [10, 10] parameters = Parameters({"pictures_dim": pictures_dim}) data = MosaicImage(bkg, parameters) nb_epochs = 50 inputs_SOM = Parameters({ "alpha": Variable(start=0.5, end=0.25, nb_steps=nb_epochs), "sigma": Variable(start=0.1, end=0.03, nb_steps=nb_epochs), "data": data.get_data(), "neurons_nbr": (10, 10), "epochs_nbr": nb_epochs }) som = SOM(inputs_SOM) for i in range(nb_epochs): print('Epoch ', i) som.run_epoch() original = data.image reconstructed = Image.fromarray( data.reconstruct(som.get_reconstructed_data()))
error[i] = np.mean(np.abs(self.data[i] - self.neurons[winners[i]])) return np.mean(error) def square_error(self, winners=None): if winners is None: winners = self.get_all_winners() error = np.zeros(winners.shape) for i in np.ndindex(winners.shape): error[i] = np.mean((self.data[i] - self.neurons[winners[i]])**2) return np.mean(error) if __name__ == '__main__': start = time.time() nb_epochs = 50 inputs = Parameters({"alpha": Variable(start=0.6, end=0.05, nb_steps=nb_epochs), "sigma": Variable(start=0.5, end=0.001, nb_steps=nb_epochs), "data": sierpinski_carpet(200), "neurons_nbr": (10, 10), "epochs_nbr": nb_epochs}) som = SOM(inputs) som.run() end = time.time() print("Executed in " + str(end - start) + " seconds.") print(som.mean_error(som.get_all_winners())) print(som.square_error(som.get_all_winners()))