在支持OpenCL的GPU上进行像素分类#

除了对象或”实例”分割外,我们还可以通过对像素进行分类来实现语义分割。

让我们先加载一个示例图像和一些地面真实标签:

from skimage.io import imread
import matplotlib.pyplot as plt
import numpy as np
import apoc
import pyclesperanto_prototype as cle

image = imread('../../data/blobs.tif')
cle.imshow(image)
../_images/697332ed931701bd777704b279b47e121b548544b81f7a635c3783b21c04fb53.png
manual_annotations = imread('../../data/blobs_annotations.tif')

from skimage.io import imshow
cle.imshow(manual_annotations, labels=True)
../_images/6c1441d186140c2fdbd15cd26860006b84a192e2d8c9478cbedbe26fa88c3a97.png

训练#

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

# define features: original image, a blurred version and an edge image
features = "original gaussian_blur=2 sobel_of_gaussian_blur=2"

# this is where the model will be saved
cl_filename = 'my_model.cl'

apoc.erase_classifier(cl_filename)
clf = apoc.PixelClassifier(opencl_filename=cl_filename)
clf.train(features, manual_annotations, image)

预测#

然后可以使用该分类器对给定图像中的所有像素进行分类。起点仍然是特征堆栈。因此,用户必须确保在训练和预测时使用相同的特征。

result = clf.predict(image=image)
cle.imshow(result, labels=True)
../_images/dffe6e2a853106d4bb93d795fa0723339d2c5f4723dd060266e876b5cc0ca337.png