<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matt Noe on Medium]]></title>
        <description><![CDATA[Stories by Matt Noe on Medium]]></description>
        <link>https://medium.com/@matt.noe?source=rss-fb92b5a12f43------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Ei674BEvLE29THyN</url>
            <title>Stories by Matt Noe on Medium</title>
            <link>https://medium.com/@matt.noe?source=rss-fb92b5a12f43------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:27:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matt.noe/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[Tutorial] How to Train LayoutLM on a Custom Dataset with Hugging Face]]></title>
            <link>https://medium.com/@matt.noe/tutorial-how-to-train-layoutlm-on-a-custom-dataset-with-hugging-face-cda58c96571c?source=rss-fb92b5a12f43------2</link>
            <guid isPermaLink="false">https://medium.com/p/cda58c96571c</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[ocr]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Matt Noe]]></dc:creator>
            <pubDate>Wed, 09 Nov 2022 21:49:03 GMT</pubDate>
            <atom:updated>2022-11-09T21:49:03.066Z</atom:updated>
            <content:encoded><![CDATA[<h3>Overview</h3><p>LayoutLMv3 is a pre-trained transformer model published by Microsoft that can be used for various document AI tasks, including:</p><ol><li>Information Extraction</li><li>Document Classification</li><li>Document Question Answering</li></ol><p>LayoutLMv3 incorporates both text and visual image information into a single multimodal transformer model, making it quite good at both text-based tasks (form understanding, id card extraction and document question answering) and image-based tasks (document classification and layout analysis).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vrQYZuLVJFIlZA81.png" /></figure><p>If you’d like to learn more about what LayoutLMv3 is, you can check out the <a href="https://arxiv.org/pdf/2204.08387.pdf">white paper</a> or the <a href="https://github.com/microsoft/unilm/tree/master/layoutlmv3">Github repo</a>.</p><h3>What this guide will cover</h3><p>Many great guides exist on how to train LayoutLM on common large public datasets such as FUNSD and CORD. If that is what you are looking for, we recommend taking a look at <a href="https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLMv3/Fine_tune_LayoutLMv3_on_FUNSD_(HuggingFace_Trainer).ipynb">this great python notebook</a> that Niels Rogge from Hugging Face team has put out.</p><p>When it comes to building a document processing model for real-world applications, you’ll typically be training a model on your own custom documents, rather than one of these common datasets.</p><p>This guide is intended to walk you through the process of training LayoutLM on your own custom documents.</p><p>We’ll walk through a python notebook that covers:</p><ol><li>Building your annotated document dataset using the <a href="https://www.butlerlabs.ai/">Butler</a> UI</li><li>Converting annotated documents into the format expected by LayoutLMv3 using the <a href="https://github.com/butlerlabs/docai"><em>DocAI</em> SDK</a></li><li>Using Hugging Face transformers to train LayoutLMv3 on your custom dataset</li><li>Running inference on your trained model</li></ol><p>For the purposes of this guide, we’ll train a model for extracting information from US Driver’s Licenses, but feel free to follow along with any document dataset you have.</p><p>If you just want the code, you can check it <a href="https://colab.research.google.com/drive/1KtzdLOpLQhHrca8oRM5s8fjKiMgkl5mb#scrollTo=P_bmoVcShf6E">out here</a>.</p><p>Let’s get to it!</p><h3>Preparing the LayoutLM Development Environment</h3><p>To start off, let’s install all of the necessary packages:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fff3e3b9dadc3108125c8339d2e93462/href">https://medium.com/media/fff3e3b9dadc3108125c8339d2e93462/href</a></iframe><h3>Building your annotated dataset</h3><p>Now that our environment is set up, we’ll need to prepare our annotated document dataset so that we can use it during training.</p><p>We’ll use the Butler document annotation interface for this purpose. If you haven’t already, head over to the <a href="https://app.butlerlabs.ai/">Butler app</a> and create your account.</p><p>We won’t cover the full details of how to use the Butler UI to annotate documents in this guide (feel free to<a href="https://docs.butlerlabs.ai/reference/building-custom-form-extraction-models"> check out the documentation</a> if you want to learn more), but we’ll cover most of the key points.</p><h3>Upload Documents</h3><p>Once you have created your first model and given it a helpful name, you’ll want to upload your unannotated documents. Typically when training a LayoutLM model, you’ll want a training dataset that includes a few hundred annotated documents in it.</p><p>For this guide, we’ll get started with just 50 example driver’s licenses. We can always annotate more after we see how the model initially performs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Mzk3xCnSSZ9axQDh.png" /></figure><p>Once the initial OCR has finished on your documents, you should see the documents loaded and ready for annotation.</p><h3>Define Schema and Annotate your Documents</h3><p>The next step is to define what information you’d like to extract from your documents. We’ll be extracting the following fields from our driver’s licenses:</p><ol><li>First Name (we’ll include Middle Initial here as well)</li><li>Last Name</li><li>Address</li><li>Date of Birth</li><li>Expiration Date</li><li>Driver’s License Number</li></ol><p>Click on the Add button to add each individual field. Once finished, your extraction schema is defined, and you are ready to start annotating:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cTHzRX-bKpZRsY_S.png" /></figure><p>To annotate, make sure the right field is selected and simply drag a box around the text in the document. You can also click on individual pieces of text if you’d like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P2svmq4ApJiKzSPl.png" /></figure><p>Once your first document is annotated, you can deselect the active field, and you’ll be able to see all of the values you annotated on the document.</p><p>Great work! Now we’ll go through and annotate the remaining documents.</p><p>A few quick hot keys you might find helpful:</p><ol><li><strong>Tab (and Shift+Tab): </strong>Switch between fields</li><li><strong>Cmd+A: </strong>While annotating a table, adds a new row</li><li>Note: <strong>Windows Key + A</strong> on Windows machines</li></ol><h3>Converting your annotations into LayoutLM Format</h3><h3>Download annotations from Butler</h3><p>Now that we have our annotated dataset, we need to download it from Butler, so that we can prepare it for use with LayoutLM in the transformers library. This is very easy to do with the <a href="https://github.com/butlerlabs/docai">DocAI Python SDK</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9567453cf0223e9ef9508ca0d7f9fe56/href">https://medium.com/media/9567453cf0223e9ef9508ca0d7f9fe56/href</a></iframe><p>First, we download all the annotations from the model we created in Butler. You can follow <a href="https://docs.butlerlabs.ai/reference/authentication">this guide to find your API Key</a> and <a href="https://docs.butlerlabs.ai/reference/finding-a-model-id">this guide to find your model id</a>.</p><p>The <em>load_annotations </em>function returns an Annotations object which enables you to convert your annotations into different formats.</p><h3>Prepare for use with LayoutLM</h3><p>We’ll then need to convert into a format more readily usable by the transformers library and LayoutLMv3 specifically.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09c908cd9bd21a0906b1c51017aa7c71/href">https://medium.com/media/09c908cd9bd21a0906b1c51017aa7c71/href</a></iframe><p>There is a lot actually occurring in these two simple lines of code.</p><p>First, we convert our annotations into a common NER format. Let’s take a look:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/0*riWq2hE0Be3veSfC.png" /></figure><p>Here is a brief description of each field:</p><ol><li><strong>id:</strong> The id of the document in Butler</li><li><strong>tokens:</strong> The words in the document</li><li><strong>bboxes:</strong> The bounding box for the corresponding word in tokens. Bounding boxes are in Min/Max format: [x_min, y_min, x_max, y_max]</li><li><strong>ner_tags:</strong> The string form of the annotation for the corresponding word in tokens. Notice that we called <em>as_ner</em> with <em>as_iob2=True </em>so the annotations have been broken up into<a href="https://www.google.com/search?q=iob+format&amp;oq=iob&amp;aqs=chrome.0.69i59j69i57.1093j0j7&amp;sourceid=chrome&amp;ie=UTF-8"> Inside-Outside-Beginning format.</a></li><li><strong>image:</strong> A pillow image</li></ol><p>Note: The load_annotations function does not currently support multi-page documents. Only the first page of a multi-page document is included.</p><p>After loading in the NER format, we normalize the bounding boxes so that they are between 0 and 1000, which is the format that LayoutLMv3 expects.</p><p>Finally, we load our annotations into a Hugging Face Dataset object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81b2e4dec5d9fc2154b73154c39a995a/href">https://medium.com/media/81b2e4dec5d9fc2154b73154c39a995a/href</a></iframe><p>We are now ready to train our model!</p><h3>Training LayoutLMv3 on the Custom Dataset</h3><p>Let’s create a few helpful variables, as well as prepare our dataset for training:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e15ca99dd062c7e777384b7ffed7928/href">https://medium.com/media/4e15ca99dd062c7e777384b7ffed7928/href</a></iframe><p>Now our dataset is officially ready for training! From here on out, we’ll be following the guides put out by the Hugging Face team for how to fine-tune LayoutLM (here it is for <a href="https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLMv3/Fine_tune_LayoutLMv3_on_FUNSD_(HuggingFace_Trainer).ipynb">reference</a>).</p><h3>Convert dataset into LayoutLM format</h3><p>First load the layoutlmv3-base processor from the Hugging Face hub:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/884eae4d6d27e98bb112c811911cf3f9/href">https://medium.com/media/884eae4d6d27e98bb112c811911cf3f9/href</a></iframe><p>Then prepare the train and eval datasets:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31bf3886400386e0d53a4504156270f8/href">https://medium.com/media/31bf3886400386e0d53a4504156270f8/href</a></iframe><h3>Define evaluation metrics</h3><p>Now we are ready to define the evaluation function. We’ll use a utility fn from the <em>DocAI</em> SDK to do so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d29468dfd24f33fc36c74db20e6586b6/href">https://medium.com/media/d29468dfd24f33fc36c74db20e6586b6/href</a></iframe><h3>Train the model</h3><p>Once that is done, we can define the model and training parameters:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c86f763d9f485201b7b21d695acef2d1/href">https://medium.com/media/c86f763d9f485201b7b21d695acef2d1/href</a></iframe><p>There are a lot of different hyperparameters that can be tuned. Check out the Hugging Face documentation <a href="https://huggingface.co/docs/transformers/v4.23.1/en/main_classes/trainer#transformers.TrainingArguments">here</a> to read through all of them.</p><p>Finally, the best part:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e61a6bcf18770204010de0e9e9e302c4/href">https://medium.com/media/e61a6bcf18770204010de0e9e9e302c4/href</a></iframe><p>If everything was done correctly, your model will begin training.</p><p>Depending on the machine, this may take a little while, so sit back and relax. You can also explore some of the pre-trained document extraction models we have ready for use at <a href="https://app.butlerlabs.ai/library">Butler</a> if you’d like :)</p><p>Once training finishes, you should see the metrics about how it performed on the eval dataset:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6o8dfYsC6EC90VA0.png" /></figure><p>You can see our model achieved a near 100% accuracy which is quite good. Given our dataset was a relatively simple set of example US Driver’s License images, we’d expect to see this high accuracy.</p><p>Even for more complicated use cases, LayoutLM can still reach impressively high accuracy!</p><p>If you want, you can publish your model to the Hugging Face hub for future use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75ebfae0817151d109a5a8a5d0bd6351/href">https://medium.com/media/75ebfae0817151d109a5a8a5d0bd6351/href</a></iframe><h3>Running inference</h3><p>Now let’s try using our model to make predictions on a document. First we’ll generate the predictions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4840348c5f25bea2748382eb2af1b31f/href">https://medium.com/media/4840348c5f25bea2748382eb2af1b31f/href</a></iframe><p>Before visualizing the predicted results, let’s define a few utility functions to make this a bit easier:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12dbcdf621ed90addfd1686d12e34bda/href">https://medium.com/media/12dbcdf621ed90addfd1686d12e34bda/href</a></iframe><p>Then let’s visualize the predicted results:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4cc969334bb0221006ecac5a2144760/href">https://medium.com/media/a4cc969334bb0221006ecac5a2144760/href</a></iframe><p>‍</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xl_ZLPP7H2e1YE_B.png" /></figure><p>And compare against the actual ground truth values:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/996ac29472bdc058d22ff8c35d873031/href">https://medium.com/media/996ac29472bdc058d22ff8c35d873031/href</a></iframe><p>When comparing, we can see how accurate our LayoutLM model is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LaVnY_WmPlGTk0cV.png" /></figure><h3>Conclusion</h3><p>Great work! As you can see, LayoutLM is a powerful multimodal model that you can apply for many different Document AI tasks.‍</p><p>In this tutorial you:</p><ol><li>Built an annotated dataset for your custom documents with Butler</li><li>Converted those annotations into a format usable by LayoutLMv3 using the <a href="https://github.com/butlerlabs/docai"><em>DocAI</em> SDK</a></li><li>Trained a custom LayoutLMv3 model with Hugging Face transformers</li><li>Visualized the results of running inference on your trained model</li></ol><p>You can find the full code available at our <a href="https://colab.research.google.com/drive/1KtzdLOpLQhHrca8oRM5s8fjKiMgkl5mb#scrollTo=P_bmoVcShf6E">Train LayoutLM on a Custom Dataset notebook</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cda58c96571c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>