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
Implement convert_to_onnx.py #4
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca1dc3c
Implement convert_to_onnx.py script
GiulioRomualdi 364b507
Ask fro torch 1.13.0 to add the possibility to export the model as onnx
GiulioRomualdi 635407a
Retrieve the input size from the model when converting in onnx
GiulioRomualdi 9ed8fec
Add model_49.onnx
GiulioRomualdi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
numpy==1.22.2 | ||
protobuf==3.19.4 | ||
tensorboard==2.8.0 | ||
torch==1.12.1 | ||
torch==1.13.0 | ||
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,47 @@ | ||
import torch | ||
from pathlib import Path | ||
import argparse | ||
|
||
|
||
def convert_model(model_path: Path, onnx_model_path: Path, opset_version: int): | ||
# Restore the model with the trained weights | ||
mann_restored = torch.load(str(model_path)) | ||
|
||
# Set dropout and batch normalization layers to evaluation mode before running inference | ||
mann_restored.eval() | ||
|
||
# Input to the model | ||
batch_size = 1 | ||
x = torch.randn(batch_size, 137, requires_grad=True) | ||
GiulioRomualdi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Export the model | ||
torch.onnx.export(mann_restored, # model being run | ||
x, # model input (or a tuple for multiple inputs) | ||
str(onnx_model_path), # where to save the model (can be a file or file-like object) | ||
export_params=True, # store the trained parameter weights inside the model file | ||
opset_version=opset_version, # the ONNX version to export the model to | ||
do_constant_folding=True, # whether to execute constant folding for optimization | ||
input_names=['input'], # the model's input names | ||
output_names=['output'], # the model's output names | ||
dynamic_axes={'input': {0: 'batch_size'}, # variable length axes | ||
'output': {0: 'batch_size'}}) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Convert mann-pytorch model into a onnx model.') | ||
parser.add_argument('--output', '-o', type=lambda p: Path(p).absolute(), required=True, | ||
help='Onnx model path.') | ||
parser.add_argument('--torch_model_path', '-i', type=lambda p: Path(p).absolute(), | ||
default=Path(__file__).absolute().parent.parent / | ||
"models" / "storage_20220909-131438" / "models" / "model_49.pth", | ||
required=False, | ||
help='Pytorch model location.') | ||
parser.add_argument('--onnx_opset_version', type=int, default=12, required=False, | ||
help='The ONNX version to export the model to. At least 12 is required.') | ||
args = parser.parse_args() | ||
|
||
convert_model(model_path=args.torch_model_path, onnx_model_path=args.output, opset_version=args.onnx_opset_version) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.
I tried to run both the training and testing scripts with this PyTorch version. They work fine.