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 / 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]
@prerakmody
prerakmody / conversion.py
Last active July 19, 2023 00:09
Simple ITK (SITK) + ITK hacks (python)
import itk
import SimpleITK as sitk
def convert_sitk_to_itk(sitk_img):
img_array = sitk.GetArrayFromImage(sitk_img)
itk_img = itk.GetImageFromArray(img_array)
itk_img.SetOrigin(tuple(sitk_img.GetOrigin()))
itk_img.SetSpacing(tuple(sitk_img.GetSpacing()))
itk_img.SetDirection(itk.GetMatrixFromArray(np.reshape(np.array(sitk_img.GetDirection()), [sitk_img.GetDimension()]*2)))
@prerakmody
prerakmody / autoseg_han.csv
Last active January 26, 2021 11:34
Paper Review Gists
Feature AnatomyNet FocusNet StratifiedNet DeepMindNet
Input 3D Volume 3D volume 3D Sub-Volume 3D Sub-Volume
Crop Info 178×302×225 N/A 128x128x64 512x512x21
Training Data 38(MICCAI) + 46(Cetuximab) + 177 (Quebec) 38(MICCAI) 38(MICCAI) 663 (Private dataset)
Augmentation Affine + Elastic N/A Scaling 2D Augmentations
Architecture 3D UNet-variant 3D UNet-variant P-HNN 3D UNet-variant
Additional Blocks Squeeze-Excitation Residual Blocks Squeeze-Excitation Residual Blocks + DenseASPP NAS Deptwise-Separable Convs
Cascaded Architecture No Yes (for smaller optic organs) Yes (from anchor to small organs) No
Downsampling Dim/2 Dim/2 N/A Dim/64
Final Activation softmax N/A N/A Sigmoid
@prerakmody
prerakmody / dataset_loop.py
Last active December 26, 2020 16:29
Medical Dataloader in TensorFlow
class HaNMICCAI2015Dataset:
def __init__():
# initialize map, filter parameters
def generator():
# define generator, map and filter functions
batchsize = None # user defined value
prefetch_buffer = None # user defined value
shuffle_buffer = None # user defined value
def test(num1, num2):
return num1/num2
num = 1
den = 2
import timeit
t = timeit.timeit(lambda: test(num, den), number=5)
@prerakmody
prerakmody / convert_elastix_to_itk_transform.py
Last active October 22, 2020 10:16
ITK (Insight Toolkit)
from pathlib import Path
import itk
import SimpleITK as sitk
config = {
'transform' : {
'affine': 'AffineTransform'
, 'bspline': 'BSplineTransform'
}
@prerakmody
prerakmody / concurrent.py
Created October 1, 2020 10:32
Multi Threading Python
import time
import tqdm
import numpy as np
import concurrent
import concurrent.futures
def rand_func(param):
time.sleep(3)
print (param)