File size: 12,846 Bytes
61d88db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# /// script
# requires-python = ">=3.11"
# dependencies = [
#     "datasets>=3.1.0",
#     "huggingface-hub",
#     "tqdm",
#     "toolz",
#     "Pillow", 
# ]
# ///

"""
Convert bounding box formats in a Hugging Face object detection dataset.

Mirrors panlabel's convert command. Converts between:
  - COCO xywh: [x, y, width, height] in pixels
  - XYXY: [xmin, ymin, xmax, ymax] in pixels
  - VOC: [xmin, ymin, xmax, ymax] in pixels (alias for xyxy)
  - YOLO: [center_x, center_y, width, height] normalized 0-1
  - TFOD: [xmin, ymin, xmax, ymax] normalized 0-1
  - Label Studio: [x, y, width, height] percentage 0-100

Reads from HF Hub, converts bboxes in-place, and pushes the result to a new
(or the same) dataset repo on HF Hub.

Examples:
  uv run convert-hf-dataset.py merve/coco-dataset merve/coco-xyxy --from coco_xywh --to xyxy
  uv run convert-hf-dataset.py merve/yolo-dataset merve/yolo-coco --from yolo --to coco_xywh
  uv run convert-hf-dataset.py merve/dataset merve/converted --from xyxy --to yolo --max-samples 100
  uv run convert-hf-dataset.py merve/dataset merve/converted --from tfod --to coco_xywh
  uv run convert-hf-dataset.py merve/dataset merve/converted --from label_studio --to xyxy
"""

import argparse
import json
import logging
import os
import sys
import time
from datetime import datetime
from typing import Any

from datasets import load_dataset
from huggingface_hub import DatasetCard, login
from toolz import partition_all
from tqdm.auto import tqdm

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

BBOX_FORMATS = ["coco_xywh", "xyxy", "voc", "yolo", "tfod", "label_studio"]


def convert_bbox(
    bbox: list[float],
    from_fmt: str,
    to_fmt: str,
    img_w: float = 1.0,
    img_h: float = 1.0,
) -> list[float]:
    """Convert a single bbox between formats via XYXY pixel-space intermediate."""
    # Step 1: to XYXY pixel space
    if from_fmt == "coco_xywh":
        x, y, w, h = bbox[:4]
        xmin, ymin, xmax, ymax = x, y, x + w, y + h
    elif from_fmt in ("xyxy", "voc"):
        xmin, ymin, xmax, ymax = bbox[:4]
    elif from_fmt == "yolo":
        cx, cy, w, h = bbox[:4]
        xmin = (cx - w / 2) * img_w
        ymin = (cy - h / 2) * img_h
        xmax = (cx + w / 2) * img_w
        ymax = (cy + h / 2) * img_h
    elif from_fmt == "tfod":
        xmin_n, ymin_n, xmax_n, ymax_n = bbox[:4]
        xmin = xmin_n * img_w
        ymin = ymin_n * img_h
        xmax = xmax_n * img_w
        ymax = ymax_n * img_h
    elif from_fmt == "label_studio":
        x_pct, y_pct, w_pct, h_pct = bbox[:4]
        xmin = x_pct / 100.0 * img_w
        ymin = y_pct / 100.0 * img_h
        xmax = (x_pct + w_pct) / 100.0 * img_w
        ymax = (y_pct + h_pct) / 100.0 * img_h
    else:
        raise ValueError(f"Unknown source format: {from_fmt}")

    # Step 2: from XYXY pixel space to target
    if to_fmt in ("xyxy", "voc"):
        return [xmin, ymin, xmax, ymax]
    elif to_fmt == "coco_xywh":
        return [xmin, ymin, xmax - xmin, ymax - ymin]
    elif to_fmt == "yolo":
        if img_w <= 0 or img_h <= 0:
            raise ValueError("YOLO format requires positive image dimensions")
        w = xmax - xmin
        h = ymax - ymin
        cx = (xmin + w / 2) / img_w
        cy = (ymin + h / 2) / img_h
        return [cx, cy, w / img_w, h / img_h]
    elif to_fmt == "tfod":
        if img_w <= 0 or img_h <= 0:
            raise ValueError("TFOD format requires positive image dimensions")
        return [xmin / img_w, ymin / img_h, xmax / img_w, ymax / img_h]
    elif to_fmt == "label_studio":
        if img_w <= 0 or img_h <= 0:
            raise ValueError("Label Studio format requires positive image dimensions")
        x_pct = xmin / img_w * 100.0
        y_pct = ymin / img_h * 100.0
        w_pct = (xmax - xmin) / img_w * 100.0
        h_pct = (ymax - ymin) / img_h * 100.0
        return [x_pct, y_pct, w_pct, h_pct]
    else:
        raise ValueError(f"Unknown target format: {to_fmt}")


