From f09861a01c3743cfcda89430c1aef554b810058c Mon Sep 17 00:00:00 2001 From: Denys Seredenko Date: Tue, 26 Nov 2024 16:52:53 +0100 Subject: [PATCH] READY --- src/all_feature_vectors.csv | 3 +- src/classes/neural/network.py | 4 +- src/main.py | 85 ++++++++++++++++++----------------- src/training.csv | 1 - 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/all_feature_vectors.csv b/src/all_feature_vectors.csv index b2974cc..13570c6 100644 --- a/src/all_feature_vectors.csv +++ b/src/all_feature_vectors.csv @@ -1,5 +1,4 @@ brightness;filename;concept;vector - +1;X-10Y-10.png;Concept.LINKS_ABBIEGEN;[0, 58, 0, 40, 0, 2, 0, 18, 0, 79, 0, 3, 0, 64, 0, 33, 0, 3, 0, 79, 0, 18, 0, 3, 0, 21, 0, 76, 0, 3, 0, 69, 0, 28, 0, 3, 0, 39, 0, 60, 0, 1, 0, 68, 0, 31, 0, 1, 0, 77, 0, 20, 0, 3, 0, 79, 0, 18, 0, 3, 0, 100, 0, 0, 0, 0, 0, 62, 0, 37, 0, 1, 0, 73, 0, 23, 0, 4, 0, 20, 0, 76, 0, 4, 0, 77, 0, 20, 0, 3, 0, 62, 0, 34, 0, 4, 0, 15, 0, 82, 0, 3, 9, 33, 3] +1;X-10Y-20.png;Concept.LINKS_ABBIEGEN;[0, 58, 0, 40, 0, 2, 0, 17, 0, 81, 0, 2, 0, 64, 0, 33, 0, 3, 0, 80, 0, 17, 0, 3, 0, 22, 0, 75, 0, 3, 0, 65, 0, 32, 0, 3, 0, 41, 0, 57, 0, 2, 0, 71, 0, 28, 0, 1, 0, 77, 0, 20, 0, 3, 0, 81, 0, 16, 0, 3, 0, 100, 0, 0, 0, 0, 0, 61, 0, 38, 0, 1, 0, 70, 0, 26, 0, 4, 0, 23, 0, 73, 0, 4, 0, 78, 0, 18, 0, 4, 0, 59, 0, 37, 0, 4, 0, 12, 0, 85, 0, 3, 9, 32, 3] +1;X-10Y-30.png;Concept.LINKS_ABBIEGEN;[0, 57, 0, 40, 0, 3, 0, 15, 0, 83, 0, 2, 0, 63, 0, 34, 0, 3, 0, 81, 0, 16, 0, 3, 0, 23, 0, 73, 0, 4, 0, 61, 0, 36, 0, 3, 0, 43, 0, 55, 0, 2, 0, 75, 0, 24, 0, 1, 0, 76, 0, 20, 0, 4, 0, 83, 0, 14, 0, 3, 0, 99, 0, 0, 0, 1, 0, 61, 0, 38, 0, 1, 0, 66, 0, 30, 0, 4, 0, 27, 0, 70, 0, 3, 0, 78, 0, 18, 0, 4, 0, 57, 0, 39, 0, 4, 0, 9, 0, 88, 0, 3, 7, 31, 2] @@ -43336,4 +43335,4 @@ brightness;filename;concept;vector 0;X80Y50.png;Concept.VORFAHRT_GEWAEHREN;[43, 0, 0, 0, 0, 57, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 80, 0, 0, 0, 0, 20, 100, 0, 0, 0, 0, 0, 50, 0, 0, 0, 0, 50, 40, 0, 0, 0, 0, 60, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 10, 60, 0, 0, 0, 0, 40, 50, 0, 0, 0, 0, 50, 20, 0, 0, 0, 0, 80, 0, 0, 0, 0, 0, 100, 9, 0, 1] 0;X80Y60.png;Concept.VORFAHRT_GEWAEHREN;[38, 0, 0, 0, 0, 62, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 20, 0, 0, 0, 0, 80, 90, 0, 0, 0, 0, 10, 60, 0, 0, 0, 0, 40, 20, 0, 0, 0, 0, 80, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 10, 60, 0, 0, 0, 0, 40, 50, 0, 0, 0, 0, 50, 20, 0, 0, 0, 0, 80, 0, 0, 0, 0, 0, 100, 4, 0, 1] 0;X80Y70.png;Concept.VORFAHRT_GEWAEHREN;[34, 0, 0, 0, 0, 66, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 40, 0, 0, 0, 0, 60, 80, 0, 0, 0, 0, 20, 25, 0, 0, 0, 0, 75, 80, 0, 0, 0, 0, 20, 100, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 10, 50, 0, 0, 0, 0, 50, 60, 0, 0, 0, 0, 40, 10, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 100, 3, 0, 1] -0;X80Y80.png;Concept.VORFAHRT_GEWAEHREN;[29, 0, 0, 0, 0, 71, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 20, 0, 0, 0, 0, 80, 70, 0, 0, 0, 0, 30, 12, 0, 0, 0, 0, 88, 70, 0, 0, 0, 0, 30, 100, 0, 0, 0, 0, 0, 80, 0, 0, 0, 0, 20, 50, 0, 12, 0, 0, 38, 50, 0, 0, 0, 0, 50, 10, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 100, 2, 0, 0] +0;X80Y80.png;Concept.VORFAHRT_GEWAEHREN;[29, 0, 0, 0, 0, 71, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 20, 0, 0, 0, 0, 80, 70, 0, 0, 0, 0, 30, 12, 0, 0, 0, 0, 88, 70, 0, 0, 0, 0, 30, 100, 0, 0, 0, 0, 0, 80, 0, 0, 0, 0, 20, 50, 0, 12, 0, 0, 38, 50, 0, 0, 0, 0, 50, 10, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 100, 2, 0, 0] \ No newline at end of file diff --git a/src/classes/neural/network.py b/src/classes/neural/network.py index 3044e21..0560fea 100644 --- a/src/classes/neural/network.py +++ b/src/classes/neural/network.py @@ -33,11 +33,11 @@ class NeuralNetwork: return [interpritated, True] # takes feature vectors to train and generates weights - def learn(self, training_set: list[FeatureVector]): + def learn(self, training_set: list[FeatureVector], epochs = 10): correct_amount = 0 false_amount = 0 - for i in range(len(training_set)*100): + for i in range(len(training_set)*epochs): fv_to_train = training_set[i%len(training_set)] # random.choice(training_set) classified_concept, correct = self.classify(fv_to_train) diff --git a/src/main.py b/src/main.py index 87e96dd..ff52bcf 100644 --- a/src/main.py +++ b/src/main.py @@ -2,6 +2,7 @@ import os import ast import csv import numpy as np +import random import multiprocessing from classes.concept import Concept @@ -59,56 +60,60 @@ def decision_tree(training = os.path.abspath(os.path.join(__file__, "..", "train # print(f"classified {cnt_correct}/{cnt_data} correctly ({round(cnt_correct/cnt_data*100, 3)}%)") return [cnt_correct, cnt_data - cnt_correct, cnt_data] # richtig, falsch, amount of testing data -def neural_network(training = os.path.abspath(os.path.join(__file__, "..", "training.csv")), testing = os.path.abspath(os.path.join(__file__, "..", "testing.csv")), num_training_vectors = 100): +def neural_network(training_set, testing_set, epochs = 10): from classes.neural.network import NeuralNetwork from classes.neural.neuron import Neuron - - training_data = [] - with open(training, 'r') as csv_file: - reader = csv.reader(csv_file, delimiter=";") - next(reader) - i = 0 - for row in reader: - if i > num_training_vectors: - break - i += 1 - - float_list = ast.literal_eval(row[3]) - - fv = FeatureVector(concept = Concept.identify_by_str(row[2].split(".")[1]), features_list = [element * 0.01 for element in float_list], loaded = True) - training_data.append(fv) - while len(training_data) < num_training_vectors: - training_data += training_data - training_data = training_data[:num_training_vectors+1] + nn = NeuralNetwork(neurons=None) + nn.learn(training_set, epochs) - nn = NeuralNetwork() - nn.learn(training_data) - - cnt_data = 0 cnt_correct = 0 - with open(testing, 'r') as csv_file: - reader = csv.reader(csv_file, delimiter=";") - next(reader) - for row in reader: - cnt_data += 1 - fv = FeatureVector(concept = Concept.identify_by_str(row[2].split(".")[1]), features_list = ast.literal_eval(row[3]), loaded = True) - classified_concept, _ = nn.classify(fv) - cnt_correct += 1 if fv.concept == classified_concept else 0 - # print(f"{fv.concept} was classified as {classified_concept}") + for testing_fv in testing_set: + classified_concept, _ = nn.classify(testing_fv) + cnt_correct += 1 if testing_fv.concept == classified_concept else 0 + + # print(f"{fv.concept} was classified as {classified_concept}") #print(f"classified {cnt_correct}/{cnt_data} correctly ({round(cnt_correct/cnt_data*100, 3)}%)") - return [cnt_correct, cnt_data - cnt_correct, cnt_data] # richtig, falsch, amount of testing data + return [cnt_correct, len(testing_set) - cnt_correct, len(testing_set)] # richtig, falsch, amount of testing data -def repeat_n_times(n: int, num_training_vectors: int): +def repeat_n_times(n: int, num_training_vectors: int, epochs: int): + cnt_correct = 0 + cnt_data = 0 for _ in range(n): - cnt_correct = 0 - cnt_data = 0 - result = neural_network(num_training_vectors=num_training_vectors) + result = neural_network(num_training_vectors=num_training_vectors, epochs=epochs) cnt_correct += result[0] cnt_data += result[2] - print(f"{num_training_vectors}: classified {cnt_correct}/{cnt_data} correctly ({round(cnt_correct/cnt_data*100, 3)}%)") + print(f"{epochs}: classified {cnt_correct}/{cnt_data} correctly ({round(cnt_correct/cnt_data*100, 3)}%)") + +def load_fv(path_to_training_data = os.path.abspath(os.path.join(__file__, "..", "all_feature_vectors.csv"))): + training_data = [] + with open(path_to_training_data, 'r') as csv_file: + reader = csv.reader(csv_file, delimiter=";") + next(reader) + for row in reader: + fv_float = ast.literal_eval(row[3]) + + fv = FeatureVector(concept = Concept.identify_by_str(row[2].split(".")[1]), features_list = [element * 0.01 for element in fv_float], loaded = True) + training_data.append(fv) + + return training_data if __name__ == "__main__": - for num_training_vectors in range(100, 1000, 100): - multiprocessing.Process(target=repeat_n_times, args=(100, num_training_vectors, )).start() + all_vectors = load_fv() + + for input in range(40000, len(all_vectors), 400): + inp_res = [] + for epoch in range(10, 1000, 10): + l = [] + for test in range(0, 1): + training_set = random.sample(all_vectors, input) + testing_set = list(set(all_vectors)-set(training_set)) + + # print(f"Start for {input} trainingsdata and test number {test} with epoch {epoch}") + correct, false, ammount = neural_network(training_set, testing_set, epoch) + print(f"Epoch {epoch}: correct_percentage={(correct/ammount)*100}%") + l.append(false/ammount) + + inp_res.append(np.mean(l)) + diff --git a/src/training.csv b/src/training.csv index 3c4ddde..bfbc90a 100644 --- a/src/training.csv +++ b/src/training.csv @@ -2167,7 +2167,6 @@ brightness;filename;concept;vector +1;X-10Y-50.png;Concept.VORFAHRT_STRASSE;[0, 0, 15, 77, 0, 8, 0, 0, 0, 99, 0, 1, 0, 0, 3, 86, 0, 11, 0, 0, 1, 85, 0, 14, 0, 0, 0, 99, 0, 1, 0, 0, 11, 79, 0, 10, 0, 0, 85, 12, 0, 3, 0, 0, 35, 60, 0, 5, 0, 0, 1, 82, 0, 17, 0, 1, 10, 78, 0, 11, 0, 0, 79, 17, 0, 4, 0, 0, 16, 74, 0, 10, 0, 0, 1, 88, 0, 11, 0, 0, 0, 92, 0, 8, 0, 0, 1, 86, 0, 13, 0, 0, 0, 94, 0, 6, 0, 0, 0, 100, 0, 0, 10, 38, 1] +2;X50Y50.png;Concept.STOP;[43, 0, 0, 0, 0, 57, 0, 0, 0, 0, 0, 100, 8, 0, 0, 0, 0, 92, 65, 0, 0, 0, 0, 35, 73, 0, 0, 0, 0, 27, 11, 0, 0, 0, 0, 89, 65, 0, 0, 0, 0, 35, 70, 0, 0, 0, 0, 30, 64, 0, 0, 0, 0, 36, 48, 0, 0, 0, 0, 52, 59, 0, 0, 0, 0, 41, 57, 0, 0, 0, 0, 43, 10, 0, 0, 0, 0, 90, 72, 0, 0, 0, 0, 28, 74, 0, 0, 0, 0, 26, 15, 0, 0, 0, 0, 85, 0, 0, 0, 0, 0, 100, 9, 16, 5] -1;X-80Y10.png;Concept.STOP;[49, 0, 0, 49, 0, 2, 22, 0, 0, 76, 0, 2, 71, 0, 0, 27, 0, 2, 54, 0, 0, 44, 0, 2, 6, 0, 0, 93, 0, 1, 48, 0, 0, 49, 0, 3, 64, 0, 0, 34, 0, 2, 47, 0, 0, 50, 0, 3, 44, 0, 0, 54, 0, 2, 44, 0, 0, 54, 0, 2, 98, 0, 0, 1, 0, 1, 98, 0, 0, 1, 0, 1, 62, 0, 0, 36, 0, 2, 0, 0, 0, 98, 0, 2, 49, 0, 0, 48, 0, 3, 55, 0, 0, 42, 0, 3, 17, 0, 0, 80, 0, 3, 19, 13, 56] - +1;X60Y-80.png;Concept.VORFAHRT_STRASSE;[0, 0, 3, 94, 0, 3, 0, 0, 0, 93, 0, 7, 0, 0, 0, 98, 0, 2, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 99, 0, 1, 0, 0, 21, 72, 0, 7, 0, 0, 2, 94, 0, 4, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 1, 97, 0, 2, 0, 0, 28, 66, 0, 6, 0, 0, 0, 97, 0, 3, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 99, 0, 1, 0, 0, 1, 92, 0, 7, 16, 19, 3] -2;X10Y60.png;Concept.VORFAHRT_GEWAEHREN;[24, 0, 0, 74, 0, 2, 59, 0, 0, 35, 1, 5, 43, 0, 0, 53, 0, 4, 34, 0, 0, 63, 0, 3, 11, 0, 0, 87, 0, 2, 38, 0, 0, 58, 0, 4, 16, 0, 0, 83, 0, 1, 3, 0, 0, 95, 0, 2, 40, 0, 0, 58, 0, 2, 4, 0, 0, 93, 0, 3, 45, 0, 0, 53, 0, 2, 28, 0, 0, 70, 0, 2, 3, 0, 0, 95, 0, 2, 0, 0, 0, 99, 0, 1, 43, 0, 0, 52, 0, 5, 11, 0, 0, 87, 0, 2, 0, 0, 0, 99, 0, 1, 8, 23, 5] 0;X-40Y10.png;Concept.VORFAHRT_STRASSE;[0, 0, 15, 77, 0, 8, 0, 0, 0, 97, 0, 3, 0, 0, 7, 80, 0, 13, 0, 0, 9, 81, 0, 10, 0, 0, 0, 98, 0, 2, 0, 0, 0, 85, 0, 15, 0, 0, 75, 20, 0, 5, 0, 0, 87, 10, 0, 3, 0, 0, 4, 83, 0, 13, 0, 0, 0, 91, 0, 9, 0, 0, 16, 74, 0, 10, 0, 0, 41, 53, 0, 6, 0, 0, 0, 86, 0, 14, 0, 0, 0, 100, 0, 0, 0, 0, 0, 90, 0, 10, 0, 0, 0, 80, 6, 14, 0, 0, 0, 99, 0, 1, 8, 43, 1]