import fastai.medical.imaging
Simulated MR artifacts (torchio)
If you use these transformations which all build on TorchIO, please cite the following paper: > Pérez-García et al., TorchIO: a Python library for efficient loading, preprocessing, augmentation and patch-based sampling of medical images in deep learning. Link: https://arxiv.org/abs/2003.04696
Spike artifact
import matplotlib.pyplot as plt
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
get_spike_series
get_spike_series (image, model, start=0, end=2.5, step=0.5, spikePosition=[0.1, 0.1], **kwargs)
spikeTransform
spikeTransform (image, intensityFactor, spikePosition=[0.1, 0.1])
= "example/b0/images/val_example_0.png"
fname = lambda: Image.open(fname).resize((256,256)).convert ("RGB")
img = lambda: Image.open(fname.replace("image","mask")) trueMask
1, spikePosition=[.5,.5]) spikeTransform(img(),
def interactiveSpikeTransform(intensity, x, y):
#figsize=(8,8)) plt.imshow(spikeTransform(img(), intensity, [x,y]))
= interactive(
interactive_plot
interactiveSpikeTransform,=widgets.FloatSlider(min=0, max=3, step=.1, value=1, continuous_update=True),
intensity=widgets.FloatSlider(min=0, max=.99, step=.05, value=.5, continuous_update=True),
x=widgets.FloatSlider(min=0, max=.99, step=.05, value=.5, continuous_update=True)
y
) interactive_plot
.8, [.4,.3]), cmap="bone") #figsize=(8,8), plt.imshow(spikeTransform(img(),
<matplotlib.image.AxesImage>
def label_func(x):
pass
def acc_seg(input, target):
pass
def diceComb(input, targs):
pass
def diceLV(input, targs):
pass
def diceMY(input, targs):
pass
= Fastai2_model('chfc-cmi/transversal-cmr-seg', 'b0_transversal_5_5', force_reload=False) trainedModel
Using cache found in /home/markus/.cache/torch/hub/chfc-cmi_transversal-cmr-seg_master
Spike intensity
= get_spike_series(img(), trainedModel, truth=trueMask(), tfm_y=False) series
plot_series(series)
= [.45,.55]
spikePosition =trueMask(), spikePosition=spikePosition)) plot_series(get_spike_series(img(), trainedModel, truth
= [.5,.52]
spikePosition =trueMask(), spikePosition=spikePosition)) plot_series(get_spike_series(img(), trainedModel, truth
eval_spike_series
eval_spike_series (image, mask, model, step=0.1, start=0, end=2.5, spikePosition=[0.1, 0.1], param_name='intensity', **kwargs)
= eval_spike_series(img(), trueMask(), trainedModel)
results 'intensity'], results['c1'], label='LV')
plt.plot(results['intensity'], results['c2'], label='MY')
plt.plot(results[
plt.legend()'relative intensity')
plt.xlabel('dice')
plt.ylabel(= plt.title(f'spike position: {[.1,.1]}') _
Spike position
get_spike_pos_series
get_spike_pos_series (image, model, start=0.1, end=0.9, step=0.1, intensityFactor=0.5, spikePositionY=0.1, **kwargs)
spikePosTransform
spikePosTransform (image, spikePositionX, spikePositionY=0.1, intensityFactor=0.5)
= get_spike_pos_series(img(), trainedModel, intensityFactor=0.1, spikePositionY=0.1)
series =1, figsize=(20,15), param_name='Spike X Position') plot_series(series, nrow
eval_spike_pos_series
eval_spike_pos_series (image, mask, model, step=0.1, start=0.1, end=0.9, intensityFactor=0.1, param_name='Spike X Position', **kwargs)
= 0.2
intensityFactor = eval_spike_pos_series(img(), trueMask(), trainedModel, step=0.05, intensityFactor=intensityFactor)
results 'Spike X Position'], results['c1'], label='LV')
plt.plot(results['Spike X Position'], results['c2'], label='MY')
plt.plot(results[
plt.legend()'X position of the spike')
plt.xlabel('Dice')
plt.ylabel(= plt.title(f'Spike intensity = {intensityFactor}') _
gif_series(=0.1, end=0.9 ,step=0.1, intensityFactor=0.2),
get_spike_pos_series(img(),trainedModel, start"example/kaggle/spike_pos.gif",
="Spike X Position",
param_name=400
duration )
Bias Field (\(B_0\))
The bias field function is still experimental as it lacks configurability. To make it deterministic all coefficients are set to the exact same value. This represents only one specific possibility of field inhomogeneity.
= img()
image = np.array(image)[:,:,0]
image = np.expand_dims(np.array(image), 0)
image = np.expand_dims(np.array(image), 0)
image image.shape
(1, 1, 256, 256)
= RandomBiasField().get_params(3,[-.3,-.3]) coefficients
= BiasField.generate_bias_field(image, order=3, coefficients=coefficients) bf
0]) plt.imshow(bf[
<matplotlib.image.AxesImage>
0][0].shape
image[= image/255 image
= torch.from_numpy(image)
image = image[0][0] * bf[0]
result = torch.stack((result,result,result))
result = np.array((torch.clamp(result,0,1)))
result = result*255
result = result.astype(np.uint8)
result = np.moveaxis(result, 0, 2)
result = Image.fromarray(result)
result plt.imshow(result)
<matplotlib.image.AxesImage>
get_biasfield_series
get_biasfield_series (image, model, start=0, end=-0.6, step=-0.2, order=3, **kwargs)
biasfieldTransform
biasfieldTransform (image, coef, order=3)
=-.1),nrow=1) plot_series(get_biasfield_series(img(),trainedModel,step
-.5) biasfieldTransform(img(),
eval_biasfield_series
eval_biasfield_series (image, mask, model, step=-0.05, start=0, end=-0.55, order=3, **kwargs)
= eval_biasfield_series(img(), trueMask(), trainedModel)
results 'coefficient'], results['c1'], label='LV')
plt.plot(results['coefficient'], results['c2'], label='MY')
plt.plot(results[
plt.legend()'coefficient')
plt.xlabel(= plt.ylabel('dice') _