Measures and metrics for image2image tasks. PyTorch.
Project description
PhotoSynthesis.Metrics
PyTorch library with measures and metrics for various image-to-image tasks like denoising, super-resolution, image generation etc. This easy to use yet flexible and extensive library is developed with focus on reliability and reproducibility of results. Use your favourite measures as losses for training neural networks with ready-to-use PyTorch modules.
Getting started
import torch
from photosynthesis_metrics import ssim
prediction = torch.rand(3, 3, 256, 256)
target = torch.rand(3, 3, 256, 256)
ssim_index = ssim(prediction, target, data_range=1.)
Minimal examples
Structural Similarity (SSIM)
To compute SSIM index as a measure, use lower case function from the library:
import torch
from photosynthesis_metrics import ssim
from typing import Union, Tuple
prediction = torch.rand(3, 3, 256, 256)
target = torch.rand(3, 3, 256, 256)
ssim_index: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]] = ssim(prediction, target, data_range=1.)
In order to use SSIM as a loss function, use corresponding PyTorch module:
import torch
from photosynthesis_metrics import SSIMLoss
loss = SSIMLoss()
prediction = torch.rand(3, 3, 256, 256, requires_grad=True)
target = torch.rand(3, 3, 256, 256)
output: torch.Tensor = loss(prediction, target, data_range=1.)
output.backward()
Multi-Scale Structural Similarity (MS-SSIM)
To compute MS-SSIM index as a measure, use lower case function from the library:
import torch
from photosynthesis_metrics import multi_scale_ssim
prediction = torch.rand(3, 3, 256, 256)
target = torch.rand(3, 3, 256, 256)
ms_ssim_index: torch.Tensor = multi_scale_ssim(prediction, target, data_range=1.)
In order to use MS-SSIM as a loss function, use corresponding PyTorch module:
import torch
from photosynthesis_metrics import MultiScaleSSIMLoss
loss = MultiScaleSSIMLoss()
prediction = torch.rand(3, 3, 256, 256, requires_grad=True)
target = torch.rand(3, 3, 256, 256)
output: torch.Tensor = loss(prediction, target, data_range=1.)
output.backward()
Total Variation (TV)
To compute TV as a measure, use lower case function from the library:
import torch
from photosynthesis_metrics import total_variation
data = torch.rand(3, 3, 256, 256)
tv: torch.Tensor = total_variation(data)
In order to use TV as a loss function, use corresponding PyTorch module:
import torch
from photosynthesis_metrics import TVLoss
loss = TVLoss()
prediction = torch.rand(3, 3, 256, 256, requires_grad=True)
output: torch.Tensor = loss(prediction)
output.backward()
Visual Information Fidelity (VIF)
To compute VIF as a measure, use lower case function from the library:
import torch
from photosynthesis_metrics import vif_p
predicted = torch.rand(3, 3, 256, 256)
target = torch.rand(3, 3, 256, 256)
vif: torch.Tensor = vif_p(predicted, target, data_range=1.)
In order to use VIF as a loss function, use corresponding PyTorch class:
import torch
from photosynthesis_metrics import VIFLoss
loss = VIFLoss(sigma_n_sq=2.0, data_range=1.)
prediction = torch.rand(3, 3, 256, 256, requires_grad=True)
target = torch.rand(3, 3, 256, 256)
ouput: torch.Tensor = loss(prediction, target)
output.backward()
Note, that VIFLoss returns 1 - VIF
value.
Multi-Scale Intrinsic Distance (MSID)
Use MSID
class to compute MSID score from image features,
pre-extracted from some feature extractor network:
import torch
from photosynthesis_metrics import MSID
msid_metric = MSID()
prediction_feats = torch.rand(10000, 1024)
target_feats = torch.rand(10000, 1024)
msid: torch.Tensor = msid_metric(prediction_feats, target_feats)
If image features are not available, extract them using _compute_feats
of MSID
class.
Please note that _compute_feats
consumes a data loader of predefined format.
import torch
from torch.utils.data import DataLoader
from photosynthesis_metrics import MSID
first_dl, second_dl = DataLoader(), DataLoader()
msid_metric = MSID()
first_feats = msid_metric._compute_feats(first_dl)
second_feats = msid_metric._compute_feats(second_dl)
msid: torch.Tensor = msid_metric(first_feats, second_feats)
Frechet Inception Distance(FID)
Use FID
class to compute FID score from image features,
pre-extracted from some feature extractor network:
import torch
from photosynthesis_metrics import FID
fid_metric = FID()
prediction_feats = torch.rand(10000, 1024)
target_feats = torch.rand(10000, 1024)
msid: torch.Tensor = fid_metric(prediction_feats, target_feats)
If image features are not available, extract them using _compute_feats
of FID
class.
Please note that _compute_feats
consumes a data loader of predefined format.
import torch
from torch.utils.data import DataLoader
from photosynthesis_metrics import FID
first_dl, second_dl = DataLoader(), DataLoader()
fid_metric = FID()
first_feats = fid_metric._compute_feats(first_dl)
second_feats = fid_metric._compute_feats(second_dl)
msid: torch.Tensor = fid_metric(first_feats, second_feats)
Kernel Inception Distance(KID)
Use KID
class to compute KID score from image features,
pre-extracted from some feature extractor network:
import torch
from photosynthesis_metrics import KID
kid_metric = KID()
prediction_feats = torch.rand(10000, 1024)
target_feats = torch.rand(10000, 1024)
msid: torch.Tensor = kid_metric(prediction_feats, target_feats)
If image features are not available, extract them using _compute_feats
of KID
class.
Please note that _compute_feats
consumes a data loader of predefined format.
import torch
from torch.utils.data import DataLoader
from photosynthesis_metrics import KID
first_dl, second_dl = DataLoader(), DataLoader()
kid_metric = KID()
first_feats = kid_metric._compute_feats(first_dl)
second_feats = kid_metric._compute_feats(second_dl)
msid: torch.Tensor = kid_metric(first_feats, second_feats)
Inception Score(IS)
Use inception_score
function to compute IS from image features,
pre-extracted from some feature extractor network. Note, that we follow recomendations from paper A Note on the Inception Score, which proposed small modification to original algorithm:
import torch
from photosynthesis_metrics import inception_score
prediction_feats = torch.rand(10000, 1024)
mean: torch.Tensor, variance: torch.Tensor = inception_score(prediction_feats, num_splits=10)
To compute difference between IS for 2 sets of image features, use IS
class.
import torch
from photosynthesis_metrics import IS
is_metric = IS(distance='l1')
prediction_feats = torch.rand(10000, 1024)
target_feats = torch.rand(10000, 1024)
distance: torch.Tensor = is_metric(prediction_feats, target_feats)
Table of Contents
Overview
PhotoSynthesis.Metrics helps you to concentrate on your experiments without the boilerplate code. The library contains a set of measures and metrics that is constantly getting extended. For measures/metrics that can be used as loss functions, corresponding PyTorch modules are implemented.
Installation
$ pip install photosynthesis-metrics
If you want to use the latest features straight from the master, clone the repo:
$ git clone https://github.com/photosynthesis-team/photosynthesis.metrics.git
Roadmap
See the open issues for a list of proposed features and known issues.
Community
Contributing
We appreciate all contributions. If you plan to:
- contribute back bug-fixes, please do so without any further discussion
- close one of open issues, please do so if no one has been assigned to it
- contribute new features, utility functions or extensions, please first open an issue and discuss the feature with us
Please see the contribution guide for more information.
Contact
Sergey Kastryulin - @snk4tr - snk4tr@gmail.com
Project Link: https://github.com/photosynthesis-team/photosynthesis.metrics
PhotoSynthesis Team: https://github.com/photosynthesis-team
Other projects by PhotoSynthesis Team:
Acknowledgements
- Pavel Parunin - @PavelParunin - idea proposal and development
- Djamil Zakirov - @zakajd - development
- Denis Prokopenko - @denproc - development
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
Built Distribution
Hashes for photosynthesis_metrics-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8d5a255501d8127143f503eeb3375cb65ee1004b553b86172fed8ac978f948c |
|
MD5 | 8545af9017dd9960020dc0ac090baa12 |
|
BLAKE2b-256 | a01e769cdb490d8b3308144a601422bc32114b32b3f7af8253a80e88d0643799 |
Hashes for photosynthesis_metrics-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2ffbe02b7b8f5746c638aa3defeb7690f95caa04c2c96e91e627cfc762fc3a3 |
|
MD5 | eef58857b26c419a146f0bcf230cea56 |
|
BLAKE2b-256 | 84d3f65937331c3753f535757cb6800551ef45ebda53736583b18e5f2d6d94ef |