-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
implemented coreml exporter #3813
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
|
||
import coremltools as ct | ||
import torch | ||
|
||
from ludwig.api import LudwigModel | ||
from ludwig.model_export.base_model_exporter import BaseModelExporter, LudwigTorchWrapper | ||
|
||
|
||
class CoreMLExporter(BaseModelExporter): | ||
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. @saad-palapa I cannot find any problems; I would only like to request -- if you do not mind -- to please add type hints, at least to the non-obvious variables and all return values of methods (for me, personally, I prefer them everywhere). Also, could someone else -- @arnavgarg1, @geoffreyangus, @justinxzhao please review? Thank you. |
||
"""Class that abstracts the convertion of torch to onnx.""" | ||
|
||
def export(self, model_path, export_path, output_model_name): | ||
ludwig_model = LudwigModel.load(model_path) | ||
model = ludwig_model.model.to("cpu").eval() | ||
|
||
# option 1, works but hacky | ||
# encoder = model.input_features.module_dict.image_path__ludwig.encoder_obj.model # type: ignore | ||
# decoder = model.output_features.module_dict.label__ludwig.decoder_obj # type: ignore | ||
# model = torch.nn.Sequential(encoder, decoder) | ||
|
||
# option 2, doesn't work | ||
# throws error: RuntimeError: PyTorch convert function for op 'dictconstruct' not implemented. | ||
model = LudwigTorchWrapper(model) | ||
|
||
width = ludwig_model.config["input_features"][0]["preprocessing"]["width"] | ||
height = ludwig_model.config["input_features"][0]["preprocessing"]["height"] | ||
example_input = torch.randn(1, 3, width, height) | ||
|
||
traced_model = torch.jit.trace(model, example_input, strict=False) | ||
|
||
image_input = ct.ImageType( | ||
name="image", | ||
shape=example_input.shape, | ||
scale=1 / 255.0, | ||
bias=[0.0, 0.0, 0.0], | ||
) | ||
|
||
print(f"converting to core_ml, input_input={image_input}") | ||
|
||
coreml_model = ct.convert( | ||
traced_model, | ||
convert_to="neuralnetwork", | ||
inputs=[image_input], | ||
debug=True, | ||
) | ||
|
||
coreml_path = os.path.join(export_path, output_model_name) | ||
coreml_model.save(coreml_path) # type: ignore | ||
|
||
def check_model_export(self, path): | ||
coreml_model = ct.models.MLModel(path) | ||
coreml_model.get_spec() |
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.
Please add
from __future__ import annotations
at the very top followed by a blank line.This will help modernize type hints (and make them easier to add -- without cyclical conflicts). Thank you!