Skip to content

Instantly share code, notes, and snippets.

View enochkan's full-sized avatar
❤️

Enoch Kan enochkan

❤️
View GitHub Profile
@enochkan
enochkan / test.py
Created November 6, 2020 17:28
testing adam optim
w_0 = 0
b_0 = 0
adam = AdamOptim()
t = 1
converged = False
while not converged:
dw = grad_function(w_0)
db = grad_function(b_0)
w_0_old = w_0
@enochkan
enochkan / loss_and_grad_funcs.py
Created November 6, 2020 17:26
loss and grad funcs
def loss_function(m):
return m**2-2*m+1
## take derivative
def grad_function(m):
return 2*m-2
def check_convergence(w0, w1):
return (w0 == w1)
@enochkan
enochkan / adam_optim.py
Last active December 31, 2022 16:16
class definition of adam optimizer
import numpy as np
class AdamOptim():
def __init__(self, eta=0.01, beta1=0.9, beta2=0.999, epsilon=1e-8):
self.m_dw, self.v_dw = 0, 0
self.m_db, self.v_db = 0, 0
self.beta1 = beta1
self.beta2 = beta2
self.epsilon = epsilon
self.eta = eta
def update(self, t, w, b, dw, db):
@enochkan
enochkan / torch_metrics_demo.txt
Last active November 4, 2020 05:17
torch-metrics
from torch_metrics import MSEMetric, MAEMetric, RSquaredMetric
import torch
metric = MSEMetric()
t1 = torch.tensor([1., 2., 3.])
t2 = torch.tensor([1., 5., 25.])
print(metric(t1, t2))
@enochkan
enochkan / generator.py
Created June 12, 2020 05:41
vox2vox generator
class GeneratorUNet(nn.Module):
def __init__(self, in_channels=1, out_channels=1):
super(GeneratorUNet, self).__init__()
self.down1 = UNetDown(in_channels, 64, normalize=False)
self.down2 = UNetDown(64, 128)
self.down3 = UNetDown(128, 256)
self.down4 = UNetDown(256, 512)
self.mid1 = UNetMid(1024, 512, dropout=0.2)
self.mid2 = UNetMid(1024, 512, dropout=0.2)
@enochkan
enochkan / diceloss.py
Created June 12, 2020 05:36
Generalized Dice Loss
class diceloss(torch.nn.Module):
def init(self):
super(diceLoss, self).init()
def forward(self,pred, target):
smooth = 1.
iflat = pred.contiguous().view(-1)
tflat = target.contiguous().view(-1)
intersection = (iflat * tflat).sum()
A_sum = torch.sum(iflat * iflat)
B_sum = torch.sum(tflat * tflat)
@enochkan
enochkan / loss.py
Created June 12, 2020 05:33
vox2vox model losses
# ---------------------
# Train Discriminator, only update every disc_update batches
# ---------------------
# Real loss
fake_B = generator(real_A)
pred_real = discriminator(real_B, real_A)
loss_real = criterion_GAN(pred_real, valid)
# Fake loss
pred_fake = discriminator(fake_B.detach(), real_A)
@enochkan
enochkan / models.py
Created June 12, 2020 05:11
Vox2Vox Encoder, Bottleneck and Decoder blocks
#***********************#
#***Code by:************#
#***Chi Nok Enoch Kan***#
#***********************#
#*******<(^.^)>*********#
#***********************#
#*****Encoder Block*****#
#***********************#
#***********************#
#***********************#
@enochkan
enochkan / simple.py
Last active November 7, 2019 01:59
miptools usage demonstration
import miptools as mt
#metadata windowing
mt.preprocess('./data/test.dcm', org='brain', windowing='simple', resample=False, visualize=True)
#bsb windowing
mt.preprocess('./data/test.dcm', org='brain', windowing='bsb', resample=False, visualize=True)
#sigmoid windowing
mt.preprocess('./data/test.dcm', org='brain', windowing='sigmoid', resample=False, visualize=True)