--- license: apache-2.0 library_name: executorch pipeline_tag: keypoint-detection language: - en tags: - pose-estimation - keypoint-detection - executorch - zephyr - microcontroller - tinyml - cmsis-nn - int8 - quantized - esp32-s3 - coco --- # PoseMicro Single-person pose for microcontrollers: a 96 x 96 picture goes in and the 17 COCO keypoints come out, as an [ExecuTorch](https://github.com/pytorch/executorch) program small enough for a board that runs [Zephyr](https://zephyrproject.org). It comes with the training code, the export, and a Zephyr application for the Seeed Studio XIAO ESP32S3 Sense that finds the pose in camera frames. ![PoseMicro on validation crops](assets/samples.jpg) *The fp32 model on COCO val2017 crops, taken in a fixed order and not chosen for how they came out. The person's left is blue, right is red. Sources: [assets/CREDITS.md](assets/CREDITS.md).* **38,067 parameters, 6.97 million multiply-adds a picture.** | | fp32 | int8 | |---|---|---| | Program | `firmware/model/pose_micro.pte` | `firmware/model/pose_micro_cmsis_nn.pte` | | Kernels | ExecuTorch portable | CMSIS-NN, plain C path | | Flash | 165,708 bytes | 89,932 bytes | | Working memory the runtime plans | 294,912 bytes | 64,624 bytes | | Operators | convolution, relu | quantized_conv2d, quantized_depthwise_conv2d | | Picture goes in as | float, 0 to 1, one plane a channel | int8, pixel minus 128, channels together | Scored on 4,604 single-person crops of COCO val2017: | | PCK@0.1 | PCK@0.2 | OKS | Crops with OKS of 0.5 or more | Mean error, of 96 px | |---|---|---|---|---|---| | Average pose, whatever the picture | 32.0% | 69.1% | 0.214 | 1.9% | 13.2 px | | PoseMicro fp32 | 63.1% | 81.1% | 0.473 | 48.2% | 9.2 px | | PoseMicro int8 | 62.6% | 80.9% | 0.466 | 46.6% | 9.3 px | The int8 row is the int8 program itself, run on a PC on the kernels the firmware builds: ExecuTorch's Cortex-M operators on CMSIS-NN in plain C (`firmware/tests/host_int8`). Its output values are 0.7 steps of the int8 scale from the quantised model in PyTorch on average and 10 at most; the integer kernels round differently and the layers add up. In PyTorch the quantised model scores PCK@0.1 of 62.6% and OKS of 0.468. PCK@0.2 of the fp32 model by part of the body: head 92%, shoulders 87%, elbows 74%, wrists 64%, hips 82%, knees 78%, ankles 76%. These weights were trained for 60 minutes on 2 CPU threads: 25.5 passes over 111,658 crops. How to read the scores: - **PCK@0.1 and PCK@0.2**: the share of labelled keypoints placed within 10% and 20% of the longer side of the person's box. - **OKS**: COCO's object keypoint similarity, averaged over the crops. - **The first row is the number to beat.** People in these crops are mostly upright and centred, so answering the same average pose for every picture already scores that. PCK@0.2 flatters; read PCK@0.1 and OKS. - Each crop holds one person, framed by that person's annotated box. This is not COCO's keypoint AP, which also has to find the people. ## What it is for - One person, upright, filling most of a square picture: someone standing in front of a board's camera. - Coarse pose: which way the arms and legs go, not where a fingertip is. - Boards with a few hundred kilobytes of memory, where the usual pose models, megabytes of weights, do not fit. It does not find people and it does not handle a crowd. At this size it is coarse, and weakest at the arms: see the scores by part of the body above. Check the heat of a keypoint (0.3 and over counts as found) before using it. ## The network A stack of plain and depthwise convolutions with ReLU, nothing else, so it runs on the same kernels as any small CNN: fp32 on ExecuTorch's portable kernels, int8 on CMSIS-NN through ExecuTorch's Cortex-M backend. It reduces the picture by eight and answers on a 12 x 12 grid, 51 numbers a cell: the heat of each keypoint, then where in the cell it is, across and down. Decoding is one argmax per keypoint and two additions (`firmware/src/pose_decode.h`, `decode()` in `train/pose_model.py`). The int8 program takes the picture as int8 and answers in int8: the firmware hands over camera bytes, and only the 51 numbers the decoder reads are turned back into floats. ## What has been checked The GitHub workflow does this on every push, and nothing is published unless it all passes: - **The fp32 program** runs on ExecuTorch's Python runtime and must agree with PyTorch. - **The int8 program** runs on every validation crop on the kernels the firmware builds: ExecuTorch's Cortex-M operators on CMSIS-NN in plain C, at the versions in `west.yml` (`firmware/tests/host_int8`). The int8 row of the table above is that run. - **The firmware**, built by Zephyr as a program for a PC (`native_sim/native/64`: the same application, ExecuTorch module and CMSIS-NN, no board), runs its start-up self-test: both programs load in the pools of `prj.conf` and answer what the export expects. Console: [docs/console-native-sim.txt](docs/console-native-sim.txt). - **The camera loop** runs there too, with Zephyr's test-pattern generator standing in for the camera: the capture code gets its frames through Zephyr's video API and a pose line comes out for each ([docs/console-native-sim-camera.txt](docs/console-native-sim-camera.txt)). A test pattern is no person, so this shows that frames flow, not that poses are right. - **The firmware's decoder** gives the keypoints the Python one gives (`firmware/tests/test_pose_decode.py`). Not checked yet: a board. The workflow builds the firmware for the XIAO ESP32S3, but the ESP32-S3 itself, its camera, the display and the speed are untested until the console of a real XIAO ESP32S3 Sense is in `docs/`. The firmware runs the same self-test at start-up and prints PASS or FAIL for each program. ## Speed Not measured on a board yet. On the XIAO ESP32S3, BrickNet from [executorch-zephyr-xiao-esp32s3](https://github.com/eoinjordan/executorch-zephyr-xiao-esp32s3) has the same kinds of layer and takes 60 ms in int8 and 724 ms in fp32 for 1.30 million multiply-adds. PoseMicro has 6.97 million, which by the same ratio is about 0.3 s in int8 and about 4 s in fp32. The firmware prints the measured times of both programs at start-up. ## Try it on a PC ```sh pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu python train/pose_predict.py person.jpg --out pose.jpg python train/pose_predict.py person.jpg --out pose.jpg --threshold 0.2 # count and draw fainter keypoints ``` This runs `firmware/model/pose_micro.pte`, the program the board runs, through the ExecuTorch Python runtime, and prints the line the firmware would print. ## Run it on a XIAO ESP32S3 Sense The workspace, versions and build are those of executorch-zephyr-xiao-esp32s3: Zephyr v4.4.0 and ExecuTorch v1.5.1 as a Zephyr module. Clone this repository as `pose-micro` into an empty folder, and there: ```sh pip install -r pose-micro/requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu west init -l pose-micro && west update --narrow -o=--depth=1 west packages pip --install west blobs fetch hal_espressif west sdk install --toolchains xtensa-espressif_esp32s3_zephyr-elf # self-test on a baked-in picture, then the camera west build -p always -b xiao_esp32s3/esp32s3/procpu/sense pose-micro/firmware -- -DEXTRA_CONF_FILE=camera.conf west flash # with the Seeed Studio Round Display for XIAO west build -p always -b xiao_esp32s3/esp32s3/procpu/sense --shield seeed_xiao_round_display pose-micro/firmware -- \ "-DEXTRA_CONF_FILE=camera.conf;display.conf" -DEXTRA_DTC_OVERLAY_FILE=round_display.overlay ``` At start-up the firmware loads both programs and runs each on a picture baked into the firmware. It compares what the fp32 program answers with PyTorch, and what the int8 program answers with what the same program answered on a PC at export, and prints PASS or FAIL, sizes and times. Then it prints one line a frame, of this shape: ``` POSE n=15 52,18,93 55,16,90 49,16,91 ... ms= ``` `n` keypoints found, then `x,y,heat` of all 17 in COCO order (nose, eyes, ears, shoulders, elbows, wrists, hips, knees, ankles; left before right), in hundredths: x from 0 at the left to 100 at the right, y from 0 at the top to 100 at the bottom. Send `c` on the console for the next frame as the model sees it, `i` for the start-up report again, `t` to run the baked-in picture. A keypoint counts as found, and is drawn on the display, when its heat reaches a threshold: 30 hundredths to start with. A camera's pictures are not the photographs the model learnt from, so tune it to yours: `+` and `-` on the console move it by 5 while the firmware runs, and `CONFIG_APP_POSE_THRESHOLD_PERCENT` in `camera.conf` keeps the value that works. Lower it when keypoints that are plainly there go missing, raise it when limbs are drawn where there are none. The self-test always counts with 30. `-DAPP_CMSIS_NN=OFF` builds the fp32 program alone. Without a board, on Linux, the same firmware runs its self-test in Zephyr's native simulator (times read 0 there: its clock stands still while code runs): ```sh ZEPHYR_TOOLCHAIN_VARIANT=host west build -p always -b native_sim/native/64 pose-micro/firmware build/zephyr/zephyr.exe -no-rt -stop_at=2 ``` ## Train it ```sh python train/pose_data.py --split val # 4,604 crops from 2,011 pictures python train/pose_data.py --split train # about 111,700 crops from 48,900 pictures, half an hour python train/train_pose.py --epochs 60 sh firmware/tests/host_int8/build.sh # once, on Linux: the firmware's int8 kernels for this PC python train/export_pose.py # both programs, their scores, the firmware's test picture python firmware/tests/test_pose_decode.py # the firmware's decoder against Python's python tools/make_card.py # the numbers and the samples on this page ``` `train/pose_data.py` fetches the COCO pictures one by one and keeps only the crops, about 4.4 GB. Training and export write to `train/out/`. The GitHub workflow `.github/workflows/pose.yml` does all of the above, copies the weights and their scores to `weights/`, builds the firmware, and publishes to the Hub. ## Files | | | |---|---| | `firmware/model/pose_micro.pte`, `pose_micro_cmsis_nn.pte` | the two programs | | `weights/pose_micro.pt` | the PyTorch weights they were exported from | | `weights/pose_micro_scores.json` | every number on this page, and more | | `train/` | data, model, training, export, prediction on a PC | | `firmware/` | the Zephyr application | | `firmware/tests/` | the decoder test, and the int8 kernels built for a PC | | `docs/console-native-sim.txt` | the firmware's start-up self-test in Zephyr's native simulator | | `docs/console-native-sim-camera.txt` | the same with the camera loop, on Zephyr's test-pattern camera | | `firmware/prebuilt/` | on the Hub only: the `zephyr.bin` of each build of the last workflow run | | `west.yml`, `requirements.txt` | the workspace and the host tools | ## Licence and credits Code and weights: Apache-2.0. Trained on [COCO](https://cocodataset.org) 2017 person keypoints (annotations CC BY 4.0; the pictures are Flickr photographs under their own licences and are not part of this repository, except the crops in `assets/samples.jpg` and the firmware's test picture, which are under licences that allow it and are credited in `assets/CREDITS.md` and `firmware/src/pose_test_input.h`). The Zephyr application, its build and the camera and display code come from [executorch-zephyr-xiao-esp32s3](https://github.com/eoinjordan/executorch-zephyr-xiao-esp32s3). `train/pose_model.py` sizes the network from the timings measured there.