-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
107 additions
and
9 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
|
||
|
||
def compute_coverage_rate(prediction_sets,labels): | ||
from deepcp.utils.registry import Registry | ||
|
||
METRICS_REGISTRY = Registry("METRICS") | ||
|
||
|
||
@METRICS_REGISTRY.register() | ||
def coverage_rate(prediction_sets,labels): | ||
cvg = 0 | ||
for index,ele in enumerate(zip(prediction_sets,labels)): | ||
if ele[1] in ele[0]: | ||
cvg += 1 | ||
return cvg/len(prediction_sets) | ||
|
||
@METRICS_REGISTRY.register() | ||
def average_size(prediction_sets,labels): | ||
avg_size = 0 | ||
for index,ele in enumerate(prediction_sets): | ||
avg_size += len(ele) | ||
return avg_size/len(prediction_sets) | ||
|
||
|
||
|
||
|
||
class Metrics: | ||
def __init__(self,metrics_list=[]) -> None: | ||
self.metrics_list = metrics_list | ||
|
||
|
||
def compute(self,prediction_sets,labels): | ||
# for metric in self.metrics_list: | ||
return compute_coverage_rate(prediction_sets,labels) | ||
metrics = {} | ||
for metric in self.metrics_list: | ||
metrics[metric] = METRICS_REGISTRY.get(metric)(prediction_sets,labels) | ||
return metrics | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .common import * | ||
from .registry import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
|
||
from difflib import SequenceMatcher | ||
|
||
__all__ = ["Registry"] | ||
|
||
|
||
class Registry: | ||
"""A registry providing name -> object mapping, to support | ||
custom modules. | ||
To create a registry (e.g. a backbone registry): | ||
.. code-block:: python | ||
BACKBONE_REGISTRY = Registry('BACKBONE') | ||
To register an object: | ||
.. code-block:: python | ||
@BACKBONE_REGISTRY.register() | ||
class MyBackbone(nn.Module): | ||
... | ||
Or: | ||
.. code-block:: python | ||
BACKBONE_REGISTRY.register(MyBackbone) | ||
""" | ||
|
||
def __init__(self, name): | ||
self._name = name | ||
self._obj_map = dict() | ||
|
||
def _do_register(self, name, obj, force=False): | ||
if name in self._obj_map and not force: | ||
raise KeyError( | ||
'An object named "{}" was already ' | ||
'registered in "{}" registry'.format(name, self._name) | ||
) | ||
|
||
self._obj_map[name] = obj | ||
|
||
def register(self, obj=None, force=False): | ||
if obj is None: | ||
# Used as a decorator | ||
def wrapper(fn_or_class): | ||
name = fn_or_class.__name__ | ||
self._do_register(name, fn_or_class, force=force) | ||
return fn_or_class | ||
|
||
return wrapper | ||
|
||
# Used as a function call | ||
name = obj.__name__ | ||
self._do_register(name, obj, force=force) | ||
|
||
def get(self, name): | ||
if name not in self._obj_map: | ||
raise KeyError( | ||
'Object name "{}" does not exist ' | ||
'in "{}" registry'.format(name, self._name) | ||
) | ||
|
||
return self._obj_map[name] | ||
|
||
def registered_names(self): | ||
return list(self._obj_map.keys()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters