Skip to content

Learned Segmenter

This example trains a segmenter from a volume annotation and applies it to the whole dataset.

Volume annotation used for training

Volume annotation used for training

Result of the trained segmenter on the full dataset

Result of the trained segmenter on the full dataset

It builds upon the two previous examples using the Dataset API and dataset upload. Additionally, it downloads this manual volume annotation of a subset of the skin example dataset which is used for training.

This example additionally needs the scikit-learn package.

from functools import partial
from time import gmtime, strftime

import numpy as np
from skimage import feature
from skimage.future import TrainableSegmenter

import webknossos as wk

annotation = wk.open_annotation(
training_data_bbox = wk.BoundingBox.from_tuple6(
    annotation.skeleton.user_bounding_boxes[0]  # pylint: disable=unsubscriptable-object
time_str = strftime("%Y-%m-%d_%H-%M-%S", gmtime())
new_dataset_name = annotation.dataset_name + f"_segmented_{time_str}"
dataset = wk.download_dataset(
) = new_dataset_name

volume_annotation = annotation.save_volume_annotation(dataset)
volume_annotation.bounding_box = training_data_bbox

mag = wk.Mag(1)
mag_view = dataset.layers["color"].mags[mag]

features_func = partial(
    feature.multiscale_basic_features, multichannel=True, edges=False
segmenter = TrainableSegmenter(features_func=features_func)

print("Starting training…")
img_data_train =
    training_data_bbox.in_mag(mag).topleft, training_data_bbox.in_mag(mag).size
# move channels to last dimension, remove z dimension
X_train = np.moveaxis(np.squeeze(img_data_train), 0, -1)
Y_train = np.squeeze(volume_annotation.mags[mag].get_view().read()), Y_train)

print("Starting prediction…")
X_predict = np.moveaxis(np.squeeze(, 0, -1)
Y_predicted = segmenter.predict(X_predict)
segmentation = Y_predicted[:, :, None]  # adds z dimension
assert segmentation.max() < 256
segmentation = segmentation.astype("uint8")

segmentation_layer = dataset.add_layer(
segmentation_layer.bounding_box = dataset.layers["color"].bounding_box
segmentation_layer.add_mag(mag, compress=True).write(segmentation)

with wk.webknossos_context(url="http://localhost:9000", token="secretScmBoyToken"):
    url = dataset.upload()
print(f"Successfully uploaded {url}")

This example is inspired by the trainable segmentation example from scikit-image.

Back to top