-
Notifications
You must be signed in to change notification settings - Fork 769
π feat(model): Add Dinomaly Model #2835
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
base: main
Are you sure you want to change the base?
π feat(model): Add Dinomaly Model #2835
Conversation
β¦ature extraction
β¦dation steps - Added detailed docstrings for the Dinomaly class and its methods. - Improved error handling in training and validation steps. - Updated pre-processor configuration to include crop size validation. - Refined output structure in the training step for clarity.
β¦zer configuration; enhance Gaussian kernel function
β¦ SSLMetaArch implementation - Deleted `train.py`, `__init__.py`, and `ssl_meta_arch.py` files from the DINOv2 training module. - Removed unused imports and commented-out code in `vit_encoder.py`. - Streamlined the model loading process and eliminated unnecessary complexity in the architecture. - Ensured that the remaining code adheres to the latest standards and practices for clarity and maintainability.
β¦er and torch model classes
- Rearranged import statements for better organization and consistency. - Updated type hints to use the new syntax for optional types. - Simplified conditional checks and improved readability in various functions. - Enhanced logging messages for clarity during model loading and training. - Modified the `get_params_groups_with_decay` function to improve parameter handling. - Updated the `DinoV2Loader` class to streamline model loading and weight management. - Improved the `ViTill` class by refining feature processing and anomaly map calculations. - Adjusted the `simple_script.py` to utilize the new export types for model exporting. - Reduced the number of epochs in the training script for quicker testing.
β¦ clarity and accuracy style: adjust training configuration in simple_script.py
β¦integration refactor: enhance training configuration and streamline model initialization in ViTill chore: add benchmark configuration and script for Padim model evaluation fix: update simple script for MVTecAD category and improve timing output
β¦related utilities refactor: update attention and drop path layers for improved efficiency and clarity
β¦ timm library equivalents and clean up unused code
β¦ clarity; remove unused layer files
β¦ross components
β¦ding initialization
β¦ handling - Added type hints and ClassVar annotations in model_loader.py for better clarity and type checking. - Enhanced error messages in model_loader.py to provide clearer guidance on model name and architecture issues. - Updated global_cosine_hm_percent and modify_grad functions in utils.py with type hints and improved gradient modification logic. - Improved documentation and type hints in vision_transformer.py, including detailed docstrings for methods and parameters. - Refined training configuration in lightning_model.py with type hints and assertions for better validation of input parameters. - Enhanced ViTill class in torch_model.py with static methods and type safety checks for architecture configuration. - General code cleanup and consistency improvements across all modified files.
β¦er; remove unused max_steps from training config
β¦oss function and dynamic training steps
β¦rectory handling in DinoV2Loader
β¦ep configuration in Dinomaly model
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.
Pull Request Overview
This PR adds the Dinomaly modelβa Vision Transformer-based anomaly detection approachβinto anomalib, including both a raw PyTorch implementation and a Lightning training/inference module.
- Introduces
ViTill
PyTorch model with encoder, bottleneck, decoder and map computation - Adds
Dinomaly
LightningModule for training, validation, optimizer/scheduler setup - Bundles DINOv2 transformer components, training utilities (loss, optimizer, scheduler), model loader, layer definitions, and example config
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.
Show a summary per file
File | Description |
---|---|
src/anomalib/models/image/dinomaly/torch_model.py | Core ViTill model implementation & anomaly-map computation |
src/anomalib/models/image/dinomaly/lightning_model.py | LightningModule wrapping ViTill for training/inference |
src/anomalib/models/image/dinomaly/components/vision_transformer.py | DINOv2 Vision Transformer implementation |
src/anomalib/models/image/dinomaly/components/training_utils.py | Loss, optimizer, scheduler utilities |
src/anomalib/models/image/dinomaly/components/model_loader.py | Loader for pretrained DINOv2 weights |
src/anomalib/models/image/dinomaly/components/layers.py | Attention, MLP, transformer block components |
src/anomalib/models/image/dinomaly/components/init.py | Component exports |
src/anomalib/models/image/dinomaly/init.py | Dinomaly module export |
src/anomalib/models/image/init.py | Registered Dinomaly in image models |
src/anomalib/models/init.py | Registered Dinomaly globally |
examples/configs/model/dinomaly.yaml | Example YAML config for Dinomaly |
src/anomalib/models/image/dinomaly/README.md | Model README with usage and benchmarks placeholder |
src/anomalib/models/image/dinomaly/components/training_utils.py
Outdated
Show resolved
Hide resolved
β¦ for Dinomaly model;
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.
Thanks @rajeshgangireddy, overall the code is quite clean and easy to follow!
I've added some comments to discuss
src/anomalib/models/image/dinomaly/components/training_utils.py
Outdated
Show resolved
Hide resolved
src/anomalib/models/image/dinomaly/components/vision_transformer.py
Outdated
Show resolved
Hide resolved
on increasingly difficult examples as training progresses. | ||
""" | ||
del args, kwargs # These variables are not used. | ||
try: |
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.
why would there be a training error?
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.
Also is it possible to move this loss computation to the torch model by any chance?
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.
Nope. It is now removed.
Loss calculation for this model depends on an additional components - p which get's updated based on the number of trained epochs.
p (float): Percentage of well-reconstructed points to down-weight (0.0 to 1.0).
Higher values make training focus on fewer, harder examples. Default is 0.9.
That's why lightning model (which has access to this) is a good place to calculate loss.
Let me think, if we can move loss to torch model.
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.
Hmm tricky let's discuss this. This makes it valid to keep loss in lightning module. Let's discuss what should be the scope of torch model and what should be in the lightning model. I think we need to refactor all the models to ensure that our inferencers work correctly.
max_epochs * len(self.trainer.datamodule.train_dataloader()), | ||
) | ||
|
||
optimizer_config = TRAINING_CONFIG["optimizer"] |
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.
@ashwinvaidya17, can you confirm if this dict-based config is API/CLI friendly ?
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.
Great work! A few comments
Refactor Dinomaly model as per comments plus some small improvements. Attention class was used only for type checking - replaced it with attention from timm. Separated loss and optimiser into different files. Code readability improvements. Added explicitly pass the image size to create anomaly maps instead of a default. Docstring improvements. Removed unnecessary TRY CATCH blocks
π Description
Pending:
β¨ Changes
Select what type of change your PR is:
β Checklist
Before you submit your pull request, please make sure you have completed the following steps:
For more information about code review checklists, see the Code Review Checklist.
Anomaly Maps
###Metrics
<style> </style>*NOTE : The Pixel_F1 are slighly off as the paper publishes F1_Max and I benchmarked F1Score. I am running some benchmarks to get F1_Max and will update the table.
Overall, it looks like the difference is less than 2%