> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Document Classification Training

Document classification assigns a document to a predefined category before extracting the data. This helps match Document Definitions faster and improve data extraction quality. For best classification results, use classification training.

## Prepare for training

The batch for classification training should contain enough images of each document class for the classifier to learn the difference between the classes. In addition, you need to set up the mapping between classes and sections of the Document Definitions. This will allow the trained classifier to apply the correct Document Definition to each image.

1. Create a new classification training batch using the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingbatches-addnew.md) method of the [**ClassificationTrainingBatches**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingbatches.md) object.
2. Add the images for classification training. Convert the training batch to a working batch using the [**AsBatch**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingbatch.md#asbatch) property of the **ClassificationTrainingBatch** object and then [add images](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-addingimages.md).\
   Important! Make sure that all the setting up described in the following steps is done within the same **Open**/**Close** batch session. Once you close the working batch, the **PageClass** object is no longer available.
3. Add new page classes to the [**PageClasses**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/pageclasses.md) collection of the batch.
4. Assign page classes and training states to the images. For each document in the [**ClassificationTrainingDocuments**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingdocuments.md) collection of the classification training batch:
   1. Iterate through its [**ClassificationTrainingPages**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingpages.md) collection and assign the page classes you've just set up to the [**ReferenceClass**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingpage.md#referenceclass) property of each [**ClassificationTrainingPage**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingpage.md) object.
   2. Set the [**TrainingState**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingdocument.md#trainingstate) property of each [**ClassificationTrainingDocument**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingdocument.md) to DTS\_ForTraining or DTS\_ForTesting. We recommend a 70:30 ratio of training to testing images for each class.
5. Link each page class to a Document Definition section. In non-invoice projects, you can also link a page class to a section variant.
   1. Get the section definition from the Document Definition using its [**Sections**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinition.md#sections) property.
   2. Find the section variant you need in the "Variants" data set using its [**LookUp**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasettablerecords-lookup.md) method.
   3. Create links using the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/linkstosections-addnew.md) method of the [**LinksToSections**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/linkstosections.md) collection of each [**PageClass**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/pageclass.md) object.

> [accordion] C# code
>
> ```csharp theme={null}
> IClassificationTrainingBatches classificationTrainingBatches = project.ClassificationTrainingBatches;
>
> // Create and fill a training batch
> IClassificationTrainingBatch classificationTrainingBatch = classificationTrainingBatches.AddNew();
> IBatch batch = classificationTrainingBatch.AsBatch;
> batch.Open();
> batch.AddImage("ImagePath") ;
>
> // Create page classes
> IPageClasses pageClasses = classificationTrainingBatch.Classes;
> IPageClass pageClass = pageClasses.AddNew("ClassName");
>
> IClassificationTrainingDocuments classificationTrainingDocuments = classificationTrainingBatch.ClassificationTrainingDocuments;
>
> // Assign page classes and training states to the images
> foreach (IClassificationTrainingDocument classificationTrainingDocument in classificationTrainingDocuments)
> {
>     IClassificationTrainingPages trainingPages = classificationTrainingDocument.ClassificationTrainingPages;
>     IClassificationTrainingPage trainingPage = trainingPages[0];
>     trainingPage.ReferenceClass = pageClass;
>     classificationTrainingDocument.TrainingState = DocumentTrainingStateEnum.DTS_ForTraining;
> }
>
> // Link the classes to section definitions
> ILinksToSections links = pageClass.LinksToSections;
> links.AddNew(sectionDefinition, PageRoleEnum.PR_AnyPage, variantRecord);
>
> // Close the batch
> batch.Close();
> ```

## Train the classifier

During training, the classifier attempts to learn the differences between the images that belong to different classes. Since training may decrease classification quality in some cases, it's up to you to decide whether the classifier should be updated when the training is completed. You can add more documents to the training batch and run training again, examine the validation results and update the classifier when needed.

1. Get the classification trainer object using the [**ClassificationTrainer**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingbatch.md#classificationtrainer) property of the **ClassificationTrainingBatch** object.
2. Call the [**CheckOut**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainer-checkout.md) method of the [**ClassificationTrainer**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainer.md) object to switch the classifier to training mode. Set up classification trainer parameters using its [**ClassificationTrainingParams**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainingparams.md) property.
3. Perform training by calling the [**Train**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainer-train.md) method.
4. Examine the training results. From the [**TrainingResults**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/trainingresults.md) object returned from the previous call, you can access the [**CandidateResults**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/trainingresults.md#candidateresults) and the [**CurrentResults**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/trainingresults.md#currentresults) properties. The **CandidateResults** refers to the [**ValidationResults**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/validationresults.md) object with statistics for the newly trained classifier. The **CurrentResults** object contains the statistics for the previously used classifier.
5. Compare the statistics and decide whether you want to use the newly trained classifier or not. You can also consult the [**ShouldApply**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/trainingresults.md#shouldapply) property of the **TrainingResults** object. This boolean property is an integral estimate of whether the new classifier is "better" than the old one. Depending on your decision, call one of the two methods:
   1. The [**CheckIn**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainer-checkin.md) method of the **ClassificationTrainer** object — if you choose to apply the new classifier, this method will publish the classifier and switch off the training mode.
   2. The [**UndoCheckout**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classificationtrainer-undocheckout.md) method of the **ClassificationTrainer** object — if you prefer the previously used classifier, this method will discard the changes and switch off the training mode.

> [accordion] C# code
>
> ```csharp theme={null}
> IClassificationTrainer classificationTrainer = classificationTrainingBatch.ClassificationTrainer;
>
> // Start the training session and train a classifier
> classificationTrainer.CheckOut();
> ITrainingResults trainingResults = classificationTrainer.Train();
>
> // Get the classification statistics for the previously used and newly trained classifiers
> IValidationResults currentValidationResults = trainingResults.CurrentResults;
> IValidationResults candidateValidationResults = trainingResults.CandidateResults;
> // Compare the statistics and decide whether you want to use the newly trained classifier
> ...
> classificationTrainer.CheckIn();
> ```

## Use the classifier

Document recognition uses the classifier automatically to match the Document Definitions faster. To enable this behavior, you should set the working batch to use the classifier from the right classification training batch.

1. Create a new batch type and link it to the classifier:
   1. Create a [**BatchTypeParams**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batchtypeparams.md) object using the [**CreateBatchTypeParams**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/project-createbatchtypeparams.md) method of the **Project** object.
   2. Set the [**UseClassifier**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batchtypeparams.md#useclassifier) property of the **BatchTypeParams** object to TRUE, so that all batches of this type will use classifier.
   3. Link the **BatchTypeParams** object to the classification training batch by setting its [**ClassificationBatch**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batchtypeparams.md#classificationbatch) property.\
      Note: You can also specify text recognition parameters using the [**FullTextRecognitionParams**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batchtypeparams.md#fulltextrecognitionparams) property.
   4. Create a batch type: call the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batchtypes-addnew.md) method of the [**BatchTypes**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batchtypes.md) object and pass the **BatchTypeParams** you have just configured.
2. Create a working batch using the [**AddNewEx**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batches-addnewex.md) method of the [**Batches**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batches.md) collection and specify the batch type you have just created. [Add the images](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-addingimages.md) to the working batch.
3. Process the images in the batch using the [**Recognize**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batch-recognize.md) method. During recognition, the Document Definitions will be matched with the help of the classifier.\
   Note: To check which Document Definition has been matched to a document, you can use the [**DocumentDefinition**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/document.md#documentdefinition) property of the [**Document**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/document.md) object.

> [accordion] C# code
>
> ```csharp theme={null}
> // Create a new batch type, link it to the classifier
> IBatchTypeParams batchTypeParams = project.CreateBatchTypeParams();
> batchTypeParams.UseClassifier = true;
> batchTypeParams.ClassificationBatch = classificationTrainingBatch;
> IBatchType batchType = project.BatchTypes.AddNew("BatchTypeName", batchTypeParams);
>
> // Create a new working batch and specify the batch type
> IBatch workingBatch = project.Batches.AddNewEx(batchType);
>
> // Add images to the batch and recognize them with classification
> workingBatch.Open();
> workingBatch.AddImage("ImagePath");
> workingBatch.Recognize(null, RecognitionModeEnum.RM_ReRecognizeAll, null);
> workingBatch.Export(null, null);
> workingBatch.Close();
> ```

## Samples

See the [Classification](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/samples.md#classification) code sample for an implementation of this scenario.

## See also

[Classification Training Objects](https://docs.abbyy.com/flexi-capture/sdk/api-reference/classification-overview.md)
