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

Dataclasses and post-processing refactor #2098

Conversation

djdameln
Copy link
Contributor

@djdameln djdameln commented May 31, 2024

📝 Description

Dataclasses:

  • Adds new module anomalib.dataclasses, which contains image and batch level dataclasses across different modalities.
  • The fields of the dataclasses are instances of the new FieldDescriptor class. This allows specifying a validation method for each field that gets called every time the field is updated. This ensures consistency in the typing and shape formatting of the fields of all instances of the dataclass.

Post-processing:

  • adds new PostProcessor base class and OneClassPostProcessor concrete subclass.
  • PostProcessor inherits from Callback, so post processing operations can easily be applied in any of the callback hooks.
  • PostProcessor inherits from nn.Module, so that the post-processing steps can be included in the forward pass of the exported model (onnx/torch/openvino). This means that subclasses of PostProcessor have to implement the forward method. In the exported model, the post processor's forward method is called after the torch model's forward method.
  • The new OneClassPostProcessor class implements our previous post-processing behaviour, consisting of normalization and thresholding.
  • Custom post-processing can be achieved by passing a post processor instance to the engine using the post_processor argument.

✨ Changes

Select what type of change your PR is:

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • 🔨 Refactor (non-breaking change which refactors the code base)
  • 🚀 New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔒 Security update

✅ Checklist

Before you submit your pull request, please make sure you have completed the following steps:

  • 📋 I have summarized my changes in the CHANGELOG and followed the guidelines for my type of change (skip for minor changes, documentation updates, and test enhancements).
  • 📚 I have made the necessary updates to the documentation (if applicable).
  • 🧪 I have written tests that support my changes and prove that my fix is effective or my feature works (if applicable).

For more information about code review checklists, see the Code Review Checklist.

@samet-akcay samet-akcay marked this pull request as draft May 31, 2024 12:09
Copy link
Collaborator

@ashwinvaidya17 ashwinvaidya17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave visualizer refactor to a different PR. It might require a lot more changes. We can just add a TODO to the comments in the inferencers.
The rest looks good to me. Once we remove the commented code we can push this as is.

tools/inference/torch_inference.py Outdated Show resolved Hide resolved
@ashwinvaidya17 ashwinvaidya17 self-requested a review July 31, 2024 11:48
@samet-akcay samet-akcay marked this pull request as ready for review August 29, 2024 11:13
@samet-akcay samet-akcay changed the base branch from main to feature/design-simplifications August 29, 2024 19:26
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link

codecov bot commented Sep 2, 2024

Codecov Report

Attention: Patch coverage is 90.88472% with 102 lines in your changes missing coverage. Please review.

Please upload report for BASE (feature/design-simplifications@4b1e08c). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/anomalib/dataclasses/generic.py 82.31% 29 Missing ⚠️
src/anomalib/dataclasses/torch.py 90.20% 28 Missing ⚠️
src/anomalib/dataclasses/numpy.py 85.86% 13 Missing ⚠️
src/anomalib/utils/visualization/image.py 84.00% 8 Missing ⚠️
...rc/anomalib/models/video/ai_vad/lightning_model.py 58.33% 5 Missing ⚠️
src/anomalib/models/image/dsr/torch_model.py 66.66% 4 Missing ⚠️
.../anomalib/models/components/base/anomaly_module.py 81.25% 3 Missing ⚠️
src/anomalib/post_processing/one_class.py 96.93% 3 Missing ⚠️
...anomalib/deploy/inferencers/openvino_inferencer.py 91.30% 2 Missing ⚠️
src/anomalib/models/video/ai_vad/torch_model.py 50.00% 2 Missing ⚠️
... and 5 more
Additional details and impacted files
@@                        Coverage Diff                        @@
##             feature/design-simplifications    #2098   +/-   ##
=================================================================
  Coverage                                  ?   80.00%           
=================================================================
  Files                                     ?      250           
  Lines                                     ?    11092           
  Branches                                  ?        0           
=================================================================
  Hits                                      ?     8874           
  Misses                                    ?     2218           
  Partials                                  ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@samet-akcay samet-akcay merged commit a298a3a into openvinotoolkit:feature/design-simplifications Sep 2, 2024
7 checks passed
djdameln added a commit to djdameln/anomalib that referenced this pull request Sep 12, 2024
* use dataclass for model in- and outputs

