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

OOD Detection using COOD #493

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d3f52d4
Initial Commit
rajeshgangireddy Jul 18, 2024
632233e
Copy of notebook 103
rajeshgangireddy Jul 22, 2024
edbce1b
Rename OODModel to COODModel
rajeshgangireddy Jul 22, 2024
d95c4d3
Add code for initial steps - fetch Deployment models,
rajeshgangireddy Jul 22, 2024
013a6c4
Create ood_debug_delete_before_pr.py
rajeshgangireddy Jul 22, 2024
50b24bb
fix logic to get xai model
rajeshgangireddy Jul 23, 2024
99ab08e
add method to download, infer the id data
rajeshgangireddy Jul 31, 2024
ac76588
Update ood_model.py
rajeshgangireddy Jul 31, 2024
f718269
added code to prepare id and ood data - draft
rajeshgangireddy Aug 1, 2024
d99ca98
Update ood_model.py
rajeshgangireddy Aug 2, 2024
23213e8
Update ood_model.py
rajeshgangireddy Aug 5, 2024
e5b5991
Update ood_model.py
rajeshgangireddy Aug 6, 2024
e5eb494
training and inference works.
rajeshgangireddy Aug 12, 2024
c5984d0
move the deployment getting method to utils
rajeshgangireddy Aug 13, 2024
2821ea3
change number of cutouts to be 2
rajeshgangireddy Aug 14, 2024
9b1ef26
Refactored OOD Model
rajeshgangireddy Aug 14, 2024
9885fbd
Abstract class for sub models
rajeshgangireddy Aug 14, 2024
34ecff3
change corruption parameters again
rajeshgangireddy Aug 14, 2024
22e5d0b
Update ood_debug_delete_before_pr.py
rajeshgangireddy Aug 14, 2024
bdd3822
add more ood measures
rajeshgangireddy Aug 14, 2024
85cd5ed
Cleanup and added docstrings
rajeshgangireddy Aug 15, 2024
500b8da
Initial commit for notebook.
rajeshgangireddy Aug 21, 2024
8a0117c
Update 104_post_inference_hook_ood.ipynb
rajeshgangireddy Sep 4, 2024
5990dd1
Possibility to provide a image dir as reference for OOD data
rajeshgangireddy Sep 6, 2024
edd7392
Add train and test split functionality
rajeshgangireddy Sep 6, 2024
c36234e
better listing of images
rajeshgangireddy Sep 6, 2024
8b0c98e
Updated FRE calculations , added a new variant scores
rajeshgangireddy Sep 9, 2024
5fffa23
Determine best threshold for certain metrics.
rajeshgangireddy Sep 10, 2024
f97a151
WIP files for testing and notebook
rajeshgangireddy Sep 10, 2024
fe687c2
Refactor - Move functions into utils, move sub ood methods
rajeshgangireddy Sep 16, 2024
74e91cf
Demo Notebook
rajeshgangireddy Sep 20, 2024
8f2af0a
Delete ood_debug_delete_before_pr.py
rajeshgangireddy Sep 20, 2024
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
37 changes: 37 additions & 0 deletions geti_sdk/detect_ood/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (C) 2023 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

"""
Introduction
------------

The "detect_ood" package contains the
:py:class:`~geti_sdk.detect_ood.OODModel` class, which provides
Out-of-distribution detection functions (training an OODModel as well as detecting OOD samples).

Primarily, it is used by the OODTrigger class (~geti_sdk.post_inference_hooks.triggers.ood_trigger.OODTrigger)
to detect out-of-distribution samples.

Module contents
---------------

.. automodule:: geti_sdk.detect_ood.OODModel
:members:
:undoc-members:
:show-inheritance:
"""

from .ood_model import COODModel

__all__ = ["COODModel"]
108 changes: 108 additions & 0 deletions geti_sdk/detect_ood/ood_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Copyright (C) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

from enum import Enum
from typing import Union

import numpy as np

from geti_sdk.data_models import Prediction


class DistributionDataItemPurpose(Enum):
"""
Enum to represent the purpose of the DistributionDataItem.
This is used during splitting of the data into TRAIN, VAL, TEST
"""

TRAIN = "train"
VAL = "val"
TEST = "test"


class DistributionDataItem:
"""
A class to store the data for the COOD model.
An DistributionDataItem for an image contains the following:
- media_name: Name of the media (optional)
- image_path: Path to the image (optional)
- annotated_label: Annotated label for the image (optional)
- raw_prediction: Prediction object for the image (required)
- feature_vector: Feature vector extracted from the image (extracted from raw_prediction)

All OOD models take a list of DistributionDataItems as input for training and inference.
"""

def __init__(
self,
raw_prediction: Prediction,
media_name: Union[str, None],
media_path: Union[str, None],
annotated_label: Union[str, None],
normalise_feature_vector: bool = True,
purpose: Union[DistributionDataItemPurpose, None] = None,
):
self.media_name = media_name
self.image_path = media_path
self.annotated_label = annotated_label
self.raw_prediction = raw_prediction
self.purpose = purpose

feature_vector = raw_prediction.feature_vector

if len(feature_vector.shape) != 1:
feature_vector = feature_vector.flatten()

if normalise_feature_vector:
feature_vector = self.normalise_features(feature_vector)[0]

self._normalise_feature_vector = normalise_feature_vector
self.feature_vector = feature_vector
self.max_prediction_probability = (
raw_prediction.annotations[0].labels[0].probability,
)
self.predicted_label = raw_prediction.annotations[0].labels[0].name

@property
def is_feature_vector_normalised(self) -> bool:
"""
Return True if the feature vector is normalised.
"""
return self._normalise_feature_vector

@staticmethod
def normalise_features(feature_vectors: np.ndarray) -> np.ndarray:
"""
Feature embeddings are normalised by dividing each feature embedding vector by its respective 2nd-order vector
norm (vector Euclidean norm). It has been shown that normalising feature embeddings lead to a significant
improvement in OOD detection.
:param feature_vectors: Feature vectors to normalise
:return: Normalised feature vectors.
"""
if len(feature_vectors.shape) == 1:
feature_vectors = feature_vectors.reshape(1, -1)

return feature_vectors / (
np.linalg.norm(feature_vectors, axis=1, keepdims=True) + 1e-10
)

def __repr__(self):
"""
Return a string representation of the DistributionDataItem.
"""
return (
f"DataItem(media_name={self.media_name}, "
f"shape(feature_vector)={self.feature_vector.shape}), "
f"feature_vector normalised={self.is_feature_vector_normalised})"
)
Loading
Loading