Skip to content
ChernobyllightPublic

About

Python toolkit for evaluating image style transfer with LPIPS, Art-Inception FID, ArtFID, and CFSD.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

๐ŸŽจ StyleEval

English | ็ฎ€ไฝ“ไธญๆ–‡

Compute LPIPS, FID, ArtFID, and CFSD for image style transfer. Provide content images, style references, and stylized outputs, then run the Python entry points to evaluate them.

  • evaluate.py computes all four metrics.
  • eval_lpips.py, eval_fid.py, eval_artfid.py, and eval_cfsd.py compute individual metrics.
  • Images in a sample can use different file formats.
  • Input images can have different resolutions and rectangular shapes. The default evaluation size is 512ร—512; use --size N to change it, for example --size 256 or --size 1024.
  • Supports CPU and NVIDIA CUDA. CPU is used automatically when CUDA is unavailable.

๐Ÿ”ง 1. Dependencies

Required packages: torch, torchvision, numpy, scipy, Pillow, lpips, and tqdm.

python -m pip install -r requirements.txt

๐Ÿ“ฆ 2. Model weights

Download the weights from Google Drive and place the required .pth files in the project's weights/ directory. Keep the original filenames.

Filename Required for Approx. size Download
alexnet-owt-7be5be79.pth LPIPS, ArtFID 244 MB Google Drive
lpips_alex_v0.1.pth LPIPS, ArtFID 6 KB Google Drive
art_inception.pth FID, ArtFID 108 MB Google Drive
vgg19-dcbb9e9d.pth CFSD 575 MB Google Drive

Computing all four metrics requires these four files, totaling approximately 927 MB. For a single metric, download only its listed weights. ArtFID reuses the LPIPS and FID weights.

weights/
โ”œโ”€โ”€ alexnet-owt-7be5be79.pth
โ”œโ”€โ”€ lpips_alex_v0.1.pth
โ”œโ”€โ”€ art_inception.pth
โ””โ”€โ”€ vgg19-dcbb9e9d.pth

Use --weights-dir /path/to/weights to select another directory. Evaluation automatically checks the local files and reports missing or corrupted weights.

๐Ÿ–ผ๏ธ 3. Data layout

Each subdirectory in data/ represents one sample with three images:

Filename stem Meaning
c Content image
s Style reference
cs Stylized output

For example:

data/
โ”œโ”€โ”€ 001/
โ”‚   โ”œโ”€โ”€ c.jpg
โ”‚   โ”œโ”€โ”€ s.jpg
โ”‚   โ””โ”€โ”€ cs.jpg
โ”œโ”€โ”€ 002/
โ”‚   โ”œโ”€โ”€ c.png
โ”‚   โ”œโ”€โ”€ s.png
โ”‚   โ””โ”€โ”€ cs.jpg
โ”œโ”€โ”€ 003/
โ”œโ”€โ”€ 004/
โ”œโ”€โ”€ 005/
โ””โ”€โ”€ 006/

Directories 003โ€“006 also contain c.jpg, s.jpg, and cs.jpg. You can choose your own directory names. The program reads immediate subdirectories in name order. Extra files and hidden entries are ignored, but every non-hidden subdirectory must contain a complete sample.

Extensions may differ, and filenames are case-insensitive. For example, c.jpeg, s.webp, and cs.PNG form a valid sample. Images are paired by their filename stems, not by file order.

Having both c.jpg and c.png in one sample produces an ambiguity error. Missing images also produce an error; samples are not silently skipped.

The six bundled samples, 001โ€“006, are 512ร—512 images with StyTRยฒ outputs as cs. They demonstrate the layout and evaluation workflow. Place your own outputs in the same structure and use a fixed test set for formal evaluation.

๐ŸŽจ 3.1 Sample previews

Sample Content c Style s StyTRยฒ output cs
001
002
003
004
005
006

๐Ÿš€ 4. Run evaluation

Run these commands from the project root.

Check image pairing and decoding without loading weights:

python evaluate.py --data-dir ./data --check-data

Compute all four metrics:

python evaluate.py --data-dir ./data --output ./results/all

