from misas.fastai_model import Fastai2_model

Evaluation functions

Sensitivity Analysis

Example data (kaggle)

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
def img():
    """
    Opens the sample image as a PIL image
    """
    return Image.open("example/kaggle/images/1-frame014-slice005.png").convert("RGB")
#img = lambda: Image.open("example/kaggle/images/1-frame014-slice005.png")

def trueMask():
    """
    Opens the true mask as a PIL image
    """
    return Image.open("example/kaggle/masks/1-frame014-slice005.png").convert("I")
#trueMask = lambda: Image.open("example/kaggle/masks/1-frame014-slice005.png")
trainedModel = Fastai2_model("chfc-cmi/cmr-seg-tl", "cmr_seg_base")

Default color map

Define a default color map for the sample image derived from viridis and a default color map for the true map derived from plasma but setting the color for class "0" to completely transparent. This makes sense if class "0" is the background.

Generic functions

get_generic_series[source]

get_generic_series(image, model, transform, truth=None, tfm_y=False, start=0, end=180, step=30, log_steps=False)

Generic function for transforming images. Input: image (PIP image, usually your sample image opened by img()), model (the function for your model that manages the prediction for your mask), transform (your specific transformation function), truth = None (replaces with a true mask if available), tfm_y = False (set to True if your true mask has to be transformed as well to fit the transformed sample image e.g in case of a rotation of the sample image), start, end, step as values the transform function, log_steps = False (if enabled logarithmic steps as parameters for the transform function are possible) Output: a list containing lists of [param, img, pred, trueMask] after img and optionally trueMask have been transformed and pred has been determined by using a modell on the transformed img for each different param

plot_series[source]

plot_series(series, nrow=1, figsize=(16, 6), param_name='param', overlay_truth=False, vmax=None, vmin=0, cmap=<matplotlib.colors.ListedColormap object at 0x7f43ba4832e0>, cmap_true_mask=<matplotlib.colors.ListedColormap object at 0x7f43b9eff040>, **kwargs)

