This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
References | |
1. Calculating area under the curve (AUC) using Riemann Sum - https://en.wikipedia.org/wiki/Riemann_sum | |
2. https://scikit-learn.org/stable/modules/generated/sklearn.metrics.auc.html#sklearn.metrics.auc | |
- it uses np.trapz() | |
- Code: https://github.com/numpy/numpy/blob/v1.22.0/numpy/lib/function_base.py#L4686-L4797 | |
3. Other methods implemented in TFlow | |
- https://wikidocs.net/24605 | |
- Note: This method only works right when we provide a list x that has evenly spaced values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
OG Ref: https://github.com/tensorflow/probability/issues/620 | |
Goals | |
- To experiment with DNNs built using Flipout layers in both eager and non-eager mode | |
- Eager mode allows for debugging using tf.print() | |
- Non-eager mode is supposed to be faster and less memory consuming since its pre-computes functions in a graph | |
- Models will be made using functional API and the dataset will not use tf.data.Dataset | |
Notes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Understanding Entropy and Mutual information (epistemic uncertainty) with Stochastic Forward Passes (=M) | |
Binary Classification | |
- y = {(p1, (1-p1)), (pn, (1-pn)), ... (pn, (1-pn))} | |
- Ent = -(pbar.log(pbar) + (1-pbar).log(1-pbar)) | |
- MIF = Ent + avg_M(p1.log(p1) + ... + pn.log(pn) + (1-p1).log(p1) + ... + pn.log(1-pn)) | |
- Case1 | |
-- y = {(1,0), (1,0), ..., (1,0)} | |
-- pbar = (1,0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib | |
import matplotlib.pyplot as plt | |
cmap = matplotlib.cm.get_cmap('magma') | |
f,axarr = plt.subplots(1,3) | |
x = np.arange(0,1,0.01) | |
img = np.zeros((len(x),len(x),4)) | |
for x_id, x_ in enumerate(x):img[x_id,:,:] = np.repeat(np.expand_dims(np.array(cmap(x_)), axis=0), len(x), axis=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import skimage | |
import skimage.measure | |
import plotly.graph_objects as go | |
voxel_mask = # | |
verts, faces, _, _ = skimage.measure.marching_cubes(voxel_mask, step_size=1) | |
fig = go.Figure(data=[go.Mesh3d(x=verts[:,0], y=verts[:,1], z=verts[:,2], i=faces[:,0],j=faces[:,1],k=faces[:,2])]) | |
fig.write_html('tmp.html') # in VS-Code install the "HTML Preview" extension (Analytic Signal Limited) | |
fig.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
pip install tensorflow | |
pip install tf2onnx keras2onnx onnxmltools | |
""" | |
import os | |
import pdb | |
import json | |
import traceback | |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This method uses the max pooling operation to get the boundary around a binary mask | |
Can be used in segmentation tasks where one wants to specific a loss for the boundary pixels/voxels | |
""" | |
import os | |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" | |
import nrrd | |
import numpy as np | |
import tensorflow as tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" | |
import tensorflow as tf # v2.4 with CUDA11.0.0 & CuDNN8.0.0 | |
import tensorflow_probability as tfp # v0.12.1 | |
if len(tf.config.list_physical_devices('GPU')):tf.config.experimental.set_memory_growth(tf.config.list_physical_devices('GPU')[0], True) | |
x = tf.random.normal((1,100,100,100,10)) # B, H,W,D,C | |
layer = tfp.layers.Convolution3DFlipout(filters=16, kernel_size=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Expected Calibration Error for semantic segmentation tasks | |
""" | |
import pdb | |
import nrrd # pip install pynrrd | |
import traceback | |
import numpy as np | |
from pathlib import Path | |
import matplotlib.pyplot as plt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
f,axarr = plt.subplots(1,2) | |
# Step 1 | |
x = [0.01, 0.02, 0.03, 0.04] + np.arange(0.05,1.05,0.05).tolist() | |
y_ce = [-np.log(each) for each in x] | |
y_focal1 = [-1*(1 - each)**1*np.log(each) for each in x] | |
y_focal2 = [-1*(1 - each)**2*np.log(each) for each in x] |