From 527daa62819fdb7d19759126d198a7db51811a9f Mon Sep 17 00:00:00 2001 From: mr2-alkhateeb <marwan2.alkhateeb@live.uwe.ac.uk> Date: Sun, 12 May 2024 18:11:45 +0000 Subject: [PATCH] Added comments and organised --- mlmodel/{ => Version1}/evaluation.py | 7 ++++++ mlmodel/{ => Version1}/main.py | 6 +++++ mlmodel/{ => Version1}/preprocessing.py | 2 ++ mlmodel/{ => Version1}/training.py | 0 mlmodel/Version2/categorization.py | 31 ------------------------ mlmodel/Version2/cnn_training.py | 2 ++ mlmodel/Version2/evaluation.py | 2 ++ mlmodel/Version2/preprocessing.py | 12 +-------- mlmodel/Version3/eval.py | 6 ++--- mlmodel/Version3/preprocess.py | 1 + mlmodel/{ => Version3}/resnet_model5.h5 | Bin 11 files changed, 24 insertions(+), 45 deletions(-) rename mlmodel/{ => Version1}/evaluation.py (85%) rename mlmodel/{ => Version1}/main.py (77%) rename mlmodel/{ => Version1}/preprocessing.py (96%) rename mlmodel/{ => Version1}/training.py (100%) rename mlmodel/{ => Version3}/resnet_model5.h5 (100%) diff --git a/mlmodel/evaluation.py b/mlmodel/Version1/evaluation.py similarity index 85% rename from mlmodel/evaluation.py rename to mlmodel/Version1/evaluation.py index 8d309fc..55ef648 100644 --- a/mlmodel/evaluation.py +++ b/mlmodel/Version1/evaluation.py @@ -2,23 +2,30 @@ from joblib import load from sklearn.metrics import accuracy_score, classification_report import numpy as np +# Define function to evaluate model def evaluate_model(model_path, preprocessed_data_file): + # Load model model = load(model_path) + # Load preprocessed data preprocessed_data = np.load(preprocessed_data_file) test_images = preprocessed_data['test_images'] test_labels = preprocessed_data['test_labels'] + # Reshape images to 2D if len(test_images.shape) > 2: num_samples, height, width, channels = test_images.shape test_images = test_images.reshape(num_samples, height * width * channels) + # Predict predicted_labels = model.predict(test_images) + # Check accuracy accuracy = accuracy_score(test_labels, predicted_labels) print(f"Model Accuracy: {accuracy:.2f}") + Print Classification Report report = classification_report(test_labels, predicted_labels) print("Classification Report:") print(report) diff --git a/mlmodel/main.py b/mlmodel/Version1/main.py similarity index 77% rename from mlmodel/main.py rename to mlmodel/Version1/main.py index bd2e8ce..25e4f0c 100644 --- a/mlmodel/main.py +++ b/mlmodel/Version1/main.py @@ -1,13 +1,19 @@ from preprocessing import preprocess_images, split_data, save_preprocessed_data from training import train_model +# Set directory for images dataset_dir = r'Cars Dataset\train' + +# Preprocess images images, labels = preprocess_images(dataset_dir) +# Split images into training and testing train_images, test_images, train_labels, test_labels = split_data(images, labels) +# Save the preprocessed data output_file = 'preprocessed_data.npz' save_preprocessed_data(train_images, test_images, train_labels, test_labels, output_file) +# Train model model = train_model(train_images, train_labels) diff --git a/mlmodel/preprocessing.py b/mlmodel/Version1/preprocessing.py similarity index 96% rename from mlmodel/preprocessing.py rename to mlmodel/Version1/preprocessing.py index ce69547..4e427eb 100644 --- a/mlmodel/preprocessing.py +++ b/mlmodel/Version1/preprocessing.py @@ -4,6 +4,7 @@ import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler +# Function to preprocess images def preprocess_images(dataset_dir, image_size=(224, 224)): images = [] labels = [] @@ -28,6 +29,7 @@ def preprocess_images(dataset_dir, image_size=(224, 224)): return images, labels +# Function to reshape images def preprocess_images_2d(images): # Reshape the images into 2D arrays num_samples, height, width, channels = images.shape diff --git a/mlmodel/training.py b/mlmodel/Version1/training.py similarity index 100% rename from mlmodel/training.py rename to mlmodel/Version1/training.py diff --git a/mlmodel/Version2/categorization.py b/mlmodel/Version2/categorization.py index 6edd44b..deb9711 100644 --- a/mlmodel/Version2/categorization.py +++ b/mlmodel/Version2/categorization.py @@ -1,34 +1,3 @@ -# import os -# import shutil - -# # Set the directory where your images are stored -# source_dir = 'Car Model Training' - -# # Set the directory where you want to organize the folders by category -# target_dir = 'categories' - -# # Check if the target directory exists, if not, create it -# if not os.path.exists(target_dir): -# os.mkdir(target_dir) - -# # Loop through each file in the source directory -# for filename in os.listdir(source_dir): -# if filename.endswith((".png", ".jpg", ".jpeg")): # Check for image files -# # Assuming the category is the first three parts of the filename (Make_Model_Year) -# category_parts = filename.split('_')[:3] -# category = '_'.join(category_parts) # This will join them as 'Make_Model_Year' - -# # Create a new folder path for the category if it doesn't exist -# category_path = os.path.join(target_dir, category) -# if not os.path.exists(category_path): -# os.mkdir(category_path) - -# # Move the file -# shutil.move(os.path.join(source_dir, filename), os.path.join(category_path, filename)) - -# print("Images have been organized into category folders.") - - import os import shutil diff --git a/mlmodel/Version2/cnn_training.py b/mlmodel/Version2/cnn_training.py index 9f2d5cf..9c68d16 100644 --- a/mlmodel/Version2/cnn_training.py +++ b/mlmodel/Version2/cnn_training.py @@ -4,6 +4,7 @@ from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout from tensorflow.keras.preprocessing.image import ImageDataGenerator +# Define cnn model def create_cnn_model(input_shape, num_classes): model = Sequential([ Conv2D(32, (3, 3), activation='relu', input_shape=input_shape), @@ -19,6 +20,7 @@ def create_cnn_model(input_shape, num_classes): model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) return model +# Define function to train the model def train_cnn_model(train_images, train_labels): # Assume train_images and train_labels are already provided and preprocessed diff --git a/mlmodel/Version2/evaluation.py b/mlmodel/Version2/evaluation.py index 8f758da..5d724b0 100644 --- a/mlmodel/Version2/evaluation.py +++ b/mlmodel/Version2/evaluation.py @@ -2,12 +2,14 @@ from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.image import ImageDataGenerator import numpy as np +# Define function to evaluate model def evaluate_model(model_path, preprocessed_data_file): model = load_model(model_path) preprocessed_data = np.load(preprocessed_data_file) test_images = preprocessed_data['test_images'] / 255.0 test_labels = preprocessed_data['test_labels'] + # Evaluate model using the test images and labels results = model.evaluate(test_images, test_labels) print(f"Test Loss, Test Accuracy: {results}") diff --git a/mlmodel/Version2/preprocessing.py b/mlmodel/Version2/preprocessing.py index 0eb1ad6..e5eefa5 100644 --- a/mlmodel/Version2/preprocessing.py +++ b/mlmodel/Version2/preprocessing.py @@ -4,6 +4,7 @@ import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler +# Define function to preprocess images def preprocess_images(dataset_dir, image_size=(224, 224)): images = [] labels = [] @@ -32,17 +33,6 @@ def preprocess_images(dataset_dir, image_size=(224, 224)): return images, labels -# def preprocess_images_2d(images): -# # Reshape the images into 2D arrays -# num_samples, height, width, channels = images.shape -# images_2d = images.reshape(num_samples, height * width * channels) - -# # Standardize the pixel values -# scaler = StandardScaler() -# images_2d_scaled = scaler.fit_transform(images_2d) - -# return images_2d_scaled - # Split the dataset for training def split_data(images, labels, test_size=0.2): return train_test_split(images, labels, test_size=test_size) diff --git a/mlmodel/Version3/eval.py b/mlmodel/Version3/eval.py index e547659..e0bebe1 100644 --- a/mlmodel/Version3/eval.py +++ b/mlmodel/Version3/eval.py @@ -1,11 +1,11 @@ import tensorflow as tf -from tensorflow.keras.models import Model +import numpy as np import cv2 - +from tensorflow.keras.models import Model from tensorflow.keras.preprocessing import image from tensorflow.keras.preprocessing.image import load_img, img_to_array -import numpy as np +# Car brands categories = ['Acura', 'Alfa Romeo', 'Aston Martin', 'Audi', 'Bentley'] # Load the model diff --git a/mlmodel/Version3/preprocess.py b/mlmodel/Version3/preprocess.py index a793d38..3c3d2bf 100644 --- a/mlmodel/Version3/preprocess.py +++ b/mlmodel/Version3/preprocess.py @@ -2,6 +2,7 @@ import os import shutil from sklearn.model_selection import train_test_split +# Function to split data def split_data(source_folder, train_size=0.7, val_size=0.15, test_size=0.15): # Specify the car brands to include included_brands = ['Audi', 'Bentley', 'Acura', 'Aston Martin', 'Alfa Romeo'] diff --git a/mlmodel/resnet_model5.h5 b/mlmodel/Version3/resnet_model5.h5 similarity index 100% rename from mlmodel/resnet_model5.h5 rename to mlmodel/Version3/resnet_model5.h5 -- GitLab