javacpp-presets
javacpp-presets copied to clipboard
ONNX: can't get text format of ModelProto
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?
@EmergentOrder Any ideas?
You can serialize it to a string with model.SerializeAsString()
, although it's not exactly readable.
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.
BTW, we should probably bundle the Java interface generated by Protocol Buffers as well.
Thanks @EmergentOrder, that's clever and an improvement on SerializeAsString()
.
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?
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.
@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