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

✨ CS-Flow model #657

Merged
merged 32 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
65345f9
Use FrEIA from pip + add initial cs-flow model
Oct 31, 2022
dbf7e98
Merge branch 'main' into feature/ashwin/cs-flow
Oct 31, 2022
d35c842
Explicitly freeze feature extractor weights
Nov 1, 2022
cec6b53
Fix pre-commit
Nov 1, 2022
274a022
Fix pylint issues
Nov 1, 2022
77024ce
Fix pylint issues
Nov 1, 2022
9c2b8e3
Rename variable
Nov 1, 2022
0bdedd3
Remove requires grad from timm
Nov 1, 2022
3c585fd
Add csflow to tests
Nov 1, 2022
f6a5921
Support two map modes
Nov 7, 2022
1c2a567
Merge branch 'main' into feature/ashwin/cs-flow
samet-akcay Nov 8, 2022
5bea408
Add metrics for cs-flow
Nov 14, 2022
f28f986
Merge branch 'feature/ashwin/cs-flow' of github.com:openvinotoolkit/a…
Nov 14, 2022
b99e13e
Merge branch 'main' into feature/ashwin/cs-flow
Nov 18, 2022
77c4594
Use the same betas as in paper
Nov 18, 2022
6862524
Add model description + images
Nov 21, 2022
9c22398
Merge branch 'main' into feature/ashwin/cs-flow
Nov 25, 2022
27fa94b
TimmFeatureExtractor->FeatureExtractor
Nov 25, 2022
ab1fc29
Convert torchfx feature extractor to nn.Module
Nov 25, 2022
ab9fb9b
Migrate to different markdown linter + format files based on it
Nov 28, 2022
be856a3
Initial PR feedback changes
Nov 29, 2022
6d742b1
Merge branch 'main' into feature/ashwin/cs-flow
Nov 29, 2022
1fe03ec
refactor torch_model
Nov 29, 2022
8acde5a
Address changes in lightning_model
Nov 29, 2022
cb5d0e6
Fix tests
Nov 29, 2022
d96a3ce
Merge branch 'main' into feature/ashwin/cs-flow
Dec 8, 2022
472b330
Fix parameters
Dec 9, 2022
473fb71
Comments + minor refactor
Dec 13, 2022
6ef2063
Refactor method name
Dec 13, 2022
d2e0066
Add comments to cross-conv block
Dec 13, 2022
a474c0e
Merge branch 'main' into feature/ashwin/cs-flow
Dec 20, 2022
18544c6
Add loss logging to csflow
Dec 20, 2022
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
12 changes: 6 additions & 6 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ labels: ""
assignees: ""
---

**Describe the bug**
## Describe the bug

- A clear and concise description of what the bug is.

**To Reproduce**
## To Reproduce

Steps to reproduce the behavior:

Expand All @@ -19,22 +19,22 @@ Steps to reproduce the behavior:
1. Scroll down to '....'
1. See error

**Expected behavior**
## Expected behavior

- A clear and concise description of what you expected to happen.

**Screenshots**
## Screenshots

- If applicable, add screenshots to help explain your problem.

**Hardware and Software Configuration**
## Hardware and Software Configuration

- OS: [Ubuntu, OD]
- NVIDIA Driver Version [470.57.02]
- CUDA Version [e.g. 11.4]
- CUDNN Version [e.g. v11.4.120]
- OpenVINO Version [Optional e.g. v2021.4.2]

**Additional context**
## Additional context

- Add any other context about the problem here.
8 changes: 4 additions & 4 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ labels: ""
assignees: ""
---

**Is your feature request related to a problem? Please describe.**
## Is your feature request related to a problem? Please describe

- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
## Describe the solution you'd like

- A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
## Describe alternatives you've considered

- A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
## Additional context

- Add any other context or screenshots about the feature request here.
34 changes: 34 additions & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Default state for all rules
default: true

# Path to configuration file to extend
extends: null

