-
Notifications
You must be signed in to change notification settings - Fork 444
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
[Anomaly Task] Fix non deterministic + sample.py #1118
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
OpenVINO Training Extension interacts with the anomaly detection library ([Anomalib](https://github.com/openvinotoolkit/anomalib)) by providing interfaces in the `external/anomaly` of this repository. The `sample.py` file contained in this folder serves as an end-to-end example of how these interfaces are used. To begin using this script, first ensure that `ote_cli`, `ote_sdk` and `external/anomaly` dependencies are installed. | ||
|
||
To get started, we provide a handy script in `ote_anomalib/data/create_mvtec_ad_json_annotations.py` to help generate annotation json files for MVTec dataset. Assuming that you have placed the MVTec dataset in a directory your home folder (`~/dataset/MVTec`), you can run the following command to generate the annotations. | ||
|
||
```bash | ||
python create_mvtec_ad_json_annotations.py --data_path ~/datasets/MVTec --annotation_path ~/training_extensions/data/MVtec/ | ||
``` | ||
|
||
This will generate three folders in `~/training_extensions/data/MVtec/` for classification, segmentation and detection task. | ||
|
||
Then, to run sample.py you can use the following command. | ||
|
||
```bash | ||
python tools/sample.py --dataset_path ~/datasets/MVTec --category bottle --train-ann-files ../../data/MVtec/bottle/segmentation/train.json --val-ann-files ../../data/MVtec/bottle/segmentation/val.json --test-ann-files ../../data/MVtec/bottle/segmentation/test.json --model_template_path ./configs/anomaly_segmentation/padim/template.yaml | ||
``` | ||
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. Can you split the line here
|
||
|
||
Optionally, you can also optimize to `nncf` or `pot` by using the `--optimization` flag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
import os | ||
import shutil | ||
from argparse import Namespace | ||
from typing import Any, Dict, Type, Union | ||
from typing import Any, Dict, Optional, Type, Union | ||
|
||
from ote_anomalib import AnomalyNNCFTask, OpenVINOAnomalyTask | ||
from ote_anomalib.data.dataset import ( | ||
|
@@ -61,13 +61,18 @@ def __init__( | |
val_subset: Dict[str, str], | ||
test_subset: Dict[str, str], | ||
model_template_path: str, | ||
seed: Optional[int] = 0, | ||
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. If the variable |
||
) -> None: | ||
"""Initialize OteAnomalyTask. | ||
|
||
Args: | ||
dataset_path (str): Path to the MVTec dataset. | ||
seed (int): Seed to split the dataset into train/val/test splits. | ||
train_subset (Dict[str, str]): Dictionary containing path to train annotation file and path to dataset. | ||
val_subset (Dict[str, str]): Dictionary containing path to validation annotation file and path to dataset. | ||
test_subset (Dict[str, str]): Dictionary containing path to test annotation file and path to dataset. | ||
model_template_path (str): Path to model template. | ||
seed (Optional[int]): Setting seed to a value other than 0 also marks PytorchLightning trainer's | ||
deterministic flag to True. | ||
|
||
Example: | ||
>>> import os | ||
|
@@ -78,9 +83,12 @@ def __init__( | |
|
||
>>> model_template_path = "./configs/anomaly_classification/padim/template.yaml" | ||
>>> dataset_path = "./datasets/MVTec" | ||
>>> seed = 0 | ||
>>> task = OteAnomalyTask( | ||
... dataset_path=dataset_path, seed=seed, model_template_path=model_template_path | ||
... dataset_path=dataset_path, | ||
... train_subset={"ann_file": train.json, "data_root": dataset_path}, | ||
... val_subset={"ann_file": val.json, "data_root": dataset_path}, | ||
... test_subset={"ann_file": test.json, "data_root": dataset_path}, | ||
... model_template_path=model_template_path | ||
... ) | ||
|
||
>>> task.train() | ||
|
@@ -110,6 +118,7 @@ def __init__( | |
self.openvino_task: OpenVINOAnomalyTask | ||
self.nncf_task: AnomalyNNCFTask | ||
self.results = {"category": dataset_path} | ||
self.seed = seed | ||
|
||
def get_dataclass( | ||
self, | ||
|
@@ -176,9 +185,7 @@ def train(self) -> ModelEntity: | |
configuration=self.task_environment.get_model_configuration(), | ||
) | ||
self.torch_task.train( | ||
dataset=self.dataset, | ||
output_model=output_model, | ||
train_parameters=TrainParameters(), | ||
dataset=self.dataset, output_model=output_model, train_parameters=TrainParameters(), seed=self.seed | ||
) | ||
|
||
logger.info("Inferring the base torch model on the validation set.") | ||
|
@@ -364,6 +371,7 @@ def main() -> None: | |
val_subset=val_subset, | ||
test_subset=test_subset, | ||
model_template_path=args.model_template_path, | ||
seed=args.seed, | ||
) | ||
|
||
task.train() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the variable
Optional
, shouldn't it be set toNone
?