MCPcopy Create free account
hub / github.com/CompVis/zigma / PRDC

Class PRDC

utils/torchmetric_prdc.py:74–203  ·  view source on GitHub ↗

Example: >>> import torch >>> _ = torch.manual_seed(123) >>> from torchmetric_prdc import PRDC >>> prdc = PRDC(nearest_k=5) >>> # generate two slightly overlapping image intensity distributions >>> imgs_dist1 = torch.randint(0, 200, (100, 3, 299,

Source from the content-addressed store, hash-verified

72
73
74class PRDC(Metric):
75 """
76 Example:
77 >>> import torch
78 >>> _ = torch.manual_seed(123)
79 >>> from torchmetric_prdc import PRDC
80 >>> prdc = PRDC(nearest_k=5)
81 >>> # generate two slightly overlapping image intensity distributions
82 >>> imgs_dist1 = torch.randint(0, 200, (100, 3, 299, 299), dtype=torch.uint8)
83 >>> imgs_dist2 = torch.randint(100, 255, (100, 3, 299, 299), dtype=torch.uint8)
84 >>> prdc.update(imgs_dist1, real=True)
85 >>> prdc.update(imgs_dist2, real=False)
86 >>> result = prdc.compute()
87 >>> print(result)
88 {'precision': 0.07, 'recall': 0.05, 'density': 0.016, 'coverage': 0.07}
89
90 """
91 real_features: List[Tensor]
92 fake_features: List[Tensor]
93 higher_is_better: bool = True
94 is_differentiable: bool = False
95
96 def __init__(
97 self,
98 feature: Union[str, int, torch.nn.Module] = 2048,
99 reset_real_features: bool = True,
100 nearest_k: int = 5,
101 realism: bool = False,
102 **kwargs: Dict[str, Any],
103 ) -> None:
104 super().__init__(**kwargs)
105
106 if isinstance(feature, (str, int)):
107 if not _TORCH_FIDELITY_AVAILABLE:
108 raise ModuleNotFoundError(
109 "Precision Recall Density Coverage metric requires that `Torch-fidelity` is installed."
110 " Either install as `pip install torchmetrics[image]` or `pip install torch-fidelity`."
111 )
112 valid_int_input = ("logits_unbiased", 64, 192, 768, 2048)
113 if feature not in valid_int_input:
114 raise ValueError(
115 f"Integer input to argument `feature` must be one of {valid_int_input}," f" but got {feature}."
116 )
117
118 self.inception: Module = NoTrainInceptionV3(name="inception-v3-compat", features_list=[str(feature)])
119 elif isinstance(feature, Module):
120 self.inception = feature
121 else:
122 raise TypeError("Got unknown input to argument `feature`")
123
124 if not isinstance(reset_real_features, bool):
125 raise ValueError("Arugment `reset_real_features` expected to be a bool")
126 self.reset_real_features = reset_real_features
127 self.nearest_k = nearest_k
128 self.realism = realism
129 # states for extracted features
130 self.add_state("real_features", [], dist_reduce_fx=None)
131 self.add_state("fake_features", [], dist_reduce_fx=None)

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected