Create a Custom Document Classifier in the Google Cloud console

Custom classifier is designed to classify documents. It is built from the ground up using your own documents and custom classes.

Create a custom classifier in the Google Cloud console

You can create custom classifiers that are specifically suited to your documents and trained and evaluated with your data. This processor identifies classes of documents from a user-defined set of classes. You can then use this trained processor on additional documents. You typically would use a custom classifier on documents that are different types, then use the identification to pass the documents to an extraction processor to extract the entities.

A typical workflow to create and use a custom classifier is as follows:

  1. Create a custom classifier in Document AI.
  2. Create a dataset using an empty Cloud Storage bucket.
  3. Import documents.
  4. Annotate documents manually in Document AI Workbench or with labeling tasks.
  5. Assign documents to the training and test sets.
  6. Train the processor.
  7. Evaluate the processor.
  8. Deploy the processor.
  9. Test the processor.
  10. Use the processor on your documents.

You can make your own configuration choices that suit your workflow.


To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:

Guide me


Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Document AI, Cloud Storage APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Document AI, Cloud Storage APIs.

    Enable the APIs

Create a processor

  1. Go to Google Cloud console
  2. Go to the Workbench page, and select the following:

  3. Then select:

    Workbench

  4. For Custom Document Classifier, select Create processor.

    custom-classifier-1

  5. In the Create processor menu, enter a name for your processor, such as my-custom-document-classifier.

    custom-classifier-2

  6. Select the region closest to you.

  7. Select Create. The Processor Details tab appears.

Configure dataset

In order to train this new processor, you must create a dataset with training and testing data to help the processor identify the documents that you want to split and classify.

This dataset requires a new location. This can be an empty Cloud Storage bucket or folder, or you can allow an internally managed location.

After Processor Details tab appears, then you can:

  • Select Google-managed storage in case you want to use Cloud Storage.
  • Select I'll specify my own storage location if you want to use your own storage in order to use Customer-Managed Encryption Keys (CMEK), and follow the later procedure.

custom-classifier-3

Create a Cloud Storage bucket for the dataset

  1. Choose your processor's Train tab.

  2. Select Set Dataset Location. You are prompted to select or create an empty Cloud Storage bucket or folder.

    custom-classifier-4

  3. Select Browse to open Select folder.

  4. Select the Create a new bucket icon and follow the prompts to create a new bucket. For more information on creating a Cloud Storage bucket, refer to Cloud Storage buckets.

    Note: A bucket is the top-level storage entity, in which you can nest folders. Instead of creating and selecting a bucket, you can also create and select an empty folder inside an existing bucket. For more information, see Cloud Storage simulated folders.

    After you create the bucket, the Select folder page appears for that bucket.

  5. On the Select folder page for your bucket, select the Select button at the bottom of the dialog.

    custom-classifier-5

  6. Make sure the destination path is populated with the bucket name you selected. Select Create Dataset. The dataset might take up to several minutes to create.

Import documents into a dataset

Next, you import your documents into your dataset.

  1. On the Train tab, select Import documents.

    custom-classifier-6

  2. For this example, enter this bucket name in Source path. This links directly to one document.

    cloud-samples-data/documentai/Custom/Patents/PDF/computer_vision_20.pdf
    
  3. For Data split, select Unassigned. The document in this folder is not assigned to either the testing or training set. Leave Import with auto-labeling unchecked.

  4. Select Import. Document AI reads the documents from the bucket into the dataset. It does not modify the import bucket or read from the bucket after the import is complete.

When you import documents, you can optionally assign the documents to the Training or Test set when imported, or wait to assign them later.

If you want to delete a document or documents that you have imported, select them on the Train tab, and select Delete.

For more information about preparing your data for import, refer to the Data preparation guide.

Define processor schema

You can create the processor schema either before or after you import documents into your dataset. The schema provides labels that you use to annotate documents.

  1. On the Train tab, select Edit Schema in the lower left. The Manage labels page opens.

  2. Select Create label.

  3. Enter the name for the label. Select the Data type. Select Create. Refer to Define processor schema for detailed instructions on creating and editing a schema.

  1. Create each of the following labels for the processor schema.

    Name Data Type
    computer_vision Document type
    crypto Document type
    med_tech Document type
    other Document type
  2. Select Save when the labels are complete.

    custom-classifier-7

Label a document

The process of selecting text in a document and applying labels is known as annotation.

  1. Return to the Train tab, and select a document to open the Label management console.

  2. In the Document type drop-down, select the appropriate label for the document.

  3. If you're using the sample document provided, select computer_vision.

    The labeled document should look like this when complete: custom-classifier-8

  4. Select Mark as Labeled when you have finished annotating the document.

    On the Train tab, the left-hand panel shows that 1 document has been labeled.

Assign annotated document to the training set

Now that you have labeled this example document, you can assign it to the training set.

  1. On the Train tab, select the Select All checkbox.

  2. From the Assign to Set list, select Training.

In the left-hand panel, you can find that 1 document has been assigned to the training set.

Import pre-labeled data to the training and test sets

In this guide, you are provided with pre-labeled data.

If working on your own project, you have to determine how to label your data. Refer to Labeling options. Document AI custom processors require a minimum of 10 documents in both the training and test sets, along with 10 instances of each label in each set. We recommend that you have at least 50 documents in each set, with 50 instances of each label for best performance. In general, more training data produces higher accuracy.

  1. Select Import documents.

  2. Enter the following path in Source path. This bucket contains pre-labeled documents in the Document JSON format.

    cloud-samples-data/documentai/Custom/Patents/JSON/Classification-InventionType
    
  3. From the Data split list, select Auto-split. This automatically splits the documents to have 80% in the training set, and 20% in the test set. Ignore the Apply labels section.

  4. Select Import. The import might take several minutes to complete.

When the import is finished, you find the documents on the Train tab.

Optional: Batch label documents at import

After the schema has been configured, you can label all documents that are in a particular directory at import to save time with labeling.

custom-classifier-9

  1. Select Import documents.

  2. Enter the following path in Source path. This bucket contains unlabeled documents in PDF format.

    cloud-samples-data/documentai/Custom/Patents/PDF-CDC-BatchLabel
    
  3. From the Data split list, select Auto-split. This automatically splits the documents to have 80% in the training set, and 20% in the test set.

  4. In the Apply labels section, select Choose label.

  5. For these sample documents, select other.

  6. Select Import and wait for the process to finish. You can leave this page and return later. When complete, you find the documents on the Train tab with the label applied.

Train the processor

Now that you have imported the training and test data, you can train the processor. Because training might take several hours, make sure you have set up the processor with the appropriate data and labels before you begin training.

  1. Select Train New Version.

  2. In the Version name field, enter a name for this processor version, such as my-cdc-version-1.

  3. (Optional) Select View Label Stats to find information about the document labels. That can help determine your coverage. Select Close to return to the training setup.

  4. Select Start training You can check the status on the right-hand panel.

Deploy the processor version

  1. After training is complete, navigate to the Manage Versions tab. You can view details about the version you just trained.

  2. Select the three vertical dots on the right of the version you want to deploy, and select Deploy version.

  3. Select Deploy from the popup window.

    Deployment takes a few minutes to complete.

Evaluate and test the processor

  1. After deployment is complete, navigate to the Evaluate & Test tab.

    On this page, you can view evaluation metrics including the F1 score, Precision and Recall for the full document, and individual labels. For more information about evaluation and statistics, refer to Evaluate processor.

  2. Download a document that has not been involved in previous training or testing so that you can use it to evaluate the processor version. If using your own data, you would use a document set aside for this purpose.

    Download PDF

  3. Select Upload Test Document and select the document you just downloaded.

    The Custom Document Classifier analysis page opens. The output demonstrates how well the document was classified.

    You can also re-run the evaluation against a different test set or processor version.

Optional: Auto-label newly imported documents

After deploying a trained processor version, you can use Auto-labeling to save time on labeling when importing new documents.

  1. On the Train page, Import documents.

  2. Copy and paste the following Cloud Storage path. This directory contains 5 unlabeled Patent PDFs. From the Data split dropdown list, select Training.

    cloud-samples-data/documentai/Custom/Patents/PDF-CDC-AutoLabel
    
  3. In the Apply labels section, select Auto-labeling.

  4. Select an existing processor version to label the documents.

    • For example: 2af620b2fd4d1fcf
  5. Select Import and wait for the process to finish. You can leave this page and return later. When complete, the documents appear in the Auto-labeled section of the Train page.

  6. You cannot use auto-labeled documents for training or testing without marking them as labeled. Go to the Auto-labeled section to view the auto-labeled documents.

  7. Select the first document to enter the labeling console.

  8. Verify the label to ensure it is correct. Adjust if it is incorrect.

  9. Select Mark as Labeled when finished.

  10. Repeat the label verification for each auto-labeled document, then return to the Train page to use the data for training.

Use the processor

You have successfully created and trained a Custom Document Classifier processor.

You can manage your custom-trained processor versions just like any other processor version. For more information, refer to Managing processor versions.

You can Send a processing request to your custom processor, and the response can be handled the same as other classifier processors.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

To avoid unnecessary Google Cloud charges, use the Google Cloud console to delete your processor and project if you don't need them.

If you created a new project to learn about Document AI and you no longer need the project, delete the project.

If you used an existing Google Cloud project, delete the resources you created to avoid incurring charges to your account:

  1. In the Google Cloud console navigation menu, select Document AI, then My Processors.

  2. Select More actions in the same row as the processor you want to delete.

  3. Select Delete processor, enter the processor name, then select Delete again to confirm.

What's next

See Guides for more reference.