Skip to content

Instantly share code, notes, and snippets.

View jcreinhold's full-sized avatar

Jacob Reinhold jcreinhold

View GitHub Profile
@jcreinhold
jcreinhold / config.json
Last active April 30, 2021 14:29
Example config file for synthtorch
{
"Data Augmentation Options": {
"block": null,
"gain": null,
"gamma": null,
"hflip": false,
"mean": null,
"noise_pwr": 0,
"prob": [1.0, 0, 0, 0, 0],
"rotate": 15,
@jcreinhold
jcreinhold / check_imgs.py
Created January 31, 2019 19:32
check if all images in a directory are ok
#/usr/bin/env python
# check to see if any image in a directory has a problem
import argparse
import csv
from glob import glob
import os
import sys
import nibabel as nib
@jcreinhold
jcreinhold / list_attr_celeba.csv
Created April 24, 2019 21:07
Attributes for CelebA dataset in CSV format
We can't make this file beautiful and searchable because it's too large.
Filename,FiveoClockShadow,ArchedEyebrows,Attractive,BagsUnderEyes,Bald,Bangs,BigLips,BigNose,BlackHair,BlondHair,Blurry,BrownHair,BushyEyebrows,Chubby,DoubleChin,Eyeglasses,Goatee,GrayHair,HeavyMakeup,HighCheekbones,Male,MouthSlightlyOpen,Mustache,NarrowEyes,NoBeard,OvalFace,PaleSkin,PointyNose,RecedingHairline,RosyCheeks,Sideburns,Smiling,StraightHair,WavyHair,WearingEarrings,WearingHat,WearingLipstick,WearingNecklace,WearingNecktie,Young
1,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,1
2,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1
3,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1
4,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,1,0,1,1,0,1
5,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1
6,0,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,1,0,1,0,0,1
7,1,0,1,1,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1
8,1,1,
@jcreinhold
jcreinhold / tif_to_nii.py
Last active November 23, 2022 15:46
Convert TIFF image directory (of one image) to NIfTI
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
tif_to_nii
command line executable to convert a directory of tif images
(from one image) to a nifti image stacked along a user-specified axis
call as: python tif_to_nii.py /path/to/tif/ /path/to/nifti
(append optional arguments to the call as desired)
@jcreinhold
jcreinhold / uncertainty_dnn_classification.ipynb
Last active June 20, 2020 19:41
Toy classification example with uncertainty
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jcreinhold
jcreinhold / uncertainty_dnn_regression.ipynb
Last active June 20, 2020 18:40
Toy regression example with uncertainty
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jcreinhold
jcreinhold / extended_mse_loss.py
Last active June 19, 2020 21:17
MSE with uncertainty
class ExtendedMSELoss(nn.Module):
""" modified MSE loss for variance fitting """
def forward(self, out:torch.Tensor, y:torch.Tensor) -> torch.Tensor:
yhat, s = out
loss = torch.mean(0.5 * (torch.exp(-s) * F.mse_loss(yhat, y, reduction='none') + s))
return loss
@jcreinhold
jcreinhold / extended_l1_loss.py
Last active June 19, 2020 21:17
L1 loss with uncertainty
class ExtendedL1Loss(nn.Module):
""" modified L1 loss for scale param. fitting """
def forward(self, out:torch.Tensor, y:torch.Tensor) -> torch.Tensor:
yhat, s = out
loss = torch.mean((torch.exp(-s) * F.l1_loss(yhat, y, reduction='none')) + s)
return loss
@jcreinhold
jcreinhold / extended_bce_loss.py
Last active June 20, 2020 19:42
BCE loss with uncertainty
class ExtendedBCELoss(nn.Module):
""" modified BCE loss for variance fitting """
def forward(self, out:torch.Tensor, y:torch.Tensor, n_samp:int=10) -> torch.Tensor:
logit, sigma = out
dist = torch.distributions.Normal(logit, torch.exp(sigma))
mc_logs = dist.rsample((n_samp,))
loss = 0.
for mc_log in mc_logs:
loss += F.binary_cross_entropy_with_logits(mc_log, y)
loss /= n_samp
@jcreinhold
jcreinhold / regression_uncertainty.py
Last active June 20, 2020 19:31
Calculating uncertainty in regression tasks
def regression_uncertainty(yhat:torch.Tensor, s:torch.Tensor, mse:bool=True) -> Tuple[torch.Tensor, torch.Tensor]:
""" calculate epistemic and aleatory uncertainty quantities based on whether MSE or L1 loss used """
# variance over samples (dim=0), mean over channels (dim=1, after reduction by variance calculation)
epistemic = torch.mean(yhat.var(dim=0, unbiased=True), dim=1, keepdim=True)
aleatory = torch.mean(torch.exp(s), dim=0) if mse else torch.mean(2*torch.exp(s)**2, dim=0)
return epistemic, aleatory