# MD001/heading-increment/header-increment - Heading levels should only increment by one level at a time
MD001: true

# MD013/line-length - Line length
MD013:
# Number of characters
line_length: 1000

# This is not useful for some files such as `CHANGELOG.md`
MD024:
# Only check sibling headings
allow_different_nesting: true
# Only check sibling headings
siblings_only: true

MD033: false

# If a page is printed, it helps if the URL is viewable.
MD034: false # Bare URL used

# This is needed for the anomalib tagline in the README
MD036: false # Emphasis used instead of a header

# Some md files have comments or links at the top of the files.
MD041: false # First line in file should be a top level header

# Badges have empty links
MD042: false # No empty links
1 change: 0 additions & 1 deletion .mdlrc

This file was deleted.

12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ repos:
hooks:
- id: flake8
args: ["--max-line-length=120", "--ignore=E203,W503"]
exclude: "tests|anomalib/models/components/freia"
exclude: "tests"

# python linting
- repo: https://github.com/PyCQA/pylint
Expand All @@ -42,15 +42,15 @@ repos:
entry: pylint --score=no
language: system
types: [python]
exclude: "tests|docs|anomalib/models/components/freia"
exclude: "tests|docs"

# python static type checking
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.971"
hooks:
- id: mypy
additional_dependencies: [types-PyYAML]
exclude: "tests|anomalib/models/components/freia"
exclude: "tests"

- repo: https://github.com/PyCQA/pydocstyle
rev: 6.1.1
Expand All @@ -61,7 +61,7 @@ repos:
entry: pydocstyle
language: python
types: [python]
exclude: "tests|docs|anomalib/models/components/freia"
exclude: "tests|docs"

# notebooks.
- repo: https://github.com/nbQA-dev/nbQA
Expand All @@ -78,8 +78,8 @@ repos:
hooks:
- id: prettier

- repo: https://github.com/jumanjihouse/pre-commit-hooks
rev: 2.1.6
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.32.2
hooks:
- id: markdownlint

Expand Down
14 changes: 7 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## v0.3.7

## What's Changed
### What's Changed

- Feature/comet logging by @sherpan in <https://github.com/openvinotoolkit/anomalib/pull/517>
- 🐞 Fix linting issues by @ashwinvaidya17 in <https://github.com/openvinotoolkit/anomalib/pull/535>
Expand Down Expand Up @@ -47,7 +47,7 @@ New Contributors

## v.0.3.6

## What's Changed
### What's Changed

- Add publish workflow + update references to main by @ashwinvaidya17 in <https://github.com/openvinotoolkit/anomalib/pull/480>
- Fix Dockerfile by @ORippler in <https://github.com/openvinotoolkit/anomalib/pull/478>
Expand Down Expand Up @@ -85,7 +85,7 @@ New Contributors

## v.0.3.5

## What's Changed
### What's Changed

- 🐞 Fix inference for draem by @djdameln in <https://github.com/openvinotoolkit/anomalib/pull/470>
- 🐞 🛠 Bug fix in the inferencer by @samet-akcay in <https://github.com/openvinotoolkit/anomalib/pull/475>
Expand All @@ -94,7 +94,7 @@ New Contributors

## v.0.3.4

## What's Changed
### What's Changed

- Add encoding to LONG_DESCRIPTION in setup.py by @samet-akcay in <https://github.com/openvinotoolkit/anomalib/pull/419>
- Fix visualization by @ORippler in <https://github.com/openvinotoolkit/anomalib/pull/417>
Expand Down Expand Up @@ -130,7 +130,7 @@ New Contributors

## v.0.3.3

## What's Changed
### What's Changed

- 🚚 Move initialization log message to base class by @djdameln in <https://github.com/openvinotoolkit/anomalib/pull/363>
- 🚚 Move logging from train.py to the getter functions by @samet-akcay in <https://github.com/openvinotoolkit/anomalib/pull/365>
Expand All @@ -155,7 +155,7 @@ New Contributors

