davanstrien HF Staff commited on
Commit
520c0ce
·
verified ·
1 Parent(s): 7d98f3f

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
+ viewer: false
3
+ tags:
4
+ - uv-script
5
+ - object-detection
6
+ - vlm
7
+ - hf-jobs
8
+ - dataset-labelling
9
+ license: apache-2.0
10
+ ---
11
+
12
+ # VLM Object Detection
13
+
14
+ Instruction-prompted object detection with vision-language models via vLLM. Designed as a **VLM-as-labeller** primitive for bootstrapping object-detection datasets — give it a free-form prompt ("detect every photograph and illustration", "detect all PPE items", "detect every electronic component and identify its reference designator") and it returns bbox JSON ready for downstream labelling tools (Label Studio, FiftyOne, COCO conversion).
15
+
16
+ **Sibling**: [`uv-scripts/sam3`](https://huggingface.co/datasets/uv-scripts/sam3) does class-prompted detection with SAM3 — pass it `--class-name photograph` and it detects that class. Use SAM3 when you know your classes ahead of time and want speed at scale; use this repo when you want free-form instruction prompts and richer per-element labels (sub-categories, descriptive sub-labels, OCR-style transcription embedded in detections).
17
+
18
+ | Repo | Prompt style | Output | Best for |
19
+ |---|---|---|---|
20
+ | `sam3/` | Class names | bbox + score | Known categories, scale |
21
+ | `vlm-object-detection/` | Free-form instruction | bbox + label + sub_label | Exploratory labelling, structured per-element labels |
22
+
23
+ ## Scripts
24
+
25
+ | Script | Status | Use |
26
+ |---|---|---|
27
+ | `qwen3vl-detect.py` | **Stable** | Standard zero-shot detection |
28
+ | `qwen3vl-detect-fewshot.py` | **Experimental** | One-shot in-context example — pass an example image + its labelled output to demonstrate the task |
29
+
30
+ The script name says `qwen3vl` because the bbox-output convention (`bbox_2d` JSON normalised to 0-1000) is the Qwen-VL family standard, but the script is mostly model-agnostic — you can pass any vLLM-supported VLM via `--model` and adjust the prompt accordingly.
31
+
32
+ ## Quick start
33
+
34
+ ```bash
35
+ hf jobs uv run \
36
+ --image vllm/vllm-openai:latest \
37
+ --flavor a100-large \
38
+ --python /usr/bin/python3 \
39
+ -e PYTHONPATH=/usr/local/lib/python3.12/dist-packages \
40
+ -s HF_TOKEN \
41
+ https://huggingface.co/datasets/uv-scripts/vlm-object-detection/raw/main/qwen3vl-detect.py \
42
+ INPUT_DATASET OUTPUT_DATASET \
43
+ --model Qwen/Qwen3.6-35B-A3B \
44
+ --max-model-len 32768 \
45
+ --max-image-tokens 9800 \
46
+ --gpu-memory-utilization 0.90 \
47
+ --max-samples 20 --shuffle --seed 42 \
48
+ --prompt "Detect every visible object. For each, return a JSON object with bbox_2d (normalised 0-1000), label, and sub_label."
49
+ ```
50
+
51
+ **Recommended model right now**: `Qwen/Qwen3.6-35B-A3B`. It has the strongest detection benchmarks of the Qwen-VL family (ODInW13 zero-shot detection 50.8) and produces labelling-grade boxes on natural images. As newer VLMs land we expect to update this recommendation; the script's defaults will be revisited then.
52
+
53
+ ## Output schema
54
+
55
+ Each row of the output dataset gets three new columns:
56
+
57
+ - **`detections`**: `list[{bbox: [x1, y1, x2, y2], label: str, sub_label: str}]`
58
+ - **`bbox`** is in **original-image pixel coordinates** (integers, clipped to image bounds). The script denormalises the model's 0-1000 output internally.
59
+ - **`raw_response`**: full model text (for re-parsing, debugging, audit)
60
+ - **`inference_info`**: JSON string with model, prompt, image_size, min/max pixels, timestamp
61
+
62
+ All original input columns are preserved.
63
+
64
+ ## Detection prompt format
65
+
66
+ Qwen-VL family models emit bbox JSON when prompted with the `bbox_2d` / `label` / `sub_label` schema. The script parses that JSON robustly (tolerates fenced code blocks, trailing commas, comments).
67
+
68
+ A working prompt template:
69
+
70
+ ```
71
+ Detect every {target description}. For each, return a JSON object with
72
+ "bbox_2d": [x1, y1, x2, y2] (coordinates normalised to 0-1000),
73
+ "label" ({category constraints — list classes, or just "the object category"}),
74
+ and "sub_label" ({what to put in sub_label, e.g. colour, sub-category, transcribed text}).
75
+ Return a JSON array.
76
+ ```
77
+
78
+ Empirical lessons:
79
+
80
+ - **Keep prompts positive-only.** Telling the model what to *find* works better than "ignore X" clauses, which can trigger over-conservative empty outputs on busy pages.
81
+ - **Give the model a broad taxonomy and filter the output post-hoc** rather than narrowing the prompt. Asking for 7 categories then `label in {"Photograph", "Illustration"}` produces better detections on the categories you care about than asking for just those 2 classes upfront.
82
+ - **Sub-labels are descriptive prose**, not constrained. The model uses them for colour ("blue nitrile"), material ("white N95 respirator"), reference designators ("R246", "U201"), or transcribed text (actual headline text on a newspaper page).
83
+
84
+ ## Hardware notes
85
+
86
+ - **A100-large + `vllm/vllm-openai:latest` image** is the working combo. The default uv-script image installs PyPI's latest PyTorch, which has a CUDA driver mismatch on a100 hosts (driver 12.9, PyTorch wants newer). Workaround: `--image vllm/vllm-openai:latest --python /usr/bin/python3 -e PYTHONPATH=/usr/local/lib/python3.12/dist-packages` (uses the image's pre-installed driver-compatible torch + vllm).
87
+ - **H200 was unreliable** in our testing — flashinfer's runtime JIT compile of cutlass kernels for `compute_90a` failed in the same image. Stick with A100 for now.
88
+ - **`max_num_seqs` is auto-capped to `batch_size`**. Qwen3.6's hybrid Gated-DeltaNet architecture allocates per-sequence SSM cache blocks; vLLM's default 256 can exceed available blocks and crash CUDA graph capture. We never decode > batch_size concurrently anyway.
89
+
90
+ ## CLI reference
91
+
92
+ Run `uv run qwen3vl-detect.py --help` for the full flag set. Most-tuned flags:
93
+
94
+ - `--prompt` / `--prompt-file` — the detection instruction
95
+ - `--model` — any vLLM-supported VLM. Default: `Qwen/Qwen3.6-35B-A3B`. For smaller GPUs use `Qwen/Qwen3.5-9B` and lower `--max-model-len` / `--max-image-tokens`.
96
+ - `--image-column` (default: `image`)
97
+ - `--split` (default: `train`)
98
+ - `--max-samples`, `--shuffle`, `--seed` — for test/exploration runs
99
+ - `--batch-size` (default: 8)
100
+ - `--max-tokens` (default: 8192) — output cap; bump if outputs get truncated
101
+ - `--max-model-len`, `--max-image-tokens`, `--gpu-memory-utilization` — VRAM tuning
102
+ - `--repetition-penalty` (default: 1.05) — prevents the "100 duplicate detections" failure mode
103
+ - `--grayscale` — convert input to L channel then RGB; sometimes helps on sepia/discoloured historical scans
104
+ - `--private`, `--create-pr` — output dataset visibility / commit mode
105
+
106
+ ## Worked examples
107
+
108
+ These public datasets were generated during development and can be inspected as smoke-test reference:
109
+
110
+ | Dataset | Prompt | Notes |
111
+ |---|---|---|
112
+ | `davanstrien/qwen3.6-cppe5-20samples` | Medical PPE detection (5 classes) | Cleanest result — labelling-grade boxes for face shields, gloves, goggles, masks, coveralls |
113
+ | `davanstrien/qwen3.6-beyond-words-20samples` | Historical newspaper visual elements (7 classes) | Demonstrates instruction-prompted detection on dense layouts. Includes OCR-style transcription embedded in `sub_label` (actual headline text) |
114
+
115
+ ## Known limitations
116
+
117
+ - **Bbox precision varies by model and image.** On natural-object photos (PPE, products, animals), boxes are often within 10 px of ground truth. On dense document layouts, boxes are looser and occasionally model gives up on a page entirely.
118
+ - **Output is "labelling-grade" not "training-grade".** Useful as a step-zero seed for a labelling pipeline (export to Label Studio / FiftyOne for human review/correction), not as final ground truth.
119
+ - **Dense layouts (newspapers, magazine pages)**: the model occasionally returns empty or over-detects. Mitigations: keep prompts positive-only, use a broad taxonomy + post-filter, try `qwen3vl-detect-fewshot.py` with a representative example.
120
+
121
+ ## Few-shot variant
122
+
123
+ `qwen3vl-detect-fewshot.py` adds two flags:
124
+
125
+ - `--example-image PATH` — a single image to use as the in-context demonstration
126
+ - `--example-answer-file PATH` — a text file containing the expected JSON output for that image (the script reads it verbatim and inlines it in the prompt)
127
+
128
+ When both are set, each target image is preceded by the example + its answer in a single user-turn message. Boosts precision on rows where it fires, at the cost of lower recall (more empty outputs on pages that don't look like the example).
129
+
130
+ In HF Jobs, pass example artifacts via volume mount:
131
+
132
+ ```bash
133
+ -v hf://datasets/your-namespace/your-example-dataset:/example \
134
+ --example-image /example/example.png \
135
+ --example-answer-file /example/answer.json
136
+ ```
137
+
138
+ Pick an example that looks similar to your typical target page — the model anchors heavily to "what a page should look like."
139
+
140
+ ## Acknowledgements
141
+
142
+ Adapted from the [`Qwen/Object-Detection-with-Qwen`](https://modelscope.cn/studios/Qwen/Object-Detection-with-Qwen) demo on ModelScope. The Qwen-VL family is from Alibaba's Qwen team; weights are available on Hugging Face under apache-2.0.