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

Update nncf export #2286

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 src/anomalib/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,22 +912,22 @@ def export(
CLI Usage:
1. To export as a torch ``.pt`` file you can run the following command.
```python
anomalib export --model Padim --export_mode torch --ckpt_path <PATH_TO_CHECKPOINT>
anomalib export --model Padim --export_type torch --ckpt_path <PATH_TO_CHECKPOINT>
```
2. To export as an ONNX ``.onnx`` file you can run the following command.
```python
anomalib export --model Padim --export_mode onnx --ckpt_path <PATH_TO_CHECKPOINT> \
anomalib export --model Padim --export_type onnx --ckpt_path <PATH_TO_CHECKPOINT> \
--input_size "[256,256]"
```
3. To export as an OpenVINO ``.xml`` and ``.bin`` file you can run the following command.
```python
anomalib export --model Padim --export_mode openvino --ckpt_path <PATH_TO_CHECKPOINT> \
--input_size "[256,256] --compression_type "fp16"
anomalib export --model Padim --export_type openvino --ckpt_path <PATH_TO_CHECKPOINT> \
--input_size "[256,256] --compression_type FP16
```
4. You can also quantize OpenVINO model with the following.
```python
anomalib export --model Padim --export_mode openvino --ckpt_path <PATH_TO_CHECKPOINT> \
--input_size "[256,256]" --compression_type "int8_ptq" --data MVTec
anomalib export --model Padim --export_type openvino --ckpt_path <PATH_TO_CHECKPOINT> \
--input_size "[256,256]" --compression_type INT8_PTQ --data MVTec
```
"""
export_type = ExportType(export_type)
Expand Down
9 changes: 6 additions & 3 deletions src/anomalib/models/components/base/export_mixin.py
ashwinvaidya17 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,14 @@
if datamodule is None:
msg = "Datamodule must be provided for OpenVINO INT8_PTQ compression"
raise ValueError(msg)
datamodule.setup("fit")

Check warning on line 333 in src/anomalib/models/components/base/export_mixin.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/models/components/base/export_mixin.py#L333

Added line #L333 was not covered by tests

model_input = model.input(0)

if model_input.partial_shape[0].is_static:
datamodule.train_batch_size = model_input.shape[0]

dataloader = datamodule.train_dataloader()
dataloader = datamodule.val_dataloader()

Check warning on line 340 in src/anomalib/models/components/base/export_mixin.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/models/components/base/export_mixin.py#L340

Added line #L340 was not covered by tests
if len(dataloader.dataset) < 300:
logger.warning(
f">300 images recommended for INT8 quantization, found only {len(dataloader.dataset)} images",
Expand Down Expand Up @@ -373,6 +374,8 @@
if datamodule is None:
msg = "Datamodule must be provided for OpenVINO INT8_PTQ compression"
raise ValueError(msg)
datamodule.setup("fit")

Check warning on line 377 in src/anomalib/models/components/base/export_mixin.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/models/components/base/export_mixin.py#L377

Added line #L377 was not covered by tests

if metric is None:
msg = "Metric must be provided for OpenVINO INT8_ACQ compression"
raise ValueError(msg)
Expand All @@ -383,14 +386,14 @@
datamodule.train_batch_size = model_input.shape[0]
datamodule.eval_batch_size = model_input.shape[0]

dataloader = datamodule.train_dataloader()
dataloader = datamodule.val_dataloader()

Check warning on line 389 in src/anomalib/models/components/base/export_mixin.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/models/components/base/export_mixin.py#L389

Added line #L389 was not covered by tests
if len(dataloader.dataset) < 300:
logger.warning(
f">300 images recommended for INT8 quantization, found only {len(dataloader.dataset)} images",
)

calibration_dataset = nncf.Dataset(dataloader, lambda x: x["image"])
validation_dataset = nncf.Dataset(datamodule.val_dataloader())
validation_dataset = nncf.Dataset(datamodule.test_dataloader())

Check warning on line 396 in src/anomalib/models/components/base/export_mixin.py

View check run for this annotation

Codecov / codecov/patch

src/anomalib/models/components/base/export_mixin.py#L396

Added line #L396 was not covered by tests

if isinstance(metric, str):
metric = create_metric_collection([metric])[metric]
Expand Down
Loading