Skip to content

Commit ce061bb

Browse files
Update e2e tests to use convert_model (openvinotoolkit#2152)
### Changes - Update torch examples, added argument `--export-model-path` instead of `--to-onnx` that select output type by suffix '.xml' or '.onnx'. - Update tests to use Command to run examples. - Remove tests of q_dq export path. - Remove code to make html report, now metrics dumps in `results.csv`. - Added "build url" in `results.csv`. - Actualize reference metrics. - Move normalization of input to accuracy cheker configs. - Use `target_ov` and `target_pt` to define reference metrics for backends. - Skip test `unet_mapillary_int8` and `unet_mapillary_magnitude_sparsity_int8` models by 123448. - Skip `resnet18_imagenet_binarization_dorefa` model by 22543. Updates in CI: - Update script to report `final e2e_result.html` for e2e tests. - Now if any test falls trigger_job will be fails. After merge requires update ci-pipelines. ### Related tickets 117885 ### Tests tests/torch/test_sota_checkpoints.py ### TODO - update metrics after merge openvinotoolkit#2227 - check metrics after openvinotoolkit#2211
1 parent db786a8 commit ce061bb

File tree

93 files changed

+1047
-840
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1047
-840
lines changed

examples/torch/classification/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ python main.py \
6464
- Use the `--resume` flag with the path to a previously saved model to resume training.
6565
- For Torchvision-supported image classification models, set `"pretrained": true` inside the NNCF config JSON file supplied via `--config` to initialize the model to be compressed with Torchvision-supplied pretrained weights, or, alternatively:
6666
- Use the `--weights` flag with the path to a compatible PyTorch checkpoint in order to load all matching weights from the checkpoint into the model - useful if you need to start compression-aware training from a previously trained uncompressed (FP32) checkpoint instead of performing compression-aware training from scratch.
67-
- Use the `--no_strip_on_export` to export not stripped model.
67+
- Use `--export-model-path` to specify the path to export the model in OpenVINO or ONNX format by using the .xml or .onnx suffix, respectively.
68+
- Use the `--no-strip-on-export` to export not stripped model.
69+
- Use the `--export-to-ir-via-onnx` to to export to OpenVINO, will produce the serialized OV IR object by first exporting the torch model object to an .onnx file and then converting that .onnx file to an OV IR file.
6870

6971
### Validate Your Model Checkpoint
7072

@@ -86,7 +88,7 @@ To export trained model to the ONNX format, use the following command:
8688
python main.py -m export \
8789
--config=configs/quantization/mobilenet_v2_imagenet_int8.json \
8890
--resume=../../results/quantization/mobilenet_v2_int8/6/checkpoints/epoch_1.pth \
89-
--to-onnx=../../results/mobilenet_v2_int8.onnx
91+
--to-ir=../../results
9092
```
9193

9294
### Export to OpenVINO™ Intermediate Representation (IR)

examples/torch/classification/configs/binarization/resnet18_imagenet_binarization_dorefa.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
"{re}ResNet/Sequential\\[layer4\\]/BasicBlock\\[0\\]/Sequential\\[downsample\\]/.*"]
2828
}
2929
],
30-
"no_strip_on_export": true
30+
"no_strip_on_export": true,
31+
"export_to_ir_via_onnx": true
3132
}

examples/torch/classification/configs/binarization/resnet18_imagenet_binarization_xnor.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
"{re}ResNet/Sequential\\[layer4\\]/BasicBlock\\[0\\]/Sequential\\[downsample\\]/.*"]
2828
}
2929
],
30-
"no_strip_on_export": true
30+
"no_strip_on_export": true,
31+
"export_to_ir_via_onnx": true
3132
}

examples/torch/classification/configs/mixed_precision/mobilenet_v2_imagenet_mixed_int_autoq_staged.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@
4040
"lr_poly_drop_duration_epochs": 10
4141
}
4242
},
43-
"no_strip_on_export": true
43+
"no_strip_on_export": true,
44+
"export_to_ir_via_onnx": true
4445
}

examples/torch/classification/configs/mixed_precision/mobilenet_v2_imagenet_mixed_int_hawq.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535
}
3636
}
3737
},
38-
"no_strip_on_export": true
38+
"no_strip_on_export": true,
39+
"export_to_ir_via_onnx": true
3940
}

examples/torch/classification/configs/mixed_precision/mobilenet_v2_imagenet_mixed_int_manual_staged.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,6 @@
166166
"disable_wd_start_epoch": 50
167167
}
168168
},
169-
"no_strip_on_export": true
169+
"no_strip_on_export": true,
170+
"export_to_ir_via_onnx": true
170171
}

examples/torch/classification/configs/mixed_precision/resnet50_imagenet_mixed_int_autoq_staged.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545
"lr_poly_drop_duration_epochs": 10
4646
}
4747
},
48-
"no_strip_on_export": true
48+
"no_strip_on_export": true,
49+
"export_to_ir_via_onnx": true
4950
}

examples/torch/classification/configs/mixed_precision/resnet50_imagenet_mixed_int_hawq.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535
"disable_wd_start_epoch": 20
3636
}
3737
},
38-
"no_strip_on_export": true
38+
"no_strip_on_export": true,
39+
"export_to_ir_via_onnx": true
3940
}

examples/torch/classification/configs/mixed_precision/resnet50_imagenet_mixed_int_manual.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,6 @@
171171
}
172172
}
173173
},
174-
"no_strip_on_export": true
174+
"no_strip_on_export": true,
175+
"export_to_ir_via_onnx": true
175176
}

examples/torch/classification/configs/mixed_precision/resnet50_imagenet_mixed_int_manual_staged.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,6 @@
173173
"disable_wd_start_epoch": 20
174174
}
175175
},
176-
"no_strip_on_export": true
176+
"no_strip_on_export": true,
177+
"export_to_ir_via_onnx": true
177178
}

examples/torch/classification/configs/mixed_precision/squeezenet1_1_imagenet_mixed_int_hawq.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535
}
3636
}
3737
},
38-
"no_strip_on_export": true
38+
"no_strip_on_export": true,
39+
"export_to_ir_via_onnx": true
3940
}

examples/torch/classification/configs/mixed_precision/squeezenet1_1_imagenet_mixed_int_hawq_old_eval.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"target_device": "TRIAL",
1010
"compression": {
1111
"algorithm": "quantization"
12-
}
12+
},
13+
"no_strip_on_export": true
1314
}

examples/torch/classification/configs/mixed_precision/squeezenet1_1_imagenet_mixed_int_manual.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,6 @@
9999
}
100100
}
101101
},
102-
"no_strip_on_export": true
102+
"no_strip_on_export": true,
103+
"export_to_ir_via_onnx": true
103104
}

examples/torch/classification/configs/mixed_precision/squeezenet1_1_imagenet_mixed_int_manual_staged.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,6 @@
111111
"disable_wd_start_epoch": 50
112112
}
113113
},
114-
"no_strip_on_export": true
114+
"no_strip_on_export": true,
115+
"export_to_ir_via_onnx": true
115116
}

examples/torch/classification/main.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ def model_eval_fn(model):
235235
load_state(model, model_state_dict, is_resume=True)
236236

237237
if is_export_only:
238-
export_model(compression_ctrl, config.to_onnx, config.no_strip_on_export)
239-
logger.info(f"Saved to {config.to_onnx}")
238+
export_model(compression_ctrl, config)
240239
return
241240

242241
model, _ = prepare_model_for_execution(model, config)
@@ -328,8 +327,7 @@ def configure_optimizers_fn():
328327
config.mlflow.end_run()
329328

330329
if "export" in config.mode:
331-
export_model(compression_ctrl, config.to_onnx, config.no_strip_on_export)
332-
logger.info(f"Saved to {config.to_onnx}")
330+
export_model(compression_ctrl, config)
333331

334332

335333
def train(

examples/torch/classification/staged_quantization_worker.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def autoq_eval_fn(model, eval_loader):
210210

211211
best_acc1 = 0
212212
# optionally resume from a checkpoint
213-
if resuming_checkpoint is not None and config.to_onnx is None:
213+
if resuming_checkpoint is not None and config.export_model_path is None:
214214
best_acc1 = resuming_checkpoint["best_acc1"]
215215
if "train" in config.mode:
216216
kd_loss_calculator.original_model.load_state_dict(resuming_checkpoint["original_model_state_dict"])
@@ -228,8 +228,7 @@ def autoq_eval_fn(model, eval_loader):
228228
log_common_mlflow_params(config)
229229

230230
if is_export_only:
231-
export_model(compression_ctrl, config.to_onnx, config.no_strip_on_export)
232-
logger.info(f"Saved to {config.to_onnx}")
231+
export_model(compression_ctrl, config)
233232
return
234233

235234
if config.execution_mode != ExecutionMode.CPU_ONLY:
@@ -262,8 +261,7 @@ def autoq_eval_fn(model, eval_loader):
262261
validate(val_loader, model, criterion, config)
263262

264263
if "export" in config.mode:
265-
export_model(compression_ctrl, config.to_onnx, config.no_strip_on_export)
266-
logger.info(f"Saved to {config.to_onnx}")
264+
export_model(compression_ctrl, config)
267265

268266

269267
def train_staged(

examples/torch/common/argparser.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ def get_common_argument_parser():
104104
parser.add_argument("--dist-url", default="tcp://127.0.0.1:8899", help="URL used to set up distributed training")
105105
parser.add_argument("--rank", default=0, type=int, help="Node rank for distributed training")
106106
parser.add_argument("--dist-backend", default="nccl", type=str, help="Distributed backend")
107-
parser.add_argument("--no_strip_on_export", help="Set to export not stripped model.", action="store_true")
107+
parser.add_argument("--no-strip-on-export", help="Set to export not stripped model.", action="store_true")
108+
parser.add_argument(
109+
"--export-to-ir-via-onnx",
110+
help="When used with the `exported-model-path` option to export to OpenVINO, will produce the serialized "
111+
"OV IR object by first exporting the torch model object to an .onnx file and then converting that .onnx file "
112+
"to an OV IR file.",
113+
action="store_true",
114+
)
108115

109116
# Hyperparameters
110117
parser.add_argument(
@@ -141,7 +148,7 @@ def get_common_argument_parser():
141148

142149
# Dataset
143150
parser.add_argument(
144-
"--data", dest="dataset_dir", type=str, help="Path to the root directory of the selected dataset. "
151+
"--data", dest="dataset_dir", type=str, help="Path to the root directory of the selected dataset."
145152
)
146153

147154
# Settings
@@ -169,8 +176,13 @@ def get_common_argument_parser():
169176
)
170177

171178
parser.add_argument("--save-freq", default=5, type=int, help="Checkpoint save frequency (epochs). Default: 5")
172-
173-
parser.add_argument("--to-onnx", type=str, metavar="PATH", default=None, help="Export to ONNX model by given path")
179+
parser.add_argument(
180+
"--export-model-path",
181+
type=str,
182+
metavar="PATH",
183+
default=None,
184+
help="The path to export the model in OpenVINO or ONNX format by using the .xml or .onnx suffix, respectively.",
185+
)
174186

175187
# Display
176188
parser.add_argument(
@@ -191,6 +203,6 @@ def get_common_argument_parser():
191203

192204
def parse_args(parser, argv):
193205
args = parser.parse_args(argv)
194-
if "export" in args.mode and args.to_onnx is None:
195-
raise RuntimeError("--mode export requires --to-onnx argument to be set")
206+
if "export" in args.mode and args.export_model_path is None:
207+
raise RuntimeError("--mode export requires --export-model-path argument to be set")
196208
return args

examples/torch/common/export.py

+44-9
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,64 @@
88
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
11+
from pathlib import Path
12+
1113
import torch
1214

15+
from examples.common.sample_config import SampleConfig
16+
from examples.torch.common.example_logger import logger
1317
from nncf.api.compression import CompressionAlgorithmController
1418
from nncf.torch.exporter import count_tensors
1519
from nncf.torch.exporter import generate_input_names_list
1620
from nncf.torch.exporter import get_export_args
1721

1822

19-
def export_model(ctrl: CompressionAlgorithmController, save_path: str, no_strip_on_export: bool) -> None:
23+
def export_model(ctrl: CompressionAlgorithmController, config: SampleConfig) -> None:
2024
"""
21-
Export compressed model. Supported only 'onnx' format.
25+
Export compressed model ot OpenVINO format.
2226
2327
:param controller: The compression controller.
24-
:param save_path: Path to save onnx file.
25-
:param no_strip_on_export: Set to skip strip model before export.
28+
:param config: The sample config.
2629
"""
27-
28-
model = ctrl.model if no_strip_on_export else ctrl.strip()
29-
30+
model = ctrl.model if config.no_strip_on_export else ctrl.strip()
3031
model = model.eval().cpu()
3132

3233
export_args = get_export_args(model, device="cpu")
3334
input_names = generate_input_names_list(count_tensors(export_args))
3435

35-
with torch.no_grad():
36-
torch.onnx.export(model, export_args, save_path, input_names=input_names)
36+
input_tensor_list = []
37+
input_shape_list = []
38+
for info in model.nncf.input_infos.elements:
39+
input_shape = tuple([1] + info.shape[1:])
40+
input_tensor_list.append(torch.rand(input_shape))
41+
input_shape_list.append(input_shape)
42+
43+
if len(input_tensor_list) == 1:
44+
input_tensor_list = input_tensor_list[0]
45+
input_shape_list = input_shape_list[0]
46+
47+
model_path = Path(config.export_model_path)
48+
model_path.parent.mkdir(exist_ok=True, parents=True)
49+
extension = model_path.suffix
50+
51+
if extension == ".onnx":
52+
with torch.no_grad():
53+
torch.onnx.export(model, input_tensor_list, model_path, input_names=input_names)
54+
elif extension == ".xml":
55+
import openvino as ov
56+
from openvino.tools.mo import convert_model
57+
58+
if config.export_to_ir_via_onnx:
59+
model_onnx_path = model_path.with_suffix(".onnx")
60+
with torch.no_grad():
61+
torch.onnx.export(model, input_tensor_list, model_onnx_path, input_names=input_names)
62+
ov_model = convert_model(model_onnx_path)
63+
else:
64+
ov_model = convert_model(model, example_input=input_tensor_list, input_shape=input_shape_list)
65+
# Rename input nodes
66+
for input_node, input_name in zip(ov_model.inputs, input_names):
67+
input_node.node.set_friendly_name(input_name)
68+
ov.save_model(ov_model, model_path)
69+
else:
70+
raise ValueError(f"--export-model-path argument should have suffix `.xml` or `.onnx` but got {extension}")
71+
logger.info(f"Saved to {model_path}")

examples/torch/object_detection/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ This scenario demonstrates quantization with fine-tuning of SSD300 on VOC datase
4949
- Use `--weights` flag with the path to a compatible PyTorch checkpoint in order to load all matching weights from the checkpoint into the model - useful if you need to start compression-aware training from a previously trained uncompressed (FP32) checkpoint instead of performing compression-aware training from scratch. This flag is optional, but highly recommended to use.
5050
- Use `--multiprocessing-distributed` flag to run in the distributed mode.
5151
- Use `--resume` flag with the path to a previously saved model to resume training.
52-
- Use the `--no_strip_on_export` to export not stripped model.
52+
- Use `--export-model-path` to specify the path to export the model in OpenVINO or ONNX format by using the .xml or .onnx suffix, respectively.
53+
- Use the `--no-strip-on-export` to export not stripped model.
54+
- Use the `--export-to-ir-via-onnx` to to export to OpenVINO, will produce the serialized OV IR object by first exporting the torch model object to an .onnx file and then converting that .onnx file to an OV IR file.
5355

5456
### Validate your model checkpoint
5557

@@ -62,7 +64,7 @@ If you want to validate an FP32 model checkpoint, make sure the compression algo
6264
### Export compressed model
6365

6466
To export trained model to ONNX format use the following command:
65-
`python main.py -m export --config configs/ssd300_vgg_voc_int8.json --data <path_to_dataset> --resume <path_to_compressed_model_checkpoint> --to-onnx=../../results/ssd300_int8.onnx`
67+
`python main.py -m export --config configs/ssd300_vgg_voc_int8.json --data <path_to_dataset> --resume <path_to_compressed_model_checkpoint> --to-ir=../../results`
6668

6769
### Export to OpenVINO Intermediate Representation (IR)
6870

examples/torch/object_detection/configs/ssd300_mobilenet_voc.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
"clip": false,
3535
"flip": true,
3636
"top_k": 200
37-
}
37+
},
38+
"export_to_ir_via_onnx": true
3839
}
39-

examples/torch/object_detection/configs/ssd300_mobilenet_voc_magnitude_int8.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
{
5858
"algorithm": "quantization"
5959
}
60-
]
60+
],
61+
"export_to_ir_via_onnx": true
6162
}
62-

examples/torch/object_detection/configs/ssd300_mobilenet_voc_rb_sparsity_int8.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@
5050
{
5151
"algorithm": "quantization"
5252
}
53-
]
53+
],
54+
"export_to_ir_via_onnx": true
5455
}
55-

examples/torch/object_detection/configs/ssd300_vgg_voc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
"steps": [8, 16, 32, 64, 100, 300],
3232
"aspect_ratios": [[2], [2, 3], [2, 3], [2, 3], [2], [2]],
3333
"flip": true
34-
}
34+
},
35+
"export_to_ir_via_onnx": true
3536
}

examples/torch/object_detection/configs/ssd300_vgg_voc_int8.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
"num_init_samples": 1280
4242
}
4343
}
44-
}
44+
},
45+
"export_to_ir_via_onnx": true
4546
}

examples/torch/object_detection/configs/ssd300_vgg_voc_int8_accuracy_aware.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@
4949
"num_init_samples": 1280
5050
}
5151
}
52-
}
52+
},
53+
"export_to_ir_via_onnx": true
5354
}

examples/torch/object_detection/configs/ssd300_vgg_voc_magnitude_sparsity_int8.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@
5757
}
5858
}
5959
}
60-
]
60+
],
61+
"export_to_ir_via_onnx": true
6162
}

examples/torch/object_detection/configs/ssd300_vgg_voc_pruning_geometric_median.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@
4444
"filter_importance": "geometric_median"
4545
}
4646
}
47-
]
47+
],
48+
"export_to_ir_via_onnx": true
4849
}

examples/torch/object_detection/configs/ssd512_vgg_voc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
"variance": [0.1, 0.1, 0.2, 0.2],
3333
"clip": false,
3434
"flip": true
35-
}
35+
},
36+
"export_to_ir_via_onnx": true
3637
}

examples/torch/object_detection/configs/ssd512_vgg_voc_int8.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
"num_init_samples": 640
3939
}
4040
}
41-
}
41+
},
42+
"export_to_ir_via_onnx": true
4243
}

0 commit comments

Comments
 (0)