This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env python | |
# convert all nifti files in a directory to float32 | |
import argparse | |
from glob import glob | |
import os | |
import sys | |
import nibabel as nib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# prune files from a directory that are different dimensions than the mode | |
import argparse | |
from collections import Counter | |
from glob import glob | |
import os | |
import shutil | |
import sys |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
rpca_cpu | |
implementations of RPCA on the CPU for low-rank and sparse | |
matrix decomposition as well as a nuclear-norm | |
minimization routine via singular value thresholding | |
for matrix completion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fastai.vision as faiv | |
import nibabel as nib | |
def open_nii(fn:str) -> faiv.Image: | |
""" Return fastai `Image` object created from NIfTI image in file `fn`.""" | |
x = nib.load(str(fn)).get_data() | |
return faiv.Image(torch.Tensor(x)) | |
class NiftiItemList(faiv.ImageItemList): | |
""" custom item list for nifti files """ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import singledispatch | |
@faiv.TfmPixel | |
@singledispatch | |
def crop(x, pct, axis:int) -> torch.Tensor: | |
"""" crop a 3d image along an axis """ | |
s = x.shape | |
i0, i1 = int(s[axis]*pct[0]), int(s[axis]*pct[1]) | |
return x[np.newaxis,i0:i1,:,:].contiguous() if axis == 0 else \ | |
x[np.newaxis,:,i0:i1,:].contiguous() if axis == 1 else \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
idb = (NiftiNiftiList.from_folder(data_dir/'t1', extensions=('.gz')) | |
.split_by_folder() | |
.label_from_func(get_y_fn) | |
.transform((tfms,tfms), tfm_y=True) | |
.databunch(bs=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def conv3d(ni:int, nf:int, ks:int=3, stride:int=1, pad:int=1, norm='batch'): | |
bias = not norm == 'batch' | |
conv = faiv.init_default(nn.Conv3d(ni,nf,ks,stride,pad,bias=bias)) | |
conv = spectral_norm(conv) if norm == 'spectral' else \ | |
weight_norm(conv) if norm == 'weight' else conv | |
layers = [conv] | |
layers += [nn.ReLU(inplace=True)] # use inplace due to memory constraints | |
layers += [nn.BatchNorm3d(nf)] if norm == 'batch' else [] | |
return nn.Sequential(*layers) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
norm = 'batch' | |
layers = ([res3d_block(1,15,7,norm=norm,dense=True)] + | |
[res3d_block(16,16,norm=norm) for _ in range(4)] + | |
[conv3d(16,1,ks=1,pad=0,norm=None)]) | |
model = nn.Sequential(*layers) |
OlderNewer