Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta b #101

Merged
merged 3 commits into from
Jan 10, 2024
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
50 changes: 3 additions & 47 deletions BETA_E_Model_T&T.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2771,8 +2771,8 @@
"for i in range(10):\n",
" plt.subplot(2, 5, i+1)\n",
" img = x_val[i]\n",
" heatmap = make_gradcam_heatmap(img[np.newaxis, ...], model, 'top_activation', second_last_conv_layer_name = 'top_conv', sensitivity_map = 2) \n",
" heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))\n",
" heatmap = make_gradcam_heatmap(img[np.newaxis, ...], model, 'top_activation', second_last_conv_layer_name = 'top_conv', sensitivity_map = 1) \n",
" heatmap = cv2.resize(np.clip(heatmap, 0, 1), (img.shape[1], img.shape[0]))\n",
" heatmap = np.uint8(255 * heatmap)\n",
" # Apply Adaptive Histogram Equalization\n",
" clahe = cv2.createCLAHE(clipLimit=1, tileGridSize=(8,8)) # Create CLAHE object\n",
Expand Down Expand Up @@ -2854,51 +2854,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 Interface/CLI/Data/CLI_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def CI_pwai(Auto: bool = False):
model, 'top_activation',
second_last_conv_layer_name = 'top_conv',
sensitivity_map = 2, pred_index=tf.argmax(model_prediction_ORG[0]))
Grad_cam_heatmap = cv2.resize(Grad_cam_heatmap, (img_array.shape[1], img_array.shape[2]))
Grad_cam_heatmap = cv2.resize(np.clip(Grad_cam_heatmap, 0, 1), (img_array.shape[1], img_array.shape[2]))
Grad_cam_heatmap = np.uint8(255 * Grad_cam_heatmap)
Grad_cam_heatmap = cv2.applyColorMap(Grad_cam_heatmap, cv2.COLORMAP_VIRIDIS)
Grad_cam_heatmap = np.clip(np.uint8((Grad_cam_heatmap * 0.3) + ((img_array * 255) * 0.7)), 0, 255)
Expand Down
10 changes: 3 additions & 7 deletions Interface/CLI/Data/Utils/Grad_cam.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import glob
import numpy as np
import tensorflow as tf
# Other
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.get_logger().setLevel('ERROR')
physical_devices = tf.config.list_physical_devices('GPU')
for gpu_instance in physical_devices:
Expand Down Expand Up @@ -37,7 +39,7 @@ def make_gradcam_heatmap(img_array,
model,
last_conv_layer_name,
second_last_conv_layer_name=None,
pred_index=None, threshold=0,
pred_index=None,
sensitivity_map=1.0):
"""
Function to compute the Grad-CAM heatmap for a specific class, given an input image.
Expand All @@ -48,17 +50,11 @@ def make_gradcam_heatmap(img_array,

# Compute heatmap for the last convolutional layer
heatmap = _compute_heatmap(model, img_array, last_conv_layer_name, pred_index)

# Apply threshold and adjust sensitivity
heatmap = np.where(heatmap > threshold, heatmap, 0)
heatmap = heatmap ** sensitivity_map

if second_last_conv_layer_name is not None:
# Compute heatmap for the second last convolutional layer
heatmap_second = _compute_heatmap(model, img_array, second_last_conv_layer_name, pred_index)

# Apply threshold and adjust sensitivity
heatmap_second = np.where(heatmap_second > threshold, heatmap_second, 0)
heatmap_second = heatmap_second ** sensitivity_map

# Average the two heatmaps
Expand Down
33 changes: 33 additions & 0 deletions Interface/CLI/Run_CLI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import subprocess
import traceback
import sys
import os
# Other
from Data.Utils.print_color_V1_OLD import print_Color
def run_program(file_path):
while True:
try:
try:
# Run the other Python program using subprocess
subprocess.run(["python", file_path], check=True)
except subprocess.CalledProcessError as ERROR_Py:
print_Color("~*An error occurred: \nERROR: ~*" + str(ERROR_Py), ['yellow', 'red'], advanced_mode=True)
print_Color('~*Do you want to see the detailed error message? ~*[~*Y~*/~*n~*]: ',
['yellow', 'normal', 'green', 'normal', 'red', 'normal'],
advanced_mode = True,
print_END='')
show_detailed_error = input('')
if show_detailed_error.lower() == 'y':
print_Color('detailed error message:', ['yellow'])
#print_Color('1th ERROR (FILE ERROR) [!MAIN!]:', ['red'])
print_Color('2th ERROR (subprocess.run ERROR):', ['red'])
traceback.print_exc()
choice = input("Do you want to restart the program? (y/n): ")
if choice.lower() != "y":
break
os.system('cls' if os.name == 'nt' else 'clear')
else:
break
except OSError:
break
run_program('Data\CLI_main.py')
50 changes: 3 additions & 47 deletions Model_T&T.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2771,8 +2771,8 @@
"for i in range(10):\n",
" plt.subplot(2, 5, i+1)\n",
" img = x_val[i]\n",
" heatmap = make_gradcam_heatmap(img[np.newaxis, ...], model, 'top_activation', second_last_conv_layer_name = 'top_conv', sensitivity_map = 2) \n",
" heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))\n",
" heatmap = make_gradcam_heatmap(img[np.newaxis, ...], model, 'top_activation', second_last_conv_layer_name = 'top_conv', sensitivity_map = 1) \n",
" heatmap = cv2.resize(np.clip(heatmap, 0, 1), (img.shape[1], img.shape[0]))\n",
" heatmap = np.uint8(255 * heatmap)\n",
" # Apply Adaptive Histogram Equalization\n",
" clahe = cv2.createCLAHE(clipLimit=1, tileGridSize=(8,8)) # Create CLAHE object\n",
Expand Down Expand Up @@ -2854,51 +2854,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
10 changes: 3 additions & 7 deletions Utils/Grad_cam.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import glob
import numpy as np
import tensorflow as tf
# Other
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.get_logger().setLevel('ERROR')
physical_devices = tf.config.list_physical_devices('GPU')
for gpu_instance in physical_devices:
Expand Down Expand Up @@ -37,7 +39,7 @@ def make_gradcam_heatmap(img_array,
model,
last_conv_layer_name,
second_last_conv_layer_name=None,
pred_index=None, threshold=0,
pred_index=None,
sensitivity_map=1.0):
"""
Function to compute the Grad-CAM heatmap for a specific class, given an input image.
Expand All @@ -48,17 +50,11 @@ def make_gradcam_heatmap(img_array,

# Compute heatmap for the last convolutional layer
heatmap = _compute_heatmap(model, img_array, last_conv_layer_name, pred_index)

# Apply threshold and adjust sensitivity
heatmap = np.where(heatmap > threshold, heatmap, 0)
heatmap = heatmap ** sensitivity_map

if second_last_conv_layer_name is not None:
# Compute heatmap for the second last convolutional layer
heatmap_second = _compute_heatmap(model, img_array, second_last_conv_layer_name, pred_index)

# Apply threshold and adjust sensitivity
heatmap_second = np.where(heatmap_second > threshold, heatmap_second, 0)
heatmap_second = heatmap_second ** sensitivity_map

# Average the two heatmaps
Expand Down