Creating a Dataset and Training a Classifier

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

C++

#include <eVision/EasyDeepLearning/EasyClassify/EClassifier.h>
using namespace eVision::EasyDeepLearning;

// Creating dataset and classifier objects
EDataset dataset;
EDataset trainingDataset;
EDataset validationDataset;
EClassifier classifier;

// Adding images using a glob pattern
dataset.AddImages("*good*.png", "good");
dataset.AddImages("*defective*.png", "defective");

// 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 classifier for 50 epochs
classifier.Train(trainingDataset, validationDataset, augmentation, 50);
classifier.WaitForTrainingCompletion();

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

C#

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

// Adding images using a glob pattern
dataset.AddImages("*good*.png", "good");
dataset.AddImages("*defective*.png", "defective");

// 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 classifier for 50 epochs
classifier.Train(trainingDataset, validationDataset, augmentation, 50);
classifier.WaitForTrainingCompletion();

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

Python

from eVision import EasyDeepLearning as edl

# Creating dataset and classifier objects
dataset = edl.EDataset()
training_dataset = edl.EDataset()
validation_dataset = edl.EDataset()
classifier = edl.EClassifier()

# Adding images using a glob pattern
dataset.AddImages("*good*.png", "good")
dataset.AddImages("*defective*.png", "defective")

# 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 classifier for 50 epochs
classifier.Train(training_dataset, validation_dataset, augmentation, 50)
classifier.WaitForTrainingCompletion()

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