DECT - Differentiable Euler Characteristic Transform
This is the official implementation for the Differentiable Euler Characteristic
Transform, a geometrical-topological method for shape classification. Our
implementation is fully optimized for hardware acceleration,
yielding a blazingly fast implementation for machine learning research with
PyTorch.
Installation
For the installation we require an up-to-date installation of PyTorch, either
with or without CUDA support. Our package can be installed with either
pip or added as a git submodule.
Installation as a git submodule (from the root folder) and afterward as and (editable) package can be done as follows.
git submodule add https://github.com/aidos-lab/dect.git
cd dect
pip install [-e] .
Or use pip to install the git repository.
pip install git+https://github.com/aidos-lab/dect.git
Usage
For a demonstration of our new ECT computation, we provide the
notebooks/01_example_usage.ipynb file and the code therein provides an intuitive
example with explanation. The code is provided on an as-is basis; see
LICENSE.md for more
information. You are cordially invited to both contribute and provide feedback.
Do not hesitate to contact us!
import torch
from dect.directions import generate_2d_directions
from dect.ect import compute_ect
from dect.ect_fn import scaled_sigmoid
# Added for visualization.
import matplotlib.pyplot as plt
# Basic dataset with three points,three edges and one face.
points_coordinates = torch.tensor([[0.5, 0.0], [-0.5, 0.0], [0.5, 0.5]])
# Generate a set of structured directions along the unit circle.
v = generate_2d_directions(num_thetas=64)
# Compute the ECT.
ect = compute_ect(
points_coordinates,
v=v,
radius=1,
resolution=64,
scale=500,
ect_fn=scaled_sigmoid
)
# Visualize as an image.
plt.imshow(ect.detach().squeeze().numpy().T)
plt.show()
Compute the ECT of point clouds with channels.
It is often of interest to compute the ECT of point clouds with different categorical types,
such as atom numbers in molecules. The compute_ect_channels function provides a method to
compute the ECT per categorical type, resulting in a set of ECTs.
We include an example below.
import torch
from dect.directions import generate_2d_directions
from dect.ect import compute_ect_channels
v = generate_2d_directions()
x = torch.rand(size=(10, 2))
batch = torch.repeat_interleave(torch.tensor([0, 1]), repeats=5)
z = torch.randint(low=0, high=3, size=(10,))
ect = compute_ect_channels(x, v, radius=1, resolution=64, scale=200, index=batch, channels=z)
ect.shape # Result is an ECT of shape [2,3,64,64]
Fast Euler Characteristic Transform
from dect.directions import generate_2d_directions
from dect.fect import compute_fast_ect
v = generate_2d_directions(num_thetas=2048).cuda()
x_true = 0.5 * torch.rand(size=(10000, 2)).cuda()
x = torch.nn.Parameter(0.2 * (torch.rand(size=(10000, 2), device="cuda") - 0.5))
optimizer = torch.optim.Adam([x], lr=0.01)
for epoch in range(200):
optimizer.zero_grad()
ect_true = fastect(x_true, v)
ect_pred = fastect(x, v)
loss = torch.nn.functional.mse_loss(ect_pred, ect_true)
loss.backward()
optimizer.step()
License
Our code is released under a BSD-3-Clause license. This license essentially
permits you to freely use our code as desired, integrate it into your projects,
and much more --- provided you acknowledge the original authors. Please refer to
LICENSE.md for more information.
Contributing
We welcome contributions and suggestions for our DECT package! Here are some
basic guidelines for contributing:
How to Submit an Issue
Check Existing Issues: Before submitting a new issue, please check if it
has already been reported.
Open a New Issue: If your issue is new, open a new issue in the
repository. Provide a clear and detailed description of the problem,
including steps to reproduce the issue if applicable.
Include Relevant Information: Include any relevant information, such as
system details, version numbers, and screenshots, to help us understand and
resolve the issue more efficiently.
How to Contribute
If you're unfamiliar with contributing to open source repositories, here is a
basic roadmap:
Fork the Repository: Start by forking the repository to your own GitHub
account.
Clone the Repository: Clone the forked repository to your local machine.
git clone https://github.com/your-username/dect.git
Create a Branch: Create a new branch for your feature or bug fix.
git checkout -b feature/your-feature-name
Make Changes: Implement your changes in the new branch.
Commit Changes: Commit your changes with a descriptive commit message.
git commit -m "Description of your changes"
Push Changes: Push the changes to your forked repository.
git push origin feature/your-feature-name
Submit a Pull Request: Open a pull request to the main repository with a
clear description of your changes and the purpose of the contribution.
Need Help?
If you need any help or have questions, feel free to reach out to the authors or
submit a pull request. We appreciate your contributions and are happy to assist!
Citation
If you find our work useful, please consider using the following citation:
@inproceedings{Roell24a,
title = {Differentiable Euler Characteristic Transforms for Shape Classification},
author = {Ernst R{\"o}ell and Bastian Rieck},
year = 2024,
booktitle = {International Conference on Learning Representations},
eprint = {2310.07630},
archiveprefix = {arXiv},
primaryclass = {cs.LG},
repository = {https://github.com/aidos-lab/dect-evaluation},
url = {https://openreview.net/forum?id=MO632iPq3I},
}
Acknowledgement
The authors would like to thank Juan and Johannes for their valuable feedback, greatly helping the code quality and for the many bug fixes.