Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Python 3.8
uses: actions/setup-python@v2
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: 3.12

- name: Install and run pre-commit hooks
uses: pre-commit/action@v2.0.0
uses: pre-commit/action@v3.0.1
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ venv.bak/

# IDEs
.idea

# Project data
data.tar.gz
/data/
51 changes: 29 additions & 22 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,43 @@ default_language_version:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v5.0.0
hooks:
- id: check-added-large-files
args: [ --maxkb=15000 ]
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-yaml
- id: debug-statements
- id: detect-private-key
- id: check-executables-have-shebangs
- id: check-toml
- id: check-case-conflict
- id: check-added-large-files
args: [ --maxkb=15000 ]

- repo: https://github.com/timothycrosley/isort
rev: 5.12.0
hooks:
- id: isort
types: [ python ]

- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
types: [ python ]

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
# python linting and code formatting
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.0
hooks:
- id: flake8
args: [--config, ./setup.cfg]
types: [ python ]
- id: ruff
args: [--fix]
- id: ruff-format

# jupyter notebook cell output clearing
- repo: https://github.com/kynan/nbstripout
rev: 0.6.1
rev: 0.8.1
hooks:
- id: nbstripout
files: '.ipynb'

# md formatting
- repo: https://github.com/hukkin/mdformat
rev: 0.7.22
hooks:
- id: mdformat
args: ["--number"]
additional_dependencies:
- mdformat-gfm
- mdformat-frontmatter
- mdformat-footnote
- mdformat-gfm-alerts
- mdformat-tables
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,69 @@
# Auto-encoder Tutorials

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## Setup

### Virtual Environment
If you don't operate inside a virtual environment, or only have access to an incompatible python version (<3.8), it is

If you don't operate inside a virtual environment, or only have access to an incompatible python version (\<3.8), it is
recommended you create a virtual environment using [`conda`](https://docs.conda.io/en/latest/):

```shell script
conda env create -f environment.yml
conda activate deep-learning-tutorials
```

Creating the environment this way also takes care of installing the dependencies for you, so you can skip the rest of
the setup and dive straight into one of the tutorials.

### Installing Dependencies

If you already have a python environment set aside for this project and just want to install the dependencies, you can
do that using the following command:

```shell script
pip install -e .
```


## How to Run

Once you've went through the [setup](#setup) instructions above, you can start exploring the tutorial's notebooks.
We recommend using JupyterLab to run the notebooks, which can be launched by running (from within your environment):

```shell script
jupyter-lab
```

When you've launched JupyterLab's web interface, you can simply navigate to any of the
[tutorials listed below](#available-tutorials), and follow the instructions in there!


## Available Tutorials

### Representation Learning

- [Basic Variational Autoencoders](tutorials/mnist-autoencoders.ipynb)
- [Variational Autoencoders Applied to Cardiac MRI](tutorials/cardiac-mri-autoencoders.ipynb)

You may download the MNIST and ACDC datasets [here](https://drive.google.com/file/d/1H5pTOYjcSFR6B5GhA0sEPW0wgPVfBq8S/view?usp=sharing). Once downloaded, you may untar the file and copy the **data/** folder in the root of your code, at the same level than the **src/** and the **tutorials/** folders.
The datasets used in this tutorial (MNIST and ACDC) will automatically be downloaded at the beginning of the notebooks.
However, if you experience issues with the download and want to download the datasets, you can find them [here](https://drive.google.com/file/d/1H5pTOYjcSFR6B5GhA0sEPW0wgPVfBq8S/view?usp=sharing) or download it with the following command:

```shell script
gdown https://drive.google.com/uc?id=1H5pTOYjcSFR6B5GhA0sEPW0wgPVfBq8S -c -O data.tar.gz
```

Once downloaded, you may untar the file and and copy the `data/` folder at the root of the project, i.e. at the same level as the `src/` and the `tutorials/` folders.

```shell script
tar -xvzf data.tar.gz
```

## How to Contribute

If you want to contribute to the project, then you have to install development dependencies and pre-commit hooks, on
top of the basic setup for using the project, detailed [above](#setup). The pre-commit hooks are there to ensure that
any code committed to the repository meets the project's format and quality standards.

```shell script
# Install development dependencies
pip install -e .[dev]
Expand Down
4 changes: 1 addition & 3 deletions dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pre-commit
isort
black
flake8
ruff
10 changes: 3 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
[tool.black]
[tool.ruff]
line-length = 120
target-version = ["py38"]
exclude = "(.eggs|.git|.hg|.mypy_cache|.nox|.tox|.venv|.svn|_build|buck-out|build|dist)"

[tool.isort]
profile = "black"
line_length = 120
src_paths = ["src"]
[tool.ruff.lint.pydocstyle]
convention = "google"
10 changes: 5 additions & 5 deletions requirements.txt
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Base requirements
gdown
holoviews[recommended]
jupyterlab
jupyter
matplotlib
numpy
torch
torchvision
torchinfo
torchvision
tqdm
numpy
matplotlib
holoviews[recommended]
umap-learn

# cardiac-mri-autoencoders
h5py
torchmetrics
16 changes: 0 additions & 16 deletions setup.cfg

This file was deleted.

42 changes: 42 additions & 0 deletions src/data/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Optional

import torch
from torch import Tensor


def to_onehot(
label_tensor: Tensor,
num_classes: Optional[int] = None,
) -> Tensor:
"""Convert a dense label tensor to one-hot format.

References:
Copied from `torchmetrics.utilities.data.to_onehot` (v1.6.2, 2025-03-25):
https://lightning.ai/docs/torchmetrics/stable/references/utilities.html#to-onehot

Args:
label_tensor: dense label tensor, with shape [N, d1, d2, ...]
num_classes: number of classes C

Returns:
A sparse label tensor with shape [N, C, d1, d2, ...]

Example:
>>> x = torch.tensor([1, 2, 3])
>>> to_onehot(x)
tensor([[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
"""
if num_classes is None:
num_classes = int(label_tensor.max().detach().item() + 1)

tensor_onehot = torch.zeros(
label_tensor.shape[0],
num_classes,
*label_tensor.shape[1:],
dtype=label_tensor.dtype,
device=label_tensor.device,
)
index = label_tensor.long().unsqueeze(1).expand_as(tensor_onehot)
return tensor_onehot.scatter_(1, index, 1.0)
Loading