def convert_example(
    example: dict[str, Any],
    bbox_column: str,
    from_fmt: str,
    to_fmt: str,
    width_column: str | None,
    height_column: str | None,
) -> dict[str, Any]:
    """Convert bboxes in a single example."""
    objects = example.get("objects")
    is_nested = objects is not None and isinstance(objects, dict)

    if is_nested:
        bboxes = objects.get(bbox_column, []) or []
    else:
        bboxes = example.get(bbox_column, []) or []

    img_w = 1.0
    img_h = 1.0
    if width_column:
        img_w = float(example.get(width_column, 1.0) or 1.0)
    if height_column:
        img_h = float(example.get(height_column, 1.0) or 1.0)

    converted = []
    for bbox in bboxes:
        if bbox is None or len(bbox) < 4:
            converted.append(bbox)
            continue
        converted.append(convert_bbox(bbox, from_fmt, to_fmt, img_w, img_h))

    if is_nested:
        new_objects = dict(objects)
        new_objects[bbox_column] = converted
        return {"objects": new_objects}
    else:
        return {bbox_column: converted}


def create_dataset_card(
    source_dataset: str,
    output_dataset: str,
    from_fmt: str,
    to_fmt: str,
    num_samples: int,
    processing_time: str,
    split: str,
) -> str:
    return f"""---
tags:
- object-detection
- bbox-conversion
- panlabel
- uv-script
- generated
---

# Bbox Format Conversion: {from_fmt} -> {to_fmt}

Bounding boxes converted from `{from_fmt}` to `{to_fmt}` format.

## Processing Details

- **Source Dataset**: [{source_dataset}](https://huggingface.co/datasets/{source_dataset})
- **Conversion**: `{from_fmt}` -> `{to_fmt}`
- **Number of Samples**: {num_samples:,}
- **Processing Time**: {processing_time}
- **Processing Date**: {datetime.now().strftime("%Y-%m-%d %H:%M UTC")}
- **Split**: `{split}`

## Bbox Formats

| Format | Description |
|--------|-------------|
| `coco_xywh` | `[x, y, width, height]` in pixels |
| `xyxy` | `[xmin, ymin, xmax, ymax]` in pixels |
| `voc` | `[xmin, ymin, xmax, ymax]` in pixels (alias for xyxy) |
| `yolo` | `[center_x, center_y, width, height]` normalized 0-1 |
| `tfod` | `[xmin, ymin, xmax, ymax]` normalized 0-1 |
| `label_studio` | `[x, y, width, height]` percentage 0-100 |

## Reproduction

```bash
uv run convert-hf-dataset.py {source_dataset} {output_dataset} --from {from_fmt} --to {to_fmt}
```

Generated with panlabel-hf (convert-hf-dataset.py)
"""


