Skip to content

Instantly share code, notes, and snippets.

View prerakmody's full-sized avatar
🏠
Working from home

pmod prerakmody

🏠
Working from home
View GitHub Profile
@prerakmody
prerakmody / auc.py
Created January 13, 2022 11:17
Area Under the Curve (AUC) calculation
"""
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
@prerakmody
prerakmody / flipout_noneager_functionalapi.py
Last active September 30, 2021 13:27
Bayesian Models (Tensorflow 2.4.0 + Tensorflow Prob 0.12.1)
"""
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
@prerakmody
prerakmody / unc.py
Created August 12, 2021 11:40
Predictive (Entropy) + Model (Mutual Information) Uncertainty for DNNs
"""
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)
@prerakmody
prerakmody / colormap.py
Created August 5, 2021 08:44
Matplotlib Colors
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)
@prerakmody
prerakmody / plotly.py
Last active July 2, 2021 09:31
Quick / Few-line 3D visualization of Organ Volumes
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()
@prerakmody
prerakmody / focusnet_sample.py
Last active November 2, 2022 11:30
Netron.app example to visualize a tensorflow 2.x model
"""
pip install tensorflow
pip install tf2onnx keras2onnx onnxmltools
"""
import os
import pdb
import json
import traceback
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
@prerakmody
prerakmody / get_mask_boundary.py
Last active July 2, 2021 10:49
Volume Processing in TFlow 2.x
"""
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
@prerakmody
prerakmody / kl_tflow.py
Created June 11, 2021 10:51
KL (Kullback Leibler) Divergence in Flipout Models (PyTorch + TFlow)
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)
@prerakmody
prerakmody / ece.py
Last active October 13, 2021 12:11
Expected Calibration Error
"""
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
@prerakmody
prerakmody / dice_vs_ce_vs_brier.py
Last active June 24, 2022 14:34
Segmentation Loss
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]