소스 검색

Add ONNX text test files (#884)

Lutz Roeder 4 년 전
부모
커밋
a1025f5db1
2개의 변경된 파일43개의 추가작업 그리고 12개의 파일을 삭제
  1. 21 5
      source/onnx.js
  2. 22 7
      test/models.json

+ 21 - 5
source/onnx.js

@@ -1764,7 +1764,8 @@ onnx.TextReader = class {
                 lines.push(line);
             }
             const content = lines.join('\n');
-            if (/^\s*<\s*ir_version\s*:/m.exec(content)) {
+            if (/^\s*<\s*ir_version\s*:/m.exec(content) ||
+                /^\s*[a-zA-Z][a-zA-Z0-9]*\s*\(.*\)\s=>\s\(/m.exec(content)) {
                 return new onnx.TextReader(data);
             }
         }
@@ -2040,13 +2041,17 @@ onnx.TextReader = class {
 
     _type(elem_type) {
         const type = new onnx.proto.TypeProto();
-        type.elem_type = elem_type;
+        type.tensor_type = new onnx.proto.TypeProto.Tensor();
+        type.tensor_type.elem_type = elem_type;
         if (this._match('[')) {
             if (!this._match(']')) {
-                type.shape = this._shape();
+                type.tensor_type.shape = this._shape();
                 this._expect(']');
             }
         }
+        else {
+            type.tensor_type.shape = new onnx.proto.TensorShapeProto();
+        }
         return type;
     }
 
@@ -2069,9 +2074,20 @@ onnx.TextReader = class {
         return shape;
     }
 
-    _tensor() {
+    _tensor(elem_type) {
         const tensor = new onnx.proto.TensorProto();
-        this._expect('=');
+        if (!this._dataTypes.has(elem_type)) {
+            this._throw("Unexpected type '" + elem_type + "'.");
+        }
+        const type = this._type(this._dataTypes.get(elem_type));
+        if (!type.tensor_type.elem_type) {
+            this._throw('Expected tensor data type.');
+        }
+        if (!type.tensor_type.shape || !type.tensor_type.shape.dim) {
+            this._throw('Expected tensor shape.');
+        }
+        tensor.data_type = type.tensor_type.elem_type;
+        tensor.dims = type.tensor_type.shape.dim.map((dim) => dim.dim_value);
         this._expect('{');
         if (!this._match('}')) {
             do {

+ 22 - 7
test/models.json

@@ -3264,13 +3264,6 @@
     "format": "ONNX v4",
     "link":   "https://github.com/lutzroeder/netron/issues/532"
   },
-  {
-    "type":   "onnx",
-    "target": "example.txt",
-    "source": "https://github.com/lutzroeder/netron/files/8174755/example.txt.zip[example.txt]",
-    "format": "ONNX Text v7",
-    "link":   "https://github.com/lutzroeder/netron/issues/532"
-  },
   {
     "type":   "onnx",
     "target": "Exermote.onnx",
@@ -3568,6 +3561,28 @@
     "format": "ONNX v7",
     "link":   "https://github.com/onnx/onnx/tree/master/onnx/backend/test/data"
   },
+  {
+    "type":   "onnx",
+    "target": "text_constant.txt",
+    "source": "https://github.com/lutzroeder/netron/files/8189630/text_constant.txt.zip[text_constant.txt]",
+    "format": "ONNX Text v0",
+    "link":   "https://github.com/lutzroeder/netron/issues/884"
+  },
+  {
+    "type":   "onnx",
+    "target": "text_function.txt",
+    "source": "https://github.com/lutzroeder/netron/files/8189629/text_function.txt.zip[text_function.txt]",
+    "format": "ONNX Text v8",
+    "link":   "https://github.com/lutzroeder/netron/issues/884"
+  },
+  {
+    "type":   "onnx",
+    "target": "text_example.txt",
+    "source": "https://github.com/lutzroeder/netron/files/8189628/text_example.txt.zip[text_example.txt]",
+    "format": "ONNX Text v7",
+    "link":   "https://github.com/lutzroeder/netron/issues/884"
+  },
+
   {
     "type":   "onnx",
     "target": "yolov3.onnx",