Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored coverage and density into their own functions #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion prdc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .prdc import compute_prdc
from .prdc import compute_prdc, compute_coverage, compute_density
52 changes: 51 additions & 1 deletion prdc/prdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import sklearn.metrics

__all__ = ['compute_prdc']
__all__ = ['compute_prdc', 'compute_density', 'compute_coverage']


def compute_pairwise_distance(data_x, data_y=None):
Expand Down Expand Up @@ -51,6 +51,56 @@ def compute_nearest_neighbour_distances(input_features, nearest_k):
return radii


def compute_density(real_features, fake_features, nearest_k):
"""
Computes density given two manifolds.

Args:
real_features: numpy.ndarray([N, feature_dim], dtype=np.float32)
fake_features: numpy.ndarray([N, feature_dim], dtype=np.float32)
nearest_k: int.
Returns:
density: np.float64
"""

real_nearest_neighbour_distances = compute_nearest_neighbour_distances(
real_features, nearest_k)
distance_real_fake = compute_pairwise_distance(
real_features, fake_features)

density = (1. / float(nearest_k)) * (
distance_real_fake <
np.expand_dims(real_nearest_neighbour_distances, axis=1)
).sum(axis=0).mean()

return density


def compute_coverage(real_features, fake_features, nearest_k):
"""
Computes coverage given two manifolds.

Args:
real_features: numpy.ndarray([N, feature_dim], dtype=np.float32)
fake_features: numpy.ndarray([N, feature_dim], dtype=np.float32)
nearest_k: int.
Returns:
coverage: np.float64
"""

real_nearest_neighbour_distances = compute_nearest_neighbour_distances(
real_features, nearest_k)
distance_real_fake = compute_pairwise_distance(
real_features, fake_features)

coverage = (
distance_real_fake.min(axis=1) <
real_nearest_neighbour_distances
).mean()

return coverage


def compute_prdc(real_features, fake_features, nearest_k):
"""
Computes precision, recall, density, and coverage given two manifolds.
Expand Down