# MotionBlurGenerator

### *class* deepinv.physics.generator.MotionBlurGenerator(psf_size, rng=None, device='cpu', dtype=torch.float32, l=0.3, sigma=0.25, n_steps=1000)

Bases: [`PSFGenerator`](https://deepinv.org/api/stubs/deepinv.physics.generator.PSFGenerator.html.md#deepinv.physics.generator.PSFGenerator)

Random motion blur generator.

See Schuler *et al.*<sup>[1](#footcite-schuler2015learning)</sup> for more details.

A blur trajectory is generated by sampling both its x- and y-coordinates independently
from a Gaussian Process with a Matérn 3/2 covariance function.

$$
f_x(t), f_y(t) \sim \mathcal{GP}(0, k(t, t'))
$$

where $k$ is defined as

$$
k(t, s) = \sigma^2 \left( 1 + \frac{\sqrt{5} |t -s|}{l} + \frac{5 (t-s)^2}{3 l^2} \right) \exp \left(-\frac{\sqrt{5} |t-s|}{l}\right)
$$

* **Parameters:**
  * **psf_size** ([*int*](https://docs.python.org/3.12/library/functions.html#int) *,* [*tuple*](https://docs.python.org/3.12/library/stdtypes.html#tuple) *[*[*int*](https://docs.python.org/3.12/library/functions.html#int) *]*) – the shape of the generated PSF in 2D, should be `(kernel_size, kernel_size)`. If an `int` is given, the same value will be used for both dimensions.
  * **l** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – the length scale of the trajectory, defaults to 0.3
  * **sigma** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – the standard deviation of the Gaussian Process, defaults to 0.25
  * **n_steps** ([*int*](https://docs.python.org/3.12/library/functions.html#int)) – the number of points in the trajectory, defaults to 1000

#### NOTE
Always generates single-channel PSFs of shape `(B, 1, H, W)`.

<hr />

* **Examples:**

```pycon
>>> from deepinv.physics.generator import MotionBlurGenerator
>>> generator = MotionBlurGenerator((5, 5))
>>> blur = generator.step()  # dict_keys(['filter'])
>>> print(blur['filter'].shape)
torch.Size([1, 1, 5, 5])
```

<hr />

* **References:**

* <a id='footcite-schuler2015learning'>**[1]**</a> Christian J Schuler, Michael Hirsch, Stefan Harmeling, and Bernhard Schölkopf. Learning to deblur. *IEEE transactions on pattern analysis and machine intelligence*, 38(7):1439–1451, 2015.

#### f_matern(batch_size=1, sigma=None, l=None)

Generates the trajectory.

* **Parameters:**
  * **batch_size** ([*int*](https://docs.python.org/3.12/library/functions.html#int)) – batch_size.
  * **sigma** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – the standard deviation of the Gaussian Process.
  * **l** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – the length scale of the trajectory.
* **Returns:**
  the trajectory of shape `(batch_size, n_steps)`

#### matern_kernel(diff, sigma=None, l=None)

Compute the Matérn 3/2 covariance.

* **Parameters:**
  * **diff** ([*torch.Tensor*](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) – the difference `t - s`
  * **sigma** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – the standard deviation of the Gaussian Process
  * **l** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – the length scale of the trajectory

#### step(batch_size=1, sigma=None, l=None, seed=None, \*\*kwargs)

Generate a random motion blur PSF with parameters $\sigma$ and $l$

* **Parameters:**
  * **batch_size** ([*int*](https://docs.python.org/3.12/library/functions.html#int)) – batch_size.
  * **sigma** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – the standard deviation of the Gaussian Process
  * **l** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – the length scale of the trajectory
  * **seed** ([*int*](https://docs.python.org/3.12/library/functions.html#int)) – the seed for the random number generator.
* **Returns:**
  dictionary with keys
  - `filter`: the generated PSF of shape `(batch_size, 1, psf_size[0], psf_size[1])`

<a id="sphx-glr-backref-deepinv-physics-generator-motionblurgenerator"></a>

## Examples using `MotionBlurGenerator`:

<div class="sphx-glr-thumbnails">
<!-- thumbnail-parent-div-open --><div class="sphx-glr-thumbcontainer" tooltip="This example shows you how to train various networks using adversarial training for deblurring problems. We demonstrate running training and inference using a conditional GAN (i.e. DeblurGAN), CSGM, AmbientGAN and UAIR implemented in the library, and how to simply train your own GAN by using deepinv.training.AdversarialTrainer. These examples can also be easily extended to train more complicated GANs such as CycleGAN.">![](auto_examples/adversarial-learning/images/thumb/sphx_glr_demo_gan_imaging_thumb.png)

[Imaging inverse problems with adversarial networks](https://deepinv.org/auto_examples/adversarial-learning/demo_gan_imaging.html.md)

  <div class="sphx-glr-thumbnail-title">Imaging inverse problems with adversarial networks</div>
</div><div class="sphx-glr-thumbcontainer" tooltip="Follow this example to get started with DeepInverse in under 5 minutes.">![](auto_examples/basics/images/thumb/sphx_glr_demo_quickstart_thumb.png)

[5 minute quickstart tutorial](https://deepinv.org/auto_examples/basics/demo_quickstart.html.md)

  <div class="sphx-glr-thumbnail-title">5 minute quickstart tutorial</div>
</div><div class="sphx-glr-thumbcontainer" tooltip="This demo shows you how to use deepinv.physics.Physics together with automatic differentiation to estimate unknown parameters of your forward operator.">![](auto_examples/blind-inverse-problems/images/thumb/sphx_glr_demo_optimizing_physics_parameter_thumb.png)

[Calibrating physics operators](https://deepinv.org/auto_examples/blind-inverse-problems/demo_optimizing_physics_parameter.html.md)

  <div class="sphx-glr-thumbnail-title">Calibrating physics operators</div>
</div><div class="sphx-glr-thumbcontainer" tooltip="This example shows how to perform inference on and fine-tune the Reconstruct Anything Model (RAM) foundation model terris2025reconstruct to solve inverse problems.">![](auto_examples/models/images/thumb/sphx_glr_demo_foundation_model_thumb.png)

[Inference and fine-tune a foundation model](https://deepinv.org/auto_examples/models/demo_foundation_model.html.md)

  <div class="sphx-glr-thumbnail-title">Inference and fine-tune a foundation model</div>
</div><div class="sphx-glr-thumbcontainer" tooltip="This example provides a tour of 2D blur operators in DeepInverse. In particular, we show how to use DiffractionBlurs (Fresnel diffraction), motion blurs and space varying blurs.">![](auto_examples/physics/images/thumb/sphx_glr_demo_blur_tour_thumb.png)

[Tour of blur operators](https://deepinv.org/auto_examples/physics/demo_blur_tour.html.md)

  <div class="sphx-glr-thumbnail-title">Tour of blur operators</div>
</div>
<!-- thumbnail-parent-div-close --></div>