## v.0.3.2

## What's Changed
### What's Changed

- Refactor `AnomalyModule` and `LightningModules` to explicitly define class arguments. by @samet-akcay in <https://github.com/openvinotoolkit/anomalib/pull/315>
- 🐞 Fix inferencer in Gradio by @ashwinvaidya17 in <https://github.com/openvinotoolkit/anomalib/pull/332>
Expand All @@ -176,7 +176,7 @@ New Contributors

## v.0.3.1

## What's Changed
### What's Changed

- 🔧 Properly assign values to dataframe in folder dataset. by @samet-akcay in <https://github.com/openvinotoolkit/anomalib/pull/272>
- ➕ Add warnings ⚠️ for inproper task setting in config files. by @samet-akcay in <https://github.com/openvinotoolkit/anomalib/pull/274>
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Anomalib is a deep learning library that aims to collect state-of-the-art anomal

![Sample Image](./docs/source/images/readme.png)

**Key features:**
## Key features

- The largest public collection of ready-to-use deep learning anomaly detection algorithms and benchmark datasets.
- [**PyTorch Lightning**](https://www.pytorchlightning.ai/) based model implementations to reduce boilerplate code and limit the implementation efforts to the bare essentials.
Expand Down Expand Up @@ -308,13 +308,16 @@ Note: Set your API Key for [Comet.ml](https://www.comet.com/signup?utm_source=an
MVTec AD dataset is one of the main benchmarks for anomaly detection, and is released under the
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License [(CC BY-NC-SA 4.0)](https://creativecommons.org/licenses/by-nc-sa/4.0/).

> Note: These metrics are collected with image size of 256 and seed `42`. This common setting is used to make model comparisons fair.

## Image-Level AUC

| Model | | Avg | Carpet | Grid | Leather | Tile | Wood | Bottle | Cable | Capsule | Hazelnut | Metal Nut | Pill | Screw | Toothbrush | Transistor | Zipper |
| ------------- | ------------------ | :-------: | :-------: | :-------: | :-----: | :-------: | :-------: | :-----: | :-------: | :-------: | :------: | :-------: | :-------: | :-------: | :--------: | :--------: | :-------: |
| **PatchCore** | **Wide ResNet-50** | **0.980** | 0.984 | 0.959 | 1.000 | **1.000** | 0.989 | 1.000 | **0.990** | **0.982** | 1.000 | 0.994 | 0.924 | 0.960 | 0.933 | **1.000** | 0.982 |
| PatchCore | ResNet-18 | 0.973 | 0.970 | 0.947 | 1.000 | 0.997 | 0.997 | 1.000 | 0.986 | 0.965 | 1.000 | 0.991 | 0.916 | **0.943** | 0.931 | 0.996 | 0.953 |
| CFlow | Wide ResNet-50 | 0.962 | 0.986 | 0.962 | **1.0** | 0.999 | **0.993** | **1.0** | 0.893 | 0.945 | **1.0** | **0.995** | 0.924 | 0.908 | 0.897 | 0.943 | **0.984** |
| CS-Flow | EfficientNet-B5 | 0.972 | 0.995 | 0.982 | **1** | 0.972 | 0.988 | **1** | 0.97 | 0.907 | 0.995 | 0.972 | 0.953 | 0.896 | 0.969 | 0.987 | 0.987 |
| PaDiM | Wide ResNet-50 | 0.950 | **0.995** | 0.942 | 1.0 | 0.974 | **0.993** | 0.999 | 0.878 | 0.927 | 0.964 | 0.989 | **0.939** | 0.845 | 0.942 | 0.976 | 0.882 |
| PaDiM | ResNet-18 | 0.891 | 0.945 | 0.857 | 0.982 | 0.950 | 0.976 | 0.994 | 0.844 | 0.901 | 0.750 | 0.961 | 0.863 | 0.759 | 0.889 | 0.920 | 0.780 |
| STFPM | Wide ResNet-50 | 0.876 | 0.957 | 0.977 | 0.981 | 0.976 | 0.939 | 0.987 | 0.878 | 0.732 | 0.995 | 0.973 | 0.652 | 0.825 | 0.5 | 0.875 | 0.899 |
Expand All @@ -325,13 +328,14 @@ Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
| DFKDE | ResNet-18 | 0.762 | 0.646 | 0.577 | 0.669 | 0.965 | 0.863 | 0.951 | 0.751 | 0.698 | 0.806 | 0.729 | 0.607 | 0.694 | 0.767 | 0.839 | 0.866 |
| GANomaly | | 0.421 | 0.203 | 0.404 | 0.413 | 0.408 | 0.744 | 0.251 | 0.457 | 0.682 | 0.537 | 0.270 | 0.472 | 0.231 | 0.372 | 0.440 | 0.434 |

### Pixel-Level AUC
## Pixel-Level AUC

| Model | | Avg | Carpet | Grid | Leather | Tile | Wood | Bottle | Cable | Capsule | Hazelnut | Metal Nut | Pill | Screw | Toothbrush | Transistor | Zipper |
| ------------- | ------------------ | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :-------: | :--------: | :--------: | :-------: |
| **PatchCore** | **Wide ResNet-50** | **0.980** | 0.988 | 0.968 | 0.991 | 0.961 | 0.934 | 0.984 | **0.988** | **0.988** | 0.987 | **0.989** | 0.980 | **0.989** | 0.988 | **0.981** | 0.983 |
| PatchCore | ResNet-18 | 0.976 | 0.986 | 0.955 | 0.990 | 0.943 | 0.933 | 0.981 | 0.984 | 0.986 | 0.986 | 0.986 | 0.974 | 0.991 | 0.988 | 0.974 | 0.983 |
| CFlow | Wide ResNet-50 | 0.971 | 0.986 | 0.968 | 0.993 | **0.968** | 0.924 | 0.981 | 0.955 | **0.988** | **0.990** | 0.982 | **0.983** | 0.979 | 0.985 | 0.897 | 0.980 |
| CS-Flow | EfficientNet B5 | 0.845 | 0.847 | 0.746 | 0.851 | 0.775 | 0.677 | 0.853 | 0.863 | 0.882 | 0.895 | 0.932 | 0.92 | 0.779 | 0.892 | 0.96 | 0.803 |
| PaDiM | Wide ResNet-50 | 0.979 | **0.991** | 0.970 | 0.993 | 0.955 | **0.957** | **0.985** | 0.970 | **0.988** | 0.985 | 0.982 | 0.966 | 0.988 | **0.991** | 0.976 | **0.986** |
| PaDiM | ResNet-18 | 0.968 | 0.984 | 0.918 | **0.994** | 0.934 | 0.947 | 0.983 | 0.965 | 0.984 | 0.978 | 0.970 | 0.957 | 0.978 | 0.988 | 0.968 | 0.979 |
| STFPM | Wide ResNet-50 | 0.903 | 0.987 | **0.989** | 0.980 | 0.966 | 0.956 | 0.966 | 0.913 | 0.956 | 0.974 | 0.961 | 0.946 | 0.988 | 0.178 | 0.807 | 0.980 |
Expand All @@ -344,6 +348,7 @@ Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
| **PatchCore** | **Wide ResNet-50** | **0.976** | 0.971 | 0.974 | **1.000** | **1.000** | 0.967 | **1.000** | 0.968 | **0.982** | **1.000** | 0.984 | 0.940 | 0.943 | 0.938 | **1.000** | **0.979** |
| PatchCore | ResNet-18 | 0.970 | 0.949 | 0.946 | **1.000** | 0.98 | **0.992** | **1.000** | **0.978** | 0.969 | **1.000** | **0.989** | 0.940 | 0.932 | 0.935 | 0.974 | 0.967 |
| CFlow | Wide ResNet-50 | 0.944 | 0.972 | 0.932 | **1.0** | 0.988 | 0.967 | **1.0** | 0.832 | 0.939 | **1.0** | 0.979 | 0.924 | **0.971** | 0.870 | 0.818 | 0.967 |
| CS-Flow | EfficientNet B5 | 0.965 | 0.983 | 0.982 | **1** | 0.957 | 0.966 | **1** | 0.945 | 0.944 | 0.986 | 0.963 | 0.965 | 0.906 | 0.949 | 0.938 | 0.987 |
| PaDiM | Wide ResNet-50 | 0.951 | **0.989** | 0.930 | **1.0** | 0.960 | 0.983 | 0.992 | 0.856 | **0.982** | 0.937 | 0.978 | **0.946** | 0.895 | 0.952 | 0.914 | 0.947 |
| PaDiM | ResNet-18 | 0.916 | 0.930 | 0.893 | 0.984 | 0.934 | 0.952 | 0.976 | 0.858 | 0.960 | 0.836 | 0.974 | 0.932 | 0.879 | 0.923 | 0.796 | 0.915 |
| STFPM | Wide ResNet-50 | 0.926 | 0.973 | 0.973 | 0.974 | 0.965 | 0.929 | 0.976 | 0.853 | 0.920 | 0.972 | 0.974 | 0.922 | 0.884 | 0.833 | 0.815 | 0.931 |
Expand Down
3 changes: 3 additions & 0 deletions anomalib/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from anomalib.models.cflow import Cflow
from anomalib.models.components import AnomalyModule
from anomalib.models.cs_flow import CsFlow
from anomalib.models.dfkde import Dfkde
from anomalib.models.dfm import Dfm
from anomalib.models.draem import Draem
Expand All @@ -25,6 +26,7 @@

__all__ = [
"Cflow",
"CsFlow",
"Dfkde",
"Dfm",
"Draem",
Expand Down Expand Up @@ -73,6 +75,7 @@ def get_model(config: Union[DictConfig, ListConfig]) -> AnomalyModule:

model_list: List[str] = [
"cflow",
"cs_flow",
ashwinvaidya17 marked this conversation as resolved.
Show resolved Hide resolved
"dfkde",
"dfm",
"draem",
Expand Down
5 changes: 2 additions & 3 deletions anomalib/models/cflow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

import numpy as np
import torch
from FrEIA.framework import SequenceINN
from FrEIA.modules import AllInOneBlock
from torch import nn

from anomalib.models.components.freia.framework import SequenceINN
from anomalib.models.components.freia.modules import AllInOneBlock

logger = logging.getLogger(__name__)


Expand Down
5 changes: 3 additions & 2 deletions anomalib/models/components/feature_extractors/torchfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BackboneParams:
init_args: Dict = field(default_factory=dict)


class TorchFXFeatureExtractor:
class TorchFXFeatureExtractor(nn.Module):
"""Extract features from a CNN.

Args:
Expand Down Expand Up @@ -74,6 +74,7 @@ def __init__(
weights: Optional[Union[WeightsEnum, str]] = None,
requires_grad: bool = False,
):
super().__init__()
if isinstance(backbone, dict):
backbone = BackboneParams(**backbone)
elif not isinstance(backbone, BackboneParams): # if str or nn.Module
Expand Down Expand Up @@ -169,6 +170,6 @@ def _get_backbone_class(backbone: str) -> Callable[..., nn.Module]:

return backbone_class

def __call__(self, inputs: Tensor) -> Dict[str, Tensor]:
def forward(self, inputs: Tensor) -> Dict[str, Tensor]:
"""Extract features from the input."""
return self.feature_extractor(inputs)
7 changes: 0 additions & 7 deletions anomalib/models/components/freia/README.md

This file was deleted.

16 changes: 0 additions & 16 deletions anomalib/models/components/freia/__init__.py

This file was deleted.

9 changes: 0 additions & 9 deletions anomalib/models/components/freia/framework/__init__.py

This file was deleted.

Loading