LPIPS (Learned Perceptual Image Patch Similarity)

0.1.4 · active · verified Mon Apr 13

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.

Warnings

Install

Imports

Quickstart

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]`.

import torch
import lpips

# Ensure PyTorch is set up (e.g., for GPU if available)
# LPIPS does not typically require API keys for model loading

# Initialize the LPIPS model, using AlexNet as the default backbone
# 'alex' is recommended for best forward scores, 'vgg' for perceptual loss in optimization
loss_fn_alex = lpips.LPIPS(net='alex')

# Create two dummy images (batch_size, channels, height, width)
# IMPORTANT: Images should be RGB (3 channels) and normalized to [-1, 1]
img0 = torch.rand(1, 3, 64, 64) * 2 - 1  # Random image 1, normalized to [-1, 1]
img1 = torch.rand(1, 3, 64, 64) * 2 - 1  # Random image 2, normalized to [-1, 1]

# Compute the LPIPS distance
d = loss_fn_alex(img0, img1)

print(f"LPIPS distance: {d.item():.4f}")

# Example with VGG network, often preferred for 'perceptual loss' in training
loss_fn_vgg = lpips.LPIPS(net='vgg')
d_vgg = loss_fn_vgg(img0, img1)
print(f"LPIPS distance (VGG): {d_vgg.item():.4f}")

view raw JSON →