Skip to content

Commit

Permalink
Merge pull request #159 from Aydinhamedi/Alpha-b
Browse files Browse the repository at this point in the history
Alpha b
  • Loading branch information
Aydinhamedi authored Feb 15, 2024
2 parents afb23cd + 19da84b commit 7d023a3
Show file tree
Hide file tree
Showing 19 changed files with 5,895 additions and 903 deletions.
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ Samples/*
/Interface/GUI/GUI_DEV.cmd

# Exclude logs in GUI interface
/Interface/GUI/Data/logs
/Test_ENV_G.py
/Interface/GUI/Data/logs

# Other
/Test_ENV_G.py
/scc.exe
/SCC_Auto.cmd
/Microsoft.PowerShell_profile_Nvidia_smi.ps1
2,510 changes: 2,472 additions & 38 deletions BETA_E_Model_T&T.ipynb

Large diffs are not rendered by default.

Binary file modified Data/image_SUB_generator.pkl
Binary file not shown.
2 changes: 0 additions & 2 deletions Interface/CLI/CLI.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ if /i "%restart%"=="y" (
goto :EOF
)

REM Check it 🚧Beta🚧 --- [>>>
:errorNoPython
REM Handle the error if Python is not installed
if exist %PUE_filepath% (
Expand Down Expand Up @@ -147,7 +146,6 @@ if /I "!UserInput!"=="y" (
)
pause
goto :EOF
REM Check it 🚧Beta🚧 --- <<<]

:check_install
REM Check if a package is installed and offer to install it if not
Expand Down
8 changes: 4 additions & 4 deletions Interface/GUI/Data/GUI_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from time import sleep
import PySimpleGUI as sg
from loguru import logger
import efficientnet.tfkeras
from tkinter import filedialog
from datetime import datetime
from PIL import Image
Expand All @@ -43,6 +42,7 @@
from Utils.Grad_cam import make_gradcam_heatmap
from Utils.print_color_V2_NEW import print_Color_V2
from Utils.print_color_V1_OLD import print_Color
from Utils.FixedDropout import FixedDropout
from Utils.Other import *

# global vars>>>
Expand Down Expand Up @@ -135,7 +135,7 @@ def is_updated(self):
sg.Button('Reload Model', key='-BUTTON_RELOAD_MODEL-')
],
[
sg.Checkbox('Download Light Model', key='-CHECKBOX_DOWNLOAD_LIGHT_MODEL-', default=False)
sg.Checkbox('Download Light Model', key='-CHECKBOX_DOWNLOAD_LIGHT_MODEL-', default=True)
],
[sg.Text('Ai Model Info:', font=(None, 10, 'bold'))],
[
Expand Down Expand Up @@ -334,7 +334,7 @@ def CI_pwai(show_gradcam: bool = True) -> str:
try:
if model is None:
print_Color('loading the Ai model...', ['normal'])
model = load_model(Model_dir)
model = load_model(Model_dir, custom_objects={'FixedDropout': FixedDropout})
except (ImportError, IOError):
return 'ERROR: Failed to load the model.'
else:
Expand Down Expand Up @@ -384,7 +384,7 @@ def CI_rlmw() -> None:
model = None
GUI_Queue['-Main_log-'].put('loading the Ai model...')
try:
model = load_model(Model_dir)
model = load_model(Model_dir, custom_objects={'FixedDropout': FixedDropout})
except (ImportError, IOError):
GUI_Queue['-Main_log-'].put('ERROR: Failed to load the model.')
return None
Expand Down
19 changes: 19 additions & 0 deletions Interface/GUI/Data/Utils/FixedDropout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from tensorflow.keras import layers, backend

class FixedDropout(layers.Dropout):
def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape

symbolic_shape = backend.shape(inputs)
noise_shape = [symbolic_shape[axis] if shape is None else shape
for axis, shape in enumerate(self.noise_shape)]
return tuple(noise_shape)

def get_config(self):
config = super().get_config()
return config

@classmethod
def from_config(cls, config):
return cls(**config)
78 changes: 69 additions & 9 deletions Interface/GUI/Data/Utils/Other.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from Utils.print_color_V2_NEW import print_Color_V2
from Utils.print_color_V1_OLD import print_Color
from tabulate import tabulate
import numpy as np
import pickle
import gzip

def save_list(history, filename, compress=True):
# Saves the given history list to the specified filename.
# If compress is True, the file will be gzip compressed.
# Otherwise it will be saved as a normal pickle file.
"""Saves a list to a file.
Args:
history: The list to save.
filename: The file to save the list to.
compress: Whether to gzip compress the file. Default is True.
"""
if compress:
with gzip.open(filename, 'wb') as f:
pickle.dump(history, f)
Expand All @@ -16,17 +24,69 @@ def save_list(history, filename, compress=True):


def load_list(filename, compressed=True):
# Loads a pickled object from a file.
# If compressed=True, it will load from a gzip compressed file.
# Otherwise loads from a regular file.
"""Loads a list from a file.
Args:
filename: The file to load from.
compressed: Whether the file is gzip compressed. Default is True.
Returns:
The loaded list from the file.
"""
if compressed:
with gzip.open(filename, 'rb') as f:
return pickle.load(f)
else:
with open(filename, 'rb') as f:
return pickle.load(f)


def P_warning(msg):
# Prints a warning message with color formatting.
# msg: The message to print as a warning.
"""Prints a warning message to the console.
Args:
msg (str): The warning message to print.
"""
print_Color_V2(f'<light_red>Warning: <yellow>{msg}')


def evaluate_model_full(y_test, model_pred, model=None, x_test=None):
"""Evaluates a machine learning model on a test set.
Args:
x_test: Test set features.
y_test: Test set labels.
model_pred: Model predictions.
model: The model object.
Returns:
None. Prints a table with accuracy, precision, recall and
F1 score.
"""
# Get the model predictions
if model_pred is None:
y_pred = model.predict(x_test)
else:
y_pred = model_pred

# Convert one-hot encoded predictions and labels to label encoded form
y_pred_bin = np.argmax(y_pred, axis=1)
y_test_bin = np.argmax(y_test, axis=1)

# Calculate normal metrics
accuracy = accuracy_score(y_test_bin, y_pred_bin)

# Calculate weighted metrics
weighted_precision = precision_score(
y_test_bin, y_pred_bin, average='macro')
weighted_f1 = f1_score(y_test_bin, y_pred_bin, average='macro')
weighted_recall = recall_score(y_test_bin, y_pred_bin, average='macro')

# Prepare data for the table
metrics = [["Accuracy", round(accuracy * 100, 6)],
["Precision", round(weighted_precision * 100, 6)],
["F1 Score", round(weighted_f1 * 100, 6)],
["Recall", round(weighted_recall * 100, 6)]]

# Print the table
print(tabulate(metrics, headers=["Metric", "Value"], tablefmt="pretty"))

10 changes: 10 additions & 0 deletions Interface/GUI/Data/Utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@
## Grad_cam (by GPT-4 😁)

## Other.py (by Me)

## FixedDropout.py (by Me)
For EfficientNet model. Example:
```python
from Utils.FixedDropout import FixedDropout
from keras.models import load_model

# Load the model
model = load_model('PAI_model_T.h5', custom_objects={'FixedDropout': FixedDropout})
```
Loading

0 comments on commit 7d023a3

Please sign in to comment.