LPIPS is a Python library that implements the Learned Perceptual Image Patch Similarity metric. This metric is designed to measure the similarity between two images in a way that aligns more closely with human perception than traditional metrics like MSE or SSIM. It leverages deep features extracted from pre-trained convolutional neural networks (like AlexNet, VGG, or SqueezeNet). The library is currently at version 0.1.4 and is primarily maintained through its GitHub repository, with releases tied to significant updates. It's often used in image generation and restoration tasks to evaluate perceptual quality or as a perceptual loss function.
pip install lpipsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the LPIPS library, initialize the `LPIPS` model with different backbone networks ('alex' or 'vgg'), and compute the perceptual distance between two randomly generated PyTorch tensors. It highlights the crucial requirement for input images to be 3-channel RGB and normalized to the range `[-1, 1]`.
For `v0.1` and later, ensure `lpips=True` (which is the default) or upgrade to the latest version. If replicating results from papers using `v0.0`, you might need to manually set `lpips=False` to disable the linear scaling for compatibility.
Always ensure your input tensors have shape `(N, 3, H, W)` and pixel values are scaled to `[-1, 1]`. You can convert `[0, 1]` images to `[-1, 1]` using `img * 2 - 1` and grayscale `(N, 1, H, W)` images can be tiled to `(N, 3, H, W)` if appropriate for your use case.
When using LPIPS as a loss function in a generative model or similar optimization task, consider initializing `lpips.LPIPS(net='vgg')`. For pure evaluation, `net='alex'` is generally sufficient.
Be aware of this vulnerability when using LPIPS in security-sensitive contexts or for evaluating adversarial robustness. For more robust metrics, consider investigating alternative implementations like E-LPIPS or R-LPIPS if adversarial robustness is a critical concern.
Reduce batch size, lower image resolution, or consider using Automatic Mixed Precision (AMP) if supported by your PyTorch setup.