File size: 10,772 Bytes
9274a8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Manifest-driven QA: a stats block, then a contact sheet.

Run this immediately after the first extractor and before writing any more
code. A sign error or an axis-order mistake in the 3D->2D projection produces
plausible-looking garbage that stays invisible until the depth numbers make no
sense a day later.

What to look for in the sheet:
  1. boxes sit on the objects, not offset or mirrored
  2. box bottoms sit at the ground contact point for cones and barriers
  3. objects lower in the frame carry smaller distances than objects near the horizon
  4. stop-sign boxes do not include the pole
  5. nuScenes and AV2 frames look like the same kind of scene

Usage:
    python -m src.data.view_samples --manifest data/unified/manifest.parquet
    python -m src.data.view_samples --per-source 8 --classes cone barrier
    python -m src.data.view_samples --source nuscenes --only-annotated --out qa/nusc.png
    python -m src.data.view_samples --split val --classes cone
"""

from __future__ import annotations

import argparse
from pathlib import Path

import numpy as np
import pandas as pd
from PIL import Image, ImageDraw, ImageFont

from src.common import paths, schema

CLASS_COLOURS = {
    "cone": (255, 140, 0),
    "barrier": (60, 170, 255),
    "stop_sign": (80, 220, 120),
}
NEGATIVE_COLOUR = (150, 150, 150)


# ---------------------------------------------------------------------------
# Stats -- printed before anything is drawn
# ---------------------------------------------------------------------------


def print_stats(frame: pd.DataFrame) -> None:
    objects = schema.objects_only(frame)

    print("=" * 72)
    print(f"frames  : {frame['image_path'].nunique()}")
    print(f"objects : {len(objects)}")
    print(f"sensors : {sorted(frame['sensor_id'].dropna().unique())}")
    print("=" * 72)

    print("\ninstances per source x class")
    if objects.empty:
        print("  (none)")
    else:
        print(pd.crosstab(objects["source"], objects["class"]).to_string())

    print("\nbox height (px) per class")
    if objects.empty:
        print("  (none)")
    else:
        heights = (objects["y2"] - objects["y1"]).rename("height")
        print(
            heights.groupby(objects["class"])
            .describe(percentiles=[0.05, 0.25, 0.5, 0.75, 0.95])
            .to_string()
        )

    print("\ngt_distance_m per class")
    with_distance = objects[objects["gt_distance_m"].notna()]
    if with_distance.empty:
        print("  (none)")
    else:
        print(
            with_distance.groupby("class")["gt_distance_m"]
            .describe(percentiles=[0.05, 0.5, 0.95])
            .to_string()
        )

    print_warnings(frame, objects)


def print_warnings(frame: pd.DataFrame, objects: pd.DataFrame) -> None:
    warnings: list[str] = []

    if objects.empty:
        warnings.append("manifest contains no objects at all")

    if objects["gt_distance_m"].notna().sum() == 0:
        warnings.append(
            "NO gt_distance_m ANYWHERE. This manifest will train YOLO fine and "
            "leave phase 2 with nothing to score against."
        )

    non_positive = objects[objects["gt_distance_m"].notna() & (objects["gt_distance_m"] <= 0)]
    if len(non_positive):
        warnings.append(f"{len(non_positive)} objects with gt_distance_m <= 0")

    degenerate = objects[(objects["x2"] <= objects["x1"]) | (objects["y2"] <= objects["y1"])]
    if len(degenerate):
        warnings.append(f"{len(degenerate)} degenerate boxes (x2<=x1 or y2<=y1)")

    for class_name in schema.CLASSES:
        if class_name not in set(objects["class"]):
            warnings.append(f"class '{class_name}' has zero instances")

    if "split" in frame.columns and frame["split"].notna().any():
        val_cones = objects[
            (objects["split"] == "val")
            & (objects["class"] == "cone")
            & objects["gt_distance_m"].notna()
        ]
        if len(val_cones) < 200:
            warnings.append(
                f"val split has only {len(val_cones)} cone instances with distance "
                f"-- phase 2 wants at least a few hundred"
            )

    print("\nwarnings")
    if warnings:
        for warning in warnings:
            print(f"  !! {warning}")
    else:
        print("  none")
    print()


# ---------------------------------------------------------------------------
# Contact sheet
# ---------------------------------------------------------------------------


def choose_images(frame: pd.DataFrame, per_source: int, only_annotated: bool,
                  seed: int) -> list[str]:
    """Pick `per_source` image paths from each source."""
    candidates = frame
    if only_annotated:
        annotated = set(schema.objects_only(frame)["image_path"])
        candidates = frame[frame["image_path"].isin(annotated)]

    chosen: list[str] = []
    rng = np.random.default_rng(seed)
    for source in sorted(candidates["source"].unique()):
        image_paths = candidates[candidates["source"] == source]["image_path"].unique()
        if len(image_paths) == 0:
            continue
        count = min(per_source, len(image_paths))
        picked = rng.choice(image_paths, size=count, replace=False)
        chosen.extend(sorted(picked.tolist()))
    return chosen


def load_font(size: int) -> ImageFont.ImageFont:
    for candidate in (
        "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
    ):
        if Path(candidate).exists():
            return ImageFont.truetype(candidate, size)
    return ImageFont.load_default()


def render_cell(image_path: str, rows: pd.DataFrame, root: Path,
                cell_width: int) -> Image.Image | None:
    """Draw one image with its boxes, scaled to cell_width."""
    full_path = paths.resolve_image(root, image_path)
    try:
        image = Image.open(full_path).convert("RGB")
    except (FileNotFoundError, OSError) as error:
        print(f"  !! cannot open {full_path}: {error}")
        return None

    scale = cell_width / image.width
    image = image.resize((cell_width, max(1, round(image.height * scale))))
    draw = ImageDraw.Draw(image)
    font = load_font(max(12, cell_width // 45))

    objects = rows[rows["class"].notna()]
    for _, row in objects.iterrows():
        colour = CLASS_COLOURS.get(row["class"], NEGATIVE_COLOUR)
        box = [row["x1"] * scale, row["y1"] * scale, row["x2"] * scale, row["y2"] * scale]
        draw.rectangle(box, outline=colour, width=2)

        # Label exactly as the final output will: class then distance.
        label = row["class"]
        if pd.notna(row["gt_distance_m"]):
            label = f"{label} {row['gt_distance_m']:.1f}m"
        text_xy = (box[0] + 2, max(0.0, box[1] - font.size - 3))
        text_box = draw.textbbox(text_xy, label, font=font)
        draw.rectangle(text_box, fill=colour)
        draw.text(text_xy, label, fill=(0, 0, 0), font=font)

    caption = f"{image_path}  [{len(objects)} obj]"
    caption_box = draw.textbbox((4, 4), caption, font=font)
    draw.rectangle(caption_box, fill=(0, 0, 0))
    draw.text((4, 4), caption, fill=(255, 255, 255), font=font)
    return image


def tile(cells: list[Image.Image], columns: int, padding: int = 6) -> Image.Image:
    cell_width = max(cell.width for cell in cells)
    cell_height = max(cell.height for cell in cells)
    rows = (len(cells) + columns - 1) // columns

    sheet = Image.new(
        "RGB",
        (columns * cell_width + (columns + 1) * padding,
         rows * cell_height + (rows + 1) * padding),
        (25, 25, 25),
    )
    for index, cell in enumerate(cells):
        column, row = index % columns, index // columns
        sheet.paste(
            cell,
            (padding + column * (cell_width + padding),
             padding + row * (cell_height + padding)),
        )
    return sheet


# ---------------------------------------------------------------------------


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--manifest", type=Path, default=None,
                        help="defaults to <unified-root>/manifest.parquet")
    parser.add_argument("--unified-root", type=Path, default=None)
    parser.add_argument("--per-source", type=int, default=4)
    parser.add_argument("--classes", nargs="+", default=None,
                        help="keep only these classes, and only frames containing them")
    parser.add_argument("--source", default=None, help="restrict to one source")
    parser.add_argument("--split", default=None, help="restrict to train or val")
    parser.add_argument("--only-annotated", action="store_true",
                        help="never sample a frame with no objects")
    parser.add_argument("--out", type=Path, default=Path("qa/samples.png"))
    parser.add_argument("--cell-width", type=int, default=640)
    parser.add_argument("--cols", type=int, default=4)
    parser.add_argument("--seed", type=int, default=0)
    parser.add_argument("--stats-only", action="store_true")
    args = parser.parse_args()

    root = paths.unified_root(args.unified_root)
    manifest_path = args.manifest or (root / "manifest.parquet")
    frame = schema.read_manifest(manifest_path)
    print(f"loaded {manifest_path}  ({len(frame)} rows)\n")

    if args.source:
        frame = frame[frame["source"] == args.source]
    if args.split:
        frame = frame[frame["split"] == args.split]
    if args.classes:
        # Keep the requested classes, and only frames that contain one.
        keep = frame["class"].isin(args.classes)
        frame = frame[frame["image_path"].isin(frame[keep]["image_path"])]
        frame = frame[keep | frame["class"].isna()]

    if frame.empty:
        print("!! nothing matches those filters")
        return

    print_stats(frame)
    if args.stats_only:
        return

    image_paths = choose_images(frame, args.per_source, args.only_annotated, args.seed)
    if not image_paths:
        print("!! no images to draw")
        return

    by_image = {path: group for path, group in frame.groupby("image_path")}
    cells = []
    for image_path in image_paths:
        cell = render_cell(image_path, by_image[image_path], root, args.cell_width)
        if cell is not None:
            cells.append(cell)

    if not cells:
        print("!! every image failed to load -- check the images/<source> symlink")
        return

    sheet = tile(cells, args.cols)
    args.out.parent.mkdir(parents=True, exist_ok=True)
    sheet.save(args.out)
    print(f"wrote {args.out}  ({len(cells)} frames, {sheet.width}x{sheet.height})")


if __name__ == "__main__":
    main()