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 — ring-shared kernel weights

Available on Conv2d and Conv3d. When set to True, all cells at the same hexagonal ring distance share a single weight, reducing the number of learnable parameters. Ring 0 is the center pixel; ring r covers the 6r cells at hex-distance r. This mirrors the TDSCAN triggering approach.

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.1.0.tar.gz (3.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.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytorch_hexagdly-0.1.0.tar.gz
  • Upload date:
  • Size: 3.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.1.0.tar.gz
Algorithm Hash digest
SHA256 0a2cb38c5939d753d9b4dda5a30a880a1e3c011e02e36a113e9d81fa13804e3f
MD5 a6dad1af1a263867aeb5ab328fcde528
BLAKE2b-256 b1ac476408c9dce50a37285a315e046b81709acc19a8db42b12149a3fdaadcb6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytorch_hexagdly-0.1.0.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.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch_hexagdly-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1249084953027c9b146be185c2b31fdffbd08b4743e0dc48ea432a20612fd292
MD5 2b73fb8de25d46a602a9706d1b9c96b2
BLAKE2b-256 4512a65827769eaceab86fd21af240b5e6dcfe1aa710258e26a3315bf080780d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytorch_hexagdly-0.1.0-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