* split dataclass in image and video

* use dataclass in torch inferencer

* use dataclass in openvino inferencer

* add post_processor class

* remove default metrics from CLI

* export post processing

* add post processor to patchcore

* use named tuple for inference outputs

* validate and format inputs of PredictBatch

* update torch inference

* remove base inferencer inheritance

* update openvino inference

* fix visualization

* PredictBatch -> Batch

* post processor as callback

* use callback methods to apply post processing

* temporary fix for visualization

* add DatasetItem class

* fix pred_score shape and add __len__

* make batch iterable

* add in place replace method

* use dataset items in inference

* dataset_items -> items

* use namedtuple as torch model outputs

* formatting

* split dataclasses into input/output and image/video

* merge input and output classes

* use init_subclass for attribute checking

* add descriptor class for validation

* improve error handling

* DataClassDescriptor -> FieldDescriptor

* add is_optional method

* add input validation for torch image and batch

* use image and video dataclasses in library

* add more validation

* add validation

* make postprocessor configurable from engine

* fix post processing logic

* fix data tests

* remove detection task type

* fix more tests

* use separate normalization stats for image and pixel preds

* add sensitivity parameters to one class pp

* fix utils tests

* fix utils tests

* remove metric serialization test

* remove normalization and thresholding args

* set default post processor in base model

* remove manual threshold test

* fix remaining unit tests

* add post_processor to CLI args

* remove old post processing callbacks

* remove comment

* remove references to old normalization and thresholding callbacks

* remove reshape in openvino inferencer

* export lightning model directly

* make collate accessible from dataset

* fix tools integration tests

* add update method to dataclasses

* allow missing pred_score or anomaly_map in post processor

* fix exportable centercrop conversion

* fix model tests

* test all models

* fix efficient_ad

* post processor as model arg

* disable rkde tests

* fix winclip export

* add copyright notice

* add validation for numpy anomaly map

* fix getting started notebook

* remove hardcoded path

* update dataset notebooks

* update model notebooks

* fix logging notebooks

* fix model notebook
djdameln added a commit that referenced this pull request Sep 12, 2024
* use dataclass for model in- and outputs

* split dataclass in image and video

* use dataclass in torch inferencer

* use dataclass in openvino inferencer

* add post_processor class

* remove default metrics from CLI

* export post processing

* add post processor to patchcore

* use named tuple for inference outputs

* validate and format inputs of PredictBatch

* update torch inference

* remove base inferencer inheritance

* update openvino inference

* fix visualization

* PredictBatch -> Batch

* post processor as callback

* use callback methods to apply post processing

* temporary fix for visualization

* add DatasetItem class

* fix pred_score shape and add __len__

* make batch iterable

* add in place replace method

* use dataset items in inference

* dataset_items -> items

* use namedtuple as torch model outputs

* formatting

* split dataclasses into input/output and image/video

* merge input and output classes

* use init_subclass for attribute checking

* add descriptor class for validation

* improve error handling

* DataClassDescriptor -> FieldDescriptor

* add is_optional method

* add input validation for torch image and batch

* use image and video dataclasses in library

* add more validation

* add validation

* make postprocessor configurable from engine

* fix post processing logic

* fix data tests

* remove detection task type

* fix more tests

* use separate normalization stats for image and pixel preds

* add sensitivity parameters to one class pp

* fix utils tests

* fix utils tests

* remove metric serialization test

* remove normalization and thresholding args

* set default post processor in base model

* remove manual threshold test

* fix remaining unit tests

* add post_processor to CLI args

* remove old post processing callbacks

* remove comment

* remove references to old normalization and thresholding callbacks

* remove reshape in openvino inferencer

* export lightning model directly

* make collate accessible from dataset

* fix tools integration tests

* add update method to dataclasses

* allow missing pred_score or anomaly_map in post processor

* fix exportable centercrop conversion

* fix model tests

* test all models

* fix efficient_ad

* post processor as model arg

* disable rkde tests

* fix winclip export

* add copyright notice

* add validation for numpy anomaly map

* fix getting started notebook

* remove hardcoded path

* update dataset notebooks

* update model notebooks

* fix logging notebooks

* fix model notebook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Refactor Refactoring is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Dataclass-based Input/Output in Anomalib
3 participants