def main(
    input_dataset: str,
    output_dataset: str,
    from_fmt: str,
    to_fmt: str,
    bbox_column: str = "bbox",
    width_column: str | None = "width",
    height_column: str | None = "height",
    split: str = "train",
    max_samples: int | None = None,
    batch_size: int = 1000,
    hf_token: str | None = None,
    private: bool = False,
    create_pr: bool = False,
    shuffle: bool = False,
    seed: int = 42,
):
    """Convert bbox format in a HF dataset and push to Hub."""

    if from_fmt == to_fmt:
        logger.error(f"Source and target formats are the same: {from_fmt}")
        sys.exit(1)

    start_time = datetime.now()

    HF_TOKEN = hf_token or os.environ.get("HF_TOKEN")
    if HF_TOKEN:
        login(token=HF_TOKEN)

    logger.info(f"Loading dataset: {input_dataset} (split={split})")
    dataset = load_dataset(input_dataset, split=split)

    if shuffle:
        logger.info(f"Shuffling dataset with seed {seed}")
        dataset = dataset.shuffle(seed=seed)

    if max_samples:
        dataset = dataset.select(range(min(max_samples, len(dataset))))
        logger.info(f"Limited to {len(dataset)} samples")

    total_samples = len(dataset)
    logger.info(f"Converting {total_samples:,} samples: {from_fmt} -> {to_fmt}")

    # Convert using map
    dataset = dataset.map(
        lambda example: convert_example(
            example, bbox_column, from_fmt, to_fmt, width_column, height_column
        ),
        desc=f"Converting {from_fmt} -> {to_fmt}",
    )

    processing_duration = datetime.now() - start_time
    processing_time_str = f"{processing_duration.total_seconds():.1f}s"

    # Add conversion metadata
    conversion_info = json.dumps({
        "source_format": from_fmt,
        "target_format": to_fmt,
        "source_dataset": input_dataset,
        "timestamp": datetime.now().isoformat(),
        "script": "convert-hf-dataset.py",
    })

    if "conversion_info" not in dataset.column_names:
        dataset = dataset.add_column(
            "conversion_info", [conversion_info] * len(dataset)
        )

    # Push to Hub
    logger.info(f"Pushing to {output_dataset}")
    max_retries = 3
    for attempt in range(1, max_retries + 1):
        try:
            if attempt > 1:
                logger.warning("Disabling XET (fallback to HTTP upload)")
                os.environ["HF_HUB_DISABLE_XET"] = "1"
            dataset.push_to_hub(
                output_dataset,
                private=private,
                token=HF_TOKEN,
                max_shard_size="500MB",
                create_pr=create_pr,
            )
            break
        except Exception as e:
            logger.error(f"Upload attempt {attempt}/{max_retries} failed: {e}")
            if attempt < max_retries:
                delay = 30 * (2 ** (attempt - 1))
                logger.info(f"Retrying in {delay}s...")
                time.sleep(delay)
            else:
                logger.error("All upload attempts failed.")
                sys.exit(1)

    # Push dataset card
    card_content = create_dataset_card(
        source_dataset=input_dataset,
        output_dataset=output_dataset,
        from_fmt=from_fmt,
        to_fmt=to_fmt,
        num_samples=total_samples,
        processing_time=processing_time_str,
        split=split,
    )
    card = DatasetCard(card_content)
    card.push_to_hub(output_dataset, token=HF_TOKEN)

    logger.info("Done!")
    logger.info(f"Dataset: https://huggingface.co/datasets/{output_dataset}")
    logger.info(f"Converted {total_samples:,} samples in {processing_time_str}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Convert bbox formats in a HF object detection dataset",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Bbox formats:
  coco_xywh     [x, y, width, height] in pixels
  xyxy          [xmin, ymin, xmax, ymax] in pixels
  voc           [xmin, ymin, xmax, ymax] in pixels (alias for xyxy)
  yolo          [cx, cy, w, h] normalized 0-1
  tfod          [xmin, ymin, xmax, ymax] normalized 0-1
  label_studio  [x, y, w, h] percentage 0-100

Examples:
  uv run convert-hf-dataset.py merve/coco merve/coco-xyxy --from coco_xywh --to xyxy
  uv run convert-hf-dataset.py merve/yolo merve/yolo-coco --from yolo --to coco_xywh
  uv run convert-hf-dataset.py merve/tfod merve/tfod-coco --from tfod --to coco_xywh
        """,
    )

    parser.add_argument("input_dataset", help="Input dataset ID on HF Hub")
    parser.add_argument("output_dataset", help="Output dataset ID on HF Hub")
    parser.add_argument("--from", dest="from_fmt", required=True, choices=BBOX_FORMATS, help="Source bbox format")
    parser.add_argument("--to", dest="to_fmt", required=True, choices=BBOX_FORMATS, help="Target bbox format")
    parser.add_argument("--bbox-column", default="bbox", help="Column containing bboxes (default: bbox)")
    parser.add_argument("--width-column", default="width", help="Column for image width (default: width)")
    parser.add_argument("--height-column", default="height", help="Column for image height (default: height)")
    parser.add_argument("--split", default="train", help="Dataset split (default: train)")
    parser.add_argument("--max-samples", type=int, help="Max samples to process")
    parser.add_argument("--batch-size", type=int, default=1000, help="Batch size for map (default: 1000)")
    parser.add_argument("--hf-token", help="HF API token")
    parser.add_argument("--private", action="store_true", help="Make output dataset private")
    parser.add_argument("--create-pr", action="store_true", help="Create PR instead of direct push")
    parser.add_argument("--shuffle", action="store_true", help="Shuffle dataset before processing")
    parser.add_argument("--seed", type=int, default=42, help="Random seed (default: 42)")

    args = parser.parse_args()

    main(
        input_dataset=args.input_dataset,
        output_dataset=args.output_dataset,
        from_fmt=args.from_fmt,
        to_fmt=args.to_fmt,
        bbox_column=args.bbox_column,
        width_column=args.width_column,
        height_column=args.height_column,
        split=args.split,
        max_samples=args.max_samples,
        batch_size=args.batch_size,
        hf_token=args.hf_token,
        private=args.private,
        create_pr=args.create_pr,
        shuffle=args.shuffle,
        seed=args.seed,
    )