Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Export onnx model support #187

Closed
wants to merge 3 commits into from
Closed

Export onnx model support #187

wants to merge 3 commits into from

Conversation

qibaoyuan
Copy link
Contributor

Motivation and Context

I have use org.bytedeco.javacpp-presets and deeplearning4j in my JAVA application to do Deep learning task in production env, and it can use onnx as a pre-trained model. So, I make this PR to copy the model as a .onnx file after it was converted from pytorch-type model.I make a "export_onnx_path" para, in this PR I split "--output-model-path" to "--output-caffe2-path" and "--output-onnx-path" in "export" command.

How Has This Been Tested

my test code under JAVA applciation in javacpp-presets is as belows:

onnx.OpSchemaVector allSchemas = onnx.OpSchemaRegistry.get_all_schemas();
System.out.println(allSchemas.size());

byte[] bytes = 
 Files.readAllBytes(Paths.get("/Users/qibaoyuan/Documents/source/project/pycharm/pytext/minlpdemo/ner/model.pt.prd.2.onnx"));

onnx.ModelProto model = new onnx.ModelProto();
ParseProtoFromBytes(model, new BytePointer(bytes), bytes.length);

check_model(model);

InferShapes(model);

onnx.StringVector passes = new onnx.StringVector("eliminate_nop_transpose", "eliminate_nop_pad", "fuse_consecutive_transposes", "fuse_transpose_into_gemm");
Optimize(model, passes);

check_model(model);

ConvertVersion(model, 8);

System.out.println(model.graph().input_size());

or in deeplearning4j:

    public static OnnxProto3.GraphProto loadModel(final String file) {
        try (InputStream is = new ClassPathResource(file).getInputStream()) {
            OnnxProto3.GraphProto graphProto = OnnxProto3.GraphProto.parseFrom(is);
            log.debug("load model from onnx:[{}]", graphProto);
            return graphProto;
        } catch (Exception ex) {
            log.error("error:[{}]", ExceptionUtils.getStackTrace(ex));
        }
        return null;

    }

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have completed my CLA (see CONTRIBUTING)
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Jan 4, 2019
@qibaoyuan qibaoyuan mentioned this pull request Jan 4, 2019
11 tasks
@seayoung1112 seayoung1112 mentioned this pull request Jan 4, 2019
@qibaoyuan
Copy link
Contributor Author

@seayoung1112 I have fixed the requested changes, please check.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seayoung1112 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants