# EasyClassifyTrain

| Support            | ![](assets/images/sample_programs/applies_to/cs.svg) ![](assets/images/sample_programs/applies_to/cpp.svg)   |
|--------------------|--------------------------------------------------------------------------------------------------------------|
| Required licenses  | **EasyClassify**                                                                                             |
| Recommended images | n/a                                                                                                          |
| Location           | `Deep Learning Inspection/EasyClassifyTrain`                                                                 |

## Purpose

This sample program demonstrates how to:

- Train an tool in console mode.

## Code highlights

By default, the sample program trains a tool for the dataset `MiniWaffles` in the Deep Learning Additional Resources.

#### NOTE
In the code, you need to replace `DEEP_LEARNING_ADDITIONAL_RESOURCES` by the path of the Deep Learning Additional Resources.

1. Select the first available GPU for the training.
   ```c++
   // initialize a classifier that will be trained
   EClassifier newClassifier;
   // make sure that we have a GPU to use (note: GPU's can only be detected in x64 configuration)
   std::vector<EDeepLearningDevice> devices = newClassifier.GetAvailableDevices();
   bool foundGPU = false;
   for (size_t i = 0; i < devices.size(); i++)
   {
     if (devices[i].GetDeviceType() == EDeepLearningDeviceType_GPU)
     {
       newClassifier.SetActiveDevice(devices[i]);
       foundGPU = true;
       break;
     }
   }
   ```
2. Create a dataset and add all the good and defective images to it.
   ```c++
   // load the dataset
   EClassificationDataset dataset;
   dataset.AddImages(std::string(IMAGE_SOURCE_PATH) + "mw_good*.jpg", "Good");
   dataset.AddImages(std::string(IMAGE_SOURCE_PATH) + "mw_bad*.jpg", "Bad");
   ```
3. Split the dataset in a training part and a validation part.
   ```c++
   // Put 80% of images into the training dataset and 20% in the validation dataset
   EClassificationDataset trainingDataset;
   EClassificationDataset validationDataset;
   dataset.SplitDataset(trainingDataset, validationDataset, 0.8f);
   ```
4. Train the classifier and wait for the training to end.
   ```c++
   newClassifier.Train(trainingDataset, validationDataset, 50);
   newClassifier.WaitForTrainingCompletion();
   ```
5. Check the validation accuracy of the classifier.
   ```c++
   float newClassifierAccuracy = newClassifier.GetValidationMetrics(newClassifier.GetBestIteration()).GetAccuracy();

   std::cout << "New classifier accuracy:" << newClassifierAccuracy << std::endl
   ```
