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 -*- | |
""" | |
nii_to_tif | |
convert all mha files in a directory to nifti | |
Author: Jacob Reinhold ([email protected]) | |
""" |
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) |
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
loss = nn.MSELoss() | |
learner = faiv.Learner(idb, model, loss_func=loss) | |
learner.lr_find() | |
learner.recorder.plot() # pick learning rate based on this plot | |
cbs = [faiv.callbacks.CSVLogger(learner, 'history')] | |
learner.fit_one_cycle(100, 1e-2, callbacks=cbs) |
Loading
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 | |
# convert all parrec files in a directory to nifti | |
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 | |
# -*- coding: utf-8 -*- | |
""" | |
synthnn.util.metrics | |
custom metrics for the synthnn package (used in fastai implementation) | |
Author: Jacob Reinhold ([email protected]) | |
Created on: Nov 15, 2018 |