Creating a Dataset and Training a Supervised Segmenter

This code snippet shows how to create a dataset, train a supervised segmenter and get the best performance metrics obtained during the training.

C++

#include <eVision/EasyDeepLearning/EasySegment/ESupervisedSegmenter.h>
using namespace eVision;
using namespace eVision::EasyDeepLearning;

// Creating dataset and segmenter objects
EDataset dataset;
EDataset trainingDataset;
EDataset validationDataset;
ESupervisedSegmenter segmenter;

// Example with adding a single image with a mask

// Add a segmentation label
int label_idx = dataset.AddSegmentationLabel("scratch");

// In this case, the mask must contain pixel who are either 0 or equal to `label_idx`
// If your image is a binary image, you can set all non-null pixel to `label_idx`
EImageBW16 mask;
mask.Load("mask.png");

// Image to segment
EImageBW8 image;
image.Load("image.png");

// Adding image and linking it to the loaded mask
int image_idx = dataset.AddImage(image);
dataset.SetSegmentationMap(image_idx, mask);

// Create data augmentation
EDataAugmentation augmentation;

// Rotation of up to 90
augmentation.SetMaxRotationAngle(90);

// Enabling horizontal flips
augmentation.SetEnableHorizontalFlip(true);

// Splitting the dataset with 80% of images for the training dataset
// and 20% for the validation dataset
dataset.SplitDataset(trainingDataset, validationDataset, 0.8f);

// Training the segmenter for 50 epochs
segmenter.Train(trainingDataset, validationDataset, augmentation, 50);
segmenter.WaitForTrainingCompletion();

// Get the best metrics obtained on the validation dataset
ESupervisedSegmenterMetrics bestMetrics = segmenter.GetValidationMetrics(segmenter.GetBestIteration());

C#

using eVision;
using eVision.EasyDeepLearning;
// Creating dataset and segmenter objects
EDataset dataset = new EDataset();
EDataset trainingDataset = new EDataset();
EDataset validationDataset = new EDataset();
ESupervisedSegmenter segmenter = new ESupervisedSegmenter();

// Example with adding a single image with a mask

// Add a segmentation label
int label_idx = dataset.AddSegmentationLabel("scratch");

// In this case, the mask must contain pixel who are either 0 or equal to `label_idx`
// If your image is a binary image, you can set all non-null pixel to `label_idx`
EImageBW16 mask = new EImageBW16();
mask.Load("mask.png");

// Image to segment
EImageBW8 image = new EImageBW8();
image.Load("image.png");

// Adding image and linking it to the loaded mask
int image_idx = dataset.AddImage(image);
dataset.SetSegmentationMap(image_idx, mask);

// Create data augmentation
EDataAugmentation augmentation = new EDataAugmentation();

// Rotation of up to 90°
augmentation.MaxRotationAngle = 90.0F;

// Enabling horizontal flips
augmentation.EnableHorizontalFlip = true;

// Splitting the dataset with 80% of images for the training dataset
// and 20% for the validation dataset
dataset.SplitDataset(trainingDataset, validationDataset, 0.8F);

// Training the segmenter for 50 epochs
segmenter.Train(trainingDataset, validationDataset, augmentation, 50);
segmenter.WaitForTrainingCompletion();

// Get the best metrics obtained on the validation dataset
ESupervisedSegmenterMetrics bestMetrics = segmenter.GetValidationMetrics(segmenter.BestIteration);

Python

import eVision
from eVision import EasyDeepLearning as edl

# Creating dataset and segmenter objects
dataset = edl.EDataset()
training_dataset = edl.EDataset()
validation_dataset = edl.EDataset()
segmenter = edl.ESupervisedSegmenter()

# Example with adding a single image with a mask

# Add a segmentation label
label_idx = dataset.AddSegmentationLabel("scratch")

# In this case, the mask must contain pixel who are either 0 or equal to `label_idx`
# If your image is a binary image, you can set all non-null pixel to `label_idx`
mask = eVision.EImageBW16()
mask.Load("mask.png")

# Image to segment
image = eVision.EImageBW8()
image.Load("image.png")

# Adding image and linking it to the loaded mask
image_idx = dataset.AddImage(image)
dataset.SetSegmentationMap(image_idx, mask)

# Create data augmentation
augmentation = edl.EDataAugmentation()

# Rotation of up to 90°
augmentation.MaxRotationAngle = 90.0

# Enabling horizontal flips
augmentation.EnableHorizontalFlip = True

# Splitting the dataset with 80% of images for the training dataset
# and 20% for the validation dataset
dataset.SplitDataset(training_dataset, validation_dataset, 0.8)

# Training the segmenter for 50 epochs
segmenter.Train(training_dataset, validation_dataset, augmentation, 50)
segmenter.WaitForTrainingCompletion()

# Get the best metrics obtained on the validation dataset
best_metrics = segmenter.GetValidationMetrics(segmenter.BestIteration)