Skip to content

✨ Docker settings, BatchEnsemble utilities rework + minor fixes #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 73 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 67 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
a6d0e97
:whale: Add Dockerfile
tonyzamyatin Mar 2, 2025
8b416c0
:whale: Fix flit build errors
tonyzamyatin Mar 2, 2025
3ef9132
:whale: Install OpenSSH and start server when container starts
tonyzamyatin Mar 2, 2025
4ffeb34
:whale: Fix SSH key not being recognized and add shell prompt customi…
tonyzamyatin Mar 3, 2025
ed04e00
:whale: Reformat Dockerfile
tonyzamyatin Mar 3, 2025
5d731cd
:whale: Install OpenGL and run SSH setup for GitHub-Auth
tonyzamyatin Mar 3, 2025
025f84d
:whale: Ensure setup only runs once when container is started for the…
tonyzamyatin Mar 3, 2025
4106501
:whale: Optimize setup steps and make custom prompt optional via env …
tonyzamyatin Mar 4, 2025
c8cd50b
:whale: Optimize build time and run shell prompt in initial setup
tonyzamyatin Mar 4, 2025
a49bd3e
:books: Add documentation for using Docker image
tonyzamyatin Mar 4, 2025
bb79e0b
:whale: Install dependencies in editable mode
tonyzamyatin Mar 4, 2025
eaa09d6
:bug: Fix pip install in editable mode
tonyzamyatin Mar 4, 2025
2522a31
:bug: Fix MNIST test dataloader for shifted data
tonyzamyatin Mar 4, 2025
ae0405a
:hammer: Fix and format container start script and move it to entrypo…
tonyzamyatin Mar 5, 2025
5d1d27f
:bug: Fix `RandomRescale`
alafage Mar 6, 2025
9624b2c
:shirt: Add scaler exception
o-laurent Mar 7, 2025
748f698
:shirt: Move Docker in the installation section
o-laurent Mar 7, 2025
3b6c7c6
:bug: Fast track config fixes
o-laurent Mar 7, 2025
11801da
:fire: Remove shift-severity in MNISTC
o-laurent Mar 7, 2025
16cfb44
:book: Update test dataloaders docstring
o-laurent Mar 7, 2025
9d761f1
:hammer: use datamodule's shift-severity
o-laurent Mar 7, 2025
3a186b8
:white_check_mark: Fix coverage
o-laurent Mar 7, 2025
d4d09f5
:fire: Remove documentation for removed argument
o-laurent Mar 7, 2025
6df0914
:bug: Add missing learning rate scheduler class paths to lenet configs
tonyzamyatin Mar 7, 2025
56ab652
:sparkles: Add BatchEnsemble wrapper
tonyzamyatin Mar 7, 2025
156aa91
:bug: Merge pull request #135 from tonyzamyatin/bugfix_mnist
o-laurent Mar 7, 2025
918de33
:books: Update documentation regarding (batch) ensemble usage
tonyzamyatin Mar 7, 2025
c5b62d8
:sparkles: Add LeNet BatchEnsemble and Deep Ensemble
tonyzamyatin Mar 7, 2025
940f721
:book: State support of Python 3.13
o-laurent Mar 7, 2025
0a6e32f
:bug: Make sklearn optional. Fix #136
o-laurent Mar 7, 2025
b3c1687
:shirt: Refine dependencies and import error messages
o-laurent Mar 7, 2025
1b0ce53
:zap: Update ruff and lint
o-laurent Mar 7, 2025
017d040
:fire: Remove torchaudio from workflows
o-laurent Mar 7, 2025
a92385e
:shirt: Lint
o-laurent Mar 7, 2025
eda886b
:whale: Update Dockerfile
o-laurent Mar 7, 2025
ca73b4b
:shirt: Also format
o-laurent Mar 7, 2025
5976919
:white_check_mark: Add test for BatchEnsemble wrapper and LeNet imple…
tonyzamyatin Mar 7, 2025
96e3eed
:bug: Fix `extended_out_features` computation in `PackedLinear`
alafage Mar 7, 2025
c001ea6
:white_check_mark: Update PackedLinear tests
alafage Mar 7, 2025
2242805
:whale: Install pre-commit hooks on container start
tonyzamyatin Mar 7, 2025
145181a
:art: Format PackedLinear
alafage Mar 7, 2025
a622910
:shirt: Lint
o-laurent Mar 7, 2025
0f2dca0
🐳 Merge pull request #134 from tonyzamyatin/add-dockerfile
o-laurent Mar 7, 2025
18dacb3
:heavy_check_mark: Add test for BatchEnsemble wrapper and fix bug in …
tonyzamyatin Mar 7, 2025
79b567d
:ok_hand: Comply with PEP 257
o-laurent Mar 10, 2025
dbf4df9
:books: Add note that BatchEnsemble wrapper expects model to use Batc…
tonyzamyatin Mar 10, 2025
16a235b
:hammer: Refactor BatchEnsemble test case to use framework's test format
tonyzamyatin Mar 10, 2025
698ba1a
:heavy_plus_sign: / :heavy_minus_sign: Replace wand by kornia
o-laurent Mar 10, 2025
89b26f2
:hammer: Use kornia's motion blur & remove useless covignores
o-laurent Mar 10, 2025
839444e
:book: Improve the clarity of the OOD detection behavior #139
o-laurent Mar 10, 2025
85bbe59
Merge branch 'dev' of github.com:ENSTA-U2IS-AI/torch-uncertainty into…
o-laurent Mar 10, 2025
4887f78
:white_check_mark: Fix cov
o-laurent Mar 10, 2025
12a7423
:shirt: Fix typo in corruption.py
o-laurent Mar 10, 2025
cf038e0
:hammer: Merge pull request #140 from ENSTA-U2IS-AI/motion_blur
o-laurent Mar 10, 2025
6e2596d
:hammer: Use `einops.repeat` instead of `torch.repeat` in `RepeatTarget`
alafage Mar 10, 2025
16c7705
:hammer: Refine BatchEnsemble layers and add conversion methods
alafage Mar 10, 2025
9d4b4a4
:hammer: `BatchEnsemble` wrapper overhaul
alafage Mar 10, 2025
8b29026
:books: Add BatchEnsemble wrapper utilities in the API Reference
alafage Mar 10, 2025
f2ae628
:book: `BatchEnsemble` docstring update
alafage Mar 10, 2025
c737d07
:book: `BatchEnsemble` docstring update
alafage Mar 10, 2025
819f7e0
Merge pull request #137 from tonyzamyatin/lenet_batchensemble
alafage Mar 10, 2025
0292306
:wrench: Bump version
alafage Mar 10, 2025
8b81c13
:wrench: Add doc trigger when ready for review
o-laurent Mar 10, 2025
d3fa198
:bug: Fix triggers
o-laurent Mar 10, 2025
a7127d4
:bug: Fix pytest triggers
o-laurent Mar 10, 2025
30c0f84
:shirt: Update according PR comments
alafage Mar 10, 2025
7572ca0
:ok_hand: Comply with cosmetic comments
o-laurent Mar 11, 2025
618f7db
:white_check_mark: Finish coverage
o-laurent Mar 11, 2025
1c5c7d8
:wrench: rework optional dependency groups
o-laurent Mar 11, 2025
f4ffbf7
:fire: Remove sklearn and np in AUSE
o-laurent Mar 11, 2025
42a8c83
:fire: Remove overriding of cst types
o-laurent Mar 11, 2025
1f4db81
:bug: Fix NaN handling in AUSE & add test
o-laurent Mar 11, 2025
1a07f0d
:racehorse: Don't download MNISTC in tests
o-laurent Mar 11, 2025
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
3 changes: 2 additions & 1 deletion .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
branches:
- main
pull_request:
types: [opened, reopened, ready_for_review, synchronize]
branches:
- main
schedule:
Expand Down Expand Up @@ -40,7 +41,7 @@ jobs:

