Select labels using machine learning

Select labels using machine learning#

apoc allows selecting labeled objects according to properties such as size, shape and intensity in a corresponding image. In this example, we will select elongated objects from an instance segmentation of blobs.

import apoc

from skimage.io import imread
import pyclesperanto_prototype as cle
import numpy as np

cle.select_device('RTX')
<NVIDIA GeForce RTX 3050 Ti Laptop GPU on Platform: NVIDIA CUDA (1 refs)>
image = imread('../../data/blobs.tif')
labels = cle.label(cle.threshold_otsu(image))
annotation = imread('../../data/label_annotation.tif')
cle.imshow(image)
cle.imshow(labels, labels=True)
cle.imshow(annotation, labels=True)
../_images/074888b68c318aac4eaadaf285f2b3805ee8f6a5d77939ccafc418406a2d6628.png ../_images/5ef923f8cd4e5b2b95049217b531aafef6a031da122db41be7fa6914462733b6.png ../_images/d62174c6ca905e29f8799378346f96368531856b5ba0d7af2c0fa12f88908d81.png

Training#

For training the classifier, you need to specify features. In the following we use mean and standard deviation intensity within the labeled objects and the object size and shape.

features = 'area,mean_max_distance_to_centroid_ratio,standard_deviation_intensity'

cl_filename = "object_selector.cl"

# Create an object classifier
apoc.erase_classifier(cl_filename) # delete it if it was existing before
classifier = apoc.ObjectSelector(cl_filename, positive_class_identifier=1)

# train it
classifier.train(features, labels, annotation, image)

Prediction#

After the classifier was trained, we can use it to select objects.

result = classifier.predict(labels, image)

print(result.max())

cle.imshow(result, labels=True)
23.0
../_images/2ac9a9e2d05250288a7a4a8342d64f468bb15963699af586aa5e4b33df9f1666.png

One can also load the classifier from disc and apply it to another dataset. We demonstrate that by applying the classifier to a rotated version of the image and label image from above.

image1 = image.T
labels1 = cle.label(cle.threshold_otsu(image1))

classifier = apoc.ObjectSelector(cl_filename)

result = classifier.predict(labels1, image1)

print(result.max())

cle.imshow(result, labels=True)
23.0
../_images/83d82c2c9c55fe10dcee900eeed4bbd856859f377092480cc1879033fe5aed93.png

After training, we can ask the classifier how important features were while doing the prediction.

classifier.feature_importances()
{'area': 0.29573084473661354,
 'mean_max_distance_to_centroid_ratio': 0.4264564597125618,
 'standard_deviation_intensity': 0.27781269555082466}

Exercise#

Use the code and example images above to train a classifier that selects all small objects in the label image.