Skip to main content

LPIPS (Learned Perceptual Image Patch Similarity) implementation in Flax NNX

Project description

LPIPS-NNX

LPIPS (Learned Perceptual Image Patch Similarity) implementation in Flax NNX, with weights that exactly match the official PyTorch implementation.

No PyTorch dependency at runtime - VGG16 weights are loaded from HuggingFace safetensors, LPIPS linear weights are bundled with the package.

Installation

uv add lpips-nnx

Usage

import flax.nnx as nnx
import jax.numpy as jnp
from lpips_nnx import LPIPS

# Initialize LPIPS (downloads VGG16 weights on first use, ~553MB cached)
lpips = LPIPS(rngs=nnx.Rngs(0))

# Create sample images (NHWC format, values in [-1, 1])
x = jnp.zeros((1, 224, 224, 3))
y = jnp.ones((1, 224, 224, 3)) * 0.5

# Compute perceptual distance
distance = lpips(x, y)
print(f"LPIPS distance: {distance.item():.4f}")

Input formats

Images should be in NHWC format (batch, height, width, channels) with values in [-1, 1]:

# If your images are in [0, 1] range, use normalize=True
distance = lpips(x_01, y_01, normalize=True)

JIT compilation

LPIPS is compatible with JAX's JIT compilation:

import jax

@jax.jit
def compute_lpips(x, y):
    return lpips(x, y)

distance = compute_lpips(x, y)

Dependencies

Runtime (lightweight):

  • jax, jaxlib, flax
  • safetensors - for loading VGG16 weights
  • requests - for downloading weights

Development only:

  • torch, torchvision, lpips - for comparison tests

Acknowledgments

References

Zhang, R., Isola, P., Efros, A. A., Shechtman, E., & Wang, O. (2018). The Unreasonable Effectiveness of Deep Features as a Perceptual Metric. CVPR.

License

MIT (see LICENSE for third-party attributions)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lpips_nnx-0.1.0.tar.gz (124.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lpips_nnx-0.1.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file lpips_nnx-0.1.0.tar.gz.

File metadata

  • Download URL: lpips_nnx-0.1.0.tar.gz
  • Upload date:
  • Size: 124.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.21

File hashes

Hashes for lpips_nnx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a545ab77e9b7293a3fb2bab4a025c6eae85adf4a7e5ab8b4c2fbe17c26c259d9
MD5 a1938e2910f16bf9b5ca75aea38d08d9
BLAKE2b-256 943044a918aa67573621c5133ad3c2da999697642519b4a77cbb8d7af61f7f8e

See more details on using hashes here.

File details

Details for the file lpips_nnx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: lpips_nnx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.21

File hashes

Hashes for lpips_nnx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc09f985a0f8969f51ae0d53cb2d53522a4d006e83a049eb4aa7628aa9ba1da4
MD5 ffd462e35cabc9bfc14b86ff0c483568
BLAKE2b-256 0cc998c3005d94150676e2298e8a753ed4f781e0ed4c77023d29ba08f13fe1c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page