- name: Install dependencies
run: |
python3 -m pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu
python3 -m pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cpu
python3 -m pip install .[all]

- name: Sphinx build
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
- dev
pull_request:
branches:
- main
- dev
schedule:
- cron: "42 7 * * 0"
Expand Down Expand Up @@ -64,7 +63,7 @@ jobs:
- name: Install dependencies
if: steps.changed-files-specific.outputs.only_changed != 'true'
run: |
python3 -m pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu
python3 -m pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cpu
python3 -m pip install .[all]

- name: Check style & format
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Custom
.vscode/
.idea/
data/
logs/
lightning_logs/
Expand Down Expand Up @@ -41,6 +42,7 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
.setup_done

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM pytorch/pytorch:2.6.0-cuda12.6-cudnn9-runtime

ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
LC_ALL=C.UTF-8 \
LANG=C.UTF-8

# Install Git, OpenSSH Server, and OpenGL (PyTorch's base image already includes Conda and Pip)
RUN apt-get update && apt-get install -y \
git \
openssh-server \
libgl1 \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /workspace

# Create an empty README.md file and an empty torch_uncertainty module to satisfy flit
RUN touch README.md && mkdir -p torch_uncertainty && touch torch_uncertainty/__init__.py

# Copy dependency file
COPY pyproject.toml /workspace/

