Custom Document Models
Complete the full lesson to earn 25 points
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro
Lesson: Custom Document Models in Foundry
Introduction: Why Document Intelligence Matters
In the modern enterprise, data exists in two primary forms: structured data, which lives neatly in databases, and unstructured data, which lives in PDFs, emails, invoices, contracts, and scanned forms. While structured data is easy to process, the vast majority of business-critical information is locked away in documents. Extracting this information manually is slow, error-prone, and expensive. This is where Document Intelligence comes into play.
Document Intelligence is the process of using machine learning models to identify, classify, and extract meaningful data from documents automatically. Within the Foundry ecosystem, Custom Document Models allow you to go beyond off-the-shelf solutions. Instead of relying on generic models that might struggle with your specific industry jargon or unique document layouts, you can train models tailored to your exact business processes. Whether you are processing thousands of freight bills, insurance claims, or legal discovery documents, custom models enable you to digitize your operations and turn static files into actionable data.
This lesson explores the lifecycle of creating, training, and deploying custom document models within Foundry. We will look at the technical architecture required to support these models, the best practices for data preparation, and the iterative process of model refinement. By the end of this module, you will understand how to transform raw physical or digital documents into structured, queryable data sets that integrate directly with your operational workflows.
Understanding the Foundry Document Intelligence Architecture
Foundry treats documents as first-class objects within its data ontology. Unlike traditional systems that treat documents as blobs of binary data, Foundry enables you to link extracted information directly to the objects that matter to your business. The Document Intelligence pipeline generally follows a four-stage lifecycle: ingestion, classification, extraction, and integration.
The Ingestion Layer
Before a model can look at a document, the document must be ingested into the platform. This involves connecting to external sources such as email servers, file shares, or cloud storage buckets. Once ingested, Foundry stores the file and generates a reference object in the dataset. It is essential to ensure that your documents are stored in a format that the platform can process, typically PDFs, TIFFs, or JPEGs.
The Classification Layer
Classification is the process of automatically identifying what type of document you are looking at. For example, in an accounts payable workflow, you might need to distinguish between a W-9 tax form, an invoice, a purchase order, and a delivery receipt. Custom classification models trained on your specific document types ensure that subsequent extraction steps are routed to the correct logic.
The Extraction Layer
This is the core of the custom model. Once a document is classified, the extraction model targets specific regions of the document to pull out data points. This could be a total dollar amount on an invoice, a signature date on a contract, or a patient ID on a medical form. Custom models are required when the document layout is non-standard or when the data points are buried in complex tables.
The Integration Layer
The final stage is turning the extracted data into a structured format that can be used in downstream applications, such as dashboards or automated decision engines. By mapping the extracted values to your ontology, you ensure that the data is not just "pulled out," but is contextually aware of the business entities it represents.
Preparing Your Dataset: The Foundation of Success
The most significant mistake practitioners make is rushing into model training without adequate data preparation. A custom model is only as good as the data it is trained on. To create a high-performing model, you must curate a representative, high-quality dataset.
Data Selection Strategies
You should aim for a dataset that reflects the real-world variety of your documents. If your invoices come from 50 different vendors, your training set should include samples from most of those vendors. If your documents are sometimes blurry scans and sometimes digital-native PDFs, ensure your training data contains both types.
- Diversity: Ensure you have enough examples of each document variation (e.g., different layouts from different suppliers).
- Balance: Try to maintain a balanced number of examples for each class if you are training a classifier.
- Quality: Avoid including documents with extreme noise, such as illegible smudges or pages that are completely blank, as these can confuse the training algorithm.
Labeling and Annotation
Annotation is the process of manually identifying the data points you want the model to learn. Within Foundry, you will use a built-in labeling interface to draw bounding boxes around text and assign them to specific fields.
Callout: The Importance of Ground Truth In machine learning, "ground truth" refers to the verified, correct data that serves as the basis for training. If your annotations are inconsistent—for example, if you label "Invoice Total" on one document but omit it on another—the model will struggle to learn the pattern. Always define a strict annotation schema before you start labeling.
Best Practices for Annotation
- Consistency: Define a style guide. If you are extracting a date, decide whether you will always capture the full string (e.g., "January 1, 2023") or just the digits.
- Completeness: Label every instance of a field on a page. If a document has multiple line items, every single one must be annotated for the model to learn how to parse tables.
- Iterative Refinement: Start with a small batch of 50-100 documents. Train a baseline model, test it, and then use the results to identify where the model is failing. Add more training data specifically targeting those failure points.
Building and Training the Model
Once your data is annotated, you move into the training phase. In Foundry, this is typically handled through a dedicated model training environment that abstract away the underlying infrastructure while giving you control over hyperparameters.
Choosing the Right Model Architecture
Depending on your use case, you might choose between different types of models:
- Layout-Aware Models: These models look at both the text content and the spatial arrangement of the text. They are ideal for forms and invoices where the position of the text is just as important as the text itself.
- Text-Only Models: These are better suited for unstructured text blocks, such as long-form legal contracts or medical notes, where the spatial layout is less consistent.
- Hybrid Models: These combine both approaches to provide high accuracy on documents that have a mix of dense text and structured tables.
The Training Process
You start by defining the training configuration. This involves selecting your input dataset, specifying the fields to be extracted, and setting training parameters.
# Conceptual example of configuring a training job in Foundry
from foundry_ml import DocumentModelTrainer
trainer = DocumentModelTrainer(
training_data="ri.foundry.main.dataset.my_documents",
model_type="layout_transformer",
fields=["invoice_date", "total_amount", "vendor_name"],
epochs=20,
learning_rate=0.001
)
# Start the training job
model_version = trainer.train()
The code above is a simplified representation of how you might interact with the training API. The key is to monitor the training metrics, specifically the F1 score, which balances precision (did the model find the right thing?) and recall (did the model find everything it was supposed to?).
Note: Training a model is an iterative process. Do not expect 100% accuracy on the first attempt. Monitor the validation loss during training to ensure the model is actually learning the patterns and not just memorizing the training examples (an issue known as overfitting).
Implementing the Model in Production
After training, you must deploy the model to a production environment. In Foundry, this involves wrapping the model in a service that can process incoming documents in real-time or in batch.
Batch vs. Real-Time Processing
- Batch Processing: Best for large volumes of historical data. You might run a pipeline every night that processes all documents uploaded to a specific folder during the day. This is cost-effective and allows for easier error handling.
- Real-Time Processing: Best for workflows where a human is waiting for the result. For example, if a user is uploading a form through a web portal, they might need immediate feedback on whether the form was accepted or if information is missing.
Handling Model Confidence Scores
Every document model will provide a confidence score for its extractions. You should never blindly trust a model's output. Implement a "Human-in-the-Loop" (HITL) workflow for documents where the model's confidence falls below a certain threshold (e.g., 0.85).
| Confidence Score | Action |
|---|---|
| > 0.95 | Auto-accept and push to downstream database. |
| 0.85 - 0.95 | Flag for human review in a verification queue. |
| < 0.85 | Reject or route to a specialized exception handling team. |
By setting these thresholds, you ensure that your automation is safe and that humans only spend time on the most difficult cases. This creates a feedback loop: the corrections made by humans in the verification queue can be fed back into the training dataset to improve future versions of the model.
Best Practices and Industry Standards
To successfully implement custom document models, you must follow established industry standards. These are not just technical guidelines; they are operational strategies for maintaining the health of your AI systems.
Version Control for Models
Always treat your models as code. Keep track of which dataset was used to train each version of a model. If a model starts performing poorly, you should be able to roll back to a previous version or inspect the training logs to understand what changed.
Monitoring for Data Drift
Data drift occurs when the nature of the documents you receive changes over time. For example, a vendor might change their invoice layout, or a new regulation might require new fields on a form. Your model, which was trained on the old format, will begin to fail.
- Implement Alerts: Set up automated alerts when the average confidence score of your model drops significantly.
- Periodic Retraining: Schedule periodic retraining sessions to incorporate new documents that have been processed and verified by humans.
- Audit Trails: Maintain a log of every extraction, including the document ID, the model version, and the confidence score. This is crucial for compliance and debugging.
Dealing with Complex Tables
Tables are notoriously difficult for AI models. When extracting line items from an invoice, ensure your model is explicitly trained on table structures.
Tip: Simplify Table Extraction If your model is struggling with tables, consider using a pre-processing step that converts the PDF to a structured format like CSV or JSON before passing it to the model. Sometimes, a simple rule-based script to detect table borders can significantly improve the performance of your machine learning model.
Common Pitfalls and How to Avoid Them
Even with the best tools, it is easy to fall into common traps. Recognizing these early will save you weeks of rework.
The "Black Box" Syndrome
A common mistake is treating the model as a black box and ignoring the underlying document structure. Always inspect the raw OCR (Optical Character Recognition) output if the model is failing. If the OCR is poor, no amount of model training will fix the extraction issues. Always ensure your document image quality is high before it hits the extraction pipeline.
Over-reliance on Automation
Do not attempt to automate 100% of your documents immediately. Start with a goal of automating 60-70% of the easy cases. As the model improves, you can increase this percentage. Trying to force 100% automation often leads to downstream data corruption and significant manual cleanup efforts.
Ignoring Edge Cases
During the design phase, it is tempting to focus only on the "happy path"—the standard documents that follow the expected format. However, the real world is messy. You will receive documents that are rotated, documents with handwritten notes, and documents that are missing pages. Your pipeline must include robust error handling for these edge cases.
Lack of Stakeholder Alignment
Document intelligence is not just an IT project; it is a business process project. Involve the people who currently process these documents manually. They are the domain experts who know the "gotchas" of the documents and can help you define the most important fields to extract.
Step-by-Step Implementation Guide
If you are tasked with starting a new Document Intelligence project in Foundry, follow these steps to ensure a smooth implementation.
Step 1: Define the Business Objective
Clearly define what data needs to be extracted and why. Identify the downstream systems that will consume this data. If you don't know why you need the data, you shouldn't be building a model to extract it.
Step 2: Data Acquisition
Gather at least 500 documents that are representative of the variety you expect in production. If you have fewer than this, your model will likely lack the necessary diversity to be useful.
Step 3: Schema Definition
Create a clear list of fields to extract. Define the data type for each field (e.g., date, currency, alphanumeric string). Document the expected format for each field (e.g., YYYY-MM-DD).
Step 4: Annotation
Use the Foundry labeling tool to annotate your dataset. Aim for high inter-annotator agreement if you have multiple people labeling the same documents.
Step 5: Initial Training and Evaluation
Train your first model and evaluate it on a hold-out test set that the model has never seen before. Do not use your training data to evaluate the model's performance.
Step 6: Integration
Build the pipeline to ingest documents, run them through the model, and write the results to your ontology. Ensure that the confidence scores are stored alongside the extracted data.
Step 7: Human-in-the-Loop Setup
Deploy the verification queue. Train your operations team on how to use the interface to correct model mistakes.
Step 8: Continuous Improvement
Review the performance of the model every two weeks. Use the corrections from the verification queue to create the next training dataset.
Comparison: Rule-Based vs. AI-Based Extraction
Understanding when to use AI and when to use traditional methods is a key skill for any developer.
| Feature | Rule-Based Extraction | AI-Based Extraction (Foundry) |
|---|---|---|
| Complexity | Low (Regex, Keywords) | High (Neural Networks) |
| Flexibility | Rigid (Breaks with layout changes) | Flexible (Adapts to variations) |
| Setup Time | Fast for simple forms | Longer (Requires training data) |
| Maintenance | High (Manual updates needed) | Medium (Periodic retraining) |
| Scalability | Limited | High |
Rule-based extraction is excellent for documents that are perfectly consistent, such as a standardized government form where every field is always in the exact same pixel location. However, for most business documents, the layout varies, and AI-based models are the superior choice.
Frequently Asked Questions (FAQ)
Q: How many documents do I need to train a custom model? A: While you can get a baseline with 50-100 documents, for high accuracy, you generally want 500 or more. The complexity of the document and the number of fields you are extracting will also dictate the amount of data needed.
Q: What if my documents contain handwriting? A: Modern Foundry document models are highly capable of handling handwriting, provided the OCR engine used in the ingestion layer is robust. Ensure your pre-processing steps are configured to handle handwritten text.
Q: Can I use the same model for different document types? A: It is generally better to have a separate model for each document type. Trying to train a single "master model" to handle invoices, contracts, and shipping labels simultaneously usually leads to lower accuracy across the board.
Q: How do I handle multi-page documents? A: Most Foundry models can process multi-page documents. During the labeling process, ensure you label fields across all relevant pages. The model will learn to associate the document ID with the collective information extracted from all pages.
Q: What is the biggest mistake beginners make? A: The biggest mistake is poor data quality. If your training data is inconsistent or contains OCR errors, the model will fail. Spend 80% of your time on data preparation and 20% on model training.
Key Takeaways
- Start with Data Quality: Your custom model is only as effective as your training data. Invest time in cleaning, standardizing, and consistently labeling your documents before starting the training process.
- Iterative Development: Document Intelligence is not a "set it and forget it" task. Plan for an iterative lifecycle where you train, evaluate, deploy, and refine based on real-world performance.
- Human-in-the-Loop is Essential: Never assume 100% accuracy. Always build in a verification queue where human experts can review low-confidence extractions and provide feedback to the system.
- Monitor for Drift: Business processes change, and so do document formats. Monitor your model's performance continuously and be prepared to retrain as your document types evolve.
- Use the Right Tool for the Job: Use AI models for complex, variable documents and reserve rule-based systems for perfectly consistent, static forms.
- Context Matters: By integrating extracted data into the Foundry ontology, you make the data actionable. Ensure that extracted fields are mapped to existing business objects to maximize the value of the intelligence.
- Version Control: Always track your model versions and the specific datasets used to train them. This ensures reproducibility and makes debugging significantly easier when performance issues arise.
By mastering these concepts, you move beyond simple data entry and into the realm of intelligent automation. You are no longer just building a tool to read files; you are building a system that understands your business data and helps your organization make faster, more informed decisions.
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles your daily points limit so you progress twice as fast, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× daily points limit
- ✓ Distraction-free lessons