在OpenCL兼容的GPU上进行对象分割#

APOC基于pyclesperantoscikit-learn。对于对象分割,它使用像素分类器和连通组件标记。

让我们从加载示例图像和一些真实标注开始:

from skimage.io import imread, imsave
import pyclesperanto_prototype as cle
import numpy as np
import apoc
image = imread('../../data/blobs.tif')
cle.imshow(image)
../_images/697332ed931701bd777704b279b47e121b548544b81f7a635c3783b21c04fb53.png
manual_annotations = imread('../../data/blobs_annotations.tif')

cle.imshow(manual_annotations, labels=True)
../_images/6c1441d186140c2fdbd15cd26860006b84a192e2d8c9478cbedbe26fa88c3a97.png

训练#

我们现在训练一个ObjectSegmenter,它在底层是一个scikit-learn RandomForestClassifier。训练后,分类器将被转换为clij兼容的OpenCL代码并保存到指定的文件名下的磁盘上。

# define features
features = apoc.PredefinedFeatureSet.medium_quick.value

# this is where the model will be saved
cl_filename = '../../data/blobs_object_segmenter.cl'

apoc.erase_classifier(cl_filename)
clf = apoc.ObjectSegmenter(opencl_filename=cl_filename, positive_class_identifier=2)
clf.train(features, manual_annotations, image)

预测 / 分割#

然后可以使用分类器对给定图像中的所有像素进行分类。起点仍然是特征堆栈。因此,用户必须确保在训练和预测时使用相同的特征。预测可以在CPU上使用原始的scikit-learn代码进行,也可以在GPU上使用生成的OpenCL代码进行。如果两个结果图像看起来相同,那么OCLRFC工作良好。

segmentation_result = clf.predict(features=features, image=image)
cle.imshow(segmentation_result, labels=True)
../_images/fad4c1c1b5b80dd7e8a0fe0d5c2e83387c00fdd89bb6e9c96ba39eeb2d809db6.png

从加载的分割器进行分割#

clf = apoc.ObjectSegmenter(opencl_filename=cl_filename)

segmentation_result = clf.predict(image=image)
cle.imshow(segmentation_result, labels=True)
../_images/fad4c1c1b5b80dd7e8a0fe0d5c2e83387c00fdd89bb6e9c96ba39eeb2d809db6.png