Compute individual metrics:

python eval_lpips.py --data-dir ./data --output ./results/lpips
python eval_fid.py --data-dir ./data --output ./results/fid
python eval_artfid.py --data-dir ./data --output ./results/artfid
python eval_cfsd.py --data-dir ./data --output ./results/cfsd

eval_artfid.py also reports LPIPS and FID because ArtFID depends on them. The combined entry point reuses these results. All entry points use the same image loading, weights, and metric definitions.

Choose the device and data/weight locations:

python evaluate.py --data-dir /path/to/my_data --weights-dir /path/to/weights --device cuda:0 --output ./results/my_model
python evaluate.py --data-dir ./data --device cpu --output ./results/cpu_demo

cuda:0 is the first NVIDIA GPU visible to the process, respecting CUDA_VISIBLE_DEVICES. The program does not automatically select an idle GPU.

Running python evaluate.py without arguments uses the project's data/ and weights/ directories and creates a timestamped results directory. An explicitly specified output directory must not already exist. For another run, choose a new directory or omit --output.

๐Ÿ“Š 5. Options and metric definitions

Option Default Description
--data-dir Project data directory Parent directory of the sample folders
--weights-dir Project weights directory Location of the weight files
--output Timestamped directory Location for this run's results
--device auto Uses cuda:0 when available, otherwise CPU; can be set explicitly
--preprocess fixed fixed or styleid
--size 512 Square side length N for fixed preprocessing; evaluates at Nร—N
--batch-size 4 Batch size for FID feature extraction
--cfsd-chunk-size 256 CFSD rows per chunk; use 64/128 to reduce memory use
--fid-style-pool paired paired counts one reference per sample; unique deduplicates style files by SHA-256
--cpu-threads 4 Number of PyTorch CPU threads
--save-features Off Save FID features for further inspection
--check-data Off Check the data without computing metrics

Lower is better for all four metrics.

Metric Definition
LPIPS Compares cs with c using AlexNet and LPIPS v0.1 calibration weights, then averages over images
FID Compares the distributions of all cs images and style references s using 2048-dimensional Art-Inception features
ArtFID (1 + mean LPIPS) ร— (1 + FID), using statistics from the evaluation set
CFSD Compares cs with c following StyleID: VGG19 features[11] and KL divergence from the content patch-relation distribution to the stylized distribution

FID here uses the art-domain Art-Inception weights, rather than the weights used by standard ImageNet/Inception FID implementations. ArtFID uses finite-sample FID, not ArtFID-infinity. Before comparing published scores, match the feature network, weights, preprocessing, and evaluation samples.

FID aggregates features across the selected evaluation set and computes one score; it does not average per-batch FID scores. FID/ArtFID require at least two samples, with at least two style references after deduplication in unique mode. This is only a computational minimum and does not imply a reliable estimate.

๐Ÿ“ 5.1 Input and evaluation sizes

Input images do not need to be 512ร—512 or square. Sizes such as 256ร—256, 768ร—1024, and 1024ร—1024 are supported, and resolutions can vary within the input set. Style references s can differ in size from content images c; each content image and its stylized output cs should depict the same scene and crop.

  • Default --preprocess fixed: Apply EXIF orientation, convert to RGB, then resize proportionally and center-crop to Nร—N, as specified by --size N. The default is 512ร—512, even for larger input images. Rectangular inputs may lose their outer edges through cropping. Original files are unchanged.
  • Custom evaluation sizes: Use --size 256, --size 768, or --size 1024, for example. N must be an integer: Nโ‰ฅ75 when computing FID/ArtFID or all metrics, and Nโ‰ฅ32 for LPIPS or CFSD alone. --size accepts one side length, not a width-height pair such as 768x1024.
  • --preprocess styleid: LPIPS/FID resize the shorter edge to 512 while preserving the aspect ratio. CFSD uses the original dimensions, with both axes at least 32 pixels. Leave --size at its default of 512 in this mode. LPIPS requires matching processed dimensions for c and cs; CFSD requires matching original dimensions after orientation correction. This mode does not evaluate every metric at the original resolution.

