Canonical HTML: <https://hxrshit.dev/work/portable-machine-vision/>

[← back to work](https://hxrshit.dev/work/)

# portable machine vision

i designed and built this portable PCB inspection system end to end as a solo project. it detects missing chips and screw terminals on a rotating PCB.

throughput rose from **1.65 to 15.95 inspections/s** across [separate live runs](https://hxrshit.dev/work/portable-machine-vision/#performance). the compiled model reached **95.08% box-level precision** in a [historical replay](https://hxrshit.dev/work/portable-machine-vision/#qualification).

5 minute read

## system architecture and frame matching

the system checks a rotating printed circuit board (PCB) for missing chips and screw terminals. a single SiMa Modalix board runs the detector, and the host displays the video and inspection results. when the system cannot reliably inspect a view, the result is marked as **unknown**.

scroll sideways to read the full diagram

the camera stream splits on Modalix. compressed video is forwarded to the host while the hardware decoder feeds a latest-only inference worker. the host presents continuous video and joins inspection coordinates only to the source frame with a matching pixel hash.

fig. 1. video and inference run separately. the host displays an overlay only when it can match the result to the frame used for inference.

i forward the compressed camera stream to the host so that the live preview does not have to wait for inference. the inference worker processes the most recent decoded frame and discards older frames to avoid a backlog.

frame matching matters because the PCB is rotating. even a correct detection can appear over the wrong component if it is displayed on a later frame. the worker includes a hash of the decoded image with each result. the host uses this hash to find the corresponding frame in its cache. if there is no match, it does not display the overlay.

## debugging the accelerator input and output

in the first full-frame implementation, the detector returned plausible results, but the bounding box coordinates were incorrect. to debug this, i captured the input and output buffers of the accelerator. i replayed two captured inputs and reproduced their outputs byte for byte. that gave me a repeatable test without having to run the camera.

scroll sideways to read the full diagram

RGB input is quantized and packed in compiler physical tiles before the accelerator. the accelerator emits six packed heads. postprocessing reads scores before gathering box distributions, then maps boxes to camera space.

fig. 2. the logical tensor shape describes how the values are organized. the physical tile layout determines where those values are stored in memory. the runtime needs to account for both.

1.  **input packing:** the input producer used a tile shape of `[32,128,3]`, while the compiler expected `[1280,64,3]`. as a result, about 79% of the bytes were in the wrong memory locations. packing the pixels using the expected layout restored meaningful detection scores without changing the model weights.
2.  **output decoding:** one bounding box head had 160 logical rows, but each physical tile contained only two rows. after correcting the tile dimensions, eight native detections agreed with an independent decode to within 0.473 pixels.

i updated the runtime to derive the tensor layout from compiler metadata and validate it before executing the graph. this prevents the same type of layout mismatch from reaching the accelerator.

model export and INT8 details

i exported YOLO11n with a 1280 × 1280 input and six raw ONNX output heads: bounding boxes and classes at grid sizes of 160, 80 and 40. the box outputs use 16 distribution bins per side. i moved bounding box decoding outside the graph after my custom in-graph implementations ran below 1 fps. these tests did not cover the complete vendor-optimized pipeline.

even with the correct tensor layout, INT8 and FP32 outputs can differ. in an earlier build, two of the coarser classification heads could not represent a sigmoid score above 0.5. in a later replay test of the crop model, a missing-terminal prediction was lost after compilation.

## PCB localization and crop-based inference

i use the color of the blue substrate to locate the PCB and nearby edges to refine its outline. the inference worker applies a geometric transform to the selected crop, runs the detector and maps the bounding boxes back to the original camera coordinates. it can skip inspection if two candidate regions are plausible or if the crop extends beyond the image.

the original model did not work reliably on the enlarged crops because cropping changed the apparent size of the components. i trained a separate model for cropped images and explicitly labelled empty component footprints. this is important because failing to detect a component does not establish that it is missing.

training, calibration and pose tracking

i reviewed 90 source views, generated 450 training images and trained the model for 100 epochs. across three FP32 export comparisons, the maximum coordinate difference was approximately 0.000244 pixels in model input coordinates. for INT8 calibration, i used only the 90 original training views, excluding augmented images and evaluation data.

between full localization passes, the system tracks PCB corners using pyramidal Lucas–Kanade optical flow. forward-backward consistency checks and RANSAC remove unreliable point matches. the system reuses only the board geometry, not previous component inspection results, and checks its age, motion and appearance before reusing it.

## profiling and improving throughput

model inference took about 4 ms. profiling pointed me to the CPU work before and after inference. i made three changes to reduce that processing overhead without changing the detector:

*   **direct input packing:** i use an eight-thread C++ kernel with a byte lookup table to reorder, quantize and pack pixels without allocating an intermediate normalized image.
*   **score filtering:** the decoder reads class scores first and retrieves bounding box logits only for candidates above the existing confidence threshold.
*   **restrict the corner search:** the tracker initializes points within the bounds of the PCB mask, with an additional eight-pixel margin. on 31 recorded frames, this produced the same points in the same order and reduced corner initialization time from 81.05 ms to 2.46 ms. full localization still processes the entire image.

measured throughput
| trial | rate |
| --- | --- |
| crop baseline · all iterations located | 1.65/s |
| optimized live run · 974 model calls | **15.95/s** |
| same run · host-decoded video | 30.03 fps |

the host found a matching source frame for all 974 results when they arrived. the baseline and optimized measurements came from separate, sequential live runs rather than a benchmark using identical frames. video frame rate and model throughput are separate measurements. neither measures how long it takes for a captured image to appear on screen.

## model evaluation and remaining validation

the compiled model recorded **95.08% precision and 93.55% recall** against 62 reviewed bounding boxes across 31 previously recorded views where localization succeeded. these metrics describe box-level detection performance, not the accuracy of factory acceptance decisions. localization failed to produce a crop for one of the 32 views. i also observed unwanted present-class candidates in a separate live trial.

replay counts and deployment checks

historical crop replay · match IoU 0.50
| path | true positives | false positives | false negatives |
| --- | --- | --- | --- |
| FP32 | 59 | 3 | 3 |
| compiled | 58 | 3 | 4 |

for this replay, i used a confidence threshold of 0.05 and a non-maximum suppression (NMS) IoU threshold of 0.45. the source recordings overlap with data used during development, so this is not an independent evaluation. further validation needs independent specimens under different lighting and rotation conditions, plus a policy for translating individual detections into part-level decisions.

the release uses a fixed combination of the model, library and runtime, and checks their identities before acquiring the shared decoder. offline host dependencies are provided for Windows and Apple Silicon. in a guarded continuous test lasting 335 seconds, i recorded 6,387 exact frame matches with no reported RTP gaps or host errors. this was a short continuous test, not a long-duration reliability test.

one design choice i would retain is the ability to trace each inspection result back to its source image, including the pixels displayed in the browser and the input bytes used by the accelerator. it makes both inference debugging and inspection result verification easier.

[↑ on this page](https://hxrshit.dev/work/portable-machine-vision/#contents) · [← back to work](https://hxrshit.dev/work/)