plots the transformed images with the prediction and optionally the true mask overlayed intput: series = a list containing lists of [param, img, pred, trueMask] from the function get_generic_series nrow = number of rows drawn with the transformed images figsize = (16,6) param_name='param', overlay_truth = False (if True displays the true mask over the sample along with the prediction) vmax = None (controls how many colors the prediction is going to have, can be set manually by the user, otherwise is deterimed by the max amount of colors in the prediction cmap= default_cmap (sets the default color map) output: a plot generated by mathplotlib

plot_frame[source]

plot_frame(param, img, pred, param_name='param', vmax=None, vmin=0, cmap=<matplotlib.colors.ListedColormap object at 0x7f43ba4832e0>, **kwargs)

plots the transformed images and prediction overlayed for the gif_series function

gif_series[source]

gif_series(series, fname, duration=150, param_name='param', vmax=None, vmin=0, cmap=<matplotlib.colors.ListedColormap object at 0x7f43ba4832e0>)

creates a gif from the output of plot_frame

eval_generic_series[source]

eval_generic_series(image, mask, model, transform_function, start=0, end=360, step=5, param_name='param', mask_transform_function=None, components=['bg', 'c1', 'c2'], eval_function=dice_by_component)

Perform the transformation on the sample, creates a prediction and then uses the prediction and true mask to run an evaluation function to measure the overlap between predicted mask and true mask

plot_eval_series[source]

plot_eval_series(results, chart_type='line', value_vars=None, value_name='Dice Score')

Plots the resuls of the eval_generic_function

Rotation

rotationTransform[source]

rotationTransform(image, deg)

rotates an image by x degrees (deg)

get_rotation_series[source]

get_rotation_series(image, model, start=0, end=360, step=60, **kwargs)

runs the get_generic_series with rotationTransform as transform

series = get_rotation_series(img(), trainedModel, truth=trueMask())
plot_series(series, overlay_truth = True)

eval_rotation_series[source]

eval_rotation_series(image, mask, model, step=5, start=0, end=360, param_name='deg', **kwargs)

results = eval_rotation_series(img(), trueMask(), trainedModel, components=['bg','LV','MY'])
plot_eval_series(results)

You can easily generate gifs by plotting multiple frames

gif_series(
    get_rotation_series(img(),trainedModel,start=0,end=360,step=5),
    "example/kaggle/rotation.gif",
    duration=500,
    param_name="degrees"
)

segmentation sensitivity to rotation

Cropping

cropTransform[source]

cropTransform(image, pxls, finalSize=None)

get_crop_series[source]

get_crop_series(image, model, start=0, end=256, step=10, finalSize=None, **kwargs)

series = get_crop_series(img(), trainedModel, truth=trueMask(), step=10)
plot_series(series,nrow=3,figsize=(16,15), overlay_truth = True) #,overlay_truth=True)

eval_crop_series[source]

eval_crop_series(image, mask, model, step=10, start=0, end=256, finalSize=None, param_name='pixels', **kwargs)

results = eval_crop_series(img(), trueMask(), trainedModel, components=['bg','LV','MY'])
plot_eval_series(results)

Cropping and comparing to the full original mask might not be desired. In this case it is possible to crop the mask as well. All pixels in the cropped area are set to 0 (commonly the background class). As soon as a class is completely missing, the dice score might jump to 1 because not predicting the class is correct in that case.

gif_series(
    get_crop_series(img(),trainedModel,start=0,end=256,step=5),
    "example/kaggle/crop.gif",
    duration=500,
    param_name="pixels"
)

segmentation sensitivity to cropping

Brightness

brightnessTransform[source]

brightnessTransform(image, light)

get_brightness_series[source]

get_brightness_series(image, model, start=0.25, end=8, step=1.4142135623730951, log_steps=True, **kwargs)

series = get_brightness_series(img(), trainedModel, truth=trueMask(), start=1/8, end=16)
plot_series(series, nrow=3, figsize=(12,6), overlay_truth = True)

eval_bright_series[source]

eval_bright_series(image, mask, model, start=0.05, end=0.95, step=0.05, param_name='brightness', **kwargs)

results = eval_bright_series(img(), trueMask(), trainedModel, start=0, end=1.05, components=['bg','LV','MY'])
plot_eval_series(results)
gif_series(
    get_brightness_series(img(), trainedModel, step = np.sqrt(2)),
    "example/kaggle/brightness.gif",
    duration=500,
    param_name="brightness"
)

segmentation sensitivity to brightness

Contrast

contrastTransform[source]

contrastTransform(image, scale)

get_contrast_series[source]

get_contrast_series(image, model, start=0.25, end=8, step=1.4142135623730951, log_steps=True, **kwargs)

series = get_contrast_series(img(), trainedModel, truth=trueMask(), start=1/8, end=16)
plot_series(series, nrow=3, figsize=(12,8), overlay_truth = True)

eval_contrast_series[source]

eval_contrast_series(image, mask, model, start=0.25, end=8, step=1.4142135623730951, param_name='contrast', **kwargs)

results = eval_contrast_series(img(), trueMask(), trainedModel, start=0.25, end=8, step=np.sqrt(2), components=['bg','LV','MY'])
plot_eval_series(results)
gif_series(
    get_contrast_series(img(), trainedModel,step=np.sqrt(2)),
    "example/kaggle/contrast.gif",
    duration=500,
    param_name="contrast"
)

segmentation sensitivity to contrast

Zoom

zoomTransform[source]

zoomTransform(image, zoom, finalSize=None)

get_zoom_series[source]

get_zoom_series(image, model, start=0, end=1, step=0.1, finalSize=None, **kwargs)

series = get_zoom_series(img(), trainedModel, truth=trueMask())
plot_series(series, nrow=2, figsize=(16,8), overlay_truth = True)