vankey/DocShield-4B
019
1---2library_name: transformers3license: apache-2.04language:5 - zh6 - en7pipeline_tag: image-text-to-text8tags:9 - forgery-detection10 - document-forensics11 - image-tampering12 - vision-language-model13 - vlm14 - qwen3.5-vl15---16 17<p align="center">18 <img src="docshield_showcase.png" alt="DocShield-4B showcase" width="70%">19</p>20 21# DocShield-4B22 23**DocShield-4B** is a forensic-grade vision-language model for **document / image forgery analysis**. It inspects an input document image, reasons over visual tampering traces and logical consistency, and produces a structured forgery-analysis report with localized tampered regions (grounding coordinates), per-region reasoning, an overall conclusion, and a fraud-risk score.24 25It is fine-tuned from **Qwen3.5-VL-4B** with supervised training on forensic document-forgery data, and supports Qwen3.5 thinking mode.26 27📄 **Paper:** [arxiv.org/abs/2604.02694](https://arxiv.org/abs/2604.02694)28 29## Training data30 31DocShield-4B was developed using the **RealText** forensic document datasets:32 33- [vankey/RealText-V1](https://huggingface.co/datasets/vankey/RealText-V1)34- [vankey/RealText-V2](https://huggingface.co/datasets/vankey/RealText-V2)35 36## Capabilities37 38- **Visual forgery trace analysis** — crude redaction / mosaic, font & anti-aliasing inconsistency, edge halos, copy-paste artifacts, compression mismatches.39- **Logical & fact-checking** — price/quantity contradictions, date conflicts, bulk-discount logic violations.40- **Semantic alteration detection** — subtle spec substitutions (e.g. color, material) that bypass crude visual checks.41- **Localization** — bounding-box coordinates for each tampered region with per-anomaly reasoning.42- **Structured report** — conclusion (`FORGED` / authentic) + fraud-risk score.43 44## Model details45 46| | |47|---|---|48| Base model | Qwen3.5-VL-4B |49| Architecture | Qwen3_5ForConditionalGeneration (hybrid linear / full attention) |50| Precision (weights) | bfloat16 |51| Max new tokens | 1024 (default) |52| Thinking mode | supported (`--thinking` / `--no-thinking`) |53 54> The base model is **not** bundled here. This repository only releases the55> fine-tuned DocShield-4B weights.56 57## Quick start58 59Install dependencies:60 61```bash62pip install -U transformers torch torchvision pillow qwen-vl-utils63```64 65> Requires a `transformers` version with native **Qwen3.5-VL (`qwen3_5`)** support.66> The released weights were saved with `transformers==5.13.0`.67 68Run inference (see `inference.py` in this repo):69 70```bash71# default: greedy, thinking disabled72python inference.py --image path/to/image.jpg --no-thinking --max-new-tokens 102473 74# with sampling + thinking mode75python inference.py --image path/to/image.jpg --thinking --do-sample \76 --temperature 0.6 --top-p 0.8 --top-k 20 --max-new-tokens 204877```78 79### Minimal example80 81```python82import torch83from transformers import AutoProcessor, AutoTokenizer, AutoModelForImageTextToText84from qwen_vl_utils import process_vision_info85 86MODEL = "vankey/DocShield-4B"87 88tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)89processor = AutoProcessor.from_pretrained(MODEL, trust_remote_code=True)90model = AutoModelForImageTextToText.from_pretrained(91 MODEL, torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto"92)93model.eval()94 95SYSTEM_PROMPT = (96 "你是一个图像鉴伪专家,擅长结合视觉,文字结合伪造特征分析手段鉴别输入图像的真假。"97 "分析过程中,你会逐步分析,抽丝剥茧,找到图像伪造的蛛丝马迹,最终给出专业的鉴别结果及分析。"98)99USER_PROMPT = "请分析这张文档图片是否存在伪造或篡改风险,并输出一份专业、精炼、准确的防伪分析报告。"100 101messages = [102 {"role": "system", "content": [{"type": "text", "text": SYSTEM_PROMPT}]},103 {"role": "user", "content": [104 {"type": "image", "image": "image.jpg"},105 {"type": "text", "text": USER_PROMPT},106 ]},107]108 109text = processor.apply_chat_template(messages, tokenize=False,110 add_generation_prompt=True, enable_thinking=False)111image_inputs, video_inputs = process_vision_info(messages)112inputs = processor(text=[text], images=image_inputs, videos=video_inputs,113 padding=True, return_tensors="pt").to(model.device)114 115with torch.no_grad():116 out = model.generate(**inputs, max_new_tokens=1024, do_sample=False)117 118gen = [o[len(i):] for i, o in zip(inputs["input_ids"], out)]119print(processor.batch_decode(gen, skip_special_tokens=False)[0])120```121 122## Inference notes123 124- **Image input** — pass the image path directly in the message content; the processor handles resize/tokenization.125- **Thinking mode** — `--no-thinking` (default) gives a direct report; `--thinking` enables Qwen3.5 chain-of-thought before the report (use a larger `--max-new-tokens`).126- **Decoding** — greedy by default (`do_sample=False`); pass `--do-sample` with `--temperature/--top-p/--top-k` for sampling.127- **Precision** — `bfloat16` is the tested configuration (`--dtype bf16`).128 129## Citation130 131```bibtex132@article{docshield2026,133 title={DocShield: A Forensic Vision-Language Model for Document Forgery Analysis},134 author={DocShield},135 year={2026},136 url={https://arxiv.org/abs/2604.02694}137}138```139 140## License141 142Apache-2.0.143 