javacpp-presets icon indicating copy to clipboard operation
javacpp-presets copied to clipboard

ONNX: can't get text format of ModelProto

Open alannnna opened this issue 5 years ago • 8 comments

ONNX's ModelProto (org.bytedeco.connx.ModelProto) appears to have no way to serialize to a text (human-readable) format.

I tried using TextFormat from Java protobufs but it doesn't work with a ModelProto.

Is there any other way to get a text format of a ModelProto?

alannnna avatar Jul 09 '19 18:07 alannnna

@EmergentOrder Any ideas?

saudet avatar Jul 11 '19 01:07 saudet

You can serialize it to a string with model.SerializeAsString(), although it's not exactly readable.

EmergentOrder avatar Jul 11 '19 13:07 EmergentOrder

Once you have the bytes, as per the example, try this: com.google.protobuf.Any.parseFrom(bytes).toString() Field names are lost, but should still be good enough to get an idea of the model structure.

EmergentOrder avatar Jul 11 '19 14:07 EmergentOrder

BTW, we should probably bundle the Java interface generated by Protocol Buffers as well.

saudet avatar Jul 12 '19 12:07 saudet

Thanks @EmergentOrder, that's clever and an improvement on SerializeAsString().

alannnna avatar Jul 14 '19 00:07 alannnna

I guess I figured out why it is so unconvenient. The java API already provide the API to dump readable string.

check the link 's toString.

However the Javacpp's onnx wrapper e.g. Model as a Pointer. The toString method becomes

    public String toString() {
        return this.getClass().getName() + "[address=0x" + Long.toHexString(this.address) + ",position=" + this.position + ",limit=" + this.limit + ",capacity=" + this.capacity + ",deallocator=" + this.deallocator + "]";
    }

However it should be something like

  @Override
  public final String toString() {
    return TextFormat.printer().printToString(this);
  }

@saudet what happen if a class (a pointer)'s method overlap with java's pre-defined method, like toString?

oneengineer avatar Apr 26 '21 17:04 oneengineer

My Bad. Since ONNX is translated from C, it should be DebugString API. The strange thing was that I got MessageLite at 0x1daf7208920 from this API, not the protobuf text I expected.

oneengineer avatar Apr 26 '21 17:04 oneengineer

@saudet what happen if a class (a pointer)'s method overlap with java's pre-defined method, like toString?

As long as its signature is String toString(), everything is going to work fine. Some of the classes from the C++ API of PyTorch have toString() methods and they work just fine. You could try to do something similar as this for ONNX as well: https://github.com/bytedeco/javacpp-presets/blob/master/pytorch/src/main/java/org/bytedeco/pytorch/presets/torch.java#L972

saudet avatar Apr 27 '21 00:04 saudet