> ## 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.

# Field Extraction Training

In semi-structured documents, a field may sometimes be extracted incorrectly. You can improve extraction quality by training the Document Definition. To do this, you will have to provide some documents typical for your workflow, marked up with correct regions for each field.

## Training to extract a field

ABBYY FlexiCapture SDK allows you to train field extraction for a variant of a Document Definition section. Each section in a non-invoice project has a "Variants" data set from which you can select a variant; for invoices, specify a vendor record from the "Vendors" data set instead.

1. Add a new training batch using the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatches-addnew.md) method of the [**FieldsExtractionTrainingBatches**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatches.md) object, passing the [**SectionDefinition**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/sectiondefinition.md) object for the section and the [**DataSetTableRecord**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasettablerecord.md) object for the section variant.
2. [Add the images for training to the batch](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-addingimages.md). Call the [**Recognize**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batch-recognize.md) method of the [**Batch**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batch.md) object to match the Document Definitions and recognize all the documents in the batch.
3. Correct the layout of the fields that will be trained. Assign the correct region to each field by calling the [**SetFieldRegion**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/page-setfieldregion.md) method of the [**Page**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/page.md) object.\
   Notes:
   * You can get correct regions from an external source, for example, by having a human operator verify and mark up the recognized documents.
   * Only the fields which can have region may be trained. See the description of the [**CanHaveRegion**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fielddefinition.md#canhaveregion) property of the [**FieldDefinition**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fielddefinition.md) object.
4. Train field extraction. Get the [**FieldsExtractionTrainer**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainer.md) object from the training batch and call its [**Train**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainer-train.md) method. The Document Definition will be updated automatically.

> [accordion] C# code
>
> ```csharp theme={null}
> IFieldsExtractionTrainingBatches batches = project.FieldsExtractionTrainingBatches;
>
> // Get a section for training
> IDocumentDefinitions documentDefinition = project.DocumentDefinitions[0];
> ISectionDefinition sectionDefinitionForTraining = documentDefinition.Sections[0];
>
> // Get the variant of the section
> IDataSet dataSet = documentDefinition.DataSets.Find(sectionDefinitionForTraining.Name, "Variants");
> IDataSetTableRecords foundRecords = dataSet.TableRecords.LookUp("VariantName");
> IDataSetTableRecord record = foundRecords[0];
>
> // Create and fill a training batch
> IFieldsExtractionTrainingBatch trainingBatch = batches.AddNew(sectionDefinitionForTraining, record);
> IBatch batch = trainingBatch.Batch;
> batch.Open();
> batch.AddImage("ImagePath");
>
> batch.Recognize(null, RecognitionModeEnum.RM_ReApplyDocumentDefinitions, null);
>
> // Provide the correct region
> IRegionsCollection regions = engine.CreateRegionsCollection();
> IRegion region = engine.CreateRegion();
> // The correct region is obtained from an external source
> region.AddRect(100, 150, 220, 190);
> regions.Add(region);
>
> IFields sections = document.Sections;
> IField field = sections.Children[0].Children[0]; // Provide a field to be trained
> IPage page = document.Pages[0]; // Provide the page with correct region
> // Bind the correct region to the field
> page.SetFieldRegion(field, regions, true);
>
> // Close the batch and train fields extraction
> batch.Close();
>
> IFieldsExtractionTrainer trainer = trainingBatch.FieldsExtractionTrainer;
> trainer.Train();
> ```

## Child fields training

You may train the child fields along with the parent field. The child fields should also be able to have regions. To get the child fields, use the [**Children**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/field.md#children) property of the [**Field**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/field.md) object. Correct the region for each of the child fields by calling the **SetFieldRegion** method of the **Page** object and then train fields extraction as described above.

## Repeatable fields and line items training

You can also train a field with several instances. Correct the region for each of the field instances and then train fields extraction as described above.

Line items of an invoice or a purchase order are essentially repeatable groups of fields, so everything in this section applies to them as well.

1. Get the collection of instances of the field using the [**Instances**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/field.md#instances) property of the [**Field**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/field.md) object. If some of the instances were not found on the page, call the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldinstances-addnew.md) method of the [**FieldInstances**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldinstances.md) collection object and add the missing instances.\
   Note: All instances have the same field hierarchy, that is, have the same set of child fields, etc.
2. Set the correct region for each instance using the [**SetFieldRegion**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/page-setfieldregion.md) method.\
   For line items, consider using the [**ContinueLineItems**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/document-continuelineitems.md) method of the [**Document**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/document.md) object after correcting several line items to detect the rest automatically.
3. Train fields extraction: call the **Train** method of the **FieldsExtractionTrainer** object.\
   Note: If you have a repeatable group of fields (field type [FT\_Group](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldtypeenum.md#ft-group)), you need to correct the regions of all the child fields for each of the group field instances.

> [accordion] C# code
>
> ```csharp theme={null}
> IBatch batch = trainingBatch.Batch;
> batch.Open();
> batch.AddImage("ImagePath");
> batch.Recognize(null, RecognitionModeEnum.RM_ReApplyDocumentDefinitions, null);
>
> IFieldInstances fieldInstances = repeatableField.Instances;
> // Find an instance or add a new one
> IField instance = fieldInstances.AddNew();
>
> // For a repeatable field with children set layout for each child
> foreach (IField instanceChild in instance.Children)
> {
>     IRegionsCollection regions = engine.CreateRegionsCollection();
>     regions.Add(region);
>     page.SetFieldRegion(instanceChild, regions, true);
> }
>
> // Set the region for the instance
> IRegionsCollection regions = engine.CreateRegionsCollection();
> IRegion region = engine.CreateRegion();
> region.AddRect(100, 150, 220, 190);
> regions.Add(region);
> page.SetFieldRegion(instance, regions, true);
>
> batch.Close();
> IFieldsExtractionTrainer trainer = trainingBatch.FieldsExtractionTrainer;
> trainer.Train();
> ```

## Samples

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

## See also

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

[Retrieving the Matched Section Variant](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/retrievingvariant.md)
