File size: 7,495 Bytes
766d72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
"""
Generate SFT grounding data with negative samples for improved precision and rejection ability.

Produces:
    - data/sft/grounding/sft_grounding.jsonl
    - 70% positive samples with full CoT thinking template
    - 30% negative samples (object not in image -> empty response)

Usage:
    python scripts/generate_sft_grounding_data.py \
        --coco_jsonl data/pretrain/grounding.jsonl \
        --image_root data/coco/val \
        --output data/sft/grounding/sft_grounding.jsonl \
        --neg_ratio 0.30
"""

import os
import sys
import json
import random
import argparse
from pathlib import Path
from collections import defaultdict

PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))

from utils.coco_categories import COCO_CATS

random.seed(42)

IRREGULAR_PLURALS = {
    "person": "people",
    "mouse": "mice",
    "sheep": "sheep",
    "knife": "knives",
    "child": "children",
}


def pluralize(word: str) -> str:
    low = word.lower()
    if low in IRREGULAR_PLURALS:
        return IRREGULAR_PLURALS[low]
    if " " in word:
        parts = word.rsplit(" ", 1)
        return parts[0] + " " + pluralize(parts[1])
    if word.endswith(("s", "sh", "ch", "x", "z")):
        return word + "es"
    if word.endswith("y") and word[-2] not in "aeiou":
        return word[:-1] + "ies"
    return word + "s"


GROUNDING_TEMPLATES = [
    "Locate the {category} in the image.",
    "Locate the {category} in this image.",
    "Find the {category} in the image.",
    "Where is the {category} in the image?",
    "Where is the {category}?",
    "Show me the {category} in the image.",
    "Point out the {category}.",
    "Locate the {plural} in the image.",
]


def format_box_token(boxes):
    """Format boxes into <|box|>[[x1,y1,x2,y2],...]<|/box|>."""
    if not boxes:
        return ""
    inner = ",".join(f"[{x1},{y1},{x2},{y2}]" for x1, y1, x2, y2 in boxes)
    return f"<|box|>[{inner}]<|/box|>"


def build_positive_thinking(category, boxes):
    """Build CoT thinking for positive sample."""
    refs = "\n".join(
        f"I see a <|ref|>{category}<|/ref|><|box|>[[{b[0]},{b[1]},{b[2]},{b[3]}]]<|/box|>."
        for b in (boxes if boxes else [])
    )
    if not refs:
        refs = f"I see a <|ref|>{category}<|/ref|><|box|>[]<|/box|>."

    return (
        f"1. **Analyzing the request**\n"
        f"The user asks me to locate the {category} in this image.\n"
        f"2. **Object grounding**\n"
        f"{refs}\n"
        f"3. **Conclusion**\n"
        f"The {category} is located at the specified coordinates."
    )


def build_positive_answer(category, boxes):
    if not boxes:
        return f"The {category} is not visible in the image."
    box_str = ",".join(f"[{x1},{y1},{x2},{y2}]" for x1, y1, x2, y2 in boxes)
    return f"The {category} is located at [{box_str}]."


def build_negative_thinking(category):
    return (
        f"1. **Analyzing the request**\n"
        f"The user asks me to locate the {category} in this image.\n"
        f"2. **Object grounding**\n"
        f"After carefully scanning the entire image, I do not see any {category} present.\n"
        f"3. **Conclusion**\n"
        f"There is no {category} in this image."
    )


def build_negative_answer(category):
    return f"There is no {category} in the image."


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--coco_jsonl", type=str, default="data/pretrain/grounding.jsonl")
    parser.add_argument("--image_root", type=str, default="data/coco/val")
    parser.add_argument("--output", type=str, default="data/sft/grounding/sft_grounding.jsonl")
    parser.add_argument("--neg_ratio", type=float, default=0.30)
    parser.add_argument("--max_samples", type=int, default=10000)
    args = parser.parse_args()

    out_path = Path(args.output)
    out_path.parent.mkdir(parents=True, exist_ok=True)

    # Load all positive samples
    print("Loading COCO grounding data...")
    all_samples = []
    with open(args.coco_jsonl, "r", encoding="utf-8") as f:
        for line in f:
            item = json.loads(line.strip())
            img_rel = item.get("image", "")
            label_raw = item.get("label", 0)
            try:
                label_id = int(label_raw)
                category = COCO_CATS.get(label_id, f"object_{label_id}")
            except (ValueError, TypeError):
                # label is already a string (category name)
                category = str(label_raw)
            boxes = [tuple(b) for b in item.get("boxes", [])]
            all_samples.append({
                "image": img_rel,
                "category": category,
                "label_id": label_id,
                "boxes": boxes,
            })

    # Group by image for negative sampling
    img_to_labels = defaultdict(set)
    for s in all_samples:
        img_to_labels[s["image"]].add(s["label_id"])

    # Build positive SFT samples
    # Cap boxes at 8 per sample to keep sequence length manageable for 12G VRAM
    MAX_BOXES_PER_SAMPLE = 8
    positive_samples = []
    for s in all_samples:
        if not s["boxes"]:
            continue
        boxes = s["boxes"][:MAX_BOXES_PER_SAMPLE]
        cat = s["category"]
        plural = pluralize(cat)
        question = random.choice(GROUNDING_TEMPLATES).format(category=cat, plural=plural)
        positive_samples.append({
            "image": s["image"],
            "question": question,
            "thinking": build_positive_thinking(cat, boxes),
            "answer": build_positive_answer(cat, boxes),
            "boxes": boxes,
            "points": [],
        })

    # Build negative SFT samples
    all_label_ids = list(COCO_CATS.keys())
    img_list = list(img_to_labels.keys())
    negative_samples = []
    for img_rel in img_list:
        present = img_to_labels[img_rel]
        absent = [lid for lid in all_label_ids if lid not in present]
        if absent:
            # Sample 1-2 negative categories per image
            n_neg = min(2, len(absent))
            for neg_label in random.sample(absent, n_neg):
                category = COCO_CATS[neg_label]
                plural = pluralize(category)
                question = random.choice(GROUNDING_TEMPLATES).format(category=category, plural=plural)
                negative_samples.append({
                    "image": img_rel,
                    "question": question,
                    "thinking": build_negative_thinking(category),
                    "answer": build_negative_answer(category),
                    "boxes": [],
                    "points": [],
                })

    # Shuffle and sample
    random.shuffle(positive_samples)
    random.shuffle(negative_samples)

    # Determine counts based on neg_ratio
    n_pos_target = int(args.max_samples * (1 - args.neg_ratio))
    n_neg_target = int(args.max_samples * args.neg_ratio)

    pos_selected = positive_samples[:n_pos_target]
    neg_selected = negative_samples[:n_neg_target]

    # Combine and shuffle
    combined = pos_selected + neg_selected
    random.shuffle(combined)

    # Write output
    with open(out_path, "w", encoding="utf-8") as f:
        for item in combined:
            f.write(json.dumps(item, ensure_ascii=False) + "\n")

    print(f"Generated {len(combined)} SFT samples:")
    print(f"  Positive: {len(pos_selected)}")
    print(f"  Negative: {len(neg_selected)}")
    print(f"  Saved to: {out_path}")


if __name__ == "__main__":
    main()