Deep learning (24): Calculate LPIPS and SSIM indicators of two images (python code)

1. Calculate LPIPS

1.0. Description

LPIPS: Learned Perceptual Image Patch Similarity (LPIPS), also known as "perceptual loss", is used to measure the difference between two images. From CVPR2018 "The Unreasonable Effectiveness of Deep Features as a Perceptual Metric"

1.1. Code

To calculate the LPIPS (Learned Perceptual Image Patch Similarity) distance between two pictures, you can use the already trained LPIPS model for calculation. The following is a sample code that demonstrates how to calculate the LPIPS distance of two images using PyTorch and the LPIPS model:

import torch
import lpips
from PIL import Image

# 假设您已经有了要计算LPIPS距离的两张图片 image1 和 image2
# 加载图像文件
image1 = Image.open("image1.jpg")
image2 = Image.open("image2.jpg")

# 加载预训练的LPIPS模型
lpips_model = lpips.LPIPS(net="alex")

# 将图像转换为PyTorch的Tensor格式
image1_tensor = torch.tensor(np.array(image1)).permute(2, 0, 1).unsqueeze(0).float() / 255.0
image2_tensor = torch.tensor(np.array(image2)).permute(2, 0, 1).unsqueeze(0).float() / 255.0

# 使用LPIPS模型计算距离
distance = lpips_model(image1_tensor, image2_tensor)

print("LPIPS distance:", distance.item())

In the above code, you need to store the two images to calculate the LPIPS distance in the variables image1 and image2 (you can use the Image.open() function of the PIL library to load image files).

Then, we loaded the pre-trained LPIPS model, here using the alex network. You can select other available networks such as vggor squeeze.

Next, we convert the image to PyTorch's Tensor format and perform normalization to normalize the pixel value range from [0, 255] to [0, 1]. Note that the image order here is the channel first, that is, the shape is [C, H, W].

Finally, we use the LPIPS model to calculate the Tensor of the two images to get the LPIPS distance. The value of the distance is obtained by distance.item().

Please make sure you have PyTorch and lpips library installed. You can install the lpips library with the following command:

pip install lpips

2. Calculate SSIM

2.0 Description

Structural Similarity Index (SSIM, Structural Similarity Index)

2.1 Code

Install

pip install scikit-image
import cv2
from skimage import metrics

# Assume you have two image files: image1.jpg and image2.jpg

# Load the images
image1 = cv2.imread("image1.jpg")
image2 = cv2.imread("image2.jpg")

# Convert images to grayscale
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray_image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

# Compute SSIM
ssim_score = metrics.structural_similarity(gray_image1, gray_image2)

print("SSIM score:", ssim_score)

The image files are loaded using the cv2.imread() function and stored in variables image1 and image2 respectively.

Then, the image is converted to a grayscale image in order to calculate SSIM. Use the cv2.cvtColor() function to convert the image from BGR color space to grayscale color space.

Finally, use the skimage.measure.compare_ssim() function to calculate the SSIM between the two grayscale images.

Guess you like

Origin blog.csdn.net/BIT_HXZ/article/details/131079400