-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix/methods #85
Fix/methods #85
Changes from all commits
88f0e96
16b76d6
217dc28
356525f
035c8c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -569,6 +569,7 @@ def prepare_for_training( | |
output_keys: Optional[list] = None, | ||
dict_based_fns: bool = False, | ||
shuffle_buffer_size: Optional[int] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose this line must be removed, since there is now |
||
num_workers: int = 8, | ||
) -> DataLoader: | ||
"""Prepare a DataLoader for training | ||
|
||
|
@@ -587,6 +588,7 @@ def prepare_for_training( | |
shuffle_buffer_size (int, optional): Size of the shuffle buffer. Not used | ||
in torch because we only rely on Map-Style datasets. Still as argument | ||
for API consistency. Defaults to None. | ||
num_workers (int, optional): Number of workers to use for the dataloader. | ||
|
||
Returns: | ||
DataLoader: dataloader | ||
|
@@ -621,6 +623,7 @@ def collate_fn(batch: List[dict]): | |
batch_size=batch_size, | ||
shuffle=shuffle, | ||
collate_fn=collate_fn, | ||
num_workers=num_workers, | ||
) | ||
return loader | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,13 @@ | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
import inspect | ||
from abc import ABC | ||
from abc import abstractmethod | ||
from typing import get_args | ||
|
||
import numpy as np | ||
from tqdm import tqdm | ||
|
||
from ..extractor.feature_extractor import FeatureExtractor | ||
from ..types import Callable | ||
|
@@ -104,6 +106,7 @@ def fit( | |
fit_dataset: Optional[Union[ItemType, DatasetType]] = None, | ||
feature_layers_id: List[Union[int, str]] = [], | ||
input_layer_id: Optional[Union[int, str]] = None, | ||
verbose: bool = False, | ||
**kwargs, | ||
) -> None: | ||
"""Prepare the detector for scoring: | ||
|
@@ -122,6 +125,7 @@ def fit( | |
layer of the feature extractor. | ||
If int, the rank of the layer in the layer list | ||
If str, the name of the layer. Defaults to None. | ||
verbose (bool): if True, display a progress bar. Defaults to False. | ||
""" | ||
( | ||
self.backend, | ||
|
@@ -144,7 +148,7 @@ def fit( | |
" provided to compute react activation threshold" | ||
) | ||
else: | ||
self.compute_react_threshold(model, fit_dataset) | ||
self.compute_react_threshold(model, fit_dataset, verbose=verbose) | ||
|
||
if (feature_layers_id == []) and (self.requires_internal_features): | ||
raise ValueError( | ||
|
@@ -160,6 +164,8 @@ def fit( | |
) | ||
|
||
if fit_dataset is not None: | ||
if "verbose" in inspect.signature(self._fit_to_dataset).parameters.keys(): | ||
kwargs.update({"verbose": verbose}) | ||
Comment on lines
+167
to
+168
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know what is the best practice but I think it's more clear to make |
||
self._fit_to_dataset(fit_dataset, **kwargs) | ||
|
||
def _load_feature_extractor( | ||
|
@@ -207,12 +213,14 @@ def _fit_to_dataset(self, fit_dataset: DatasetType) -> None: | |
def score( | ||
self, | ||
dataset: Union[ItemType, DatasetType], | ||
verbose: bool = False, | ||
) -> np.ndarray: | ||
""" | ||
Computes an OOD score for input samples "inputs". | ||
|
||
Args: | ||
dataset (Union[ItemType, DatasetType]): dataset or tensors to score | ||
verbose (bool): if True, display a progress bar. Defaults to False. | ||
|
||
Returns: | ||
tuple: scores or list of scores (depending on the input) and a dictionary | ||
|
@@ -236,7 +244,7 @@ def score( | |
scores = np.array([]) | ||
logits = None | ||
|
||
for item in dataset: | ||
for item in tqdm(dataset, desc="Scoring", disable=not verbose): | ||
tensor = self.data_handler.get_input_from_dataset_item(item) | ||
score_batch = self._score_tensor(tensor) | ||
logits_batch = self.op.convert_to_numpy( | ||
|
@@ -267,9 +275,13 @@ def score( | |
info = dict(labels=labels, logits=logits) | ||
return scores, info | ||
|
||
def compute_react_threshold(self, model: Callable, fit_dataset: DatasetType): | ||
def compute_react_threshold( | ||
self, model: Callable, fit_dataset: DatasetType, verbose: bool = False | ||
): | ||
penult_feat_extractor = self._load_feature_extractor(model, [-2]) | ||
unclipped_features, _ = penult_feat_extractor.predict(fit_dataset) | ||
unclipped_features, _ = penult_feat_extractor.predict( | ||
fit_dataset, verbose=verbose | ||
) | ||
self.react_threshold = self.op.quantile( | ||
unclipped_features[0], self.react_quantile | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a suggestion: we could extend this docstring with examples of usage, like "e.g.
shuffle_buffer_size
for TF data handler ornum_workers
for torch handler."There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point