Evaluate all four metrics at 256ร—256:

python evaluate.py --data-dir ./data --size 256 --output ./results/size256

Evaluate CFSD at the original image dimensions, including rectangular sizes:

python eval_cfsd.py --data-dir ./data --preprocess styleid --output ./results/cfsd_native

Larger sizes require more memory and compute, especially for CFSD. Lower --cfsd-chunk-size to reduce memory used by its chunked computation. The largest supported input also depends on the decoder and available memory. Use the same preprocessing and evaluation sizes when comparing methods.

CFSD sums over all patch rows for each image, then averages over images. CFSD_per_patch is an additional diagnostic value. Changing the resolution changes the scale of the main CFSD score. Chunking controls memory use without sampling patches or changing the formula.

๐Ÿ–ผ๏ธ 5.2 Supported image formats

Files are decoded by their contents with Pillow; there is no hard-coded extension whitelist. Common formats include JPEG/JPG, PNG, BMP, TIFF, WebP, GIF, and PPM. Support depends on the installed Pillow version and decoder plugins.

  • Grayscale images are converted to RGB; transparent images are composited onto white.
  • Only frame 0 is used for animated GIFs and multi-page TIFFs.
  • Install pillow-heif for HEIC/HEIF support; it is registered automatically when available.
  • Undecodable, corrupted, or truncated files produce an error.
  • High-bit-depth or floating-point HDR modes I, I;16, and F must first be converted to 8-bit RGB using an explicit conversion rule. RAW and SVG files may need another decoder or prior conversion.

Accepting arbitrary extensions does not mean that every encoding is supported. All preprocessing happens in memory; source images are unchanged.

๐Ÿ“ˆ 6. Results

Each successful run creates:

results/run_name/
โ”œโ”€โ”€ summary.json          # Overall scores, evaluation settings, and elapsed time
โ””โ”€โ”€ per_image.csv         # Per-image LPIPS / CFSD, depending on the selected metrics

--save-features also saves fid_features.npz. FID has no per-image score, so a FID-only CSV contains pairing information. Failed runs exit with a nonzero status and, if the output directory has already been created, write failed.json instead of a success report.

Compare methods using the same data, evaluation settings, and weights. Hardware and numerical libraries may cause differences in the scores.

๐Ÿ“‚ 7. Files and troubleshooting

styleeval/
โ”œโ”€โ”€ evaluate.py             # Combined entry point and shared evaluation flow
โ”œโ”€โ”€ eval_lpips.py            # LPIPS implementation and entry point
โ”œโ”€โ”€ eval_fid.py              # FID implementation and entry point
โ”œโ”€โ”€ eval_artfid.py           # ArtFID implementation and entry point
โ”œโ”€โ”€ eval_cfsd.py             # CFSD implementation and entry point
โ”œโ”€โ”€ common.py               # Pairing, image loading, and weight checks
โ”œโ”€โ”€ art_inception.py        # Art-Inception network architecture
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ README.md               # English documentation
โ”œโ”€โ”€ README_zh.md            # Chinese documentation
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ data/
โ””โ”€โ”€ weights/

Common issues:

  • torch/torchvision not found: Install dependencies with the same Python interpreter used to run evaluation, using python -m pip.
  • Missing weights or checksum mismatch: Download the files from Google Drive above, keep their original filenames, and check --weights-dir.
  • CUDA unavailable: Use --device cpu or install a compatible CUDA build of PyTorch.
  • Out of memory: Reduce --batch-size, then --cfsd-chunk-size. Changing the evaluation size also changes the scores.
  • Pairing errors: Keep exactly one c, one s, and one cs image in each sample directory. Pairing is based on names, not file order.

๐Ÿ“š 8. References and license

The implementations follow LPIPS, ArtFID, and StyleID. Cite the relevant original papers when using these metrics in research.

This project's own code is released under the MIT license; see LICENSE. Upstream code, weights, and images retain their respective terms.

About

Python toolkit for evaluating image style transfer with LPIPS, Art-Inception FID, ArtFID, and CFSD.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages