---
title: Learned Perceptual Image Patch Similarity (LPIPS)
description: >-
  The Learned Perceptual Image Patch Similarity (LPIPS_) calculates perceptual
  similarity between two images.
canonical: >-
  https://lightning.ai/docs/torchmetrics/stable/image/learned_perceptual_image_patch_similarity
---
# Learned Perceptual Image Patch Similarity (LPIPS) {#learned-perceptual-image-patch-similarity-lpips}

## Module Interface {#module-interface}

### `class torchmetrics.image.lpip.LearnedPerceptualImagePatchSimilarity(net_type='alex', reduction='mean', normalize=False, **kwargs)` {#torchmetrics.image.lpip.LearnedPerceptualImagePatchSimilarity}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/lpip.py#L41-L195)

The Learned Perceptual Image Patch Similarity (LPIPS\_) calculates perceptual similarity between two images.

LPIPS essentially computes the similarity between the activations of two image patches for some pre-defined network. This measure has been shown to match human perception well. A low LPIPS score means that image patches are perceptual similar.

Both input image patches are expected to have shape `(N, 3, H, W)`. The minimum size of H, W depends on the chosen backbone (see net\_type arg).

Hint

Using this metrics requires you to have `torchvision` package installed. Either install as `pip install torchmetrics[image]` or `pip install torchvision`.

As input to `forward` and `update` the metric accepts the following input

*   `img1` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): tensor with images of shape `(N, 3, H, W)`
    
*   `img2` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): tensor with images of shape `(N, 3, H, W)`
    

As output of forward and compute the metric returns the following output

*   `lpips` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): returns float scalar tensor with average LPIPS value over samples
    

Parameters:

*   **net\_type** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'vgg'`, `'alex'`, `'squeeze'`\]) – str indicating backbone network type to use. Choose between ‘alex’, ‘vgg’ or ‘squeeze’
    
*   **reduction** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'mean'`, `'sum'`, `'none'`\]\]) – str indicating how to reduce over the batch dimension. Choose between ‘sum’, ‘mean’,\`’none’\` or None.
    
*   **normalize** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – by default this is `False` meaning that the input is expected to be in the \[-1,1\] range. If set to `True` will instead expect input to be in the `[0,1]` range.
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Raises:

*   [**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If `torchvision` package is not installed
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `net_type` is not one of `"vgg"`, `"alex"` or `"squeeze"`
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `reduction` is not one of `"mean"` or `"sum"`
    

Example

```python
>>> from torch import rand
>>> from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
>>> lpips = LearnedPerceptualImagePatchSimilarity(net_type='squeeze')
>>> # LPIPS needs the images to be in the [-1, 1] range.
>>> img1 = (rand(10, 3, 100, 100) * 2) - 1
>>> img2 = (rand(10, 3, 100, 100) * 2) - 1
>>> lpips(img1, img2)
tensor(0.1024)
```

```python
>>> from torch import rand, Generator
>>> from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
>>> gen = Generator().manual_seed(42)
>>> lpips = LearnedPerceptualImagePatchSimilarity(net_type='squeeze', reduction='none')
>>> # LPIPS needs the images to be in the [-1, 1] range.
>>> img1 = (rand(2, 3, 100, 100, generator=gen) * 2) - 1
>>> img2 = (rand(2, 3, 100, 100, generator=gen) * 2) - 1
>>> lpips(img1, img2)
tensor([0.1024, 0.0938])
```

#### `plot(val=None, ax=None)` {#torchmetrics.image.lpip.LearnedPerceptualImagePatchSimilarity.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/lpip.py#L155-L195)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
>>> metric = LearnedPerceptualImagePatchSimilarity(net_type='squeeze')
>>> metric.update(torch.rand(10, 3, 100, 100), torch.rand(10, 3, 100, 100))
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/learned\_perceptual\_image\_patch\_similarity-1.png](/docs/torchmetrics/1.9.0/static/_images/learned_perceptual_image_patch_similarity-1.png)](/docs/torchmetrics/1.9.0/static/_images/learned_perceptual_image_patch_similarity-1.png)

```python
>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
>>> metric = LearnedPerceptualImagePatchSimilarity(net_type='squeeze')
>>> values = [ ]
>>> for _ in range(3):
...     values.append(metric(torch.rand(10, 3, 100, 100), torch.rand(10, 3, 100, 100)))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/learned\_perceptual\_image\_patch\_similarity-2.png](/docs/torchmetrics/1.9.0/static/_images/learned_perceptual_image_patch_similarity-2.png)](/docs/torchmetrics/1.9.0/static/_images/learned_perceptual_image_patch_similarity-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.image.learned_perceptual_image_patch_similarity(img1, img2, net_type='alex', reduction='mean', normalize=False)` {#torchmetrics.functional.image.learned_perceptual_image_patch_similarity}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/image/lpips.py#L411-L455)

The Learned Perceptual Image Patch Similarity (LPIPS\_) calculates perceptual similarity between two images.

LPIPS essentially computes the similarity between the activations of two image patches for some pre-defined network. This measure has been shown to match human perception well. A low LPIPS score means that image patches are perceptual similar.

Both input image patches are expected to have shape `(N, 3, H, W)`. The minimum size of H, W depends on the chosen backbone (see net\_type arg).

Parameters:

*   **img1** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – first set of images
    
*   **img2** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – second set of images
    
*   **net\_type** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'alex'`, `'vgg'`, `'squeeze'`\]) – str indicating backbone network type to use. Choose between ‘alex’, ‘vgg’ or ‘squeeze’
    
*   **reduction** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'sum'`, `'mean'`, `'none'`\]\]) – str indicating how to reduce over the batch dimension. Choose between ‘sum’, ‘mean’, ‘none’ or None.
    
*   **normalize** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – by default this is `False` meaning that the input is expected to be in the \[-1,1\] range. If set to `True` will instead expect input to be in the `[0,1]` range.
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

Example

```python
>>> from torch import rand
>>> from torchmetrics.functional.image.lpips import learned_perceptual_image_patch_similarity
>>> img1 = (rand(10, 3, 100, 100) * 2) - 1
>>> img2 = (rand(10, 3, 100, 100) * 2) - 1
>>> learned_perceptual_image_patch_similarity(img1, img2, net_type='squeeze')
tensor(0.1005)
```

```python
>>> from torch import rand, Generator
>>> from torchmetrics.functional.image.lpips import learned_perceptual_image_patch_similarity
>>> gen = Generator().manual_seed(42)
>>> img1 = (rand(2, 3, 100, 100, generator=gen) * 2) - 1
>>> img2 = (rand(2, 3, 100, 100, generator=gen) * 2) - 1
>>> learned_perceptual_image_patch_similarity(img1, img2, net_type='squeeze', reduction='none')
tensor([0.1024, 0.0938])
```

---

**Navigation**

- Parent: [Image](/docs/torchmetrics/1.9.0/image.md)
