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

book release #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Binary file added .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions Chap-2/.ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added Chap-2/Module-2-Uncertainty_10_0.pdf
Binary file not shown.
Binary file added Chap-2/Module-2-Uncertainty_18_0.pdf
Binary file not shown.
Binary file added Chap-2/Module-2-Uncertainty_20_0.pdf
Binary file not shown.
Binary file added Chap-2/Module-2-Uncertainty_22_4.pdf
Binary file not shown.
Binary file added Chap-2/Module-2-Uncertainty_24_5.pdf
Binary file not shown.
Binary file added Chap-2/Module-2-Uncertainty_26_0.pdf
Binary file not shown.
Binary file added Chap-2/Module-2-Uncertainty_6_0.pdf
Binary file not shown.
Binary file added Chap-2/Module-2-Uncertainty_8_0.pdf
Binary file not shown.
Binary file added Chap-2/UQs_2png.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions Chap-2/Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "11fa5e92",
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import uncertainty_wizard as uwiz\n",
"from tqdm.keras import TqdmCallback\n",
"\n",
"# Load the MNIST digits dataset\n",
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
"x_train = (x_train.astype('float32') / 255).reshape(x_train.shape[0], 28, 28, 1)\n",
"x_test = (x_test.astype('float32') / 255).reshape(x_test.shape[0], 28, 28, 1)\n",
"y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84f3eaee",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10 (tensorflow)",
"language": "python",
"name": "tensorflow"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
53 changes: 33 additions & 20 deletions Chap-2/ex01.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
"""
Created on Mon Sep 18 10:19:54 2023

@author: ozgur
@author: ozgur and murat
"""

#==============================================================================
### Part 1
#Import Libraries
import tensorflow as tf
import uncertainty_wizard as uwiz
from tqdm.keras import TqdmCallback

# Load the MNIST digits dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

# Normalize pixel values to the range [0, 1]
x_train = (x_train.astype('float32') / 255).reshape(x_train.shape[0], 28, 28, 1)
x_test = (x_test.astype('float32') / 255).reshape(x_test.shape[0], 28, 28, 1)

# Convert labels to one-hot encoding
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)

# Create the model
Expand All @@ -30,41 +36,41 @@
model.add(tf.keras.layers.Dense(10, activation='softmax'))

# Compiling and fitting is the same as in regular keras models as well:

model.compile(loss=tf.keras.losses.categorical_crossentropy,
optimizer='rmsprop',
metrics=['accuracy'])

model.fit(x_train, y_train, validation_split=0.1, batch_size=10000, epochs=20,
verbose=0, callbacks=[TqdmCallback(verbose=1)])

quantifiers = ['var_ratio', 'pred_entropy', 'mutu_info', 'mean_softmax']
# quantifiers = ['var_ratio', 'pred_entropy', 'mutu_info', 'mean_softmax']
quantifiers = ['var_ratio', 'pred_entropy', 'mean_softmax']
results = model.predict_quantified(x_test,
quantifier=quantifiers,
batch_size=64,
sample_size=32,
verbose=0)


#==============================================================================
### Part 2

from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import numpy as np

# Initialize the random number generator
np.random.seed(10)

# Calculate the predictions and prediction uncertainties using the pcs and mean_softmax quantifiers
pcs_predictions, pcs_uncertainties = results[0]
pred_entropy_predictions, pred_entropy_uncertainties = results[1]
mutu_info_predictions, mutu_info_uncertainties = results[2]
mean_softmax_predictions, mean_softmax_uncertainties = results[3]
mean_softmax_predictions, mean_softmax_uncertainties = results[2]

# Calculate the confusion matrix
# Compute the confusion matrix
confusion_mtx = confusion_matrix(y_test, pcs_predictions)

# Create a subplot with 2 rows and 3 columns
fig, axs = plt.subplots(2, 3, figsize=(15, 10))
# Create a subplot with 2 rows and 2 columns
fig, axs = plt.subplots(2, 2, figsize=(15, 10))

