Skip to content

Commit

Permalink
Merge pull request #113 from Aydinhamedi/main
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
Aydinhamedi authored Jan 25, 2024
2 parents cae5eda + cb5a8a1 commit 5069c77
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 253 deletions.
219 changes: 49 additions & 170 deletions BETA_E_Model_T&T.ipynb

Large diffs are not rendered by default.

88 changes: 12 additions & 76 deletions Model_T&T.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"source": [
"# keras/TF model\n",
"<pre>\n",
" Copyright (c) 2023 Aydin Hamedi\n",
" Copyright (c) 2024 Aydin Hamedi\n",
" \n",
" This software is released under the MIT License.\n",
" https://opensource.org/licenses/MIT\n",
Expand Down Expand Up @@ -19405,7 +19405,7 @@
"# Garbage Collection (memory)\n",
"gc.collect()\n",
"\n",
"Extra_EXT = '_T_BL' # _T or _T_BL\n",
"Extra_EXT = '_T' # _T or _T_BL\n",
"Train_data_test = False\n",
"if SAVE_TYPE == 'TF':\n",
" # Load the pre-trained model\n",
Expand All @@ -19419,39 +19419,21 @@
"\n",
"# Make predictions on validation data\n",
"val_predictions = model.predict(x_val)\n",
"val_predictions = np.argmax(val_predictions, axis=1)\n",
"\n",
"# Make predictions on Train data\n",
"if Train_data_test:\n",
" Train_predictions = model.predict(x_train)\n",
" Train_predictions = np.argmax(Train_predictions, axis=1)\n",
"\n",
"# Make predictions on test data\n",
"test_predictions = model.predict(x_test)\n",
"test_predictions = np.argmax(test_predictions, axis=1)\n",
"\n",
"# Convert y_val and y_test from one-hot encoder to their original form\n",
"y_val_original = np.argmax(y_val, axis=1)\n",
"y_test_original = np.argmax(y_test, axis=1)\n",
"if Train_data_test:\n",
" y_train_original = np.argmax(y_train, axis=1)\n",
"\n",
"# Calculate accuracy on validation data\n",
"val_accuracy = accuracy_score(y_val_original, val_predictions)\n",
"\n",
"# Calculate accuracy on Train data\n",
"if Train_data_test:\n",
" Train_accuracy = accuracy_score(y_val_original, Train_predictions)\n",
"\n",
"# Calculate accuracy on test data\n",
"test_accuracy = accuracy_score(y_test_original, test_predictions)\n",
"\n",
"# Print acc\n",
"if Train_data_test:\n",
" print(f'The accuracy of the model on Train data is {Train_accuracy:.2%}({Train_accuracy:.5%})')\n",
"print(f'The accuracy of the model on validation data is {val_accuracy:.2%}({val_accuracy:.5%})')\n",
"print(f'The accuracy of the model on test data is {test_accuracy:.2%}({test_accuracy:.5%})')\n",
"print('Val data acc:')\n",
"evaluate_model_full(y_val, val_predictions)\n",
"print('Test data acc:')\n",
"evaluate_model_full(y_test, test_predictions)\n",
"\n",
"# format data\n",
"val_predictions = np.argmax(val_predictions, axis=1)\n",
"test_predictions = np.argmax(test_predictions, axis=1)\n",
"y_val_original = np.argmax(y_val, axis=1)\n",
"y_test_original = np.argmax(y_test, axis=1)\n",
"# Visualize the predictions on validation data as a grid of squares\n",
"plt.figure(figsize=(12, 6))\n",
"for i in range(10):\n",
Expand Down Expand Up @@ -19512,8 +19494,6 @@
"\n",
"# Define the range of test data sizes to use\n",
"data_sizes = range(1, len(x_test), 4) \n",
"# Calculate the probability of a wrong prediction based on test accuracy\n",
"prob_wrong = 1 - test_accuracy\n",
"\n",
"# Create a list to store the number of incorrect predictions for each test data size\n",
"incorrect_predictions = []\n",
Expand Down Expand Up @@ -19549,51 +19529,7 @@
"plt.yticks(np.arange(0, max(incorrect_predictions) + 5, 3))\n",
"\n",
"plt.title('Number of Incorrect Predictions vs. Number of Data Points')\n",
"plt.show()\n",
"# Deprecated⚠️------------------------------>>>\n",
"# prob_L = 0.9995\n",
"# # Define the range of test data sizes to use\n",
"# data_sizes = range(1, len(x_test), 1) \n",
"\n",
"# # Calculate the probability of a wrong prediction based on test accuracy\n",
"# prob_wrong = 1 - test_accuracy\n",
"\n",
"# # Create a list to store the probability of getting at least one wrong answer for each test data size\n",
"# probabilities = []\n",
"\n",
"# # Calculate the probability of getting at least one wrong answer for each data size\n",
"# for size in data_sizes:\n",
"# # Calculate the cumulative distribution function (CDF) of the binomial distribution at 0\n",
"# cdf = binom.cdf(0, size, prob_wrong)\n",
"# # Subtract the CDF from 1 to get the probability of getting at least one wrong answer\n",
"# prob = 1 - cdf\n",
"# probabilities.append(prob)\n",
"\n",
"# # Find the index of the first data point that has a probability greater than prob_L%\n",
"# index = next((i for i, p in enumerate(probabilities) if p > prob_L), len(probabilities))\n",
"\n",
"# # Limit the x-axis to the first data point that has a probability greater than prob_L%\n",
"# data_sizes = data_sizes[:index+1]\n",
"# probabilities = probabilities[:index+1]\n",
"\n",
"# # Plot the probability vs. the number of data points\n",
"# plt.figure(figsize=(10, 6))\n",
"# plt.plot(data_sizes, probabilities)\n",
"# plt.xlabel('Number of Data Points')\n",
"# plt.ylabel('Probability')\n",
"\n",
"# # Add gridlines for the x and y axes\n",
"# plt.grid(True)\n",
"\n",
"# # Change the tick spacing for the x and y axes\n",
"# plt.xticks(np.arange(min(data_sizes), max(data_sizes)+1, 5 + 10))\n",
"# plt.yticks(np.arange(0, max(probabilities)+0.1, 5 / 100))\n",
"\n",
"# plt.ylim(top=1.01)\n",
"\n",
"# plt.title('Probability of Getting at Least One Wrong Answer vs. Number of Data Points')\n",
"# plt.show()\n",
"# Deprecated⚠️------------------------------<<<"
"plt.show()"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Pneumonia Detection AI 🤖 - (🚧Alpha-b🚧)
# Pneumonia Detection AI 🤖

<img src="https://img.shields.io/badge/Python-FFD43B?style=for-the-badge&logo=python&logoColor=blue"/> <img src="https://img.shields.io/badge/Jupyter-F37626.svg?&style=for-the-badge&logo=Jupyter&logoColor=white"/> <img src="https://img.shields.io/badge/TensorFlow-FF6F00?style=for-the-badge&logo=tensorflow&logoColor=white"/> <img src="https://img.shields.io/badge/Keras-FF0000?style=for-the-badge&logo=keras&logoColor=white"/> <img src="https://img.shields.io/badge/OpenCV-27338e?style=for-the-badge&logo=OpenCV&logoColor=white"/>

Expand Down
17 changes: 11 additions & 6 deletions Utils/Other.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,24 @@ def P_warning(msg):
"""
print_Color_V2(f'<light_red>Warning: <yellow>{msg}')

def evaluate_model_full(model, x_test, y_test):
def evaluate_model_full(y_test, model_pred, model=None, x_test=None):
"""Evaluates a machine learning model on a test set.
Args:
model: The model to evaluate.
x_test: The test data features.
y_test: The test data labels.
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.
None. Prints a table with accuracy, precision, recall and
F1 score.
"""
# Get the model predictions
y_pred = model.predict(x_test)
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)
Expand Down

0 comments on commit 5069c77

Please sign in to comment.