Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
inference: false
|
3 |
+
---
|
4 |
+
|
5 |
+
# Document Image Transformer (large-sized model)
|
6 |
+
|
7 |
+
Document Image Transformer (DiT) model pre-trained on IIT-CDIP (Lewis et al., 2006), a dataset that includes 42 million document images.. It was introduced in the paper [DiT: Self-supervised Pre-training for Document Image Transformer](https://arxiv.org/abs/2203.02378) by Li et al. and first released in [this repository](https://github.com/microsoft/unilm/tree/master/dit). Note that DiT is identical to the architecture of [BEiT](https://huggingface.co/docs/transformers/model_doc/beit).
|
8 |
+
|
9 |
+
Disclaimer: The team releasing DiT did not write a model card for this model so this model card has been written by the Hugging Face team.
|
10 |
+
|
11 |
+
## Model description
|
12 |
+
|
13 |
+
The Document Image Transformer (DiT) is a transformer encoder model (BERT-like) pre-trained on a large collection of images in a self-supervised fashion. The pre-training objective for the model is to predict visual tokens from the encoder of a discrete VAE (dVAE), based on masked patches.
|
14 |
+
|
15 |
+
Images are presented to the model as a sequence of fixed-size patches (resolution 16x16), which are linearly embedded. One also adds absolute position embeddings before feeding the sequence to the layers of the Transformer encoder.
|
16 |
+
|
17 |
+
By pre-training the model, it learns an inner representation of images that can then be used to extract features useful for downstream tasks: if you have a dataset of labeled document images for instance, you can train a standard classifier by placing a linear layer on top of the pre-trained encoder.
|
18 |
+
|
19 |
+
## Intended uses & limitations
|
20 |
+
|
21 |
+
You can use the raw model for encoding document images into a vector space, but it's mostly meant to be fine-tuned on tasks like document image classification, table detection or document layout analysis. See the [model hub](https://huggingface.co/models?search=microsoft/dit) to look for fine-tuned versions on a task that interests you.
|
22 |
+
|
23 |
+
### How to use
|
24 |
+
|
25 |
+
Here is how to use this model in PyTorch:
|
26 |
+
|
27 |
+
```python
|
28 |
+
from transformers import BeitFeatureExtractor, BeitForMaskedImageModeling
|
29 |
+
import torch
|
30 |
+
from PIL import Image
|
31 |
+
|
32 |
+
image = Image.open('path_to_your_document_image').convert('RGB')
|
33 |
+
|
34 |
+
feature_extractor = BeitFeatureExtractor.from_pretrained("microsoft/dit-large")
|
35 |
+
model = BeitForMaskedImageModeling.from_pretrained("microsoft/dit-large")
|
36 |
+
|
37 |
+
num_patches = (model.config.image_size // model.config.patch_size) ** 2
|
38 |
+
pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
|
39 |
+
# create random boolean mask of shape (batch_size, num_patches)
|
40 |
+
bool_masked_pos = torch.randint(low=0, high=2, size=(1, num_patches)).bool()
|
41 |
+
|
42 |
+
outputs = model(pixel_values, bool_masked_pos=bool_masked_pos)
|
43 |
+
loss, logits = outputs.loss, outputs.logits
|
44 |
+
```
|
45 |
+
|
46 |
+
### BibTeX entry and citation info
|
47 |
+
|
48 |
+
```bibtex
|
49 |
+
@article{Lewis2006BuildingAT,
|
50 |
+
title={Building a test collection for complex document information processing},
|
51 |
+
author={David D. Lewis and Gady Agam and Shlomo Engelson Argamon and Ophir Frieder and David A. Grossman and Jefferson Heard},
|
52 |
+
journal={Proceedings of the 29th annual international ACM SIGIR conference on Research and development in information retrieval},
|
53 |
+
year={2006}
|
54 |
+
}
|
55 |
+
```
|