Views
No views yet

[!Warning] 🚨 Note: Since there are fewer Chinese images in public data than English, we recommend you use theTokenFD-4096-English-segversion.
1import os
2import torch
3from transformers import AutoTokenizer
4from internvl.model.internvl_chat import InternVLChatModel
5from utils import post_process, generate_similiarity_map, load_image
6
7checkpoint = '/mnt/dolphinfs/hdd_pool/docker/user/hadoop-mt-ocr/guantongkun/VFM_try/processed_models/TokenFD_4096_English_seg'
8image_path = './demo_images/0000000.png'
9input_query = '11/12/2020'
10out_dir = 'results'
11
12if not os.path.exists(out_dir):
13 os.makedirs(out_dir, exist_ok=True)
14
15"""loading model, tokenizer, tok_embeddings """
16tokenizer = AutoTokenizer.from_pretrained(checkpoint, trust_remote_code=True, use_fast=False)
17model = InternVLChatModel.from_pretrained(checkpoint, low_cpu_mem_usage=True, torch_dtype=torch.bfloat16).eval()
18model = model.cuda()
19
20"""loading image """
21pixel_values, images, target_aspect_ratio = load_image(image_path)
22
23
24"""loading query texts """
25if input_query[0] in '!"#$%&\'()*+,-./0123456789:;<=>?@^_{|}~0123456789':
26 input_ids = tokenizer(input_query)['input_ids'][1:]
27else:
28 input_ids = tokenizer(' '+input_query)['input_ids'][1:]
29input_ids = torch.Tensor(input_ids).long().to(model.device)
30input_embeds = model.tok_embeddings(input_ids).clone()
31all_bpe_strings = [tokenizer.decode(input_id) for input_id in input_ids]
32
33
34"""Obtaining similarity """
35with torch.no_grad():
36 vit_embeds, _ = model.forward_tokenocr(pixel_values.to(model.device)) #(vit_batch_size, 16*16, 2048)
37 vit_embeds_local, resized_size = post_process(vit_embeds, target_aspect_ratio)
38 token_features = vit_embeds_local / vit_embeds_local.norm(dim=-1, keepdim=True)
39 input_embedings = input_embeds / input_embeds.norm(dim=-1, keepdim=True)
40 similarity = input_embedings @ token_features.t()
41 attn_map = similarity.reshape(len(input_embedings), resized_size[0], resized_size[1])
42
43"""generate map locally """
44generate_similiarity_map(images, attn_map, all_bpe_strings, out_dir, target_aspect_ratio)
45
46
47"""user command """
48# python quick_start.pyTokenFD, the first token-level visual foundation model specifically tailored for text-image-related tasks,
designed to support a variety of traditional downstream applications. To facilitate the pretraining of TokenFD,
we also devise a high-quality data production pipeline that constructs the first token-level image text dataset,
TokenIT, comprising 20 million images and 1.8 billion token-mask pairs.
Furthermore, leveraging this foundation with exceptional image-as-text capability,
we seamlessly replace previous VFMs with TokenFD to construct a document-level MLLM, TokenVL, for VQA-based document understanding tasks.
| VFM | Granularity | Dataset | #Image | #Pairs |
|---|---|---|---|---|
| CLIP | image-level | WIT400M | 400M | 0.4B |
| DINO | image-level | ImageNet | 14M | - |
| SAM | pixel-level | SA1B | 11M | 1.1B |
| TokenFD | token-level | TokenIT | 20M | 1.8B |







1@inproceedings{guan2025TokenFD,
2 title={A Token-level Text Image Foundation Model for Document Understanding},
3 author={Tongkun Guan, Zining Wang, Pei Fu, Zhentao Guo, Wei Shen, Kai zhou, Tiezhu Yue, Chen Duan, Hao Sun, Qianyi Jiang, Junfeng Luo, Xiaokang Yang},
4 booktitle={Arxiv},
5 year={2025}
6}