APOC is based on pyclesperanto and sklearn. For object segmentation, it uses a pixel classifier and connected components labeling.
Let's start with loading an example image and some ground truth:
from skimage.io import imread, imshow, imsave
import pyclesperanto_prototype as cle
import numpy as np
import apoc
image = imread('blobs.tif')
imshow(image)
<matplotlib.image.AxesImage at 0x25a1791e9d0>
if False: # you can use this to make manual annotations
import napari
# start napari
viewer = napari.Viewer()
napari.run()
# add image
viewer.add_image(image)
# add an empty labels layer and keep it in a variable
labels = np.zeros(image.shape).astype(int)
viewer.add_labels(labels)
else:
labels = imread('annotations.tif')
#imsave('annotations.tif', labels)
manual_annotations = labels
from skimage.io import imshow
imshow(manual_annotations, vmin=0, vmax=3)
C:\Users\rober\miniconda3\envs\bio_39\lib\site-packages\skimage\io\_plugins\matplotlib_plugin.py:150: UserWarning: Low image data range; displaying image with stretched contrast. lo, hi, cmap = _get_display_range(image)
<matplotlib.image.AxesImage at 0x25a189fca00>
We now train a ObjectSegmenter, which is under the hood a scikit-learn RandomForestClassifier. After training, the classifier will be converted to clij-compatible OpenCL code and save to disk under a given filename.
# define features
features = apoc.PredefinedFeatureSet.medium_quick.value
# this is where the model will be saved
cl_filename = 'my_model.cl'
apoc.erase_classifier(cl_filename)
clf = apoc.ObjectSegmenter(opencl_filename=cl_filename, positive_class_identifier=2)
clf.train(features, manual_annotations, image)
The classifier can then be used to classify all pixels in the given image. Starting point is again, the feature stack. Thus, the user must make sure that the same features are used for training and for prediction. Prediction can be done on the CPU using the original scikit-learn code and on the GPU using the generated OpenCL-code. OCLRFC works well if both result images look identical.
segmentation_result = clf.predict(features=features, image=image)
cle.imshow(segmentation_result, labels=True)
clf = apoc.ObjectSegmenter(opencl_filename=cl_filename)
segmentation_result = clf.predict(image=image)
cle.imshow(segmentation_result, labels=True)
clf.feature_importances()
{'gaussian_blur=5': 0.7543628505155221, 'sobel_of_gaussian_blur=5': 0.245637149484478}
annotation = cle.push(imread('label_annotation.tif'))
features = 'area,mean_max_distance_to_centroid_ratio,standard_deviation_intensity'
# Create an object classifier
cl_filename_object_classifier = "object_classifier.cl"
apoc.erase_classifier(cl_filename_object_classifier)
classifier = apoc.ObjectClassifier(cl_filename_object_classifier)
# train it
classifier.train(features, segmentation_result, annotation, image)
# determine object classification
classification_result = classifier.predict(segmentation_result, image)
imshow(classification_result)
C:\Users\rober\miniconda3\envs\bio_39\lib\site-packages\skimage\io\_plugins\matplotlib_plugin.py:150: UserWarning: Low image data range; displaying image with stretched contrast. lo, hi, cmap = _get_display_range(image)
<matplotlib.image.AxesImage at 0x25a235a2910>