<!-- DO NOT EDIT. -->
<!-- THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. -->
<!-- TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: -->
<!-- "auto_examples/self-supervised-learning/demo_ultrasound_despeckling.py" -->
<!-- LINE NUMBERS ARE GIVEN BELOW. -->

#### NOTE
New to DeepInverse? Get started with the basics with the
[5 minute quickstart tutorial](https://deepinv.org/auto_examples/basics/demo_quickstart.html.md#sphx-glr-auto-examples-basics-demo-quickstart-py)..

<a id="sphx-glr-auto-examples-self-supervised-learning-demo-ultrasound-despeckling-py"></a>

# Ultrasound despeckling from B-mode images

This example despeckles real clinical ultrasound B-mode images with Speckle2Self
<sup>[1](#footcite-li2025speckle2self)</sup>, a model pretrained without clean reference images.

We compare its performance against a classical BM3D [`deepinv.models.BM3D`](https://deepinv.org/api/stubs/deepinv.models.BM3D.html.md#deepinv.models.BM3D) baseline.

We use B-mode images from the carotid dataset acquired in Speckle2Self.

Ultrasound B-mode images are corrupted by speckle, a granular noise pattern that
arises from interference between backscattered echoes. Speckle lowers contrast and
hides fine tissue structure, which makes clinical interpretation harder.

Speckle arises from the beamforming inverse problem, but often in practice we only have access to
envelope-detected B-mode grayscale in the log domain. Therefore, here, we treat despeckling as a
standard image denoising problem.

This example requires the `ptwt` (for BM3D) and `zea` (for Speckle2Self) packages, which can be installed with
`pip install ptwt zea`.

<!-- GENERATED FROM PYTHON SOURCE LINES 23-30 -->
```Python
import deepinv as dinv
import torch

device = dinv.utils.get_device()
```

```none
Selected GPU 0 with 9822.4375 MiB free memory
```

<!-- GENERATED FROM PYTHON SOURCE LINES 32-42 -->

## Load the dataset

We download the Speckle2Self test set of 104 carotid B-mode images at 512x512 resolution,
acquired with a Clarius L7 portable scanner from two healthy volunteers, taken from the Clarius envelope collection API by Li *et al.*<sup>[1](#footcite-li2025speckle2self)</sup>.

#### NOTE
The data is envelope-detected B-mode grayscale, already in the log domain, where the multiplicative speckle of the linear domain can now be approximated by additive noise.

#### NOTE
For this demo, we show despeckling on two slices on CPU or four on GPU.

<!-- GENERATED FROM PYTHON SOURCE LINES 42-50 -->
```Python
url = "https://drive.usercontent.google.com/download?id=1rQxgyCzDuLao05tE8y5l8vw8YFrPjbzx&export=download"
y = dinv.io.load_np(dinv.io.load_url(url)).unsqueeze(1).to(device)  # (B, 1, H, W)

y = y[: 2 if dinv.utils.devices_equal(device, "cpu") else 4]

y = y / y.max()  # to allow a meaningful sigma
```

<!-- GENERATED FROM PYTHON SOURCE LINES 51-60 -->

## Despeckle with BM3D

As a classical baseline, we use a custom efficient implementation of BM3D
<sup>[2](#footcite-dabov2007image)</sup>, faster particularly on GPU. The `sigma` argument sets the assumed noise level.

#### NOTE
Fully-developed speckle is Gamma-distributed, so in log domain, the Gaussian is only an approximation for BM3D.
See the [denoisers user guide](https://deepinv.org/user_guide/reconstruction/denoisers.html.md#denoisers) for further classical and deep denoisers.
For example, you can train your own with [`deepinv.physics.FisherTippettNoise`](https://deepinv.org/api/stubs/deepinv.physics.FisherTippettNoise.html.md#deepinv.physics.FisherTippettNoise), which models the noise more accurately.

<!-- GENERATED FROM PYTHON SOURCE LINES 60-65 -->
```Python
model = dinv.models.BM3D(use_legacy=False, device=device)
with torch.no_grad():
    x_hat = model(y, sigma=0.3)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 66-69 -->

## Despeckle with Speckle2Self

We run the pretrained in-vivo Speckle2Self model, which is available in the `zea` toolbox <sup>[3](#footcite-stevens2026zea)</sup>.

<!-- GENERATED FROM PYTHON SOURCE LINES 69-80 -->
```Python
import os

os.environ["KERAS_BACKEND"] = "torch"
from zea.models.speckle2self import Speckle2Self

model = Speckle2Self.from_preset("hf://zeahub/speckle2self-invivo").to(device)

with torch.no_grad():
    x_net = model(y.moveaxis(1, -1)).moveaxis(-1, 1)  # zea expects channel last
```

```none
zea: Using backend 'torch'
```

<!-- GENERATED FROM PYTHON SOURCE LINES 81-84 -->

## Visualise the results

We compare the input B-mode image with the BM3D and Speckle2Self reconstructions.

<!-- GENERATED FROM PYTHON SOURCE LINES 84-90 -->
```Python
dinv.utils.plot(
    {"Vendor bmode": y, "BM3D": x_hat, "Speckle2Self": x_net},
    figsize=(7, 10),
)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 91-94 -->
* **References:**

* <a id='footcite-li2025speckle2self'>**[1]**</a> Xuesong Li, Nassir Navab, and Zhongliang Jiang. Speckle2self: self-supervised ultrasound speckle reduction without clean data. *Medical Image Analysis*, 106:103755, 2025.
* <a id='footcite-dabov2007image'>**[2]**</a> Kostadin Dabov, Alessandro Foi, Vladimir Katkovnik, and Karen Egiazarian. Image denoising by sparse 3-d transform-domain collaborative filtering. *IEEE Transactions on image processing*, 16(8):2080–2095, 2007.
* <a id='footcite-stevens2026zea'>**[3]**</a> Tristan S. W. Stevens, Wessel L. van Nierop, Ben Luijten, Vincent van de Schaft, Oisín Nolan, Beatrice Federici, Louis D. van Harten, Simon W. Penninga, Noortje I. P. Schueler, and Ruud J. G. van Sloun. Zea: a toolbox for cognitive ultrasound imaging. *Journal of Open Source Software*, 11(121):9881, 2026.

**Total running time of the script:** (0 minutes 9.606 seconds)

🏷 Tags: Denoising, Ultrasound

<a id="sphx-glr-download-auto-examples-self-supervised-learning-demo-ultrasound-despeckling-py"></a>

[`Download Jupyter notebook: demo_ultrasound_despeckling.ipynb`](https://deepinv.org/auto_examples/self-supervised-learning/_downloads/3ac45b8a7d0e656c52641779c7ac437c/demo_ultrasound_despeckling.ipynb)

[`Download Python source code: demo_ultrasound_despeckling.py`](https://deepinv.org/auto_examples/self-supervised-learning/_downloads/19dab0543560f4009cf3f0e8fff47d3a/demo_ultrasound_despeckling.py)

[`Download zipped: demo_ultrasound_despeckling.zip`](https://deepinv.org/auto_examples/self-supervised-learning/_downloads/b7d78bada90be43bfa1d48e93003a72d/demo_ultrasound_despeckling.zip)

[Gallery generated by Sphinx-Gallery](https://sphinx-gallery.github.io)