# Install dependencies all dependencies
RUN pip install --no-cache-dir -e ".[all]"

# Always activate Conda when opening a new terminal
RUN echo "source /opt/conda/bin/activate" >> /root/.bashrc

# Configure SSH server
RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config && \
echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config && \
echo "AuthorizedKeysFile .ssh/authorized_keys" >> /etc/ssh/sshd_config

# Expose port 8888 for TensorBoard and Jupyter Notebook and port 22 for SSH
EXPOSE 8888 22

# Entrypoint script (runs every time the container starts)
COPY docker/entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

# Note that if /workspace/ is a mounted volume, any files copied to /workspace/ during the build will be overwritten by the mounted volume
# This is why we copy the entrypoint script to /usr/local/bin/ instead of /workspace/
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ pip install torch-uncertainty

The installation procedure for contributors is different: have a look at the [contribution page](https://torch-uncertainty.github.io/contributing.html).

### :whale: Docker image for contributors

For contributors who want to run experiments on cloud GPU instances, we provide a pre-built Docker image that includes all necessary dependencies and configurations and the Dockerfile for building your custom Docker images.
This allows you to quickly launch an experiment-ready container with minimal setup. Please refer to [DOCKER.md](docker/DOCKER.md) for further details.

## :racehorse: Quickstart

We make a quickstart available at [torch-uncertainty.github.io/quickstart](https://torch-uncertainty.github.io/quickstart.html).
Expand Down Expand Up @@ -94,4 +99,4 @@ The following projects use TorchUncertainty:

- *A Symmetry-Aware Exploration of Bayesian Neural Network Posteriors* - [ICLR 2024](https://arxiv.org/abs/2310.08287)

**If you are using TorchUncertainty in your project, please let us know, we will add your project to this list!**
**If you are using TorchUncertainty in your project, please let us know, and we will add your project to this list!**
71 changes: 71 additions & 0 deletions docker/DOCKER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# :whale: Docker image for contributors

### Pre-built Docker image
1. To pull the pre-built image from Docker Hub, simply run:
```bash
docker pull docker.io/tonyzamyatin/torch-uncertainty:latest
```

This image includes:
- PyTorch with CUDA support
- OpenGL (for visualization tasks)
- Git, OpenSSH, and all Python dependencies

Checkout the [registry on Docker Hub](https://hub.docker.com/repository/docker/tonyzamyatin/torch-uncertainty/general) for all available images.

2. To start a container using this image, set up the necessary environment variables and run:
```bash
docker run --rm -it --gpus all -p 8888:8888 -p 22:22 \
-e VM_SSH_PUBLIC_KEY="your-public-key" \
-e GITHUB_SSH_PRIVATE_KEY="your-github-key" \
-e GITHUB_USER="your-github-username" \
-e GIT_USER_EMAIL="your-git-email" \
-e GIT_USER_NAME="your-git-name" \
docker.io/tonyzamyatin/torch-uncertainty
```

Optionally, you can also set `-e USER_COMPACT_SHELL_PROMPT="true"`
to make the VM's shell prompts compact and colorized.

**Note:** Some cloud providers offer templates, in which you can preconfigure
in advance which Docker image to pull and which environment variables to set.
In this case, the provider will pull the image, set all environment variables,
and start the container for you.

3. Once your cloud provider has deployed the VM, it will display the host address and SSH port.
You can connect to the container via SSH using:
```bash
ssh -i /path/to/private_key root@<VM_HOST> -p <VM_PORT>
```

Replace `<VM_HOST>` and `<VM_PORT>` with the values provided by your cloud provider,
and `/path/to/private_key` with the private key that corresponds to `VM_SSH_PUBLIC_KEY`.

4. The container exposes port `8888` in case you want to run Jupyter Notebooks or TensorBoard.

**Note:** The `/workspace` directory is mounted from your local machine or cloud storage,
so changes persist across container restarts.
If using a cloud provider, ensure your network volume is correctly attached to avoid losing data.

### Modifying and publishing custom Docker image

If you want to make changes to the Dockerfile, follow these steps:
1. Edit the Dockerfile to fit your needs.

2. Build the modified image:
```
docker build -t my-custom-image .
```

3. Push to a Docker registry (if you want to use it on another VM):
```
docker tag my-custom-image mydockerhubuser/my-custom-image:tag
docker push mydockerhubuser/my-custom-image:tag
```

4. Pull the custom image onto your VM:
```
docker pull mydockerhubuser/my-custom-image
```

5. Run the container using the same docker run command with the new image name.
131 changes: 131 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/bin/bash
set -e # Exit immediately if a command fails

echo "🚀 Starting container..."

# Ensure SSH directory exists and has correct permissions
if [ ! -d /root/.ssh ]; then
echo "📂 Creating SSH directory..."
mkdir -p /root/.ssh && chmod 700 /root/.ssh
fi

# Ensure the VM's public SSH key is added for authentication
if [ -z "$VM_SSH_PUBLIC_KEY" ]; then
echo "❌ Error: Please set the VM_SSH_PUBLIC_KEY environment variable."
exit 1
fi
if [ ! -f /root/.ssh/authorized_keys ] || ! grep -q "$VM_SSH_PUBLIC_KEY" /root/.ssh/authorized_keys; then
echo "🔑 Adding VM SSH public key..."
echo "$VM_SSH_PUBLIC_KEY" > /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys
fi

# Ensure GitHub SSH private key is set up for authentication
if [ -z "$GITHUB_SSH_PRIVATE_KEY" ]; then
echo "❌ Error: Please set the GITHUB_SSH_PRIVATE_KEY environment variable."
exit 1
fi
if [ ! -f /root/.ssh/github_rsa ]; then
echo "🔐 Adding GitHub SSH private key..."
echo "$GITHUB_SSH_PRIVATE_KEY" > /root/.ssh/github_rsa && chmod 600 /root/.ssh/github_rsa
fi

# Configure SSH client for GitHub authentication
if [ ! -f /root/.ssh/config ] || ! grep -q 'Host github.com' /root/.ssh/config; then
echo "⚙️ Configuring SSH client for GitHub authentication..."
cat <<EOF > /root/.ssh/config
Host github.com
User git
IdentityFile /root/.ssh/github_rsa
EOF
chmod 600 /root/.ssh/config
fi

# Add GitHub to known hosts (to avoid SSH verification prompts)
echo "📌 Ensuring GitHub is a known host..."
ssh-keygen -F github.com > /dev/null 2>&1 || ssh-keyscan github.com >> /root/.ssh/known_hosts

# Start SSH agent and add GitHub private key (if not already added)
if ! pgrep -x "ssh-agent" > /dev/null; then
echo "🕵️ Starting SSH agent..."
eval "$(ssh-agent -s)"
fi
if ssh-add -l | grep -q github_rsa; then
echo "✅ GitHub SSH key already added."
else
echo "🔑 Adding GitHub SSH key to agent..."
ssh-add /root/.ssh/github_rsa
fi

# Set Git user name and email (if provided)
if [ -n "$GIT_USER_NAME" ]; then
echo "👤 Setting Git username: $GIT_USER_NAME"
git config --global user.name "$GIT_USER_NAME"
fi
if [ -n "$GIT_USER_EMAIL" ]; then
echo "📧 Setting Git email: $GIT_USER_EMAIL"
git config --global user.email "$GIT_USER_EMAIL"
fi

# Ensure first-time setup runs only once
if [ ! -f /workspace/.setup_done ]; then
echo "🛠️ Running first-time setup..."

# Ensure GitHub username is set
if [ -z "$GITHUB_USER" ]; then
echo "❌ Error: Please set the GITHUB_USER environment variable."
exit 1
fi

# Clone GitHub repo if not already cloned
if [ ! -d "/workspace/.git" ]; then
echo "📦 Cloning repository: $GITHUB_USER/torch-uncertainty..."
git clone git@github.com:$GITHUB_USER/torch-uncertainty.git /workspace
fi

# Mark setup as completed
touch /workspace/.setup_done
echo "✅ First-time setup complete!"
else
echo "⏩ Skipping first-time setup (already done)."
fi

# Apply compact shell prompt customization (if enabled)
if [ -n "$USE_COMPACT_SHELL_PROMPT" ]; then
echo "🎨 Applying compact shell prompt customization..."
echo 'force_color_prompt=yes' >> /root/.bashrc
echo 'PS1="\[\033[01;34m\]\W\[\033[00m\]\$ "' >> /root/.bashrc
echo 'if [ -x /usr/bin/dircolors ]; then' >> /root/.bashrc
echo ' test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"' >> /root/.bashrc
echo ' alias ls="ls --color=auto"' >> /root/.bashrc
echo ' alias grep="grep --color=auto"' >> /root/.bashrc
echo ' alias fgrep="fgrep --color=auto"' >> /root/.bashrc
echo ' alias egrep="egrep --color=auto"' >> /root/.bashrc
echo 'fi' >> /root/.bashrc
fi

# Ensure /workspace is in PYTHONPATH
if ! echo "$PYTHONPATH" | grep -q "/workspace"; then
echo "📌 Adding /workspace to PYTHONPATH"
export PYTHONPATH="/workspace:$PYTHONPATH"
else
echo "✅ PYTHONPATH is already correctly set."
fi

# Check if torch_uncertainty is installed in editable mode
if pip show torch_uncertainty | grep -q "Editable project location: /workspace"; then
echo "✅ torch_uncertainty is already installed in editable mode. 🎉"
else
echo "🔄 Reinstalling torch_uncertainty in editable mode..."
pip uninstall -y torch-uncertainty
pip install -e /workspace
echo "✅ torch_uncertainty is now installed in editable mode! 🚀"
fi

# Activate pre-commit hooks (if enabled)
echo "🔗 Activating pre-commit hooks..."
pre-commit install

# Ensure SSH server is started
echo "🔑 Starting SSH server..."
mkdir -p /run/sshd && chmod 755 /run/sshd
/usr/sbin/sshd -D
2 changes: 2 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ Functions
:toctree: generated/
:nosignatures:

batch_ensemble
deep_ensembles
mc_dropout

Expand All @@ -212,6 +213,7 @@ Classes
:nosignatures:
:template: class.rst

BatchEnsemble
CheckpointEnsemble
EMA
MCDropout
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
f"{datetime.now().year!s}, Adrien Lafage and Olivier Laurent"
)
author = "Adrien Lafage and Olivier Laurent"
release = "0.4.1"
release = "0.4.2"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ model:
norm: torch.nn.Identity
groups: 1
dropout_rate: 0
last_layer_dropout: false
layer_args: {}
num_samples: 16
num_classes: 10
Expand Down
1 change: 0 additions & 1 deletion experiments/classification/mnist/configs/lenet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ model:
norm: torch.nn.Identity
groups: 1
dropout_rate: 0
last_layer_dropout: false
layer_args: {}
num_classes: 10
loss: CrossEntropyLoss
Expand Down
Loading