Skip to main content

Hexagonal convolution and pooling layers for PyTorch — an extended HexagDLy fork

Project description

pytorch-hexagdly — Hexagonal Convolutions for PyTorch

pytorch-hexagdly is a fork of HexagDLy that extends the original hexagonal convolution and pooling layers for PyTorch with two new features: ring-shared weights (share_neighbors) and depth-axis same-padding (depth_padding="same" on Conv3d).

Getting Started

Pip Installation

pip install pytorch-hexagdly
import pytorch_hexagdly

To get the dependencies needed to run the provided unit tests and notebooks, add the dev option:

pip install pytorch-hexagdly[dev]

Manual Installation

Requires a working installation of PyTorch. Clone the repository and install in editable mode:

git clone https://github.com/YugnatD/pytorch-hexagdly
cd pytorch-hexagdly
pip install -e .

New Features

share_neighbors — weight sharing across kernel cells

Available on Conv2d and Conv3d. The share_neighbors parameter reduces the number of learnable parameters by grouping kernel cells that share a single weight. Three modes are available, illustrated below for kernel_size=2 (19 cells total):

share_neighbors="ring" share_neighbors="diag" share_neighbors="sym"
ring diag sym
3 weights — cells at the same hex distance from center share one weight (concentric rings). 10 weights — visually opposite (antipodal) cells share one weight. 10 weights — geometrically adjacent 60° pairs share one weight.
  • "ring": the most aggressive reduction. All 6 direct neighbours share one weight, all 12 outer cells share another. Mirrors the TDSCAN triggering approach.
  • "diag": antipodal symmetry — each cell and its mirror image through the center share a weight. Useful when the kernel should be point-symmetric.
  • "sym": 60° adjacent pairs — consecutive neighbours along the kernel boundary share a weight. Useful when the kernel should reflect local rotational symmetry.

For kernel_size=1 (7 cells): ring=2 weights, diag=4 weights, sym=4 weights. For kernel_size=2 (19 cells): ring=3 weights, diag=10 weights, sym=10 weights. Compare to the default share_neighbors=False which gives 7 and 19 independent weights.

import torch
import pytorch_hexagdly

conv = pytorch_hexagdly.Conv2d(1, 8, kernel_size=2, stride=1, share_neighbors=True)
x = torch.randn(1, 1, 21, 21)
print(conv(x).shape)

depth_padding="same" — temporal same-padding for Conv3d

When depth_padding="same", the depth/time axis is zero-padded symmetrically so the output depth equals the input depth. The default is "valid" (upstream behaviour).

conv3d = pytorch_hexagdly.Conv3d(1, 4, kernel_size=(3, 1), stride=1,
                                  depth_padding="same")
x = torch.randn(1, 1, 10, 21, 21)
print(conv3d(x).shape)  # depth dimension preserved

How to use pytorch-hexagdly

As pytorch-hexagdly is based on PyTorch, it is of advantage to be familiar with PyTorch's functionalities and concepts. Before applying it, ensure that the input data has the correct hexagonal layout. An example notebook illustrates the steps to get data into the correct format.

Basic example:

import torch
import pytorch_hexagdly

kernel_size, stride = 1, 4
in_channels, out_channels = 1, 3

hexconv = pytorch_hexagdly.Conv2d(in_channels, out_channels, kernel_size, stride)
input = torch.rand(1, 1, 21, 21)
output = hexconv(input)

HexagDLy uses an addressing scheme to map hexagonal grid data to a square tensor. The layout from top to bottom (along tensor index 2) must be of zig-zag-edge shape and from left to right (along tensor index 3) of armchair-edge shape.

Additional examples for basic use-cases are shown in the notebooks folder.

General Concept

As common deep learning frameworks process data on square grids, hexagonally sampled data must be mapped to a square tensor. This conversion is non-trivial due to the different symmetries of square (4-fold) vs hexagonal (6-fold) grids.

HexagDLy solves this by splitting each convolution kernel into sub-kernels that together cover the true neighbours of a data point in the hexagonal grid. A full hexagonal convolution with size 1 (next-neighbour kernel) decomposes into three sub-convolutions with two different sub-kernels applied to three differently padded versions of the input.

kerne size+stride

Please note: Operations are only performed where the center point of a kernel is located within the input tensor. This could result in output columns of different length; in such cases the output will be sliced according to the shortest column.

violating_symmetry

explicit_next_neighbour_conv

Disclaimer

pytorch-hexagdly is built as an easy-to-use prototyping tool to design convolutional neural networks for hexagonally sampled data. The implemented methods aim for flexibility rather than performance. Once a model is optimized, hard-coding kernel size, stride and input dimensions will make the implementation faster.

Authors

Fork (pytorch-hexagdly)

  • Tanguy Dietrich — HEPIA / SST-1M Collaboration

Original HexagDLy

  • Tim Lukas Holch
  • Constantin Steppa

See NOTICE.md for full attribution.

License

MIT license — see LICENSE.

Citation

If this work has helped your research, please cite the original HexagDLy paper:

@article{hexagdly_paper,
    title = "HexagDLy—Processing hexagonally sampled data with CNNs in PyTorch",
    author = "Constantin Steppa and Tim L. Holch",
    journal = "SoftwareX",
    volume = "9",
    pages = "193 - 198",
    year = "2019",
    issn = "2352-7110",
    doi = "https://doi.org/10.1016/j.softx.2019.02.010",
    url = "https://www.sciencedirect.com/science/article/pii/S2352711018302723",
    keywords = "Convolutional neural networks, Hexagonal grid, PyTorch, Astroparticle physics",
    abstract = "HexagDLy is a Python-library extending the PyTorch deep learning framework with convolution and pooling operations on hexagonal grids. It aims to ease the access to convolutional neural networks for applications that rely on hexagonally sampled data as, for example, commonly found in ground-based astroparticle physics experiments."
}

HexagDLy was developed as part of a research study in ground-based gamma-ray astronomy published in Astroparticle Physics.

Acknowledgments

The original HexagDLy project evolved by exploring new analysis techniques for Imaging Atmospheric Cherenkov Telescopes with H.E.S.S. The fork was developed in the context of the SST-1M Collaboration / HEPIA TDSCAN triggering project.

Project details


Download files

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

Source Distribution

pytorch_hexagdly-0.2.1.tar.gz (4.5 MB view details)

Uploaded Source

Built Distribution

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

pytorch_hexagdly-0.2.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file pytorch_hexagdly-0.2.1.tar.gz.

File metadata

  • Download URL: pytorch_hexagdly-0.2.1.tar.gz
  • Upload date:
  • Size: 4.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytorch_hexagdly-0.2.1.tar.gz
Algorithm Hash digest
SHA256 acb7ce0781e1517b414bbc81b0883a5d2a531b9ee4106d9c43764870757fd153
MD5 0eb8499b6fc9d4732894db423bbd32fd
BLAKE2b-256 e70404ba336772ef390e8ca33e823adc1246ca6d3a469568db79fe31cb86025c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytorch_hexagdly-0.2.1.tar.gz:

Publisher: publish.yml on YugnatD/pytorch-hexagdly

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytorch_hexagdly-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch_hexagdly-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2e5cd6c568f3a8fe7f3c37dad199c4b40475e325bd130901469c155255799cb9
MD5 34785b0d0c996d7a6089ec45b3667b34
BLAKE2b-256 d4df297863e7510262ee0c8f6abedb77b3011e5d809d94d331d0ba39f367a619

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytorch_hexagdly-0.2.1-py3-none-any.whl:

Publisher: publish.yml on YugnatD/pytorch-hexagdly

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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