added neural network class; made necessary changes to project structure
2
.gitignore
vendored
@ -143,3 +143,5 @@ cython_debug/
|
||||
|
||||
# local sandbox
|
||||
sandbox/
|
||||
/algorithms/learn/decision_tree/decistion_tree.png
|
||||
/resources/data/neural_network/train
|
||||
|
0
algorithms/learn/decision_tree/__init__.py
Normal file
@ -18,7 +18,7 @@ class DecisionTree:
|
||||
self.vec = None
|
||||
|
||||
def build(self, training_file: str, depth: int):
|
||||
path = os.path.join("..", "..", "resources", "data", training_file)
|
||||
path = os.path.join("../..", "..", "resources", "data", training_file)
|
||||
|
||||
samples = list()
|
||||
results = list()
|
||||
@ -87,4 +87,4 @@ if __name__ == "__main__":
|
||||
# generate_data("training_set.txt", 12000)
|
||||
decision_tree = DecisionTree()
|
||||
decision_tree.build("training_set.txt", 15)
|
||||
decision_tree.test()
|
||||
decision_tree.save()
|
0
algorithms/learn/neural_network/__init__.py
Normal file
124
algorithms/learn/neural_network/neural_network.py
Normal file
@ -0,0 +1,124 @@
|
||||
import numpy as np
|
||||
import joblib
|
||||
import pathlib
|
||||
import tensorflow as tf
|
||||
|
||||
from tensorflow import keras
|
||||
from tensorflow.keras import layers
|
||||
from tensorflow.keras.models import Sequential
|
||||
|
||||
|
||||
class NeuralNetwork:
|
||||
def __init__(self, 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.img_height = img_height
|
||||
self.img_width = img_width
|
||||
|
||||
if saved_model_path is not None and classes_path is not None:
|
||||
self.load(saved_model_path, classes_path)
|
||||
else:
|
||||
self.model = None
|
||||
self.class_names = None
|
||||
|
||||
def build(self, num_classes=3, epochs=15, batch_size=32):
|
||||
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
||||
self.training_data_dir,
|
||||
validation_split=0.2,
|
||||
subset="training",
|
||||
seed=123,
|
||||
image_size=(self.img_height, self.img_width),
|
||||
batch_size=batch_size)
|
||||
|
||||
self.class_names = train_ds.class_names
|
||||
|
||||
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
||||
self.training_data_dir,
|
||||
validation_split=0.2,
|
||||
subset="validation",
|
||||
seed=123,
|
||||
image_size=(self.img_height, self.img_width),
|
||||
batch_size=batch_size)
|
||||
|
||||
autotune = tf.data.AUTOTUNE
|
||||
|
||||
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=autotune)
|
||||
val_ds = val_ds.cache().prefetch(buffer_size=autotune)
|
||||
|
||||
data_augmentation = keras.Sequential(
|
||||
[
|
||||
layers.experimental.preprocessing.RandomFlip("horizontal",
|
||||
input_shape=(self.img_height,
|
||||
self.img_width,
|
||||
3)),
|
||||
layers.experimental.preprocessing.RandomRotation(0.1),
|
||||
layers.experimental.preprocessing.RandomZoom(0.1),
|
||||
]
|
||||
)
|
||||
|
||||
model = Sequential([
|
||||
data_augmentation,
|
||||
layers.experimental.preprocessing.Rescaling(1. / 255),
|
||||
layers.Conv2D(16, 3, padding='same', activation='relu'),
|
||||
layers.MaxPooling2D(),
|
||||
layers.Conv2D(32, 3, padding='same', activation='relu'),
|
||||
layers.MaxPooling2D(),
|
||||
layers.Conv2D(64, 3, padding='same', activation='relu'),
|
||||
layers.MaxPooling2D(),
|
||||
layers.Dropout(0.2),
|
||||
layers.Flatten(),
|
||||
layers.Dense(128, activation='relu'),
|
||||
layers.Dense(num_classes)
|
||||
])
|
||||
|
||||
model.compile(optimizer='adam',
|
||||
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.summary()
|
||||
model.fit(
|
||||
train_ds,
|
||||
validation_data=val_ds,
|
||||
epochs=epochs
|
||||
)
|
||||
|
||||
self.model = model
|
||||
|
||||
def save(self):
|
||||
self.model.save("saved_model.h5")
|
||||
joblib.dump(self.class_names, "saved_model_classes.joblib")
|
||||
|
||||
def load(self, model_path, classes_path):
|
||||
self.model = tf.keras.models.load_model(model_path)
|
||||
self.class_names = joblib.load(classes_path)
|
||||
|
||||
def get_answer(self, image_path):
|
||||
img = keras.preprocessing.image.load_img(
|
||||
image_path, target_size=(self.img_height, self.img_width)
|
||||
)
|
||||
|
||||
img_array = keras.preprocessing.image.img_to_array(img)
|
||||
img_array = tf.expand_dims(img_array, 0)
|
||||
|
||||
predictions = self.model.predict(img_array)
|
||||
score = tf.nn.softmax(predictions[0])
|
||||
|
||||
print(
|
||||
"This image most likely belongs to {} with a {:.2f} percent confidence."
|
||||
.format(self.class_names[np.argmax(score)], 100 * np.max(score))
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Building and saving a new model:
|
||||
|
||||
# (requires a valid training set in resources/data/neural_network/train)
|
||||
# neural_network = NeuralNetwork()
|
||||
# neural_network.build(epochs=10)
|
||||
# neural_network.save()
|
||||
|
||||
# Loading a model from file:
|
||||
neural_network = NeuralNetwork("saved_model.h5", "saved_model_classes.joblib")
|
||||
|
||||
# Test
|
||||
image = r"../../../resources/data/neural_network/disarm/ships/0e91bae301a5f5b136c2a0d20ef97cf6.jpg"
|
||||
neural_network.get_answer(image)
|
BIN
algorithms/learn/neural_network/saved_model.h5
Normal file
BIN
algorithms/learn/neural_network/saved_model_classes.joblib
Normal file
BIN
resources/data/neural_network/disarm/planes/.jpg
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
resources/data/neural_network/disarm/planes/000_14P1PM.jpg
Normal file
After Width: | Height: | Size: 77 KiB |
After Width: | Height: | Size: 27 KiB |
After Width: | Height: | Size: 48 KiB |
BIN
resources/data/neural_network/disarm/planes/000_6J92K.jpg
Normal file
After Width: | Height: | Size: 94 KiB |
After Width: | Height: | Size: 35 KiB |
After Width: | Height: | Size: 1.4 MiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 11 KiB |
BIN
resources/data/neural_network/disarm/planes/01_NATO.jpg
Normal file
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 21 KiB |
After Width: | Height: | Size: 504 KiB |
After Width: | Height: | Size: 322 KiB |
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 144 KiB |
After Width: | Height: | Size: 32 KiB |
BIN
resources/data/neural_network/disarm/planes/0_PA-50078515.jpg
Normal file
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 18 KiB |
BIN
resources/data/neural_network/disarm/planes/0_usaf2.jpg
Normal file
After Width: | Height: | Size: 71 KiB |
After Width: | Height: | Size: 193 KiB |
After Width: | Height: | Size: 30 KiB |
After Width: | Height: | Size: 125 KiB |
After Width: | Height: | Size: 77 KiB |
After Width: | Height: | Size: 6.6 KiB |
BIN
resources/data/neural_network/disarm/planes/0x0.jpg
Normal file
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 24 KiB |
BIN
resources/data/neural_network/disarm/planes/1C9.jpg
Normal file
After Width: | Height: | Size: 8.3 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 91 KiB |
After Width: | Height: | Size: 134 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 27 KiB |
BIN
resources/data/neural_network/disarm/planes/1w8crbkb-900.jpg
Normal file
After Width: | Height: | Size: 53 KiB |
After Width: | Height: | Size: 117 KiB |
After Width: | Height: | Size: 22 KiB |
BIN
resources/data/neural_network/disarm/planes/2l-image-20.jpg
Normal file
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 57 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 59 KiB |
After Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 62 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 60 KiB |
After Width: | Height: | Size: 73 KiB |
After Width: | Height: | Size: 78 KiB |
After Width: | Height: | Size: 5.6 KiB |
BIN
resources/data/neural_network/disarm/planes/4soesoyb-720.jpg
Normal file
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 85 KiB |
After Width: | Height: | Size: 111 KiB |
After Width: | Height: | Size: 35 KiB |
After Width: | Height: | Size: 92 KiB |
After Width: | Height: | Size: 334 KiB |
BIN
resources/data/neural_network/disarm/planes/6.jpg
Normal file
After Width: | Height: | Size: 27 KiB |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 81 KiB |
After Width: | Height: | Size: 8.2 KiB |
After Width: | Height: | Size: 165 KiB |
After Width: | Height: | Size: 4.8 KiB |
After Width: | Height: | Size: 104 KiB |
After Width: | Height: | Size: 324 KiB |
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 5.6 KiB |
After Width: | Height: | Size: 127 KiB |
After Width: | Height: | Size: 6.1 KiB |
After Width: | Height: | Size: 63 KiB |
BIN
resources/data/neural_network/disarm/planes/_115689435_raf.jpg
Normal file
After Width: | Height: | Size: 46 KiB |
After Width: | Height: | Size: 9.8 KiB |
After Width: | Height: | Size: 56 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 60 KiB |
After Width: | Height: | Size: 49 KiB |
BIN
resources/data/neural_network/disarm/planes/_88318230_de27.jpg
Normal file
After Width: | Height: | Size: 39 KiB |
After Width: | Height: | Size: 31 KiB |
BIN
resources/data/neural_network/disarm/planes/____3.jpg
Normal file
After Width: | Height: | Size: 366 KiB |
After Width: | Height: | Size: 293 KiB |
After Width: | Height: | Size: 82 KiB |
After Width: | Height: | Size: 2.1 MiB |
After Width: | Height: | Size: 63 KiB |
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 59 KiB |
After Width: | Height: | Size: 93 KiB |
After Width: | Height: | Size: 24 KiB |