-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Export Refactor][Image Classification]
export_model
function (#1883)
* initial commit * looking good, time to cleanup * Delete src/sparseml/export/helpers.py * Delete tests/sparseml/export/test_helpers.py * ready for review * improve design * tests pass * reuse _validate_dataset_num_classes * initial commit * Update src/sparseml/pytorch/image_classification/integration_helper_functions.py * Update src/sparseml/pytorch/image_classification/integration_helper_functions.py * ready for review * Update src/sparseml/export/export.py * Update src/sparseml/integration_helper_functions.py * initial commit * fixes * ready for review * nit * add return * make export function more general
- Loading branch information
1 parent
bac0802
commit 0830372
Showing
6 changed files
with
104 additions
and
13 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
import torch | ||
|
||
from sparseml.exporters import ExportTargets | ||
from sparseml.exporters.onnx_to_deepsparse import ONNXToDeepsparse | ||
from sparseml.pytorch.opset import TORCH_DEFAULT_ONNX_OPSET | ||
from sparseml.pytorch.torch_to_onnx_exporter import TorchToONNX | ||
|
||
|
||
__all__ = ["export_model"] | ||
|
||
|
||
def export_model( | ||
model: torch.nn.Module, | ||
sample_data: torch.Tensor, | ||
target_path: Union[Path, str], | ||
onnx_model_name: str, | ||
deployment_target: str = "deepsparse", | ||
opset: int = TORCH_DEFAULT_ONNX_OPSET, | ||
**kwargs, | ||
) -> str: | ||
""" | ||
Exports the torch model to the deployment target | ||
:param model: The torch model to export | ||
:param sample_data: The sample data to use for the export | ||
:param target_path: The path to export the model to | ||
:param onnx_model_name: The name to save the exported ONNX model as | ||
:param deployment_target: The deployment target to export to. Defaults to deepsparse | ||
:param opset: The opset to use for the export. Defaults to TORCH_DEFAULT_ONNX_OPSET | ||
:param kwargs: Additional kwargs to pass to the TorchToONNX exporter | ||
:return: The path to the exported model | ||
""" | ||
|
||
model.eval() | ||
|
||
exporter = TorchToONNX(sample_batch=sample_data, opset=opset, **kwargs) | ||
exporter.export(model, os.path.join(target_path, onnx_model_name)) | ||
if deployment_target == ExportTargets.deepsparse.value: | ||
exporter = ONNXToDeepsparse() | ||
model = exporter.load_model(os.path.join(target_path, onnx_model_name)) | ||
exporter.export(model, os.path.join(target_path, onnx_model_name)) | ||
if deployment_target == ExportTargets.onnx.value: | ||
pass | ||
else: | ||
raise ValueError(f"Unsupported deployment target: {deployment_target}") | ||
return os.path.join(target_path, onnx_model_name) |
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