Skip to content

Commit

Permalink
Add examples for the Hessian
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenvivek committed Aug 8, 2024
1 parent 1524ebb commit 82b2fd3
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 1 deletion.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,47 @@ plt.show()
```

![](index_files/figure-commonmark/cell-5-output-1.png)

## Hessian matrix

``` python
from diptorch.filters import hessian, hessian_eigenvalues
from einops import rearrange
```

``` python
# Hessian matrix of an image (all second-order partial derivatives)
img = astronaut()

H = hessian(img, sigma=2.5, as_matrix=True)
H = rearrange(H, "B C1 C2 H W -> B (C1 H) (C2 W)")

plt.imshow(H.squeeze(), cmap="gray")
plt.axis("off")
plt.show()
```

![](index_files/figure-commonmark/cell-7-output-1.png)

``` python
# Eigenvalues of the Hessian matrix of an image
img = astronaut()
eig = hessian_eigenvalues(img, sigma=2.5)

plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.imshow(img.squeeze(), cmap="gray")
plt.axis("off")
plt.subplot(132)
plt.imshow(eig.squeeze()[0], cmap="gray")
plt.title("Smallest eigenvalue")
plt.axis("off")
plt.subplot(133)
plt.imshow(eig.squeeze()[1], cmap="gray")
plt.title("Largest eigenvalue")
plt.axis("off")
plt.tight_layout()
plt.show()
```

![](index_files/figure-commonmark/cell-8-output-1.png)
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ channels:
dependencies:
- pip
- pytorch
- einops
- matplotlib
- scikit-image
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions notebooks/index.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ status = 3
user = eigenvivek

### Optional ###
requirements = matplotlib scikit-image
requirements = einops matplotlib scikit-image
pip_requirements = torch
conda_requirements = pytorch
dev_requirements = nbdev black flake8 ipykernel ipywidgets jupyterlab jupyterlab_execute_time jupyterlab-code-formatter isort
Expand Down

0 comments on commit 82b2fd3

Please sign in to comment.