Skip to content

Commit

Permalink
Merge pull request #87 from Aydinhamedi/Beta-b
Browse files Browse the repository at this point in the history
Beta b
  • Loading branch information
Aydinhamedi authored Jan 10, 2024
2 parents e1c85b8 + 027c57a commit 3f63cb2
Show file tree
Hide file tree
Showing 9 changed files with 39,487 additions and 9,918 deletions.
48 changes: 45 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,28 +1,70 @@
# Exclude Visual Studio Code project settings
/.vscode

# Exclude Visual Studio project settings
/.vs

# Exclude database files
/Database

# Exclude downloaded files
/download

# Exclude model files
/models

# Exclude Python cache files
__pycache__/

# Ignore everything in the directory
# Ignore all files in the logs/fit directory
logs/fit/*

# Ignore all files in the Samples directory
Samples/*
# Except for directories ending with _STR

# Do not ignore directories in logs/fit that end with _STR
!logs/fit/*_STR/

# Do not ignore directories in Samples that end with _STR
!Samples/*_STR/

# Do not ignore .gz files in directories in Samples that end with _STR
!Samples/*_STR.gz

# Exclude specific model weight files
/PAI_model_weights.h5
/PAI_model_weights_BL.h5
/PAI_model_T.h5
/PAI_model_T_BL.h5

# Exclude cache files
/cache

# Exclude build artifacts
/Build

# Exclude validation files
/validation

# Exclude project file for Pneumonia AI
/Pneumonia AI.pyproj

# Exclude specific model file used in the CLI interface
/Interface/CLI/Data/PAI_model.h5

# Exclude temporary files
/TEMP.txt
/freearc1.tmp

# Exclude logs and dataset files in the CLI interface
/Interface/CLI/Data/logs
/Interface/CLI/Data/dataset.npy
/Interface/CLI/Data/dataset.npy

# Exclude temporary Python version file in the CLI interface
/Interface/CLI/Data/Python Ver.tmp

# Exclude specific model file used in the GUI interface
/Interface/GUI/Data/PAI_model.h5

# Exclude temporary Python version file in the GUI interface
/Interface/GUI/Data/Python Ver.tmp
12,484 changes: 7,465 additions & 5,019 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.
56 changes: 43 additions & 13 deletions Interface/CLI/CLI.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,69 @@
TITLE Pneumonia AI CLI
set DEBUG=0
set arg=%1
set PV_filepath="Data\\Python Ver.tmp"

REM Check if the fast start flag is used
if "%arg%"=="-f" (
goto :FAST_START)
REM Check Python version
echo Checking Python version...
goto :FAST_START
)

REM Check if Python is installed
python --version 2>NUL >NUL
if errorlevel 1 goto errorNoPython
FOR /F "tokens=2 delims=." %%i IN ('python --version 2^>^&1') DO set python_version=%%i
if %python_version% LSS 9 (
if errorlevel 1 goto :errorNoPython
@REM Geting the Python path and Python install time
FOR /f "delims=" %%p in ('where python') do SET PYTHONPATH=%%p
FOR %%A in (%PYTHONPATH%) do (
SET Python_INSTALLTIME=%%~tA
)
REM Check if the Python version file exists and matches the current Python version
FOR /F "delims=" %%i IN ('python --version 2^>^&1') DO set current_python_version=%%i
set "current_python_version=%current_python_version% %Python_INSTALLTIME%"
if not exist %PV_filepath% (
goto :PASS_PVF_CHECK
)
set /p file_python_version=<%PV_filepath%
if "%file_python_version%"=="%current_python_version% " (
goto :FAST_START
)

:PASS_PVF_CHECK
REM Write the current Python version to the file
echo Checking Python version...
REM Ensure Python version is 3.9 or higher
FOR /F "tokens=2 delims=." %%i IN ('python --version 2^>^&1') DO set python_version_major=%%i
if %python_version_major% LSS 9 (
echo Warning: Please update your Python version to 3.9.x or higher!
pause
exit /B
)

REM Verify required libraries
REM Check if the required packages are installed
echo Checking the required packages...
for /F "usebackq delims==" %%i in ("Data\requirements.txt") do (
call :check_install %%i
call :check_install %%i
)

REM Write the current Python version + Python install time to the file
echo %current_python_version% > %PV_filepath%
@REM Pause for user input
echo Press any key to load the CLI...
pause > nul

:FAST_START
REM Print loading message
REM Print the appropriate loading message
if "%arg%"=="-f" (
echo Loading the CLI fast...
) else (
echo Loading the CLI...
)

:restart
REM Clear the terminal
REM Clear the terminal and start the Python CLI script
timeout /t 1 >nul
cls
REM Start the Python script
python "Data\CLI_main.py"

REM Prompt to restart or quit the CLI
set /p restart="Do you want to restart the CLI or quit the CLI (y/n)? "
if /i "%restart%"=="y" (
goto :restart
Expand All @@ -45,11 +73,13 @@ if /i "%restart%"=="y" (
)

:errorNoPython
echo Error^: Python is not installed
REM Handle the error if Python is not installed
echo Error: Python is not installed
pause
goto :EOF

:check_install
REM Check if a package is installed and offer to install it if not
set userinput=Y
pip show %1 >nul
if ERRORLEVEL 1 (
Expand Down
102 changes: 63 additions & 39 deletions Interface/CLI/Data/CLI_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
Database_dir = 'Data/dataset.npy'
IMG_AF = ('JPEG', 'PNG', 'BMP', 'TIFF', 'JPG')
Github_repo_Releases_Model_name = 'PAI_model_T.h5'
Github_repo_Releases_Model_light_name = 'PAI_model_light_T.h5'
Github_repo_Releases_URL = 'https://api.github.com/repos/Aydinhamedi/Pneumonia-Detection-Ai/releases/latest'
Model_FORMAT = 'H5_SF' # TF_dir/H5_SF
IMG_RES = (224, 224, 3)
Expand Down Expand Up @@ -370,7 +371,7 @@ def CI_tmwd(argv_Split: list = ['none']):
print_Color('loading the Ai model...', ['normal'])
model = load_model(Model_dir)
except (ImportError, IOError):
print_Color('~*ERROR: ~*Failed to load the model.',
print_Color('~*ERROR: ~*Failed to load the model. Try running `uaim` first.',
['red', 'yellow'], advanced_mode=True)
else:
print('Training the model...\n')
Expand All @@ -392,7 +393,7 @@ def CI_ulmd():
['yellow'])

# CI_pwai
def CI_pwai():
def CI_pwai(Auto: bool = False):
# global var import
global model
# check for input img
Expand All @@ -402,7 +403,7 @@ def CI_pwai():
print_Color('loading the Ai model...', ['normal'])
model = load_model(Model_dir)
except (ImportError, IOError):
print_Color('~*ERROR: ~*Failed to load the model.',
print_Color('~*ERROR: ~*Failed to load the model. Try running `uaim` first.',
['red', 'yellow'], advanced_mode=True)
else:
print_Color('predicting with the Ai model...', ['normal'])
Expand All @@ -417,11 +418,14 @@ def CI_pwai():
print_Color('~*WARNING: ~*the confidence is low.',
['red', 'yellow'], advanced_mode=True)
if model_prediction == 1:
print_Color('~*Do you want to see a Grad cam of the model (BETA)? ~*[~*Y~*/~*n~*]: ',
['yellow', 'normal', 'green', 'normal', 'red', 'normal'],
advanced_mode=True,
print_END='')
Grad_cam_use = input('')
if not Auto:
print_Color('~*Do you want to see a Grad cam of the model? ~*[~*Y~*/~*n~*]: ',
['yellow', 'normal', 'green', 'normal', 'red', 'normal'],
advanced_mode=True,
print_END='')
Grad_cam_use = input('')
else:
Grad_cam_use = 'y'
if Grad_cam_use.lower() == 'y':
clahe = cv2.createCLAHE(clipLimit=1.8)
Grad_cam_heatmap = make_gradcam_heatmap(img_array,
Expand Down Expand Up @@ -458,18 +462,18 @@ def CI_rlmw():
try:
model = load_model(Model_dir)
except (ImportError, IOError):
print_Color('~*ERROR: ~*Failed to load the model.',
print_Color('~*ERROR: ~*Failed to load the model. Try running `uaim` first.',
['red', 'yellow'], advanced_mode=True)
print_Color('loading the Ai model done.', ['normal'])

# CI_liid
def CI_liid():
def CI_liid(Auto: bool = False):
# global var import
global img_array
global label
replace_img = 'y'
# check for img
if img_array is not None:
if img_array is not None and not Auto:
# Ask the user if they want to replace the image
print_Color('~*Warning: An image is already loaded. Do you want to replace it? ~*[~*Y~*/~*n~*]: ',
['yellow', 'normal', 'green', 'normal', 'red', 'normal'],
Expand All @@ -478,12 +482,14 @@ def CI_liid():
replace_img = input('')
# If the user answers 'n' or 'N', return the existing img_array
if replace_img.lower() == 'y':
print_Color('img dir. Enter \'G\' for using GUI: ',
['yellow'], print_END='')
img_dir = input().strip('"')
if img_dir.lower() == 'g':
if not Auto:
print_Color('img dir. Enter \'G\' for using GUI: ',
['yellow'], print_END='')
img_dir = input().strip('"')
if img_dir.lower() == 'g':
img_dir = open_file_GUI()
else:
img_dir = open_file_GUI()

logger.debug(f'CI_liid:img_dir {img_dir}')
# Extract file extension from img_dir
try:
Expand Down Expand Up @@ -516,39 +522,53 @@ def CI_liid():
img_array = np.expand_dims(img_array, axis=0)

# Assign labels to the image
print_Color('~*Enter label ~*(0 for Normal, 1 for Pneumonia, 2 Unknown): ', [
'yellow', 'normal'], print_END='', advanced_mode=True)
try:
label = int(input(''))
except ValueError:
print_Color('~*ERROR: ~*Invalid input.',
['red', 'yellow'], advanced_mode=True)
logger.warning('CI_liid>>ERROR: Invalid input label.')
else:
logger.debug(f'CI_liid:(INPUT) label {label}')
if label in [0, 1]:
# Convert label to categorical format
label = to_categorical(int(label), num_classes=2)
print_Color('The label is saved.', ['green'])
if not Auto:
print_Color('~*Enter label ~*(0 for Normal, 1 for Pneumonia, 2 Unknown): ', [
'yellow', 'normal'], print_END='', advanced_mode=True)
try:
label = int(input(''))
except ValueError:
print_Color('~*ERROR: ~*Invalid input.',
['red', 'yellow'], advanced_mode=True)
logger.warning('CI_liid>>ERROR: Invalid input label.')
else:
label = None
print_Color('The image is loaded.', ['green'])
logger.debug(f'CI_liid:(INPUT) label {label}')
if label in [0, 1]:
# Convert label to categorical format
label = to_categorical(int(label), num_classes=2)
print_Color('The label is saved.', ['green'])
else:
label = None
print_Color('The image is loaded.', ['green'])

# CI_csaa
def CI_csaa():
print_Color(CSAA, ['yellow', 'green'], advanced_mode=True)

# CI_uaim
def CI_uaim():
download_file_from_github(Github_repo_Releases_URL,
Github_repo_Releases_Model_name,
Model_dir,
4096)
print_Color('~*Do you want to download the light model? ~*[~*Y~*/~*n~*]: ',
['yellow', 'normal', 'green', 'normal', 'red', 'normal'],
advanced_mode=True,
print_END='')
download_light_model = input('')
if download_light_model.lower() == 'y':
Github_repo_Releases_Model_name_temp = Github_repo_Releases_Model_light_name
else:
Github_repo_Releases_Model_name_temp = Github_repo_Releases_Model_name
try:
download_file_from_github(Github_repo_Releases_URL,
Github_repo_Releases_Model_name_temp,
Model_dir,
1024)
except Exception:
print_Color('\n~*ERROR: ~*Failed to download the model.', ['red', 'yellow'], advanced_mode=True)

# CMT>>>
command_tuple = (
'help', # help
'atmd', # add to model dataset
'axid', # simple image classification
'tmwd', # train model with dataset
'ulmd', # upload model data set (not available!!!)
'pwai', # predict with Ai
Expand All @@ -574,12 +594,13 @@ def CI_uaim():
# '─'
cmd_descriptions = {
'help': 'Show the help menu with the list of all available commands',
'liid': 'Load image data for input',
'pwai': 'Make predictions using the trained AI model'
'axid': 'simple auto classification'
}
cmd_descriptions_other = {
'liid': 'Load image data for input',
'pwai': 'Make predictions using the trained AI model',
'atmd': 'Add data to the model dataset for training',
'tmwd': f'Train the model with the existing dataset.\n\
'tmwd': f'Train the model with the existing dataset. \x1b[31m(deprecated!)\x1b[0m\n\
│ └────Optional Args:\n\
│ ├────\'-i\' Ignore the limits.\n\
│ └────\'-e\' The number after \'e\' will be training epochs (default: {train_epochs_def}).\n\
Expand Down Expand Up @@ -658,6 +679,9 @@ def main():
CI_ulmd()
case 'pwai':
CI_pwai()
case 'axid':
CI_liid(Auto=True)
CI_pwai(Auto=True)
case 'rlmw':
CI_rlmw()
case 'liid':
Expand Down
Binary file removed Interface/CLI/Data/dataset.npy
Binary file not shown.
Loading

0 comments on commit 3f63cb2

Please sign in to comment.