Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Machine-Learning: Real-Time-Object-Classification

[![CML](https://github.com/Arief-AK/Machine-Learning-Real-Time-Object-Classification/actions/workflows/ci.yml/badge.svg)](https://github.com/Arief-AK/Machine-Learning-Real-Time-Object-Classification/actions/workflows/ci.yml)

Application of machine learning models to recognise objects from the [CIFAR-10](https://www.cs.toronto.edu/~kriz/cifar.html) dataset. This work uses the [TensorFlow](https://www.tensorflow.org/) framework to build, train, and deploy models to produce real-time object classifications.

>[!NOTE]
Expand Down
26 changes: 19 additions & 7 deletions Train.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
from include.TensorModel import TensorModel
from include.ModelProfiler import ModelProfiler

NUM_EPOCHS = 25
NUM_EPOCHS = 5
BATCH_FITTING = 128
BATCH_PROFILING = [32, 64, 128]

MODELS = ["base_model", "batch_norm_model", "batch_norm_model_sgd", "batch_norm_model_rmsprop"]
USE_EXISTING_MODELS = True

SAVE_MODELS = True
SAVE_MODELS_AS_H5 = True
SAVE_MODELS_AS_KERAS = True
SAVE_MODELS_AS_SavedModel = True

def create_predicition_matrix(model_handler: TensorModel, visualiser: Visualiser, model, x_test, y_test, str_model):
conf_matrix = model_handler.compute_confusion_matrix(model, x_test, y_test)
visualiser.plot_confusion_matrix(conf_matrix, model_handler.get_class_names(), str_model)
Expand All @@ -22,8 +27,6 @@ def create_predicition_matrix(model_handler: TensorModel, visualiser: Visualiser
visualiser.plot_diagonal_confusion_matrix(diagonal_matrix, model_handler.get_class_names(), str_model)

def train_model(model_name:str, model_handler: TensorModel, visualiser: Visualiser, logger: Logger, x_train, y_train, x_test, y_test, batch_size) -> tuple:
logger.info(f"Eager enabled: {tf.executing_eagerly()}")

# Check if model exists
if USE_EXISTING_MODELS and os.path.exists(f"models/{model_name}.h5"):
model = model = tf.keras.models.load_model(f"models/{model_name}.h5")
Expand All @@ -38,7 +41,16 @@ def train_model(model_name:str, model_handler: TensorModel, visualiser: Visualis

history = model.fit(x_train, y_train, epochs=NUM_EPOCHS, batch_size=batch_size, validation_data=(x_test, y_test))
test_loss, test_acc = model.evaluate(x_test, y_test)
model.save(f"models/{model_name}.h5")

if SAVE_MODELS:
if SAVE_MODELS_AS_H5:
model.save(f"models/{model_name}.h5")

if SAVE_MODELS_AS_KERAS:
model.save(f"models/{model_name}.keras")

if SAVE_MODELS_AS_SavedModel:
model.export(f"models/{model_name}_saved_model")

logger.info(f"Model accuracy: {test_acc * 100:.2f}%")
visualiser.plot_training_history(history, model_name)
Expand Down Expand Up @@ -76,14 +88,14 @@ def profile_models(model_acc_results:dict, visualiser: Visualiser, logger: Logge
logger.info(f"Accuracy:{accuracy * 100:.2f}%\n")

if __name__ == "__main__":
# Initialise variables
# # Initialise variables
model_acc_results = {}

# Create a logger
# # Create a logger
logger = Logger(__name__)
logger.set_level(logging.INFO)

# Initialise visualiser
# # Initialise visualiser
visualiser = Visualiser()

# Initalise model handler
Expand Down
Binary file modified images/Inference/base_model/base_model_128_inference_timings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/Inference/base_model/base_model_32_inference_timings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/Inference/base_model/base_model_64_inference_timings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/Training/base_model/base_model_training_history.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/augmented_sample_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions include/TensorModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ def load_data(self) -> tuple:
def get_augmentation(self) -> tf.keras.Sequential:
# Create a data augmentation layer
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip("horizontal"), # Randomly flip images horizontally
tf.keras.layers.RandomRotation(0.1), # Randomly rotate image up to 10%
tf.keras.layers.RandomZoom(0.1) # Randomly zoom image up to 10%
tf.keras.layers.RandomFlip("horizontal"), # Randomly flip images horizontally
tf.keras.layers.RandomRotation(0.1), # Randomly rotate image up to 10%
tf.keras.layers.RandomZoom(0.1), # Randomly zoom image up to 10%
tf.keras.layers.RandomContrast(0.1), # Randomly adjust contrast up to 10%
tf.keras.layers.Lambda(lambda x: tf.image.random_brightness(x, max_delta=0.2)), # Randomly adjust brightness
tf.keras.layers.Lambda(lambda x: tf.image.random_crop(x, size=[tf.shape(x)[0], tf.shape(x)[1] - 8, tf.shape(x)[2] - 8, tf.shape(x)[3]])), # Cutout augmentation
tf.keras.layers.Lambda(lambda x: 0.5 * x + 0.5 * tf.roll(x, shift=1, axis=0)) # MixUp augmentation (approximate)
])

return data_augmentation
Expand Down
Binary file modified models/base_model.h5
Binary file not shown.
Binary file added models/base_model.keras
Binary file not shown.
1 change: 1 addition & 0 deletions models/base_model_saved_model/fingerprint.pb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���������ݙ�ٝ�x�������� ������(���������2
Binary file added models/base_model_saved_model/saved_model.pb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified models/batch_norm_model.h5
Binary file not shown.
Binary file added models/batch_norm_model.keras
Binary file not shown.
Binary file modified models/batch_norm_model_rmsprop.h5
Binary file not shown.
Binary file added models/batch_norm_model_rmsprop.keras
Binary file not shown.
1 change: 1 addition & 0 deletions models/batch_norm_model_rmsprop_saved_model/fingerprint.pb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
����қ��u��ﳆЭ���������8 ���گ��ւ(Ȅ����=2
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions models/batch_norm_model_saved_model/fingerprint.pb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��̸��ҕ����ξ��$��������\ 왉ŏܐ��(�֓�ɻ��[2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified models/batch_norm_model_sgd.h5
Binary file not shown.
Binary file added models/batch_norm_model_sgd.keras
Binary file not shown.
1 change: 1 addition & 0 deletions models/batch_norm_model_sgd_saved_model/fingerprint.pb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�Ђ��Վ�p�������������͝� ��������H(�ˌ�󄴓2
Binary file not shown.
Binary file not shown.
Binary file not shown.