universalised NN class; build series-type NN

This commit is contained in:
s452645 2021-06-06 09:11:06 +02:00
parent 2fc525ad52
commit d7a552194f
1351 changed files with 21 additions and 9 deletions

3
.gitignore vendored
View File

@ -144,4 +144,5 @@ cython_debug/
# local sandbox # local sandbox
sandbox/ sandbox/
/algorithms/learn/decision_tree/decistion_tree.png /algorithms/learn/decision_tree/decistion_tree.png
/resources/data/neural_network/train /resources/data/neural_network/specificity/train
/resources/data/neural_network/series/train/

View File

@ -3,14 +3,23 @@ import joblib
import pathlib import pathlib
import tensorflow as tf import tensorflow as tf
from enum import Enum
from tensorflow import keras from tensorflow import keras
from tensorflow.keras import layers from tensorflow.keras import layers
from tensorflow.keras.models import Sequential from tensorflow.keras.models import Sequential
# int value is a number of classes
class NNType(Enum):
SERIES = "series", 4,
SPECIFICITY = "specificity", 3
class NeuralNetwork: class NeuralNetwork:
def __init__(self, saved_model_path=None, classes_path=None, img_height=180, img_width=180): def __init__(self, network_type: NNType, saved_model_path=None, classes_path=None, img_height=180, img_width=180):
self.training_data_dir = pathlib.Path(r"../../../resources/data/neural_network/train") self.type = network_type
self.training_data_dir = pathlib.Path(fr"../../../resources/data/neural_network/{self.type.value[0]}/train")
self.img_height = img_height self.img_height = img_height
self.img_width = img_width self.img_width = img_width
@ -20,7 +29,9 @@ class NeuralNetwork:
self.model = None self.model = None
self.class_names = None self.class_names = None
def build(self, num_classes=3, epochs=15, batch_size=32): def build(self, epochs=15, batch_size=32):
num_classes = self.type.value[1]
train_ds = tf.keras.preprocessing.image_dataset_from_directory( train_ds = tf.keras.preprocessing.image_dataset_from_directory(
self.training_data_dir, self.training_data_dir,
validation_split=0.2, validation_split=0.2,
@ -84,8 +95,8 @@ class NeuralNetwork:
self.model = model self.model = model
def save(self): def save(self):
self.model.save("saved_model.h5") self.model.save(fr"{self.type.value[0]}/saved_model.h5")
joblib.dump(self.class_names, "saved_model_classes.joblib") joblib.dump(self.class_names, fr"{self.type.value[0]}/saved_model_classes.joblib")
def load(self, model_path, classes_path): def load(self, model_path, classes_path):
self.model = tf.keras.models.load_model(model_path) self.model = tf.keras.models.load_model(model_path)
@ -112,13 +123,13 @@ if __name__ == "__main__":
# Building and saving a new model: # Building and saving a new model:
# (requires a valid training set in resources/data/neural_network/train) # (requires a valid training set in resources/data/neural_network/train)
# neural_network = NeuralNetwork() # neural_network = NeuralNetwork(NNType.SERIES)
# neural_network.build(epochs=10) # neural_network.build(epochs=10)
# neural_network.save() # neural_network.save()
# Loading a model from file: # Loading a model from file:
neural_network = NeuralNetwork("saved_model.h5", "saved_model_classes.joblib") neural_network = NeuralNetwork(NNType.SERIES, r"series/saved_model.h5", r"series/saved_model_classes.joblib")
# Test # Test
image = r"../../../resources/data/neural_network/disarm/ships/0e91bae301a5f5b136c2a0d20ef97cf6.jpg" image = r"../../../resources/data/neural_network/series/disarm/W/5.jpg"
neural_network.get_answer(image) neural_network.get_answer(image)

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 958 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 896 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 893 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 987 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 916 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 912 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 927 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 849 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 889 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 925 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 910 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 908 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 923 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 992 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 899 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 924 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 833 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 884 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 826 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 838 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 915 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 883 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 960 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 946 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 998 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 928 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 925 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 953 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1009 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 996 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 953 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 981 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1017 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 930 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 955 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 958 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 966 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 882 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 927 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 907 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 949 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1014 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 962 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 976 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 948 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 868 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 936 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 852 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 800 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 806 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 991 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 993 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 881 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1020 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 889 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 961 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 867 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 973 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1004 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 865 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 850 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 941 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 911 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 976 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 991 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 965 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 927 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 998 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 961 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 965 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 968 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 931 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 958 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 986 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 971 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 899 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 879 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 895 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 901 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 907 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 861 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 927 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 860 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 941 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 900 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 896 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 961 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 833 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 894 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 981 B

Some files were not shown because too many files have changed in this diff Show More