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 / hdc.py
Last active August 15, 2022 09:11
Receptive Field for dilated convolutions
"""
Motivation: Understanding Convolution for Semantic Segmentation (https://arxiv.org/pdf/1702.08502.pdf)
: https://stats.stackexchange.com/questions/265462/whats-the-receptive-field-of-a-stack-of-dilated-convolutions
"""
import pdb
import traceback
import numpy as np
import matplotlib.pyplot as plt
@prerakmody
prerakmody / actualsize.py
Created July 3, 2022 08:32
Object Size in python
import gc
import sys
import humanize # pip install humanize
def actualsize(input_obj):
# https://towardsdatascience.com/the-strange-size-of-python-objects-in-memory-ce87bdfbb97f
memory_size = 0
ids = set()
objects = [input_obj]
while objects:
@prerakmody
prerakmody / slurm.md
Last active November 16, 2022 13:44
SLURM Commands

Useful aliases

Add these aliases in your ~/.bashrc file. To use them, save the updated file and type . ~/.bashrc to reload the bash file

  1. View all running jobs
    • alias qq="watch -n 0.1 'squeue -u ppmody -o \"%.10i %.9P %.24j %.8u %.2t %.10M %.5D %R\" ; echo -e \"\n\" ; squeue -p LKEBgpu,highmemgpu,gpu --sort=\"N,-i\" -o \"%.10i %.9P %.12j %.8u %.2t %.10M %.5D %.16R %.3C %.7m %b\"; echo -e \"\n\"; sinfo -a' "
      
  2. View resources within the cluster
    • alias qqq="scontrol -o show nodes |grep -e \"lkeb\" -e \"gpu.ai\" -e \"gpu\"| awk '{ print \$1, \$4, \$5, \$9, \$23, \$24, \$25}'"
      
@prerakmody
prerakmody / contours_with_gt_pred.py
Last active September 6, 2022 12:04
Organ Contours (using matplotlib, opencv, scipy)
"""
This script shall plot contours using cv2 on an image
- Useful for plotting organ contours on a medical image
-- Note: Organ Mask should contain labels values in the [0,255] range
"""
# Standard Libraries
import pdb
import cv2
import numpy as np
@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"