def template_self_organization(file, rows, cols, time, structure, init_type=None, init_radius=None, init_rate=None, umatrix=False, pmatrix=False, awards=False): parameters = som_parameters() if (init_type is not None): parameters.init_type = init_type if (init_radius is not None): parameters.init_radius = init_radius if (init_rate is not None): parameters.init_learn_rate = init_rate sample = read_sample(file) network = som(rows, cols, structure, parameters, True) network.train(sample, time) network.show_network(False, dataset=False) if (umatrix is True): network.show_distance_matrix() if (pmatrix is True): network.show_density_matrix() if (awards is True): network.show_winner_matrix()
def templateTestAwardNeurons(file, rows, cols, time, expected_result, autostop, ccore_flag, parameters = None, **kwargs): store_load = kwargs.get('store_load', False) types = [type_conn.func_neighbor, type_conn.grid_eight, type_conn.grid_four, type_conn.honeycomb] sample = read_sample(file) if (parameters is None): parameters = som_parameters() for stucture in types: network = som(rows, cols, stucture, parameters, ccore = ccore_flag) if store_load: dump_network = pickle.dumps(network) network = pickle.loads(dump_network) network.train(sample, time, autostop) winners = network.get_winner_number() assert winners == len(expected_result) if sorted(network.awards) != expected_result: network.show_network(awards = True) assert sorted(network.awards) == expected_result total_capture_points = 0 for points in network.capture_objects: total_capture_points += len(points) assert total_capture_points == sum(expected_result) del network
def templateTestAwardNeurons(self, file, rows, cols, time, expected_result, autostop=False, ccore_flag=False, parameters=None): types = [ type_conn.func_neighbor, type_conn.grid_eight, type_conn.grid_four, type_conn.honeycomb ] sample = read_sample(file) if (parameters is None): parameters = som_parameters() for stucture in types: network = som(rows, cols, stucture, parameters, ccore=ccore_flag) network.train(sample, time, autostop) if (sorted(network.awards) != expected_result): network.show_network(awards=True) print(sorted(network.awards)) assert sorted(network.awards) == expected_result total_capture_points = 0 for points in network.capture_objects: total_capture_points += len(points) assert total_capture_points == sum(expected_result) del network
def testSomVisualizationByCore(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE4) parameters = som_parameters() network = som(5, 5, type_conn.grid_eight, parameters, ccore=True) network.train(sample, 100, True) network.show_network() network.show_winner_matrix() network.show_distance_matrix() network.show_density_matrix()
def testSomVisualizationByCore(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE4) parameters = som_parameters() network = som(5, 5, type_conn.grid_eight, parameters, ccore = True) network.train(sample, 100, True) network.show_network() network.show_winner_matrix() network.show_distance_matrix() network.show_density_matrix()
def random_state(rows, cols, connections, random_state, ccore_flag): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1) params = som_parameters() params.random_state = random_state network_1 = som(rows, cols, connections, ccore=ccore_flag) steps_1 = network_1.train(sample, 100, True) network_2 = som(rows, cols, connections, ccore=ccore_flag) steps_2 = network_2.train(sample, 100, True) assert steps_1 == steps_2 assert network_1.weights == network_2.weights assert network_1.capture_objects == network_2.capture_objects assert network_1.awards == network_2.awards
def testDoubleTrain(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1); parameters = som_parameters(); network = som(2, 2, type_conn.grid_eight, parameters, ccore = False); network.train(sample, 100, False); network.train(sample, 100, False); assert sum(network.awards) == len(sample); total_capture_points = 0; for points in network.capture_objects: total_capture_points += len(points); assert total_capture_points == len(sample);
def process(self): """! @brief Performs cluster analysis by competition between neurons in self-organized map. @return (somsc) Returns itself (SOM Simple Clustering instance). @see get_clusters() """ params = som_parameters() params.random_state = self.__random_state self.__network = som(1, self.__amount_clusters, type_conn.grid_four, params, self.__ccore) self.__network.train(self.__data_pointer, self.__epouch, True) return self
def template_self_organization(file, rows, cols, time, structure, init_type = None, init_radius = None, init_rate = None, umatrix = False, pmatrix = False, awards = False): parameters = som_parameters(); if (init_type is not None): parameters.init_type = init_type; if (init_radius is not None): parameters.init_radius = init_radius; if (init_rate is not None): parameters.init_learn_rate = init_rate; sample = read_sample(file); network = som(rows, cols, structure, parameters, True); network.train(sample, time); network.show_network(False, dataset = False); if (umatrix is True): network.show_distance_matrix(); if (pmatrix is True): network.show_density_matrix(); if (awards is True): network.show_winner_matrix();
def templateTestAwardNeurons(self, file, rows, cols, time, expected_result, autostop = False, ccore_flag = False, parameters = None): types = [type_conn.func_neighbor, type_conn.grid_eight, type_conn.grid_four, type_conn.honeycomb]; sample = read_sample(file); if (parameters is None): parameters = som_parameters(); for stucture in types: network = som(rows, cols, stucture, parameters, ccore = ccore_flag); network.train(sample, time, autostop); if (sorted(network.awards) != expected_result): network.show_network(awards = True); print(sorted(network.awards)); assert sorted(network.awards) == expected_result; total_capture_points = 0; for points in network.capture_objects: total_capture_points += len(points); assert total_capture_points == sum(expected_result); del network;
x = round(x) y = round(y) ## 3. 좌표의 BGR 값 받아오기 bgr_val.append([img_org.item(y, x, 0), img_org.item(y, x, 1), img_org.item(y, x, 2)]) print('selected_pixel_bgr :\n', bgr_val) ## 5. SOM sample = bgr_val # create SOM parameters parameters = som_parameters() # initialization of initial neuron weights, radius, rateOfLearning, autostop # create self-organized feature map with size 7x7 rows = 6 # five rows cols = 6 # five columns structure = type_conn.honeycomb # each neuron has max. four neighbors. network = som(rows, cols, structure, parameters) # train network on sample during 100 epochs. network.train(sample, 50) # simulate trained network using randomly modified point from input dataset. index_point = random.randint(0, len(sample) - 1) point = sample[index_point] # obtain randomly point from data point[0] += random.random() * 0.2 # change randomly X-coordinate
def testOneDimensionSampleSimple7Cluster(self): parameters = som_parameters(); parameters.init_type = type_init.random_surface; self.templateTestAwardNeurons(SIMPLE_SAMPLES.SAMPLE_SIMPLE7, 2, 1, 100, [10, 10], True, False, parameters);
def testOneDimensionSampleSimple7ClusterByCore(self): parameters = som_parameters() parameters.init_type = type_init.random_surface SomTestTemplates.templateTestAwardNeurons(SIMPLE_SAMPLES.SAMPLE_SIMPLE7, 2, 1, 100, [10, 10], True, True, parameters)
def testOneDimensionSampleSimple7Cluster(self): parameters = som_parameters(); parameters.init_type = type_init.random_surface; SomTestTemplates.templateTestAwardNeurons(SIMPLE_SAMPLES.SAMPLE_SIMPLE7, 2, 1, 100, [10, 10], True, False, parameters);
def testOneDimensionSampleSimple7ClusterByCore(self): parameters = som_parameters() parameters.init_type = type_init.random_surface self.templateTestAwardNeurons(SIMPLE_SAMPLES.SAMPLE_SIMPLE7, 2, 1, 100, [10, 10], True, True, parameters)
from pyclustering.nnet.som import som, type_conn, type_init, som_parameters from pyclustering.samples.definitions import FCPS_SAMPLES import csv # read sample 'Lsun' from file # sample = read_sample(FCPS_SAMPLES.SAMPLE_LSUN) # with open('C:/flashwoman/Object-detection/testfiles_sey/books_coord.csv', 'r') as f: # reader = csv.reader(f) # sample = list(reader) # # print(len(sample)) # required data_shape : [[2.0, 3.0], [0.387577, 0.268546], [0.17678, 0.582963], [3.277701, 0.814082], ...] # create SOM parameters parameters = som_parameters() # create self-organized feature map with size 7x7 rows = 10 # five rows cols = 10 # five columns structure = type_conn.grid_four # .grid_four : each neuron has max. four neighbors. / .grid_eight network = som(rows, cols, structure, parameters) # train network on 'coord' sample during 100 epochs. network.train(sample, 100) # simulate trained network using randomly modified point from input dataset. index_point = random.randint(0, len(sample) - 1) point = sample[index_point] # obtain randomly point from data point[0] += random.random() * 0.2 # change randomly X-coordinate point[1] += random.random() * 0.2 # change randomly Y-coordinate