Object Detection
LiteRT
LiteRT
vision
torchvision
File size: 13,644 Bytes
911628b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
377
#!/usr/bin/env python3
"""Inference-only sample for Faster R-CNN split across three LiteRT models.

This sample expects prebuilt TFLite files. It does not convert or compile
models. LiteRT runs the tensor-heavy submodels on CPU, while TorchVision host
code handles model-specific preprocessing, FPN, proposal decode/NMS, ROIAlign,
and final postprocessing.
"""

from __future__ import annotations

import argparse
import io
import math
from collections import OrderedDict
from pathlib import Path
import urllib.request

import numpy as np
from PIL import Image, ImageDraw
import torch
from torchvision.models.detection import (
    FasterRCNN_ResNet50_FPN_Weights,
    fasterrcnn_resnet50_fpn,
)
from torchvision.models.detection.rpn import concat_box_prediction_layers
from torchvision.transforms.functional import pil_to_tensor, to_pil_image

from ai_edge_litert.compiled_model import CompiledModel
from ai_edge_litert.hardware_accelerator import HardwareAccelerator


DEFAULT_IMAGE = "https://github.com/pytorch/hub/raw/master/images/dog.jpg"
SCRIPT_DIR = Path(__file__).resolve().parent
DEFAULT_BACKBONE_MODEL = SCRIPT_DIR / "fasterrcnn_resnet50_fpn_backbone_body_dynamic_hw.tflite"
DEFAULT_RPN_HEAD_MODEL = SCRIPT_DIR / "fasterrcnn_resnet50_fpn_rpn_head_dynamic_hw.tflite"
DEFAULT_ROI_MODEL = SCRIPT_DIR / "fasterrcnn_resnet50_fpn_roi_box_dynamic_n.tflite"


def _format_size(path: Path) -> str:
    return f"{path.stat().st_size / (1024.0 * 1024.0):.1f} MiB"


def _categories() -> list[str]:
    meta = FasterRCNN_ResNet50_FPN_Weights.DEFAULT.meta
    return [str(name) for name in meta.get("categories", [])]


def _load_model() -> torch.nn.Module:
    model = fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)
    model.eval()
    return model


def _load_image(spec: str) -> torch.Tensor:
    if spec == "synthetic":
        y = torch.linspace(0.0, 1.0, 640, dtype=torch.float32).view(1, 640, 1)
        x = torch.linspace(0.0, 1.0, 960, dtype=torch.float32).view(1, 1, 960)
        red = y.expand(1, 640, 960)
        green = x.expand(1, 640, 960)
        blue = (1.0 - red * 0.5 - green * 0.5).clamp(0.0, 1.0)
        return torch.cat([red, green, blue], dim=0)

    if spec.startswith("http://") or spec.startswith("https://"):
        with urllib.request.urlopen(spec, timeout=30) as resp:
            image = Image.open(io.BytesIO(resp.read())).convert("RGB")
    else:
        image = Image.open(spec).convert("RGB")
    return pil_to_tensor(image).to(torch.float32) / 255.0


def _require_models(paths: list[Path]) -> None:
    missing = [path for path in paths if not path.exists()]
    if not missing:
        return
    formatted = "\n".join(f"  - {path}" for path in missing)
    raise FileNotFoundError(
        "Missing prebuilt LiteRT model file(s):\n"
        f"{formatted}\n"
        "Run the conversion pipeline separately, then rerun this inference sample."
    )


def _div_floor(value: int, divisor: int) -> int:
    return int(math.floor(value / divisor))


def _backbone_output_shapes(input_shape: tuple[int, ...]) -> list[tuple[int, ...]]:
    _, _, height, width = [int(v) for v in input_shape]
    return [
        (1, 256, _div_floor(height, 4), _div_floor(width, 4)),
        (1, 512, _div_floor(height, 8), _div_floor(width, 8)),
        (1, 1024, _div_floor(height, 16), _div_floor(width, 16)),
        (1, 2048, _div_floor(height, 32), _div_floor(width, 32)),
    ]


def _rpn_output_shapes(feature_shape: torch.Size) -> list[tuple[int, ...]]:
    _, _, height, width = [int(v) for v in feature_shape]
    return [
        (1, 3, height, width),
        (1, 12, height, width),
    ]


def _pick_dtype(requirements: dict) -> np.dtype:
    types = requirements.get("supported_types") or []
    if 1 in types or "FLOAT32" in types:
        return np.float32
    if types == [4] or types == ["INT64"]:
        return np.int64
    if types == [2] or types == ["INT32"]:
        return np.int32
    raise ValueError(f"Unsupported LiteRT buffer types: {types}")


def _read_buffer(buffer: object, dtype: np.dtype, shape: tuple[int, ...]) -> np.ndarray:
    count = int(np.prod(shape))
    data = buffer.read(count, dtype)
    return np.asarray(data, dtype=dtype).reshape(shape)


def _run_litert_model(
    model_path: Path,
    input_array: np.ndarray,
    output_shapes: list[tuple[int, ...]],
) -> list[np.ndarray]:
    compiled = CompiledModel.from_file(
        str(model_path),
        hardware_accel=HardwareAccelerator.CPU,
    )
    compiled.resize_input_tensor_by_name(
        "main",
        "x",
        tuple(int(v) for v in input_array.shape),
        strict=True,
    )

    input_buffers = compiled.create_input_buffers(0)
    output_buffers = compiled.create_output_buffers(0)

    input_req = compiled.get_input_buffer_requirements(0, 0)
    input_dtype = _pick_dtype(input_req)
    input_buffers[0].write(np.asarray(input_array, dtype=input_dtype).reshape(-1))

    compiled.run_by_index(0, input_buffers, output_buffers)

    outputs: list[np.ndarray] = []
    for i, shape in enumerate(output_shapes):
        req = compiled.get_output_buffer_requirements(i, 0)
        dtype = _pick_dtype(req)
        expected_bytes = int(np.prod(shape)) * np.dtype(dtype).itemsize
        if int(req.get("buffer_size", 0)) < expected_bytes:
            raise RuntimeError(
                f"{model_path} output {i} buffer is too small: "
                f"{req.get('buffer_size')} < {expected_bytes} for shape {shape}"
            )
        outputs.append(_read_buffer(output_buffers[i], dtype, shape))
    return outputs


def _run_rpn_head(
    model_path: Path,
    fpn_features: OrderedDict[str, torch.Tensor],
) -> tuple[list[torch.Tensor], list[torch.Tensor]]:
    objectness: list[torch.Tensor] = []
    bbox_deltas: list[torch.Tensor] = []
    for feature in fpn_features.values():
        outputs = _run_litert_model(
            model_path,
            feature.detach().cpu().numpy(),
            _rpn_output_shapes(feature.shape),
        )
        objectness.append(torch.from_numpy(outputs[0]).to(dtype=feature.dtype))
        bbox_deltas.append(torch.from_numpy(outputs[1]).to(dtype=feature.dtype))
    return objectness, bbox_deltas


def _rpn_proposals_from_head_outputs(
    model: torch.nn.Module,
    images: object,
    fpn_features: OrderedDict[str, torch.Tensor],
    objectness: list[torch.Tensor],
    pred_bbox_deltas: list[torch.Tensor],
) -> list[torch.Tensor]:
    feature_list = list(fpn_features.values())
    anchors = model.rpn.anchor_generator(images, feature_list)
    num_images = len(anchors)
    num_anchors_per_level = [
        int(shape[0] * shape[1] * shape[2]) for shape in [o[0].shape for o in objectness]
    ]
    objectness_flat, pred_bbox_deltas_flat = concat_box_prediction_layers(
        objectness,
        pred_bbox_deltas,
    )
    proposals = model.rpn.box_coder.decode(pred_bbox_deltas_flat.detach(), anchors)
    proposals = proposals.view(num_images, -1, 4)
    boxes, _ = model.rpn.filter_proposals(
        proposals,
        objectness_flat,
        images.image_sizes,
        num_anchors_per_level,
    )
    return boxes


def _print_detections(
    detection: dict[str, torch.Tensor],
    *,
    topk: int,
    score_threshold: float,
    categories: list[str],
) -> None:
    scores = detection["scores"].detach().cpu()
    labels = detection["labels"].detach().cpu()
    boxes = detection["boxes"].detach().cpu()
    visible = [i for i in range(min(topk, int(scores.numel()))) if float(scores[i]) >= score_threshold]
    print(f"detections above {score_threshold:.2f}: {len(visible)}")
    for rank, i in enumerate(visible, start=1):
        label_id = int(labels[i])
        name = categories[label_id] if 0 <= label_id < len(categories) else str(label_id)
        box = [round(float(v), 2) for v in boxes[i].tolist()]
        print(f"  {rank:02d}: {name} score={float(scores[i]):.4f} box={box}")


def _save_annotated_image(
    *,
    image: torch.Tensor,
    detection: dict[str, torch.Tensor],
    out_path: Path,
    topk: int,
    score_threshold: float,
    categories: list[str],
) -> None:
    pil = to_pil_image(image.detach().cpu().clamp(0.0, 1.0))
    draw = ImageDraw.Draw(pil)
    scores = detection["scores"].detach().cpu()
    labels = detection["labels"].detach().cpu()
    boxes = detection["boxes"].detach().cpu()
    for rank in range(min(topk, int(scores.numel()))):
        score = float(scores[rank])
        if score < score_threshold:
            continue
        label_id = int(labels[rank])
        name = categories[label_id] if 0 <= label_id < len(categories) else str(label_id)
        x0, y0, x1, y1 = [float(v) for v in boxes[rank].tolist()]
        draw.rectangle((x0, y0, x1, y1), outline="red", width=3)
        draw.text((x0 + 3, y0 + 3), f"{name} {score:.2f}", fill="red")
    out_path.parent.mkdir(parents=True, exist_ok=True)
    pil.save(out_path)


def run_sample(args: argparse.Namespace) -> int:
    torch.set_grad_enabled(False)
    model_paths = [args.backbone_model, args.rpn_head_model, args.roi_model]
    _require_models(model_paths)

    model = _load_model()
    image = _load_image(args.image)
    original_sizes = [(int(image.shape[-2]), int(image.shape[-1]))]
    images, _ = model.transform([image], None)
    image_tensor = images.tensors

    print("LiteRT models:")
    print(f"  backbone body: {args.backbone_model} ({_format_size(args.backbone_model)})")
    print(f"  RPN head:      {args.rpn_head_model} ({_format_size(args.rpn_head_model)})")
    print(f"  ROI box head:  {args.roi_model} ({_format_size(args.roi_model)})")
    print(f"input image: original={original_sizes[0]} transformed={tuple(int(v) for v in image_tensor.shape)}")

    body_arrays = _run_litert_model(
        args.backbone_model,
        image_tensor.detach().cpu().numpy(),
        _backbone_output_shapes(tuple(int(v) for v in image_tensor.shape)),
    )
    body_features = OrderedDict(
        (str(i), torch.from_numpy(array).to(dtype=image_tensor.dtype))
        for i, array in enumerate(body_arrays)
    )
    print("backbone body LiteRT outputs:")
    for key, value in body_features.items():
        print(f"  C{int(key) + 2}: {tuple(int(v) for v in value.shape)}")

    fpn_features = model.backbone.fpn(body_features)
    objectness, pred_bbox_deltas = _run_rpn_head(args.rpn_head_model, fpn_features)
    print("RPN head LiteRT outputs:")
    for i, (obj, bbox) in enumerate(zip(objectness, pred_bbox_deltas)):
        print(f"  P{i + 2}: objectness={tuple(int(v) for v in obj.shape)} bbox={tuple(int(v) for v in bbox.shape)}")

    proposals = _rpn_proposals_from_head_outputs(
        model,
        images,
        fpn_features,
        objectness,
        pred_bbox_deltas,
    )
    proposal_count = int(proposals[0].shape[0])
    print(f"host proposal decode/NMS: {proposal_count} proposals")
    if proposal_count == 0:
        print("no proposals; skipping ROI stage")
        return 0

    roi_features = model.roi_heads.box_roi_pool(
        fpn_features,
        proposals,
        images.image_sizes,
    )
    roi_arrays = _run_litert_model(
        args.roi_model,
        roi_features.detach().cpu().numpy(),
        [(proposal_count, 91), (proposal_count, 364)],
    )
    class_logits = torch.from_numpy(roi_arrays[0]).to(dtype=roi_features.dtype)
    box_regression = torch.from_numpy(roi_arrays[1]).to(dtype=roi_features.dtype)
    print(
        "ROI LiteRT outputs: "
        f"logits={tuple(int(v) for v in class_logits.shape)} "
        f"box_regression={tuple(int(v) for v in box_regression.shape)}"
    )

    boxes, scores, labels = model.roi_heads.postprocess_detections(
        class_logits,
        box_regression,
        proposals,
        images.image_sizes,
    )
    detections = [{"boxes": boxes[0], "scores": scores[0], "labels": labels[0]}]
    detections = model.transform.postprocess(detections, images.image_sizes, original_sizes)
    detection = detections[0]
    categories = _categories()
    _print_detections(
        detection,
        topk=args.topk,
        score_threshold=args.score_threshold,
        categories=categories,
    )

    if args.annotated_out:
        _save_annotated_image(
            image=image,
            detection=detection,
            out_path=args.annotated_out,
            topk=args.topk,
            score_threshold=args.score_threshold,
            categories=categories,
        )
        print(f"annotated image: {args.annotated_out}")
    return 0


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--image",
        default=DEFAULT_IMAGE,
        help="Image path, URL, or 'synthetic'.",
    )
    parser.add_argument("--backbone-model", type=Path, default=DEFAULT_BACKBONE_MODEL)
    parser.add_argument("--rpn-head-model", type=Path, default=DEFAULT_RPN_HEAD_MODEL)
    parser.add_argument("--roi-model", type=Path, default=DEFAULT_ROI_MODEL)
    parser.add_argument("--topk", type=int, default=5)
    parser.add_argument("--score-threshold", type=float, default=0.5)
    parser.add_argument(
        "--annotated-out",
        default="fasterrcnn_litert_cpu_sample.jpg",
        help="Optional output image with drawn detections. Pass '' to disable.",
    )
    args = parser.parse_args()
    if args.annotated_out is not None and str(args.annotated_out).strip() == "":
        args.annotated_out = None
    elif args.annotated_out is not None:
        args.annotated_out = Path(args.annotated_out)
    return args


if __name__ == "__main__":
    raise SystemExit(run_sample(parse_args()))