| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644 |
- /* jshint esversion: 6 */
- var onnx = onnx || {};
- var protobuf = protobuf || require('./protobuf');
- var flatbuffers = flatbuffers || require('./flatbuffers');
- onnx.ModelFactory = class {
- match(context) {
- return this._format(context).length > 0;
- }
- open(context) {
- const open = (model, format) => {
- return onnx.Metadata.open(context).then((metadata) => {
- return new onnx.Model(metadata, model, format);
- });
- };
- switch (this._format(context)) {
- case 'onnx.pbtxt.ModelProto':
- return context.require('./onnx-proto').then(() => {
- try {
- onnx.proto = protobuf.get('onnx').onnx;
- const stream = context.stream;
- const reader = protobuf.TextReader.open(stream);
- const model = onnx.proto.ModelProto.decodeText(reader);
- const format = 'ONNX' + (model.ir_version ? ' v' + model.ir_version.toString() : '');
- return open(model, format);
- }
- catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error('File text format is not onnx.ModelProto (' + message.replace(/\.$/, '') + ').');
- }
- });
- case 'onnx.pb.TensorProto':
- return context.require('./onnx-proto').then(() => {
- // TensorProto
- // input_0.pb, output_0.pb
- try {
- onnx.proto = protobuf.get('onnx').onnx;
- const stream = context.stream;
- const reader = protobuf.BinaryReader.open(stream);
- const tensor = onnx.proto.TensorProto.decode(reader);
- tensor.name = tensor.name || context.identifier;
- const model = new onnx.proto.ModelProto();
- model.graph = new onnx.proto.GraphProto();
- model.graph.initializer = [ tensor ];
- model.graph.value_info = [ new onnx.proto.ValueInfoProto() ];
- model.graph.value_info[0].name = tensor.name;
- model.graph.node = [ new onnx.proto.NodeProto() ];
- model.graph.node[0].op_type = 'Constant';
- model.graph.node[0].attribute = [ new onnx.proto.AttributeProto() ];
- model.graph.node[0].attribute[0].name = 'value';
- model.graph.node[0].attribute[0].t = tensor;
- const format = 'ONNX Tensor';
- return open(model, format);
- }
- catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error('File format is not onnx.TensorProto (' + message.replace(/\.$/, '') + ').');
- }
- });
- case 'onnx.pb.GraphProto':
- return context.require('./onnx-proto').then(() => {
- // GraphProto
- try {
- onnx.proto = protobuf.get('onnx').onnx;
- const stream = context.stream;
- const reader = protobuf.BinaryReader.open(stream);
- const model = new onnx.proto.ModelProto();
- model.graph = onnx.proto.GraphProto.decode(reader);
- const format = 'ONNX';
- return open(model, format);
- }
- catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error('File format is not onnx.GraphProto (' + message.replace(/\.$/, '') + ').');
- }
- });
- case 'onnx.pb.ModelProto':
- return context.require('./onnx-proto').then(() => {
- // ModelProto
- try {
- onnx.proto = protobuf.get('onnx').onnx;
- const stream = context.stream;
- const reader = protobuf.BinaryReader.open(stream);
- const model = onnx.proto.ModelProto.decode(reader);
- const format = 'ONNX' + (model.ir_version ? ' v' + model.ir_version.toString() : '');
- return open(model, format);
- }
- catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error('File format is not onnx.ModelProto (' + message.replace(/\.$/, '') + ').');
- }
- });
- case 'onnx.flatbuffers': {
- return context.require('./ort-schema').then((/* schema */) => {
- try {
- onnx.schema = flatbuffers.get('ort').onnxruntime.experimental.fbs;
- const stream = context.stream;
- const reader = flatbuffers.BinaryReader.open(stream);
- const session = onnx.schema.InferenceSession.create(reader);
- const model = session.model;
- const graph = model.graph;
- graph.node = graph.nodes;
- graph.doc_string = model.graph_doc_string;
- graph.value_info = graph.node_args;
- graph.input = graph.inputs.map((input) => {
- return { name: input };
- });
- graph.output = graph.outputs.map((output) => {
- return { name: output };
- });
- graph.initializer = graph.initializers.map((tensor) => {
- tensor.data_location = onnx.DataLocation.DEFAULT;
- return tensor;
- });
- graph.sparse_initializer = graph.sparse_initializers.map((tensor) => {
- tensor.values.data_location = onnx.DataLocation.DEFAULT;
- tensor.indices.data_location = onnx.DataLocation.DEFAULT;
- return tensor;
- });
- delete graph.nodes;
- delete graph.node_args;
- delete graph.inputs;
- delete graph.outputs;
- delete graph.initializers;
- delete graph.sparse_initializers;
- delete model.graph_doc_string;
- for (const node of graph.node) {
- node.input = node.inputs;
- node.output = node.outputs;
- node.attribute = node.attributes;
- delete node.inputs;
- delete node.outputs;
- delete node.attributes;
- }
- const format = 'ONNX Runtime' + (model.ir_version ? ' v' + model.ir_version.toString() : '');
- return open(model, format);
- }
- catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error('File format is not ort.Model (' + message.replace(/\.$/, '') + ').');
- }
- });
- }
- default: {
- throw new onnx.Error("Unknown ONNX format '" + this._format(context) + "'.");
- }
- }
- }
- _format(context) {
- const identifier = context.identifier;
- const extension = identifier.split('.').pop().toLowerCase();
- if (identifier.endsWith('saved_model.pb') || identifier.endsWith('predict_net.pb') || identifier.endsWith('init_net.pb')) {
- return '';
- }
- if (identifier.endsWith('predict_net.pbtxt') || identifier.endsWith('predict_net.prototxt') ||
- identifier.endsWith('init_net.pbtxt') || identifier.endsWith('init_net.prototxt')) {
- return '';
- }
- let tags = context.tags('pb');
- if (tags.size > 0) {
- if (tags.size === 1 && tags.get(1) === 2) {
- const tags = context.tags('pb+');
- const match = (tags, schema) => {
- for (const pair of schema) {
- const key = pair[0];
- const inner = pair[1];
- if (!tags.has(key)) {
- continue;
- }
- else if (inner === false) {
- return false;
- }
- if (Array.isArray(inner)) {
- const value = tags.get(key);
- if (!(value instanceof Map) || !match(value, inner)) {
- return false;
- }
- }
- else if (inner !== tags.get(key)) {
- return false;
- }
- }
- return true;
- };
- // mediapipe.BoxDetectorIndex
- if (match(tags, [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] )) {
- return '';
- }
- // third_party.tensorflow.python.keras.protobuf.SavedMetadata
- if (match(tags, [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]])) {
- return '';
- }
- }
- if (Array.from(tags.keys()).every((tag) => tag <= 100) &&
- Array.from(tags.values()).every((type) => type < 5)) {
- // TensorProto
- if (tags.get(1) === 0 && tags.get(2) === 0 && tags.get(9) === 2) {
- const schema = [[1,0],[2,0],[4,2],[5,2],[7,2],[8,2],[9,2]];
- if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) {
- return 'onnx.pb.TensorProto';
- }
- }
- // GraphProto
- if (tags.get(1) === 2) {
- const schema = [[1,2],[2,2],[3,2],[4,2],[5,2],[6,0],[7,0],[8,2],[9,2],[10,2],[11,2],[12,2],[13,2],[14,2]];
- if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) {
- const decode = (buffer, value) => {
- const reader = protobuf.BinaryReader.open(buffer);
- const length = reader.length;
- while (reader.position < length) {
- const tag = reader.uint32();
- const number = tag >>> 3;
- const type = tag & 7;
- if (value === number) {
- return type === 2 ? reader.bytes() : null;
- }
- else {
- reader.skipType(type);
- }
- }
- return null;
- };
- const stream = context.stream;
- const buffer = stream.peek();
- const nodeBuffer = decode(buffer, 1);
- if (nodeBuffer) {
- const nameBuffer = decode(nodeBuffer, 4);
- if (nameBuffer && nameBuffer.every((c) => c > 0x20 && c < 0x7f)) {
- return 'onnx.pb.GraphProto';
- }
- }
- }
- }
- // ModelProto
- if (tags.get(7) === 2) {
- const schema = [[1,0],[2,2],[3,2],[4,2][5,0],[6,2],[7,2],[8,2],[14,2],[20,2]];
- if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) {
- return 'onnx.pb.ModelProto';
- }
- }
- }
- }
- const stream = context.stream;
- if (stream.length > 5) {
- const buffer = stream.peek(Math.min(stream.length, 32));
- if (buffer[0] === 0x08 && buffer[1] < 0x0A && buffer[2] === 0x12) {
- const producers = [
- 'backend-test', 'BrainwaveCompiler',
- 'CNTK',
- 'keras2onnx', 'Kneron', 'kneron_formatter', 'kneron_kl530_test_case',
- 'darknet to ONNX example',
- 'htshinichi',
- 'MATLAB Deep Learning Toolbox Converter for ONNX Model Format', 'ML.NET', 'MVTec Software',
- 'onnx-caffe2', 'onnx-example', 'onnx.quantize', 'onnx.utils.extract_model', 'OnnxMLTools', 'onnx_test', 'onnxruntime-tools', 'onnxruntime.transformers',
- 'PaddlePaddle', 'pytorch',
- 'skl2onnx',
- 'Tencent YouTu', 'tf2onnx', 'tflite2onnx',
- 'WinMLTools'
- ];
- if (producers.some((producer) => Array.from(producer).every((ch, index) => index + 4 < buffer.length && ch.charCodeAt(0) === buffer[index + 4]))) {
- return 'onnx.pb.ModelProto';
- }
- }
- }
- tags = context.tags('pbtxt');
- if (tags.has('ir_version')) {
- return 'onnx.pbtxt.ModelProto';
- }
- if (tags.has('graph') && extension !== 'model') {
- return 'onnx.pbtxt.ModelProto';
- }
- if (context.tags('flatbuffers').get('file_identifier') === 'ORTM') {
- return 'onnx.flatbuffers';
- }
- return '';
- }
- };
- onnx.Model = class {
- constructor(metadata, model, format) {
- this._graphs = [];
- this._format = format;
- this._producerName = model.producer_name;
- this._producerVersion = model.producer_version;
- this._domain = model.domain;
- this._modelVersion = model.model_version;
- this._description = model.doc_string;
- this._metadata = [];
- this._imports = null;
- const imports = new Map();
- if (model.opset_import && model.opset_import.length > 0) {
- for (const opset_import of model.opset_import) {
- const domain = opset_import.domain || 'ai.onnx';
- const version = opset_import.version ? opset_import.version.toNumber() : 0;
- if (!imports.has(domain) || imports.get(domain) > version) {
- imports.set(domain, version);
- }
- }
- this._imports = Array.from(imports).map((pair) => pair[0] + ' v' + pair[1].toString());
- }
- if (imports.size == 0) {
- imports.set('ai.onnx', 1);
- imports.set('ai.onnx.ml', 1);
- }
- let imageFormat = '';
- if (model.metadata_props) {
- const imageMetadata = {};
- for (const metadata_prop of model.metadata_props) {
- switch (metadata_prop.key) {
- case 'author':
- this._author = metadata_prop.value;
- break;
- case 'company':
- this._company = metadata_prop.value;
- break;
- case 'converted_from':
- this._converted_from = metadata_prop.value;
- break;
- case 'license':
- this._license = metadata_prop.value;
- break;
- case 'license_url':
- this._licenseUrl = metadata_prop.value;
- break;
- case 'Image.BitmapPixelFormat':
- case 'Image.ColorSpaceGamma':
- case 'Image.NominalPixelRange':
- imageMetadata[metadata_prop.key] = metadata_prop.value;
- break;
- default:
- this._metadata.push({ name: metadata_prop.key, value: metadata_prop.value});
- break;
- }
- }
- imageFormat = [ imageMetadata['Image.BitmapPixelFormat'], imageMetadata['Image.ColorSpaceGamma'], imageMetadata['Image.NominalPixelRange'] ].filter((item) => item);
- }
- this._graphs = [];
- if (model && model.graph) {
- let key = 1000;
- const context = {};
- context.metadata = new onnx.GraphMetadata(metadata, imports);
- context.imageFormat = imageFormat;
- for (const func of model.functions || []) {
- context.metadata.add(new onnx.Function(context, func));
- }
- context.graphs = new Map();
- context.graph = function(graph) {
- graph.key = graph.key || (key++).toString();
- if (!this.graphs.has(graph.key)) {
- this.graphs.set(graph.key, new onnx.Graph(this, graph));
- }
- return this.graphs.get(graph.key);
- };
- const graphs = [ model.graph ];
- while (graphs.length > 0) {
- const graph = graphs.shift();
- this._graphs.push(context.graph(graph));
- for (const node of graph.node || []) {
- for (const attribute of node.attribute || []) {
- if (attribute.g) {
- graphs.push(attribute.g);
- }
- else if (attribute.graphs && attribute.graphs.length > 0) {
- graphs.push(...attribute.graphs);
- }
- }
- }
- }
- }
- }
- get format() {
- return this._format;
- }
- get imports() {
- return this._imports;
- }
- get producer() {
- const producer = [];
- if (this._producerName) {
- producer.push(this._producerName);
- }
- if (this._producerVersion && this._producerVersion.length > 0) {
- producer.push(this._producerVersion);
- }
- if (producer.length > 0) {
- return producer.join(' ');
- }
- return null;
- }
- get domain() {
- return this._domain || null;
- }
- get description() {
- return this._description || null;
- }
- get author() {
- return this._author || null;
- }
- get company() {
- return this._company || null;
- }
- get source() {
- return this._converted_from || null;
- }
- get license() {
- const license = [];
- if (this._license && this._license.length > 0) {
- license.push(this._license);
- }
- if (this._licenseUrl && this._licenseUrl.length > 0) {
- license.push('<a href=\'' + this._licenseUrl + '\'>' + this._licenseUrl + '</a>');
- }
- if (license.length > 0) {
- return license;
- }
- return null;
- }
- get metadata() {
- return this._metadata;
- }
- get graphs() {
- return this._graphs;
- }
- };
- onnx.Graph = class {
- constructor(context, graph) {
- this._node = '';
- this._description = '';
- this._nodes = [];
- this._inputs = [];
- this._outputs = [];
- this._name = graph.name || null;
- this._description = graph.doc_string || '';
- const tensors = onnx.Utility.createTensors(graph.node);
- for (const initializer of graph.initializer) {
- const tensor = tensors.map(initializer.name);
- tensor.initializer = new onnx.Tensor(initializer, 'Initializer');
- }
- for (const sparse_initializer of graph.sparse_initializer) {
- const tensor = tensors.map(sparse_initializer.values.name);
- tensor.initializer = new onnx.Tensor(sparse_initializer, 'Sparse Initializer');
- }
- for (const tensor_annotation of graph.quantization_annotation || []) {
- const tensor = tensors.map(tensor_annotation.tensor_name);
- const annotation = {};
- for (const pair of tensor_annotation.quant_parameter_tensor_names) {
- annotation[pair.key] = pair.value;
- }
- tensor.annotation = annotation;
- }
- for (const valueInfo of graph.value_info) {
- const tensor = tensors.map(valueInfo.name);
- tensor.type = onnx.Utility.formatType(valueInfo.type, context.imageFormat);
- tensor.description = valueInfo.doc_string;
- }
- graph.input = graph.input.map((valueInfo) => {
- const tensor = tensors.map(valueInfo.name);
- tensor.type = onnx.Utility.formatType(valueInfo.type, context.imageFormat);
- tensor.description = valueInfo.doc_string;
- return tensor;
- });
- graph.output = graph.output.map((valueInfo) => {
- const tensor = tensors.map(valueInfo.name);
- tensor.type = onnx.Utility.formatType(valueInfo.type, context.imageFormat);
- tensor.description = valueInfo.doc_string;
- return tensor;
- });
- new onnx.Inference(graph.node, graph.output);
- const args = new Map();
- args.map = function(name) {
- if (!this.has(name)) {
- const tensor = tensors.map(name);
- const type = tensor.initializer ? tensor.initializer.type : tensor.type || null;
- this.set(name, new onnx.Argument(name, type, tensor.initializer, tensor.annotation, tensor.description));
- }
- return this.get(name);
- };
- this._nodes = onnx.Utility.createNodes(context, graph.node, graph.input, graph.output, tensors, args);
- for (const input of graph.input) {
- const argument = args.map(input.name);
- if (!argument.initializer) {
- this._inputs.push(new onnx.Parameter(input.name, [ argument ]));
- }
- }
- for (const output of graph.output) {
- const argument = args.map(output.name);
- if (!argument.initializer) {
- this._outputs.push(new onnx.Parameter(output.name, [ argument ]));
- }
- }
- }
- get name() {
- return this._name;
- }
- get description() {
- return this._description;
- }
- get groups() {
- return false;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get nodes() {
- return this._nodes;
- }
- toString() {
- return 'graph(' + this.name + ')';
- }
- };
- onnx.Parameter = class {
- constructor(name, args) {
- this._name = name;
- this._arguments = args;
- }
- get name() {
- return this._name;
- }
- get visible() {
- return true;
- }
- get arguments() {
- return this._arguments;
- }
- };
- onnx.Argument = class {
- constructor(name, type, initializer, annotation, description) {
- if (typeof name !== 'string') {
- throw new onnx.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
- }
- this._name = name;
- this._type = type || null;
- this._initializer = initializer || null;
- this._annotation = annotation;
- this._description = description || '';
- }
- get name() {
- return this._name;
- }
- get type() {
- return this._type;
- }
- get description() {
- return this._description;
- }
- get quantization() {
- if (this._annotation) {
- return Object.keys(this._annotation).map((key) => key + ': ' + this._annotation[key]).join(', ');
- }
- return null;
- }
- get initializer() {
- return this._initializer;
- }
- };
- onnx.Node = class {
- constructor(context, op_type, domain, name, description, attributes, inputs, outputs) {
- this._type = context.metadata.type(op_type, domain) || { name: op_type, module: domain };
- if (this.type.module !== domain && !(this._type instanceof onnx.Function)) {
- this._type = Object.assign({}, this.type);
- this._type.name = op_type;
- this._type.module = domain;
- }
- this._name = name || '';
- this._description = description || '';
- this._inputs = inputs;
- this._outputs = outputs;
- this._attributes = (attributes || []).map((attribute) => new onnx.Attribute(context, op_type, domain, attribute));
- }
- get type() {
- return this._type;
- }
- get name() {
- return this._name;
- }
- get description() {
- return this._description;
- }
- get group() {
- return null;
- }
- get attributes() {
- return this._attributes;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- };
- onnx.Attribute = class {
- constructor(context, op_type, domain, attribute) {
- this._name = attribute.name;
- this._description = attribute.doc_string || '';
- this._type = null;
- this._value = null;
- switch (attribute.type) {
- case onnx.AttributeType.FLOAT:
- this._value = attribute.f;
- this._type = 'float32';
- break;
- case onnx.AttributeType.INT:
- this._value = attribute.i;
- this._type = 'int64';
- break;
- case onnx.AttributeType.STRING:
- switch (op_type) {
- case 'Int8GivenTensorFill':
- this._value = Array.from(attribute.s);
- break;
- default:
- this._value = onnx.Utility.decodeText(attribute.s);
- break;
- }
- this._type = 'string';
- break;
- case onnx.AttributeType.TENSOR:
- this._value = new onnx.Tensor(attribute.t);
- this._type = 'tensor';
- break;
- case onnx.AttributeType.GRAPH:
- this._value = context.graph(attribute.g);
- this._type = 'graph';
- break;
- case onnx.AttributeType.FLOATS:
- this._value = attribute.floats;
- this._type = 'float32[]';
- break;
- case onnx.AttributeType.INTS:
- this._value = attribute.ints;
- this._type = 'int64[]';
- break;
- case onnx.AttributeType.STRINGS:
- this._value = attribute.strings.map((s) => onnx.Utility.decodeText(s));
- this._type = 'string[]';
- break;
- case onnx.AttributeType.TENSORS:
- this._value = attribute.tensors.map((tensor) => new onnx.Tensor(tensor));
- this._type = 'tensor[]';
- break;
- case onnx.AttributeType.GRAPHS:
- this._value = attribute.graphs.map((graph) => context.graph(graph));
- this._type = 'graph[]';
- break;
- case onnx.AttributeType.SPARSE_TENSOR:
- this._value = new onnx.Tensor(attribute.sparse_tensor);
- this._type = 'tensor';
- break;
- case onnx.AttributeType.SPARSE_TENSORS:
- this._value = attribute.sparse_tensors.map((tensor) => new onnx.Tensor(tensor));
- this._type = 'tensor[]';
- break;
- case onnx.AttributeType.TYPE_PROTO:
- this._value = onnx.Utility.formatType(attribute.tp, context.imageFormat);
- this._type = 'type';
- break;
- case onnx.AttributeType.TYPE_PROTOS:
- this._value = attribute.type_protos.map((type) => onnx.Utility.formatType(type, context.imageFormat));
- this._type = 'type[]';
- break;
- default:
- throw new onnx.Error("Unknown attribute type '" + attribute.type + "'.");
- }
- const metadata = context.metadata.attribute(op_type, domain, attribute.name);
- if (metadata && Object.prototype.hasOwnProperty.call(metadata, 'default') && this._value == metadata.default) {
- this._visible = false;
- }
- }
- get name() {
- return this._name;
- }
- get type() {
- return this._type;
- }
- get value() {
- return this._value;
- }
- get description() {
- return this._description;
- }
- get visible() {
- return this._visible == false ? false : true;
- }
- };
- onnx.Tensor = class {
- constructor(tensor, kind) {
- this._kind = kind || null;
- const data = (tensor) => {
- let data = undefined;
- if (tensor.data_location === onnx.DataLocation.DEFAULT) {
- switch (tensor.data_type) {
- case onnx.DataType.FLOAT16:
- if (tensor.int32_data && tensor.int32_data.length > 0) {
- const buffer = new Uint8Array(tensor.int32_data.length << 1);
- const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
- const array = tensor.int32_data;
- for (let i = 0; i < array.length; i++) {
- view.setUint16(i << 1, array[i], true);
- }
- data = {
- type: tensor.data_type,
- buffer: buffer
- };
- }
- break;
- case onnx.DataType.FLOAT:
- data = new Float32Array(tensor.float_data);
- break;
- case onnx.DataType.DOUBLE:
- data = new Float64Array(tensor.double_data);
- break;
- case onnx.DataType.BOOL:
- data = new Array(tensor.int32_data.size);
- for (let i = 0; i < data.length; i++) {
- data[i] = data[i] === 0 ? false : true;
- }
- break;
- case onnx.DataType.INT8:
- data = new Int8Array(tensor.int32_data);
- break;
- case onnx.DataType.UINT8:
- data = new Uint8Array(tensor.int32_data);
- break;
- case onnx.DataType.INT16:
- data = new Int32Array(tensor.int32_data);
- break;
- case onnx.DataType.UINT16:
- data = new Int32Array(tensor.int32_data);
- break;
- case onnx.DataType.INT32:
- data = new Int32Array(tensor.int32_data);
- break;
- case onnx.DataType.UINT32:
- case onnx.DataType.UINT64:
- data = tensor.uint64_data;
- break;
- case onnx.DataType.INT64:
- data = tensor.int64_data;
- break;
- }
- if (data && (Array.isArray(data) || ArrayBuffer.isView(data)) && data.length === 0) {
- data = undefined;
- }
- if (!data && tensor.raw_data && tensor.raw_data.length > 0) {
- data = {
- type: tensor.data_type,
- buffer: tensor.raw_data
- };
- }
- }
- return data;
- };
- const location = (tensor) => {
- return onnx.Utility.formatLocation(tensor.data_location);
- };
- if ((onnx.proto && tensor instanceof onnx.proto.SparseTensorProto) ||
- (onnx.schema && tensor instanceof onnx.schema.SparseTensor)) {
- this._name = tensor.values.name || '';
- this._type = new onnx.TensorType(tensor.values.data_type, new onnx.TensorShape(tensor.dims.map((dim) => dim)), null);
- this._location = Array.from(new Set([ location(tensor.values), location(tensor.indices) ])).join(':');
- this._values = data(tensor.values);
- this._indices = data(tensor.indices);
- }
- else {
- this._name = tensor.name || '';
- this._type = new onnx.TensorType(tensor.data_type, new onnx.TensorShape(tensor.dims.map((dim) => dim)), null);
- this._location = location(tensor);
- this._values = data(tensor);
- }
- }
- get name() {
- return this._name;
- }
- get kind() {
- return this._kind;
- }
- get type() {
- return this._type;
- }
- get state() {
- return this._context().state || null;
- }
- get value() {
- const context = this._context();
- if (context.state) {
- return null;
- }
- context.limit = Number.MAX_SAFE_INTEGER;
- return this._decode(context, 0);
- }
- toString() {
- const context = this._context();
- if (context.state) {
- return '';
- }
- context.limit = 10000;
- const value = this._decode(context, 0);
- return onnx.Tensor._stringify(value, '', ' ');
- }
- _context() {
- const context = {};
- context.state = null;
- if (this._sparse) {
- context.state = 'Sparse data not implemented.';
- return context;
- }
- if (this._location !== 'default') {
- context.state = "Data '" + this._location + "' location not implemented.";
- return context;
- }
- const decode = (data) => {
- if (!data || Array.isArray(data) || ArrayBuffer.isView(data)) {
- return data;
- }
- const buffer = data.buffer;
- const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
- const type = data.type;
- data = undefined;
- switch (type) {
- case onnx.DataType.BOOL:
- data = new Array(buffer.length);
- for (let i = 0; i < buffer.length; i++) {
- data[i] = view.getUint8(i) === 0 ? false : true;
- }
- break;
- case onnx.DataType.FLOAT16:
- data = new Float32Array(buffer.length >> 1);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getFloat16(i << 1, true);
- }
- break;
- case onnx.DataType.FLOAT:
- data = new Float32Array(buffer.length >> 2);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getFloat32(i << 2, true);
- }
- break;
- case onnx.DataType.DOUBLE:
- data = new Float64Array(buffer.length >> 3);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getFloat64(i << 3, true);
- }
- break;
- case onnx.DataType.INT8:
- data = new Int8Array(buffer.length);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getInt8(i, true);
- }
- break;
- case onnx.DataType.UINT8:
- data = new Uint8Array(buffer.length);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getUint8(i, true);
- }
- break;
- case onnx.DataType.INT16:
- data = new Int16Array(buffer.length >> 1);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getInt16(i << 1, true);
- }
- break;
- case onnx.DataType.UINT16:
- data = new Uint16Array(buffer.length >> 1);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getUint16(i << 1, true);
- }
- break;
- case onnx.DataType.INT32:
- data = new Int32Array(buffer.length >> 2);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getInt32(i << 2, true);
- }
- break;
- case onnx.DataType.UINT32:
- data = new Uint32Array(buffer.length >> 2);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getUint32(i << 2, true);
- }
- break;
- case onnx.DataType.INT64:
- data = new Array(buffer.length >> 3);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getInt64(i << 3, true);
- }
- break;
- case onnx.DataType.UINT64:
- data = new Array(buffer.length >> 3);
- for (let i = 0; i < data.length; i++) {
- data[i] = view.getUint64(i << 3, true);
- }
- break;
- }
- return data;
- };
- this._values = decode(this._values);
- if (!this._values) {
- context.state = 'Tensor data is empty.';
- return context;
- }
- this._indices = decode(this._indices);
- context.values = this._values;
- context.indices = this._indices;
- context.index = 0;
- context.dataType = this.type.dataType;
- context.shape = this.type.shape.dimensions;
- context.data = function() {
- if (!this._data) {
- if (this.indices && this.values && this.indices.length === this.values.length) {
- const size = context.shape.reduce((a, b) => a * b, 1);
- const indices = this.indices;
- const values = this.values;
- const array = new values.constructor(size);
- switch (this.dataType) {
- case 'boolean':
- array.fill(false);
- break;
- case 'int64':
- case 'uint64':
- break;
- }
- if (indices.length > 0) {
- if (Object.prototype.hasOwnProperty.call(indices[0], 'low')) {
- for (let i = 0; i < indices.length; i++) {
- const index = indices[i];
- array[index.high === 0 ? index.low : index.toNumber()] = values[i];
- }
- }
- else {
- for (let i = 0; i < indices.length; i++) {
- array[indices[i]] = values[i];
- }
- }
- }
- this._data = array;
- }
- else {
- this._data = this.values;
- }
- }
- return this._data;
- };
- return context;
- }
- _decode(context, dimension) {
- const shape = context.shape.length !== 0 ? context.shape : [ 1 ];
- const results = [];
- const size = shape[dimension];
- const data = context.data();
- if (dimension == shape.length - 1) {
- for (let i = 0; i < size; i++) {
- if (context.index > context.limit) {
- results.push('...');
- return results;
- }
- results.push(data[context.index++]);
- }
- }
- else {
- for (let j = 0; j < size; j++) {
- if (context.index > context.limit) {
- results.push('...');
- return results;
- }
- results.push(this._decode(context, dimension + 1));
- }
- }
- if (context.shape.length == 0) {
- return results[0];
- }
- return results;
- }
- static _stringify(value, indentation, indent) {
- if (Array.isArray(value)) {
- const result = [];
- result.push(indentation + '[');
- const items = value.map((item) => onnx.Tensor._stringify(item, indentation + indent, indent));
- if (items.length > 0) {
- result.push(items.join(',\n'));
- }
- result.push(indentation + ']');
- return result.join('\n');
- }
- if (typeof value == 'string') {
- return indentation + value;
- }
- if (value == Infinity) {
- return indentation + 'Infinity';
- }
- if (value == -Infinity) {
- return indentation + '-Infinity';
- }
- if (isNaN(value)) {
- return indentation + 'NaN';
- }
- return indentation + value.toString();
- }
- };
- onnx.TensorType = class {
- constructor(dataType, shape, denotation) {
- this._dataType = onnx.Utility.formatElementType(dataType);
- this._shape = shape;
- this._denotation = denotation || null;
- }
- get dataType() {
- return this._dataType;
- }
- get shape() {
- return this._shape;
- }
- get denotation() {
- return this._denotation;
- }
- toString() {
- return this.dataType + this._shape.toString();
- }
- };
- onnx.TensorShape = class {
- constructor(dimensions) {
- this._dimensions = dimensions;
- }
- get dimensions() {
- return this._dimensions;
- }
- toString() {
- if (!this._dimensions || this._dimensions.length == 0) {
- return '';
- }
- return '[' + this._dimensions.join(',') + ']';
- }
- };
- onnx.SequenceType = class {
- constructor(elementType, denotation) {
- this._elementType = elementType;
- this._denotation = denotation;
- }
- get elementType() {
- return this._elementType;
- }
- get dennotation() {
- return this._dennotation;
- }
- toString() {
- return 'sequence<' + this._elementType.toString() + '>';
- }
- };
- onnx.MapType = class {
- constructor(keyType, valueType, denotation) {
- this._keyType = onnx.Utility.formatElementType(keyType);
- this._valueType = valueType;
- this._denotation = denotation;
- }
- get keyType() {
- return this._keyType;
- }
- get valueType() {
- return this._valueType;
- }
- get denotation() {
- return this._denotation;
- }
- toString() {
- return 'map<' + this._keyType + ',' + this._valueType.toString() + '>';
- }
- };
- onnx.OpaqueType = class {
- constructor(domain, name) {
- this._domain = domain;
- this._name = name;
- }
- toString() {
- const name = (this._domain ? (this._domain + '.') : '') + this._name;
- return 'opaque<' + name + '>';
- }
- };
- onnx.Function = class {
- constructor(context, func) {
- this._name = func.name;
- this._domain = func.domain;
- this._description = func.doc_string;
- this._inputs = [];
- this._outputs = [];
- this._attributes = func.attribute.map((attribtue) => { return { name: attribtue }; });
- const tensors = onnx.Utility.createTensors(func.node);
- func.input = func.input.map((input) => tensors.map(input));
- func.output = func.output.map((output) => tensors.map(output));
- const args = new Map();
- args.map = function(name) {
- if (!this.has(name)) {
- const tensor = tensors.map(name);
- const type = tensor.initializer ? tensor.initializer.type : tensor.type || null;
- this.set(name, new onnx.Argument(name, type, tensor.initializer, tensor.annotation, tensor.description));
- }
- return this.get(name);
- };
- this._nodes = onnx.Utility.createNodes(context, func.node, func.input, func.output, tensors, args);
- for (const input of func.input) {
- const argument = args.map(input.name);
- if (!argument.initializer) {
- this._inputs.push(new onnx.Parameter(input.name, [ argument ]));
- }
- }
- for (const output of func.output) {
- const argument = args.map(output.name);
- if (!argument.initializer) {
- this._outputs.push(new onnx.Parameter(output.name, [ argument ]));
- }
- }
- }
- get type() {
- return 'function';
- }
- get name() {
- return this._name;
- }
- get module() {
- return this._domain;
- }
- get description() {
- return this._description;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get attributes() {
- return this._attributes;
- }
- get nodes() {
- return this._nodes;
- }
- };
- onnx.GraphMetadata = class {
- constructor(metadata, imports) {
- this._metadata = metadata;
- this._imports = imports;
- this._cache = new Map();
- this._attributeCache = new Map();
- this._functions = new Map();
- }
- add(func) {
- if (!this._functions.has(func.module)) {
- this._functions.set(func.module, new Map());
- }
- const map = this._functions.get(func.module);
- if (map.has(func.name)) {
- throw new onnx.Error("Duplicate function identifier '" + func.module + '.' + func.name + "'.");
- }
- map.set(func.name, func);
- }
- type(name, domain) {
- domain = domain || 'ai.onnx';
- const key = domain + ':' + name;
- if (!this._cache.has(key)) {
- let value = this._metadata.type(name, domain, this._imports);
- if (!value) {
- if (this._functions.has(domain)) {
- const map = this._functions.get(domain);
- if (map.has(name)) {
- value = map.get(name);
- }
- }
- }
- this._cache.set(key, value);
- }
- return this._cache.get(key);
- }
- attribute(type, domain, name) {
- const key = domain + ':' + type + ':' + name;
- if (!this._attributeCache.has(key)) {
- const schema = this.type(type, domain);
- if (schema && schema.attributes && schema.attributes.length > 0) {
- for (const attribute of schema.attributes) {
- this._attributeCache.set(type + ':' + attribute.name, attribute);
- }
- }
- if (!this._attributeCache.has(key)) {
- this._attributeCache.set(key, null);
- }
- }
- return this._attributeCache.get(key);
- }
- };
- onnx.Metadata = class {
- static open(context) {
- if (onnx.Metadata._metadata) {
- return Promise.resolve(onnx.Metadata._metadata);
- }
- return context.request('onnx-metadata.json', 'utf-8', null).then((data) => {
- onnx.Metadata._metadata = new onnx.Metadata(data);
- return onnx.Metadata._metadata;
- }).catch(() => {
- onnx.Metadata._metadata = new onnx.Metadata(null);
- return onnx.Metadata._metadata;
- });
- }
- constructor(data) {
- this._map = new Map();
- if (data) {
- const metadata = JSON.parse(data);
- for (const item of metadata) {
- if (!this._map.has(item.module)) {
- this._map.set(item.module, new Map());
- }
- const map = this._map.get(item.module);
- if (!map.has(item.name)) {
- map.set(item.name, []);
- }
- map.get(item.name).push(item);
- }
- }
- }
- type(name, domain, imports) {
- domain = domain || 'ai.onnx';
- let current = null;
- if (this._map.has(domain)) {
- const map = this._map.get(domain);
- if (map.has(name)) {
- for (const metadata of map.get(name)) {
- const matchVersion = current ? current.version : -1;
- const importVersion = imports.get(metadata.module) || 0;
- if (importVersion >= metadata.version && matchVersion < metadata.version) {
- current = metadata;
- }
- }
- }
- }
- return current;
- }
- };
- onnx.Inference = class {
- constructor(nodes, outputs) {
- this._outputs = new Map();
- for (const node of nodes) {
- for (const output of node.output) {
- this._outputs.set(output.name, node);
- }
- }
- for (const output of outputs) {
- this._infer(output.name);
- }
- }
- _infer(output) {
- if (this._outputs.has(output)) {
- let hasInputShapes = true;
- const node = this._outputs.get(output);
- for (const input of node.input) {
- if (!input.type) {
- this._infer(input);
- if (!input.type) {
- hasInputShapes = false;
- break;
- }
- }
- }
- if (hasInputShapes) {
- // continue
- }
- }
- }
- };
- onnx.DataLocation = {
- DEFAULT: 0,
- EXTERNAL: 1
- };
- onnx.DataType = {
- UNDEFINED: 0,
- FLOAT: 1,
- UINT8: 2,
- INT8: 3,
- UINT16: 4,
- INT16: 5,
- INT32: 6,
- INT64: 7,
- STRING: 8,
- BOOL: 9,
- FLOAT16: 10,
- DOUBLE: 11,
- UINT32: 12,
- UINT64: 13,
- COMPLEX64: 14,
- COMPLEX128: 15,
- BFLOAT16: 16
- };
- onnx.AttributeType = {
- UNDEFINED: 0,
- FLOAT: 1,
- INT: 2,
- STRING: 3,
- TENSOR: 4,
- GRAPH: 5,
- FLOATS: 6,
- INTS: 7,
- STRINGS: 8,
- TENSORS: 9,
- GRAPHS: 10,
- SPARSE_TENSOR: 11,
- SPARSE_TENSORS: 12,
- TYPE_PROTO: 13,
- TYPE_PROTOS: 14
- };
- onnx.Utility = class {
- static decodeText(value) {
- if (typeof value === 'string') {
- return value;
- }
- onnx.Utility._utf8Decoder = onnx.Utility._utf8Decoder || new TextDecoder('utf-8');
- return onnx.Utility._utf8Decoder.decode(value);
- }
- static formatElementType(elementType) {
- if (!onnx.Utility._elementTypeMap) {
- const map = {};
- map[onnx.DataType.UNDEFINED] = 'UNDEFINED';
- map[onnx.DataType.FLOAT] = 'float32';
- map[onnx.DataType.UINT8] = 'uint8';
- map[onnx.DataType.INT8] = 'int8';
- map[onnx.DataType.UINT16] = 'uint16';
- map[onnx.DataType.INT16] = 'int16';
- map[onnx.DataType.INT32] = 'int32';
- map[onnx.DataType.INT64] = 'int64';
- map[onnx.DataType.STRING] = 'string';
- map[onnx.DataType.BOOL] = 'boolean';
- map[onnx.DataType.FLOAT16] = 'float16';
- map[onnx.DataType.DOUBLE] = 'float64';
- map[onnx.DataType.UINT32] = 'uint32';
- map[onnx.DataType.UINT64] = 'uint64';
- map[onnx.DataType.COMPLEX64] = 'complex64';
- map[onnx.DataType.COMPLEX128] = 'complex128';
- map[onnx.DataType.BFLOAT16] = 'bfloat16';
- onnx.Utility._elementTypeMap = map;
- }
- const name = onnx.Utility._elementTypeMap[elementType];
- if (name) {
- return name;
- }
- return onnx.Utility._elementTypeMap[onnx.DataType.UNDEFINED];
- }
- static formatType(type, imageFormat) {
- if (!type) {
- return null;
- }
- let denotation = '';
- switch (type.denotation) {
- case 'TENSOR':
- denotation = 'Tensor';
- break;
- case 'IMAGE':
- denotation = 'Image' + (imageFormat ? '(' + imageFormat.join(',') + ')' : '');
- break;
- case 'AUDIO':
- denotation = 'Audio';
- break;
- case 'TEXT':
- denotation = 'Text';
- break;
- }
- switch (type.value) {
- case 'tensor_type': {
- {
- const tensor_type = type.tensor_type;
- let shape = [];
- if (tensor_type.shape && tensor_type.shape.dim) {
- shape = tensor_type.shape.dim.map((dim) => dim.dim_param ? dim.dim_param : dim.dim_value);
- }
- return new onnx.TensorType(tensor_type.elem_type, new onnx.TensorShape(shape), denotation);
- }
- }
- case 'sparse_tensor_type': {
- const tensor_type = type.sparse_tensor_type;
- let shape = [];
- if (tensor_type.shape && tensor_type.shape.dim) {
- shape = tensor_type.shape.dim.map((dim) => dim.dim_param ? dim.dim_param : dim.dim_value);
- }
- return new onnx.TensorType(tensor_type.elem_type, new onnx.TensorShape(shape), denotation);
- }
- case 'map_type': {
- return new onnx.MapType(type.map_type.key_type, onnx.Utility.formatType(type.map_type.value_type, imageFormat), denotation);
- }
- case 'sequence_type': {
- return new onnx.SequenceType(onnx.Utility.formatType(type.sequence_type.elem_type, imageFormat), denotation);
- }
- case 'opaque_type': {
- return new onnx.OpaqueType(type.opaque_type.domain, type.opaque_type.name);
- }
- }
- return null;
- }
- static formatLocation(location) {
- if (!onnx.Utility._dataLocations) {
- onnx.Utility._dataLocations = new Map(Object.keys(onnx.DataLocation).map((key) => [ onnx.DataLocation[key], key.toLowerCase() ]));
- }
- return onnx.Utility._dataLocations.get(location);
- }
- static attributeType(attribute) {
- if (attribute.type) {
- return attribute.type;
- }
- if (attribute.ints && attribute.ints.length > 0) {
- return onnx.AttributeType.INTS;
- }
- else if (attribute.floats && attribute.floats.length > 0) {
- return onnx.AttributeType.FLOATS;
- }
- else if (attribute.strings && attribute.strings.length > 0) {
- return onnx.AttributeType.STRINGS;
- }
- else if (attribute.graphs && attribute.graphs.length > 0) {
- return onnx.AttributeType.GRAPHS;
- }
- else if (attribute.s && attribute.s.length > 0) {
- return onnx.AttributeType.STRING;
- }
- else if (Object.prototype.hasOwnProperty.call(attribute, 'f')) {
- return onnx.AttributeType.FLOAT;
- }
- else if (Object.prototype.hasOwnProperty.call(attribute, 'i')) {
- return onnx.AttributeType.INT;
- }
- else if (Object.prototype.hasOwnProperty.call(attribute, 't')) {
- return onnx.AttributeType.TENSOR;
- }
- else if (Object.prototype.hasOwnProperty.call(attribute, 'g')) {
- return onnx.AttributeType.GRAPH;
- }
- else if (Object.prototype.hasOwnProperty.call(attribute, 'sparse_tensor')) {
- return onnx.AttributeType.SPARSE_TENSOR;
- }
- return onnx.AttributeType.UNDEFINED;
- }
- static createTensors(nodes) {
- const tensors = new Map();
- tensors.map = function(name) {
- if (!this.has(name)) {
- this.set(name, { name: name });
- }
- return this.get(name);
- };
- for (const node of nodes) {
- node.input = node.input.map((name) => tensors.map(name));
- node.output = node.output.map((name) => tensors.map(name));
- node.param = {};
- for (const attribute of node.attribute) {
- attribute.type = onnx.Utility.attributeType(attribute);
- }
- }
- return tensors;
- }
- static createNodes(context, nodes, inputs, outputs, tensors, args) {
- const inputMap = new Map();
- const outputMap = new Map();
- for (const node of nodes) {
- node.input.every((input) => inputMap.set(input.name, (inputMap.get(input) || 0) + 1));
- node.output.every((output) => outputMap.set(output.name, (outputMap.get(output) || 0) + 1));
- }
- inputs.every((input) => inputMap.delete(input.name));
- outputs.every((output) => outputMap.delete(output.name));
- nodes = nodes.filter((node) => {
- const constant = node &&
- node.op_type === 'Constant' &&
- node.attribute.length === 1 && node.attribute[0] &&
- node.input.length === 0 &&
- node.output.length === 1 && node.output[0] && inputMap.get(node.output[0].name) === 1 && outputMap.get(node.output[0].name) === 1;
- const attribute = constant ? node.attribute[0] : null;
- if (attribute && attribute.name === 'value' && attribute.type === onnx.AttributeType.TENSOR && attribute.t) {
- const tensor = tensors.map(node.output[0].name);
- tensor.initializer = new onnx.Tensor(attribute.t, 'Constant');
- return false;
- }
- else if (attribute && attribute.name === 'sparse_value' && attribute.type === onnx.AttributeType.SPARSE_TENSOR && attribute.sparse_tensor) {
- const tensor = tensors.map(node.output[0].name);
- tensor.initializer = new onnx.Tensor(attribute.sparse_tensor, 'Sparse Constant');
- return false;
- }
- return true;
- });
- return nodes.map((node) => {
- const schema = context.metadata.type(node.op_type, node.domain);
- const inputs = [];
- node.input = node.input || [];
- for (let i = 0; i < node.input.length; ) {
- const input = schema && schema.inputs && i < schema.inputs.length ? schema.inputs[i] : { name: i.toString() };
- const count = input.list ? node.input.length - i : 1;
- const list = node.input.slice(i, i + count).map((input) => args.map(input.name));
- inputs.push(new onnx.Parameter(input.name, list));
- i += count;
- }
- const outputs = [];
- node.output = node.output || [];
- for (let i = 0; i < node.output.length; ) {
- const output = schema && schema.outputs && i < schema.outputs.length ? schema.outputs[i] : { name: i.toString() };
- const count = output.list ? node.output.length - i : 1;
- const list = node.output.slice(i, i + count).map((output) => args.map(output.name));
- outputs.push(new onnx.Parameter(output.name, list));
- i += count;
- }
- return new onnx.Node(context, node.op_type, node.domain, node.name, node.doc_string, node.attribute, inputs, outputs);
- });
- }
- };
- onnx.Error = class extends Error {
- constructor(message) {
- super(message);
- this.name = 'Error loading ONNX model.';
- }
- };
- if (typeof module !== 'undefined' && typeof module.exports === 'object') {
- module.exports.ModelFactory = onnx.ModelFactory;
- }
|