Skip to content

Commit

Permalink
Merge pull request #115 from aai-institute/feature/dvc
Browse files Browse the repository at this point in the history
Feature: dvc
  • Loading branch information
Samuel Burbulla authored Apr 10, 2024
2 parents 544631d + 3e00117 commit 7ce9041
Show file tree
Hide file tree
Showing 36 changed files with 493 additions and 25 deletions.
1 change: 1 addition & 0 deletions benchmarks/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.png filter=lfs diff=lfs merge=lfs -text
File renamed without changes.
File renamed without changes
File renamed without changes
3 changes: 3 additions & 0 deletions benchmarks/html/img/ns_test_144.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions benchmarks/html/img/ns_test_179.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions benchmarks/html/img/ns_train_237.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions benchmarks/html/img/ns_train_420.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions benchmarks/html/navierstokes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion benchmarks/html/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
line-height: 1.2;
}

.benchmark-table td, table th {
.benchmark-table td {
text-align: left;
vertical-align: middle;
min-width: 90px;
}

.benchmark-table th {
text-align: left;
vertical-align: middle;
}
Expand Down
89 changes: 89 additions & 0 deletions benchmarks/navierstokes/plot_navierstokes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import torch
import matplotlib.pyplot as plt
from continuity.benchmarks import NavierStokes
from continuity.operators import FourierNeuralOperator

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

ns = NavierStokes()

operator = FourierNeuralOperator(
ns.train_dataset.shapes,
grid_shape=(64, 64, 10),
width=32,
depth=4,
device=device,
)

operator.load(
"mlruns/271016623891034109/8755b17d3af9494db843e3a8d0c42ad6/artifacts/final.pt"
)
operator.eval()

# Compute train loss
loss_fn = ns.losses[0]


def compute_loss(dataset):
train_loader = torch.utils.data.DataLoader(dataset, batch_size=1)
avg_loss = 0
max_loss, min_loss = 0, 1e10
max_i, min_i = 0, 0
for i, xuyv in enumerate(train_loader):
x, u, y, v = [t.to(device) for t in xuyv]
loss = loss_fn(operator, x, u, y, v)
avg_loss += loss.detach()
if loss > max_loss:
max_loss = loss
max_i = i
if loss < min_loss:
min_loss = loss
min_i = i
avg_loss = avg_loss / len(train_loader)
return avg_loss, max_loss, max_i, min_loss, min_i


loss_train, max_loss, max_i_train, min_loss, min_i_train = compute_loss(
ns.train_dataset
)
print(f"rel. error train = {loss_train:.4e}")
print(f"min loss = {min_loss:.4e} at index {min_i_train}")
print(f"max loss = {max_loss:.4e} at index {max_i_train}")

# Compute test loss
loss_test, max_loss, max_i_test, min_loss, min_i_test = compute_loss(ns.test_dataset)
print(f"rel. error test = {loss_test:.4e}")
print(f"min loss = {min_loss:.4e} at index {min_i_test}")
print(f"max loss = {max_loss:.4e} at index {max_i_test}")


# Plot
def plot_sample(split, sample):
dataset = ns.train_dataset if split == "train" else ns.test_dataset
x, u, y, v = [t.to(device) for t in dataset[sample : sample + 1]]
v_pred = operator(x, u, y)
v = v.reshape(1, 64, 64, 10, 1).cpu()
v_pred = v_pred.reshape(1, 64, 64, 10, 1).detach().cpu()

fig, axs = plt.subplots(10, 3, figsize=(4, 16))

axs[0][0].set_title("Truth")
axs[0][1].set_title("Prediction")
axs[0][2].set_title("Error")
for t in range(10):
axs[t][0].imshow(v[0, :, :, t, 0], cmap="jet")
axs[t][1].imshow(v_pred[0, :, :, t, 0], cmap="jet")
im = axs[t][2].imshow((v - v_pred)[0, :, :, t, 0], cmap="jet")
fig.colorbar(im, ax=axs[t][2])
axs[t][0].axis("off")
axs[t][1].axis("off")
axs[t][2].axis("off")

plt.tight_layout()
plt.savefig(f"navierstokes/ns_{split}_{sample}.png", dpi=500)


plot_sample("train", min_i_train)
plot_sample("train", max_i_train)
plot_sample("test", min_i_test)
plot_sample("test", max_i_test)
16 changes: 16 additions & 0 deletions benchmarks/navierstokes/results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rel. error train = 1.8508e-02
min loss = 8.8748e-03 at index 237
max loss = 3.1433e-02 at index 420
rel. error test = 1.8408e-01
min loss = 1.0220e-01 at index 144
max loss = 4.4655e-01 at index 179

--

Reference: _Li, Zongyi, et al. "Fourier neural operator for parametric partial
differential equations." arXiv preprint arXiv:2010.08895 (2020)_

Table 1: nu=1e−5 T=20 N=1000
FNO-3D: 0.1893

(Ours: 1.8408e-01)
20 changes: 20 additions & 0 deletions benchmarks/navierstokes/run_navierstokes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from functools import partial
from continuity.benchmarks.run import BenchmarkRunner, RunConfig
from continuity.benchmarks import NavierStokes
from continuity.operators import FourierNeuralOperator

config = RunConfig(
benchmark_factory=NavierStokes,
operator_factory=partial(
FourierNeuralOperator,
grid_shape=(64, 64, 10),
width=32,
depth=4,
),
lr=1e-3,
max_epochs=100,
batch_size=10,
)

if __name__ == "__main__":
BenchmarkRunner.run(config)
2 changes: 2 additions & 0 deletions build_scripts/copy_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ def on_shutdown():
file.unlink()
for file in docs_benchmarks_dir.glob("*.png"):
file.unlink()
for file in docs_benchmarks_dir.glob("*.svg"):
file.unlink()
3 changes: 3 additions & 0 deletions data/.dvc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/config.local
/tmp
/cache
4 changes: 4 additions & 0 deletions data/.dvc/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[core]
remote = gdrive
['remote "gdrive"']
url = gdrive://1Mts9tmPjKzqw-as_j1XTRwa9ulNssy5a
3 changes: 3 additions & 0 deletions data/.dvcignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Add patterns of files dvc should ignore, which could improve
# the performance. Learn more at
# https://dvc.org/doc/user-guide/dvcignore
2 changes: 2 additions & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/flame
/navierstokes
37 changes: 37 additions & 0 deletions data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Data

## Prerequisites

We use `dvc` to manage the data. You can install the required packages by
installing the benchmarks requirements.

```
pip install -e .[benchmarks]
```

## Downloading the data

The data is stored in a remote storage on GDrive.
To download the data, you can run:

```
cd data
dvc pull <NAME>
```

where `<NAME>` is the name of the data set you want to download,
e.g., `flame` or `navierstokes`, or empty.


## Data sets

### FLAME

`data/flame` contains the dataset from [2023 FLAME AI
Challenge](https://www.kaggle.com/competitions/2023-flame-ai-challenge/data).

### Navier-Stokes

`data/navierstokes` contains a part of the dataset linked in
[neuraloperator/graph-pde](https://github.com/neuraloperator/graph-pde)
(Zongyi Li et al. 2020).
5 changes: 5 additions & 0 deletions data/flame.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
outs:
- md5: 2e61c8311b09a4fdf29d3ec3527cf629.dir
size: 415040265
nfiles: 13138
path: flame
2 changes: 0 additions & 2 deletions data/flame/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions data/flame/README.md

This file was deleted.

6 changes: 6 additions & 0 deletions data/navierstokes.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
outs:
- md5: 0bb228674e976bab14e9493606e14a27.dir
size: 412877192
nfiles: 1
hash: md5
path: navierstokes
2 changes: 2 additions & 0 deletions docs/benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
table.html
style.css
img
*.png
*.svg
37 changes: 36 additions & 1 deletion docs/benchmarks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@ This is an overview of some benchmark results to compare the performance
of different operator architectures on various problems.

The benchmarks are implemented in the `benchmarks` directory and we refer to
this directory for more information on how the benchmarks are run.
this directory for detailed information on how the benchmarks are run.

## [NavierStokes](../api/continuity/benchmarks/#continuity.benchmarks.NavierStokes)

Reference: _Li, Zongyi, et al. "Fourier neural operator for parametric partial
differential equations." arXiv preprint arXiv:2010.08895 (2020)_ _Table 1 ($\nu$ = 1e−5 T=20 N=1000)_

_reported for_ FNO-3D: __0.1893__ (rel. test error)

[FourierNeuralOperator](../api/continuity/operators/#continuity.operators.FourierNeuralOperator):
0.0185 (rel. train error) __0.1841__ (rel. test error)

<table>
<tr>
<td>
Best training sample<br>
<img src="img/ns_train_237.png" alt="Best training sample"/>
rel. error = 8.8748e-03
</td>
<td>
Worst training sample<br>
<img src="img/ns_train_420.png" alt="Worst training sample"/>
rel. error = 3.1433e-02
</td>
<td>
Best test sample<br>
<img src="img/ns_test_144.png" alt="Best test sample"/>
rel. error = 1.0220e-01
</td>
<td>
Worst test sample<br>
<img src="img/ns_test_179.png" alt="Worst test sample"/>
rel. error = 4.4655e-01
</td>
</tr>
</table>

{% include 'benchmarks/table.html' %}
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ docs = [
"pygments",
]
benchmark = [
"dvc",
"dvc-gdrive",
"mlflow",
"optuna>=3.5.0,<4.0.0",
"scipy",
]

[tool.setuptools.dynamic]
Expand Down
3 changes: 2 additions & 1 deletion src/continuity/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
from .benchmark import Benchmark
from .sine import SineRegular, SineUniform
from .flame import Flame
from .navierstokes import NavierStokes

__all__ = ["Benchmark", "SineRegular", "SineUniform", "Flame"]
__all__ = ["Benchmark", "SineRegular", "SineUniform", "Flame", "NavierStokes"]
Loading

0 comments on commit 7ce9041

Please sign in to comment.