vankey commited on
Commit
ffdf306
·
verified ·
1 Parent(s): 51349a2

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +142 -0
README.md ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ license: apache-2.0
4
+ language:
5
+ - zh
6
+ - en
7
+ pipeline_tag: image-text-to-text
8
+ tags:
9
+ - forgery-detection
10
+ - document-forensics
11
+ - image-tampering
12
+ - vision-language-model
13
+ - vlm
14
+ - qwen3.5-vl
15
+ ---
16
+
17
+ <p align="center">
18
+ <img src="docshield_showcase.png" alt="DocShield-4B showcase" width="70%">
19
+ </p>
20
+
21
+ # DocShield-4B
22
+
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
+
25
+ It 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 data
30
+
31
+ DocShield-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
+ ## Capabilities
37
+
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 details
45
+
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 the
55
+ > fine-tuned DocShield-4B weights.
56
+
57
+ ## Quick start
58
+
59
+ Install dependencies:
60
+
61
+ ```bash
62
+ pip install -U transformers torch torchvision pillow qwen-vl-utils
63
+ ```
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
+
68
+ Run inference (see `inference.py` in this repo):
69
+
70
+ ```bash
71
+ # default: greedy, thinking disabled
72
+ python inference.py --image path/to/image.jpg --no-thinking --max-new-tokens 1024
73
+
74
+ # with sampling + thinking mode
75
+ python inference.py --image path/to/image.jpg --thinking --do-sample \
76
+ --temperature 0.6 --top-p 0.8 --top-k 20 --max-new-tokens 2048
77
+ ```
78
+
79
+ ### Minimal example
80
+
81
+ ```python
82
+ import torch
83
+ from transformers import AutoProcessor, AutoTokenizer, AutoModelForImageTextToText
84
+ from qwen_vl_utils import process_vision_info
85
+
86
+ MODEL = "vankey/DocShield-4B"
87
+
88
+ tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)
89
+ processor = AutoProcessor.from_pretrained(MODEL, trust_remote_code=True)
90
+ model = AutoModelForImageTextToText.from_pretrained(
91
+ MODEL, torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto"
92
+ )
93
+ model.eval()
94
+
95
+ SYSTEM_PROMPT = (
96
+ "你是一个图像鉴伪专家,擅长结合视觉,文字结合伪造特征分析手段鉴别输入图像的真假。"
97
+ "分析过程中,你会逐步分析,抽丝剥茧,找到图像伪造的蛛丝马迹,最终给出专业的鉴别结果及分析。"
98
+ )
99
+ USER_PROMPT = "请分析这张文档图片是否存在伪造或篡改风险,并输出一份专业、精炼、准确的防伪分析报告。"
100
+
101
+ messages = [
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
+
109
+ text = processor.apply_chat_template(messages, tokenize=False,
110
+ add_generation_prompt=True, enable_thinking=False)
111
+ image_inputs, video_inputs = process_vision_info(messages)
112
+ inputs = processor(text=[text], images=image_inputs, videos=video_inputs,
113
+ padding=True, return_tensors="pt").to(model.device)
114
+
115
+ with torch.no_grad():
116
+ out = model.generate(**inputs, max_new_tokens=1024, do_sample=False)
117
+
118
+ gen = [o[len(i):] for i, o in zip(inputs["input_ids"], out)]
119
+ print(processor.batch_decode(gen, skip_special_tokens=False)[0])
120
+ ```
121
+
122
+ ## Inference notes
123
+
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
+ ## Citation
130
+
131
+ ```bibtex
132
+ @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
+ ## License
141
+
142
+ Apache-2.0.