fix(ANSModelPackage): read the head off the graph, not off the package's word
A package whose family says "yolo" may hold an end-to-end export. Boxes took the family from the backend marker, and the marker names the runtime, not the head — so a fast yolo26 export arrived declared as plain yolo while its graph emits [1, N, 6] with the NMS inside it. Decoding that as a [1, 4+nc, N] grid reads coordinates as confidences: detections everywhere, and nothing that looks like a failure. OpenModelPackage now reads the first output's dims from the model it has just decrypted, and corrects the family with the reason in the warning rather than swapping it silently. It costs nothing there: the bytes are in hand. The legacy marker is left alone, so anything converting back to a zip still writes what ANSCORE expects. A two-class v8 export is [1, 6, 8400] and must not be mistaken for one, which is why the 6 has to be the last axis with a detection count before it. Both cases are tested, and the package this Mac exported before the writer was fixed now reads as yolo10 here too. 46 checks, no failures. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -328,12 +328,15 @@ static std::string pbDim(int64_t v) { // Dimension.dim_value
|
||||
static std::string pbDynamicDim(const std::string& name) {
|
||||
return pbField(1, pbField(2, name)); // dim_param: the axis is open
|
||||
}
|
||||
static std::string fakeOnnx(const std::string& dims) {
|
||||
const std::string tensor = pbField(1, pbField(2, dims));
|
||||
const std::string input = pbField(1, "images") + pbField(2, tensor);
|
||||
const std::string graph = pbField(1, std::string(4096, 'n')) +
|
||||
pbField(5, std::string(65536, 'w')) +
|
||||
pbField(11, input);
|
||||
static std::string fakeOnnx(const std::string& dims,
|
||||
const std::string& outDims = std::string()) {
|
||||
auto value = [](const std::string& name, const std::string& d) {
|
||||
return pbField(1, name) + pbField(2, pbField(1, pbField(2, d)));
|
||||
};
|
||||
std::string graph = pbField(1, std::string(4096, 'n')) +
|
||||
pbField(5, std::string(65536, 'w')) +
|
||||
pbField(11, value("images", dims));
|
||||
if (!outDims.empty()) graph += pbField(12, value("output0", outDims));
|
||||
return pbField(7, graph);
|
||||
}
|
||||
|
||||
@@ -405,6 +408,57 @@ static void testShapeFromTheGraph(const fs::path& dir) {
|
||||
CHECK(modelpkg::OpenModelPackage(path, opened, err), ("open: " + err).c_str());
|
||||
CHECK(opened.inputWidth == 224 && opened.inputHeight == 224,
|
||||
"so the engine is told 224 from the graph, not left to assume 640");
|
||||
|
||||
// AN END-TO-END HEAD IN A PACKAGE THAT SAYS "yolo". Boxes used to take the
|
||||
// family from the backend marker, and the marker names the runtime, not the
|
||||
// head — so a yolo26 export with end2end on arrived declared as plain yolo.
|
||||
// Decoding [1,N,6] as a v8 grid reads coordinates as confidences: boxes
|
||||
// everywhere, and no error to notice. The graph settles it.
|
||||
{
|
||||
const std::string graph =
|
||||
fakeOnnx(pbDim(1) + pbDim(3) + pbDim(640) + pbDim(640),
|
||||
pbDim(1) + pbDim(300) + pbDim(6));
|
||||
modelpkg::PackRequest r = req;
|
||||
r.manifest = Json{{"task", "detection"},
|
||||
{"family", "yolo"},
|
||||
{"classes", Json::array({Json{{"id", 1}, {"name", "Tooth"}}})},
|
||||
{"models", Json::array({Json{{"id", "m0"},
|
||||
{"payload", "p0"},
|
||||
{"entry", true}}})}};
|
||||
r.models.clear();
|
||||
r.models.push_back({"p0", std::vector<uint8_t>(graph.begin(), graph.end())});
|
||||
const std::string p = (dir / "e2e.ansmodel").string();
|
||||
CHECK(modelpkg::pack(p, r, err), ("pack e2e: " + err).c_str());
|
||||
modelpkg::OpenedModel m;
|
||||
CHECK(modelpkg::OpenModelPackage(p, m, err) && m.family == "yolo10",
|
||||
"an end-to-end head is read as yolo10 whatever the package says");
|
||||
CHECK(m.warning.find("end-to-end") != std::string::npos,
|
||||
"and the reason is said out loud rather than silently swapped");
|
||||
CHECK(m.legacyMarker.empty() || m.legacyMarker == "tensorrt.txt",
|
||||
"while the legacy marker is left as it was");
|
||||
}
|
||||
|
||||
// A two-class v8 export is [1, 6, 8400]: a 6 in the shape, not a head that
|
||||
// has already decoded itself.
|
||||
{
|
||||
const std::string graph =
|
||||
fakeOnnx(pbDim(1) + pbDim(3) + pbDim(640) + pbDim(640),
|
||||
pbDim(1) + pbDim(6) + pbDim(8400));
|
||||
modelpkg::PackRequest r = req;
|
||||
r.manifest = Json{{"task", "detection"},
|
||||
{"family", "yolo"},
|
||||
{"classes", Json::array({Json{{"id", 1}, {"name", "Tooth"}}})},
|
||||
{"models", Json::array({Json{{"id", "m0"},
|
||||
{"payload", "p0"},
|
||||
{"entry", true}}})}};
|
||||
r.models.clear();
|
||||
r.models.push_back({"p0", std::vector<uint8_t>(graph.begin(), graph.end())});
|
||||
const std::string p = (dir / "twoclass.ansmodel").string();
|
||||
CHECK(modelpkg::pack(p, r, err), ("pack v8: " + err).c_str());
|
||||
modelpkg::OpenedModel m;
|
||||
CHECK(modelpkg::OpenModelPackage(p, m, err) && m.family == "yolo",
|
||||
"a 6 on the wrong axis with thousands of anchors stays plain yolo");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
Reference in New Issue
Block a user