Training pixel classifiers from folders of images#

When training pixel classifiers it often makes sense to train using multiple images. For example, when images look differently, especially between conditions, it is necessary to train classifiers with mutiple images from all conditions.

In this notebook we demonstrate how to train APOC classifiers using two folders containing pairs of training data.

import apoc
import os
from import imread
import pyclesperanto_prototype as cle
import matplotlib.pyplot as plt

Data preparation#

We start by selecting two input folders. One with images, and one with sparsely annotated label images. The names of the files in that folder must be pairwise identical. We will also have a quick look into these folders.

For demonstration purposes, we reuse data of the BBBC007 dataset version 1 (Jones et al., Proc. ICCV Workshop on Computer Vision for Biomedical Image Applications, 2005), available from the Broad Bioimage Benchmark Collection Ljosa et al., Nature Methods, 2012.

image_folder = "../../data/BBBC007/images/"
masks_folder = "../../data/BBBC007/masks/"
file_list = os.listdir(image_folder)

# show all images
fig, axs = plt.subplots(1, 4, figsize=(15,15))
for i, filename in enumerate(file_list):
    image = imread(image_folder + filename)
    cle.imshow(image, plot=axs[i])
# show corresponding label images
fig, axs = plt.subplots(1, 4, figsize=(15,15))
for i, filename in enumerate(file_list):
    masks = imread(masks_folder + filename)
    cle.imshow(masks, plot=axs[i], labels=True)
../_images/cd120adece0f14c46ff32cf120b41938b7d68e763014467cc9d9e434f7e903ab.png ../_images/e4aa9a74644f61961925d1a0bd5290e63b260953eb3e289bd4ec3ce9a9fb2f8d.png


If the folders are setup properly, we can pass the folders to the training.

# setup classifer and where it should be saved
segmenter = apoc.ObjectSegmenter(opencl_filename="")

# setup feature set used for training
features = apoc.PredefinedFeatureSet.object_size_1_to_5_px.value

# train classifier on folders
    image = image_folder, 
    ground_truth = masks_folder)


After the training, we can apply the classifier to all images in the image folder. The following line reloads the classifier from disk. In that way we can ensure that it was stored correctly.

segmenter = apoc.ObjectSegmenter(opencl_filename="")
# show all images
for i, filename in enumerate(file_list):
    fig, axs = plt.subplots(1, 2, figsize=(15,15))
    image = imread(image_folder + filename)
    cle.imshow(image, plot=axs[0])
    labels = segmenter.predict(image)
    cle.imshow(labels, plot=axs[1], labels=True)
../_images/87104cad79a596217019fd247bffb31553d50efb1674aed8d1bff864f6c52974.png ../_images/20de8cbf90df9a0c549e32b707649368178f0b9ed64eb7b4311350131333d58f.png ../_images/36804e8e6857907f95887f070873b3bf099ff433abdd0f6249f549cc14a58ae0.png ../_images/4f59401cd2bbe2dbe36d79bbbd1f469f6e507dc8e24efc23b1a60672b9d324da.png