feat(ANSModelPackage): ask the model its input size when nothing else says
A package now carries what its model expects on the input, but every package written before that does not — and neither does the zip, which is most of what is in the field. The engine was then left to assume 640x640, which is wrong for a classification export at 224 and for anything trained at a rectangular size. The model itself always knows. ANSOnnxShape reads the first input's shape straight out of the ONNX protobuf, from memory or from a file, walking the top level and SEEKING past the nodes and the initializers rather than reading them — the fields it wants sit after all of the weight on the wire, and a 324 MB model answers in ten milliseconds. OpenModelPackage uses it only when the manifest is silent, which costs nothing there because the bytes are already decrypted in hand. An axis the graph leaves open is reported as zero and a file that cannot be parsed is a failure: both mean "this model does not say", and the caller keeps the default it had. Being wrong about an input size would be worse than being silent about it. Checked against python-onnx on all 46 models on this machine, static and dynamic, four-dimensional and not, with no disagreements. The test writes ONNX graphs byte by byte, with node and initializer ahead of the input as a real export has them, and packs one into a package whose manifest says nothing — which is the case this exists for. 40 checks, no failures. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -39,6 +39,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "ANSModelPackage.h"
|
||||
#include "ANSOnnxShape.h"
|
||||
|
||||
namespace ANSCENTER {
|
||||
namespace modelpkg {
|
||||
@@ -203,6 +204,19 @@ inline bool OpenModelPackage(const std::string& path, OpenedModel& out,
|
||||
out.inputHeight = in.value("height", 0);
|
||||
out.name = m.value("project", std::string());
|
||||
if (out.name.empty()) out.name = path;
|
||||
|
||||
// A package written before manifests carried an input size still has to
|
||||
// answer, and the model in hand is the thing that knows. It costs nothing
|
||||
// here: the bytes are already decrypted.
|
||||
if (out.inputWidth <= 0 || out.inputHeight <= 0) {
|
||||
onnxshape::InputShape shape;
|
||||
std::string swhy;
|
||||
if (onnxshape::fromMemory(out.model.data(), out.model.size(), shape,
|
||||
swhy)) {
|
||||
out.inputWidth = shape.width;
|
||||
out.inputHeight = shape.height;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user