# Increase the font size for all subplots
fontsize = 20
Expand All @@ -81,13 +87,13 @@
axs[0, 1].set_ylabel('Count', fontsize=fontsize)
axs[0, 1].set_title('Uncertainty - PredictiveEntropy', fontsize=fontsize)

# Plot a histogram of the prediction uncertainties from mutu_info in the third subplot
axs[0, 2].hist(mutu_info_uncertainties, bins=50)
axs[0, 2].set_xlabel('Mutual Information', fontsize=fontsize)
axs[0, 2].set_ylabel('Count', fontsize=fontsize)
axs[0, 2].set_title('Uncertainty - MutualInformation', fontsize=fontsize)
# # Plot a histogram of the prediction uncertainties from mutu_info in the third subplot
# axs[0, 2].hist(mutu_info_uncertainties, bins=50)
# axs[0, 2].set_xlabel('Mutual Information', fontsize=fontsize)
# axs[0, 2].set_ylabel('Count', fontsize=fontsize)
# axs[0, 2].set_title('Uncertainty - MutualInformation', fontsize=fontsize)

# Plot a histogram of the prediction uncertainties from mean_softmax in the fourth subplot
# Plot a histogram of the prediction uncertainties from mean_softmax in the third subplot
axs[1, 0].hist(mean_softmax_uncertainties, bins=50)
axs[1, 0].set_xlabel('Mean Softmax', fontsize=fontsize)
axs[1, 0].set_ylabel('Count', fontsize=fontsize)
Expand All @@ -102,16 +108,17 @@
axs[1, 1].set_title('Confusion Matrix', fontsize=fontsize)

# Remove the unused subplot
fig.delaxes(axs[1, 2])
#fig.delaxes(axs[1, 2])

# Adjust the padding between the subplots
plt.tight_layout()

#plt.savefig("../../../Module-2-Uncertainty_files/Module-2-Uncertainty_6_0.pdf",bbox_inches='tight')
# plt.savefig("Module-2-Uncertainty_6_0.pdf",bbox_inches='tight')

# Show the plot
plt.show()

#==============================================================================
# Part 3

# Set a threshold to determine highly uncertain instances
Expand All @@ -131,6 +138,7 @@
# Increase the font size for titles
title_fontsize = 20

# Initiate a Loop to Iterate Highly Uncertain Images
for i, idx in enumerate(highly_uncertain_indices):
row, col = divmod(i, 5)
axs[row, col].imshow(x_test[idx, :, :, 0], cmap='gray')
Expand All @@ -141,11 +149,12 @@
# Adjust the padding between the subplots
plt.tight_layout()

# plt.savefig("../../../Module-2-Uncertainty_files/Module-2-Uncertainty_8_0.pdf",bbox_inches='tight')
# plt.savefig("Module-2-Uncertainty_8_0.pdf",bbox_inches='tight')

# Show the plot
plt.show()

#==============================================================================
# Part 4

# Display the highly uncertain images
Expand All @@ -156,6 +165,7 @@
# Increase the font size for axes labels
axes_label_fontsize = 25

# Initiate a Loop to Iterate Highly Uncertain Images
for i, idx in enumerate(highly_uncertain_indices):
row, col = divmod(i, 5)
predictions = model.inner.predict(np.expand_dims(x_test[idx], axis=0), verbose=0)[0]
Expand All @@ -168,9 +178,12 @@
axs[row, col].set_ylim(0.00, 0.5)
axs[row, col].grid()

# Adjust the padding between the subplots
plt.tight_layout()

#plt.savefig("../../../Module-2-Uncertainty_files/Module-2-Uncertainty_10_0.pdf",bbox_inches='tight')
# Save results
# plt.savefig("Module-2-Uncertainty_10_0.pdf",bbox_inches='tight')

# Show the plot
plt.show()

Loading
Loading