MATLAB-aligned Python package for FADE fog density estimation.
Project description
pyfade
pyfade is a MATLAB-aligned Python package for FADE fog density estimation.
It packages the optimized Python implementation together with the original
reference model parameters and exposes one public entry point that accepts:
- a folder path
- a single image path
- a NumPy array
- a PyTorch tensor
The implementation is designed to match the MATLAB FADE code path closely:
float64image statistics- MATLAB-style
8x8patch trimming and column-major patch order - MATLAB-compatible
rgb2gray,rgb2hsv,fspecial('gaussian') imfilter(...,'replicate'),conv2(...,'same'),entropy,nanvar, andstd- direct use of the original FADE
.matreference models
Installation
Install from a local checkout:
cd pyfade
pip install .
Editable install for development:
cd pyfade
pip install -e ".[dev]"
Install with PyTorch input support:
cd pyfade
pip install ".[tensor]"
Once published, the distribution can be installed from PyPI with:
pip install fade-python
The published distribution name is fade-python, while the import package and
CLI command remain pyfade.
Usage
from pyfade import fade
Accepted inputs
- Image path:
.jpg,.jpeg,.png,.bmp,.tif,.tiff - Folder path: a flat directory of supported image files
.npypath: loaded and processed as a NumPy array- NumPy arrays:
(H, W, 3),(3, H, W),(B, H, W, 3), or(B, 3, H, W) - PyTorch tensors:
(H, W, 3),(3, H, W),(B, H, W, 3), or(B, 3, H, W)
Input conventions
uint8inputs are interpreted as0..255images- Floating-point inputs in
[0, 1]are treated as normalized images and scaled to0..255 - Other floating-point inputs are treated as already being in
0..255 - Tensor inputs are copied to CPU and processed through the same MATLAB-aligned NumPy path
Return values
- Single image input:
float, or(score, density_map)whenreturn_map=True - Batch array/tensor input:
scoreswith shape(B,), or(scores, density_maps)whenreturn_map=True - Folder input:
FolderResultwithscores, optionaldensity_maps, andmean_score,min_score,max_score
Density maps always follow MATLAB-style patch trimming, so their shape is based
on the trimmed (H // 8, W // 8) patch grid.
Examples
Single image path:
score = fade("/path/to/image.png")
score, density_map = fade("/path/to/image.png", return_map=True)
Folder path:
result = fade("/path/to/folder", workers=4, progress=True)
print(result.mean_score)
print(result.scores["example.png"])
NumPy array:
import numpy as np
image = np.zeros((256, 256, 3), dtype=np.uint8)
score = fade(image)
batch = np.random.randint(0, 256, size=(8, 3, 256, 256), dtype=np.uint8)
scores, density_maps = fade(batch, workers=4, progress=True, return_map=True)
PyTorch tensor:
import torch
batch = torch.randint(0, 256, (8, 3, 256, 256), dtype=torch.uint8)
scores = fade(batch, workers=4, progress=True)
normalized = batch.to(torch.float32) / 255.0
scores, density_maps = fade(normalized, workers=4, progress=True, return_map=True)
Parallelism and Progress
workerscontrols image-level concurrencyprogress=Trueenables atqdmprogress bar for folder and batch evaluation
Example:
result = fade("/path/to/folder", workers=8, progress=True)
scores = fade(batch_tensor, workers=4, progress=True)
CLI
The package installs a pyfade command and is imported with import pyfade:
pyfade /path/to/image.png
pyfade /path/to/folder --workers 4 --progress
pyfade /path/to/array.npy --workers 4
pyfade /path/to/image.png --return-map
Benchmark and Precision Summary
The table below uses the 5PRISM dataset with 4322 images.
| Runtime | Workers | Internal elapsed (s) | External real (s) | Throughput (img/s) | Mean score | Mean score diff vs. MATLAB | Score diff vs. MATLAB | Max score abs diff | Map diff vs. MATLAB | Max map abs diff |
|---|---|---|---|---|---|---|---|---|---|---|
| MATLAB | 1 | 801.722575 | 809.04 | 5.3909 | 0.470454676887910 | 0.000e+00 | Baseline | 0.000e+00 | baseline | 0.000e+00 |
| MATLAB | 4 | 278.207849 | 286.69 | 15.5351 | 0.470454676887910 | 0.000e+00 | N/A | N/A | N/A | N/A |
| MATLAB | 8 | 208.548858 | 216.34 | 20.7242 | 0.470454676887910 | 0.000e+00 | N/A | N/A | N/A | N/A |
| Python pre-optimization | 1 | 1784.010954 | 1785.01 | 2.4226 | 0.470454676908741 | 2.083e-11 | MAE 2.469e-11; RMSE 2.723e-10 | 1.346e-08 | MAE 2.370e-09; RMSE 2.261e-07 | 7.121e-04 |
| Python optimized | 1 | 545.379295 | 546.15 | 7.9248 | 0.470454676908513 | 2.060e-11 | MAE 2.488e-11; RMSE 2.722e-10 | 1.346e-08 | MAE 2.370e-09; RMSE 2.261e-07 | 7.121e-04 |
| Python optimized | 4 | 168.521469 | 169.47 | 25.6466 | 0.470454676908513 | 2.060e-11 | N/A | N/A | N/A | N/A |
| Python optimized | 8 | 131.086210 | 131.98 | 32.9707 | 0.470454676908513 | 2.060e-11 | N/A | N/A | N/A | N/A |
Summary:
- The initial Python port was already highly consistent with MATLAB, but slower.
- The optimized Python version preserves MATLAB-level numerical agreement while improving single-thread performance substantially.
- On this machine, the optimized Python version is faster than MATLAB at
1,4, and8image-level workers.
MATLAB vs. Initial Python Port
- Both versions implement the same FADE algorithm and use the same original reference models.
- MATLAB relies on built-in operators such as
rgb2gray,rgb2hsv,fspecial,imfilter,im2col,entropy,nanvar/std, andmrdivide. - The Python port reproduces those semantics explicitly, including patch order, border handling, convolution alignment, variance rules, and entropy behavior.
- The Python package provides a broader interface surface than the original
MATLAB function: it supports folder paths, image paths,
.npyfiles, NumPy arrays, and tensor-like inputs. - Precision of the initial Python version was already close to MATLAB:
Score MAE = 2.469e-11,Score max abs diff = 1.346e-08,Map global MAE = 2.370e-09. - Performance of the initial Python single-thread version was much worse than
MATLAB single-thread:
1784.01svs.801.72s.
Optimized Python vs. Initial Python
- The score definition, feature definition, bundled model parameters, and MATLAB-alignment rules are unchanged.
- The optimized version improves the implementation, not the algorithm:
- vectorized rank-1 update for model-distance computation
- cached MSCN and CE kernels
- faster 1D convolution path for
1xNandNx1kernels - vectorized packed-
bincountentropy evaluation
- Precision is effectively unchanged:
Score MAE:2.469e-11 -> 2.488e-11Score RMSE:2.723e-10 -> 2.722e-10Score max abs diff: unchanged at1.346e-08Map global MAE: unchanged at2.370e-09
- Single-thread performance improved from
1784.01sto545.38s, about3.27xfaster than the initial Python version.
Development
cd pyfade
pip install -e ".[dev]"
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fade_python-0.1.0.tar.gz.
File metadata
- Download URL: fade_python-0.1.0.tar.gz
- Upload date:
- Size: 17.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc154d03d1d137ef2fda0df5bda9f310cf876b2babaa9b8495a8503e3320c795
|
|
| MD5 |
aaeef131a22a686034d3489c1c80a5ad
|
|
| BLAKE2b-256 |
a0fbcb8825af44dd18f3d1ddb969a22e1d2203b08c209eb2e3e1a65322aaa617
|
File details
Details for the file fade_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fade_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52e07998f0c1dc2e9e35520cd9b03812f499407f9fccf1986a6b7caae353fe92
|
|
| MD5 |
7baf4f19b6dd5c0fb79c70407cd4beb5
|
|
| BLAKE2b-256 |
0eb564a751fbc5d6a3ba36f355c12aeb8ac676817c1c143c9b6e3d80f6ad0e26
|