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
- New Features
- Preparing the Data
- How to use pytorch-hexagdly
- General Concept
- Disclaimer
- Citing HexagDLy
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" |
|---|---|---|
| 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=None 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="ring")
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.
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytorch_hexagdly-0.2.2.tar.gz.
File metadata
- Download URL: pytorch_hexagdly-0.2.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
762a9a22509e98ad086ffbe6a0ade205783b57f8aabf7d407e8f9680d280a1f5
|
|
| MD5 |
1e1461072a9c21d50672e793a67c99b6
|
|
| BLAKE2b-256 |
8d5fa021a7080c0bb18b26e43d05c8e967d9cf155e7c961cce2ece4c730a97be
|
Provenance
The following attestation bundles were made for pytorch_hexagdly-0.2.2.tar.gz:
Publisher:
publish.yml on YugnatD/pytorch-hexagdly
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytorch_hexagdly-0.2.2.tar.gz -
Subject digest:
762a9a22509e98ad086ffbe6a0ade205783b57f8aabf7d407e8f9680d280a1f5 - Sigstore transparency entry: 2060813365
- Sigstore integration time:
-
Permalink:
YugnatD/pytorch-hexagdly@3c57cd520bdec31af1eb523f203de5890b324fa4 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/YugnatD
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3c57cd520bdec31af1eb523f203de5890b324fa4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pytorch_hexagdly-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pytorch_hexagdly-0.2.2-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fc5f3715f07ae288af92df7df71ebc817671df6a868e3c21863361f67c9c080
|
|
| MD5 |
0e26d09fec8043f095a43d142d2d4e5f
|
|
| BLAKE2b-256 |
9e7b45eac68c9dbed791874ca99aa2a75c2ce314e9779ec045314249ce56963a
|
Provenance
The following attestation bundles were made for pytorch_hexagdly-0.2.2-py3-none-any.whl:
Publisher:
publish.yml on YugnatD/pytorch-hexagdly
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytorch_hexagdly-0.2.2-py3-none-any.whl -
Subject digest:
5fc5f3715f07ae288af92df7df71ebc817671df6a868e3c21863361f67c9c080 - Sigstore transparency entry: 2060813701
- Sigstore integration time:
-
Permalink:
YugnatD/pytorch-hexagdly@3c57cd520bdec31af1eb523f203de5890b324fa4 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/YugnatD
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3c57cd520bdec31af1eb523f203de5890b324fa4 -
Trigger Event:
push
-
Statement type: