Skip to main content

Fast C++ heatmap-to-keypoints decoder for pose estimation

Project description

HeatmapToKeypoints

A high-performance C++/PyTorch extension for decoding 2D heatmaps into precise keypoint coordinates.

This library is designed for human pose estimation and keypoint detection workflows. It accepts standard NCHW heatmap tensors (e.g., from HRNet, SimpleBaseline, or KD-student models) and efficiently converts them into normalized (x, y) coordinates with confidence scores using sub-pixel refinement.

Problem Statement

Pose estimation models typically output a 4D tensor of shape [N, K, H, W]:

  • N: Batch size
  • K: Number of keypoints
  • H, W: Heatmap spatial dimensions

Each channel K contains a 2D Gaussian distribution where the peak represents the predicted location of a specific body part. Converting these heatmaps into coordinates requires finding the peak and refining it to achieve sub-pixel accuracy. Doing this efficiently on CPU for large batches can be slow in pure Python.

Solution

HeatmapToKeypoints provides a direct C++ binding to PyTorch that:

  1. Scans Memory Linearly: Optimizes memory access patterns for NCHW layout.
  2. Refines Coordinates: Uses Taylor expansion (Hessian matrix inversion) to compute sub-pixel offsets from the discrete peak.
  3. Outputs Normalized Data: Returns coordinates in [0.0, 1.0] range, independent of resolution.

Installation

Ensure you have a C++17 compatible compiler and PyTorch installed.

pip install .

Usage

The library exposes a single function decode_heatmaps.

Input format

  • Type: torch.Tensor (float32)
  • Shape: [Batch, Keypoints, Height, Width]
  • Device: CPU
  • Memory: Contiguous

Python Example

import torch
import heatmaps_to_keypoints

# Example Tensor: Batch=8, Keypoints=17, Height=96, Width=72
heatmaps = torch.randn(8, 17, 96, 72)

# Decode
# Returns a list of lists: results[batch_index][keypoint_index]
results = heatmaps_to_keypoints.decode_heatmaps(heatmaps)

# Access data for the first image in batch, first keypoint
landmark = results[0][0]

print(f"X: {landmark.x}")     # Normalized [0-1]
print(f"Y: {landmark.y}")     # Normalized [0-1]
print(f"Score: {landmark.score}") # Heatmap peak value

Algorithm Details

For each keypoint heatmap, the decoder performs:

  1. Global Argmax: Finds the discrete pixel $(p_x, p_y)$ with the maximum value.
  2. Taylor Expansion: Uses the 2nd-order Taylor series approximation around the peak to find the true sub-pixel maximum. $$ \Delta = -H^{-1} \nabla $$ Where $H$ is the Hessian matrix and $\nabla$ is the gradient vector computed from neighbors.
  3. Coordinate Normalization: $$ x_{final} = \frac{p_x + \Delta_x}{W}, \quad y_{final} = \frac{p_y + \Delta_y}{H} $$

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

heatmaps_to_keypoints-0.0.3-cp312-cp312-win_amd64.whl (95.8 kB view details)

Uploaded CPython 3.12Windows x86-64

heatmaps_to_keypoints-0.0.3-cp312-cp312-manylinux_2_28_aarch64.whl (90.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ ARM64

heatmaps_to_keypoints-0.0.3-cp312-cp312-macosx_11_0_arm64.whl (71.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

heatmaps_to_keypoints-0.0.3-cp311-cp311-win_amd64.whl (94.8 kB view details)

Uploaded CPython 3.11Windows x86-64

heatmaps_to_keypoints-0.0.3-cp311-cp311-manylinux_2_28_aarch64.whl (90.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

heatmaps_to_keypoints-0.0.3-cp311-cp311-macosx_11_0_arm64.whl (70.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

heatmaps_to_keypoints-0.0.3-cp310-cp310-win_amd64.whl (94.1 kB view details)

Uploaded CPython 3.10Windows x86-64

heatmaps_to_keypoints-0.0.3-cp310-cp310-manylinux_2_28_aarch64.whl (88.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

heatmaps_to_keypoints-0.0.3-cp310-cp310-macosx_11_0_arm64.whl (69.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

heatmaps_to_keypoints-0.0.3-cp39-cp39-win_amd64.whl (87.2 kB view details)

Uploaded CPython 3.9Windows x86-64

heatmaps_to_keypoints-0.0.3-cp39-cp39-manylinux_2_28_aarch64.whl (70.5 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ ARM64

heatmaps_to_keypoints-0.0.3-cp39-cp39-macosx_11_0_arm64.whl (60.2 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 980e3a6218e63a6d96b2fb78d6449a5c3243937b6c174c78fa83b50b2032b420
MD5 be6c847b29db0550f95ae4ed04564e81
BLAKE2b-256 12dc7780b76aaab584eb726852eb7075def8b8d6a07cba37e066c75c450151fa

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp312-cp312-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9937fc9b4ffb1d8af082e197bac891151b7d5838a38a12f99f5d48582594895d
MD5 4b54a6878b2a8ece4c06bf4a512ae5d0
BLAKE2b-256 1ab75210ba1a2cd909017c4d0c0f8a4de42ce11eff3dc5738ecfe324d171e9f4

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp312-cp312-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4892454eb5f6d0619a4d336eb1b42dd5384479a4ac9a2101bc833c3cf371abc5
MD5 fd57ecb68ca004ae6c3dde55ca328610
BLAKE2b-256 045dc185feea7cf08d8f837744badaaefc0952a087a6b02fb968f4ceb01ea861

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fc1891e678eca003d5b90d7c82e93fafcc4642569e7101353d9d064f38fc9e55
MD5 c39233bff23ba49aa306d09b51210605
BLAKE2b-256 a9b71f46843521136355b08edbba195f042d429267abda673904a8f5d1982b8b

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1868ee4f937fe9fc4652cac97f35382d8c5f066d63e6c9d8e9bbb2756a081430
MD5 76c04587e34309aaab661b0896ccdaa3
BLAKE2b-256 5bd95d331ce24c67c5a30faf577a9fd0484c7d5fd858bef298bbf9e6e743dd6f

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp311-cp311-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 69a2c752cc9ca5dab8de2543b2f39b7124d0a85e56a3e2c91fc5ba745523cfba
MD5 daa4231519c279b996e772b8f19287a6
BLAKE2b-256 ee754f8b2d327f42b6691ff63d3f5e93e2d49696214a930eb57007a07d8d0aa7

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5da9302e9ac9f0c19ffd2174580975b04518de42965381022d2f68006e511b8e
MD5 403addc9e49b468d4bfd1588117b73e5
BLAKE2b-256 425fe4dc70d8a68aeeec3309c37a7467890db95772c64ff794d8337acd15dac6

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 37a9a766c16c93d15d5a258d5c1d634891eb782d2563cb6b5d04ecab40e03a66
MD5 220165ca7e8da12fa4d416b38c6386f5
BLAKE2b-256 30de9108b151f824aed0a8dd277f8fb702db45b50527e269ac060f4e1cbbe7b8

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4c68abc3e4956f05d0907444b991ad478927b8529443b3d6c4a942eb8baa000a
MD5 1bac946400f0cddffd1a39e32511ebec
BLAKE2b-256 230dc191d15eba3fa6b005aee266ea5931601120aa60e8bcff87e8d132d7537c

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp310-cp310-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 66c0efa605b33a3e60add1727943f3f185d99fbdf66fa9b50d6e96bf6bed697a
MD5 ea75d834acf960897f73d4f1d08b2705
BLAKE2b-256 e4dd13336b7e1c35ab54a7799fb145623ca748269fe1747e83662e71026de447

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 58f510b1158e96f45b6b0b1f40a2f58ae7aa22299ccafb4c9bfdde5f315aa0ff
MD5 1002c321128ecf8ee37f64b94b1eb2b6
BLAKE2b-256 d1a466dd3d84b9fd59d8586c7190f36bf84b0a83c755aab9ecac794936011463

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 088ab356b6a2cb7b41b16855bf8d6e3bbd109d7c0baddebfedf78141a42bb50c
MD5 e94536f6279441c60e15d414d8109b95
BLAKE2b-256 8ffd48b4dcc04d55e8f0a0ac4e5edacab31e442a9ab7901af1815176f4f32309

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 2897fd776758298fec020465bcb4dd72e4f67f38cd8c66c569bd06277eeb6b04
MD5 184efb3ac91602b42df8c53b911feba5
BLAKE2b-256 fc15dc7f3aaf5561c7622e84f29cdedc0c885ec53382d2c30fdffe0186131ce7

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp39-cp39-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2fb0e392b2a48acaa45d123266b14a271aa0ccfb79f73c56543e996c0c4193c8
MD5 00f042bb5d852df39119410bfe422375
BLAKE2b-256 135b51c3077359e2872b5bf5294a556d05b27b204be5a397a6051e6646f60a4e

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 47771d1051450571ccc54296d3cf0ef22ac507c4adfe3d19d6942ce56667dbec
MD5 0cfaa0ebe3c540f1a33e8e5da2fd4e02
BLAKE2b-256 1d5c1044bbdaf9e41f1dd5f77028174496b263ae90241d2d22df7df8127eb03d

See more details on using hashes here.

File details

Details for the file heatmaps_to_keypoints-0.0.3-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for heatmaps_to_keypoints-0.0.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fac5785d013df9b9e208e5aba7beb85290825da6c6905118ca261d915d75bf1e
MD5 3114e027e6db76e5fcce6747a6a4d88d
BLAKE2b-256 09db7bda7bedd96842e1208475e093850aaf2a82257618e62b33f988d77efb22

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page