| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692 |
- var coreml = coreml || {};
- var json = json || require('./json');
- var protobuf = protobuf || require('./protobuf');
- var base = base || require('./base');
- coreml.ModelFactory = class {
- match(context) {
- const stream = context.stream;
- const identifier = context.identifier.toLowerCase();
- const extension = identifier.split('.').pop().toLowerCase();
- const tags = context.tags('pb');
- if (tags.get(1) === 0 && tags.get(2) === 2) {
- if (extension === 'pb') {
- const tags = context.tags('pb+');
- const keys = Object.keys(tags).map((key) => parseInt(key, 10));
- const match = (key) =>
- (key >= 200 && key < 220) ||
- (key >= 300 && key < 320) ||
- (key >= 400 && key < 420) ||
- (key >= 500 && key < 520) ||
- (key >= 550 && key < 560) ||
- (key >= 600 && key < 620) ||
- (key === 900) ||
- (key >= 2000 && key < 2010) ||
- (key === 3000);
- if (!keys.some((key) => match(key))) {
- return null;
- }
- }
- return 'coreml.pb';
- }
- if (identifier === 'manifest.json') {
- const obj = context.open('json');
- if (obj && obj.rootModelIdentifier && obj.itemInfoEntries) {
- const entries = Object.keys(obj.itemInfoEntries).map((key) => obj.itemInfoEntries[key]);
- if (entries.filter((entry) => entry.path.toLowerCase().endsWith('.mlmodel').length === 1)){
- return 'coreml.manifest';
- }
- }
- }
- if (identifier === 'metadata.json') {
- const obj = context.open('json');
- if (obj && obj.rootModelIdentifier && obj.itemInfoEntries) {
- return 'coreml.metadata';
- }
- }
- if (identifier === 'featuredescriptions.json') {
- const obj = context.open('json');
- if (obj && (obj.Inputs || obj.Outputs)) {
- return 'coreml.featuredescriptions';
- }
- }
- if (extension === 'bin' && stream.length > 16) {
- const buffer = stream.peek(Math.min(256, stream.length));
- for (let i = 0; i < buffer.length - 4; i++) {
- const signature = (buffer[i] | buffer[i + 1] << 8 | buffer[i + 2] << 16 | buffer [i + 3] << 24) >>> 0;
- if (signature === 0xdeadbeef) {
- return 'coreml.weights';
- }
- }
- }
- return undefined;
- }
- open(context, match) {
- return context.require('./coreml-proto').then(() => {
- return coreml.Metadata.open(context).then((metadata) => {
- const openModel = (stream, context, path, format) => {
- let model = null;
- try {
- coreml.proto = protobuf.get('coreml').CoreML.Specification;
- const reader = protobuf.BinaryReader.open(stream);
- model = coreml.proto.Model.decode(reader);
- }
- catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new coreml.Error('File format is not coreml.Model (' + message.replace(/\.$/, '') + ').');
- }
- const weightPaths = new Set();
- const walkProgram = (program) => {
- for (const entry of Object.entries(program.functions)) {
- const func = entry[1];
- for (const entry of Object.entries(func.block_specializations)) {
- const block = entry[1];
- for (const operation of block.operations) {
- for (const entry of Object.entries(operation.attributes)) {
- const value = entry[1];
- if (value.blobFileValue && value.blobFileValue.fileName) {
- weightPaths.add(value.blobFileValue.fileName);
- }
- }
- }
- }
- }
- };
- const walkModel = (model) => {
- if (model.mlProgram) {
- walkProgram(model.mlProgram);
- }
- if (model.pipeline && model.pipeline.models) {
- for (const node of model.pipeline.models) {
- walkModel(node);
- }
- }
- if (model.pipelineClassifier && model.pipelineClassifier.pipeline && model.pipelineClassifier.pipeline.models) {
- for (const node of model.pipelineClassifier.pipeline.models) {
- walkModel(node);
- }
- }
- if (model.pipelineRegressor && model.pipelineRegressor.pipeline && model.pipelineRegressor.pipeline.models) {
- for (const node of model.pipelineRegressor.pipeline.models) {
- walkModel(node);
- }
- }
- };
- walkModel(model);
- if (weightPaths.size > 0) {
- const items = path.split('/');
- items.pop();
- const folder = items.join('/');
- const keys = Array.from(weightPaths);
- const paths = keys.map((path) => {
- const items = path.split('/');
- if (items[0] === '@model_path') {
- items[0] = folder;
- }
- return items.join('/');
- });
- const promises = paths.map((path) => context.request(path, null));
- return Promise.all(promises).then((streams) => {
- const weights = new Map();
- for (let i = 0; i < keys.length; i++) {
- weights.set(keys[i], streams[i]);
- }
- return new coreml.Model(metadata, format, model, weights);
- }).catch((/* err */) => {
- return new coreml.Model(metadata, format, model, new Map());
- });
- }
- return new coreml.Model(metadata, format, model, new Map());
- };
- const openManifest = (obj, context, path) => {
- const entries = Object.keys(obj.itemInfoEntries).map((key) => obj.itemInfoEntries[key]);
- const entry = entries.filter((entry) => entry.path.toLowerCase().endsWith('.mlmodel'))[0];
- const file = path + 'Data/' + entry.path;
- return context.request(file, null).then((stream) => {
- return openModel(stream, context, file, 'Core ML Package');
- });
- };
- const openManifestStream = (context, path) => {
- return context.request(path + 'Manifest.json', null).then((stream) => {
- const reader = json.TextReader.open(stream);
- const obj = reader.read();
- return openManifest(obj, context, path);
- });
- };
- switch (match) {
- case 'coreml.pb': {
- return openModel(context.stream, context, context.identifier);
- }
- case 'coreml.manifest': {
- const obj = context.open('json');
- return openManifest(obj, context, '');
- }
- case 'coreml.featuredescriptions':
- case 'coreml.metadata': {
- return openManifestStream(context, '../../');
- }
- case 'coreml.weights': {
- return openManifestStream(context, '../../../');
- }
- default: {
- throw new coreml.Error("Unsupported Core ML format '" + match + "'.");
- }
- }
- });
- });
- }
- };
- coreml.Model = class {
- constructor(metadata, format, model, weights) {
- this._format = (format || 'Core ML') + ' v' + model.specificationVersion.toString();
- this._metadata = [];
- this._graphs = [ new coreml.Graph(metadata, model, weights) ];
- if (model.description && model.description.metadata) {
- const properties = model.description.metadata;
- if (properties.versionString) {
- this._version = properties.versionString;
- }
- if (properties.shortDescription) {
- this._description = properties.shortDescription;
- }
- if (properties.author) {
- this._metadata.push({ name: 'author', value: properties.author });
- }
- if (properties.license) {
- this._metadata.push({ name: 'license', value: properties.license });
- }
- if (metadata.userDefined && Object.keys(properties.userDefined).length > 0) {
- /* empty */
- }
- }
- }
- get format() {
- return this._format;
- }
- get version() {
- return this._version || null;
- }
- get description() {
- return this._description || null;
- }
- get metadata() {
- return this._metadata;
- }
- get graphs() {
- return this._graphs;
- }
- };
- coreml.Graph = class {
- constructor(metadata, model, weights) {
- this._metadata = metadata;
- this._description = model.description;
- this._groups = false;
- this._inputs = [];
- this._outputs = [];
- this._nodes = [];
- if (this._description) {
- this._inputs = this._description.input.map((input) => {
- const argument = new coreml.Argument(input.name, coreml.Utility.featureType(input.type), input.shortDescription, null);
- return new coreml.Parameter(input.name, true, [ argument ]);
- });
- this._outputs = this._description.output.map((output) => {
- const argument = new coreml.Argument(output.name, coreml.Utility.featureType(output.type), output.shortDescription, null);
- return new coreml.Parameter(output.name, true, [ argument ]);
- });
- }
- this._type = this._loadModel(model, {}, '', weights);
- }
- get name() {
- return '';
- }
- get type() {
- return this._type;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get nodes() {
- return this._nodes;
- }
- get groups() {
- return this._groups;
- }
- _updateOutput(name, newName) {
- for (const node of this._nodes) {
- for (const output of node.outputs) {
- for (const argument of output.arguments) {
- if (argument.name === name) {
- argument.name = newName;
- }
- }
- }
- }
- return newName;
- }
- _updateClassifierOutput(group, classifier) {
- let labelProbabilityLayerName = classifier.labelProbabilityLayerName;
- if (!labelProbabilityLayerName && this._nodes.length > 0) {
- const node = this._nodes.slice(-1).pop();
- if (node && node.outputs.length == 1 && node.outputs[0].arguments.length == 1) {
- labelProbabilityLayerName = node.outputs[0].arguments[0].name;
- }
- }
- let predictedFeatureName = this._description.predictedFeatureName;
- let predictedProbabilitiesName = this._description.predictedProbabilitiesName;
- if ((predictedFeatureName || predictedProbabilitiesName) && labelProbabilityLayerName && classifier.ClassLabels) {
- predictedFeatureName = predictedFeatureName ? predictedFeatureName : '?';
- predictedProbabilitiesName = predictedProbabilitiesName ? predictedProbabilitiesName : '?';
- const labelProbabilityInput = this._updateOutput(labelProbabilityLayerName, labelProbabilityLayerName + ':labelProbabilityLayerName');
- const type = classifier.ClassLabels;
- const inputs = [
- new coreml.Parameter('input', true, [ new coreml.Argument(labelProbabilityInput) ])
- ];
- const outputs = [
- new coreml.Parameter('probabilities', true, [ new coreml.Argument(predictedProbabilitiesName) ]),
- new coreml.Parameter('feature', true, [ new coreml.Argument(predictedFeatureName) ])
- ];
- const node = new coreml.Node(this._metadata, this._group, type, null, '', classifier[type], inputs, outputs);
- this._nodes.push(node);
- }
- }
- _updatePreprocessing(scope, group, preprocessing) {
- if (preprocessing && preprocessing.length > 0) {
- const preprocessingInput = this._description.input[0].name;
- const inputNodes = [];
- for (const node of this._nodes) {
- if (node.inputs.some((input) => input.arguments.some((arg) => arg.name == preprocessingInput))) {
- inputNodes.push(node);
- }
- }
- let preprocessorOutput = preprocessingInput;
- let preprocessorIndex = 0;
- for (const p of preprocessing) {
- const input = p.featureName ? p.featureName : preprocessorOutput;
- preprocessorOutput = preprocessingInput + ':' + preprocessorIndex.toString();
- this._createNode(scope, group, p.preprocessor, null, '', p[p.preprocessor], [ input ], [ preprocessorOutput ]);
- preprocessorIndex++;
- }
- for (const node of inputNodes) {
- for (const input of node.inputs) {
- for (const arg of input.arguments) {
- if (arg.name === preprocessingInput) {
- arg.name = preprocessorOutput;
- }
- }
- }
- }
- }
- }
- _loadModel(model, scope, group, weights) {
- this._groups = this._groups | (group.length > 0 ? true : false);
- const description = model && model.description && model.description.metadata && model.description.metadata.shortDescription ? model.description.metadata.shortDescription : '';
- switch (model.Type) {
- case 'neuralNetworkClassifier': {
- const neuralNetworkClassifier = model.neuralNetworkClassifier;
- for (const layer of neuralNetworkClassifier.layers) {
- this._createNode(scope, group, layer.layer, layer.name, group === '' ? '' : description, layer[layer.layer], layer.input, layer.output);
- }
- this._updateClassifierOutput(group, neuralNetworkClassifier);
- this._updatePreprocessing(scope, group, neuralNetworkClassifier.preprocessing);
- return 'Neural Network Classifier';
- }
- case 'neuralNetwork': {
- const neuralNetwork = model.neuralNetwork;
- for (const layer of neuralNetwork.layers) {
- this._createNode(scope, group, layer.layer, layer.name, group === '' ? '' : description, layer[layer.layer], layer.input, layer.output);
- }
- this._updatePreprocessing(scope, group, neuralNetwork.preprocessing);
- return 'Neural Network';
- }
- case 'neuralNetworkRegressor': {
- const neuralNetworkRegressor = model.neuralNetworkRegressor;
- for (const layer of neuralNetworkRegressor.layers) {
- this._createNode(scope, group, layer.layer, layer.name, description, layer[layer.layer], layer.input, layer.output);
- }
- this._updatePreprocessing(scope, group, neuralNetworkRegressor);
- return 'Neural Network Regressor';
- }
- case 'pipeline': {
- for (let i = 0; i < model.pipeline.models.length; i++) {
- this._loadModel(model.pipeline.models[i], scope, (group ? (group + '/') : '') + 'pipeline[' + i.toString() + ']');
- }
- return 'Pipeline';
- }
- case 'pipelineClassifier': {
- for (let i = 0; i < model.pipelineClassifier.pipeline.models.length; i++) {
- this._loadModel(model.pipelineClassifier.pipeline.models[i], scope, (group ? (group + '/') : '') + 'pipelineClassifier[' + i.toString() + ']');
- }
- return 'Pipeline Classifier';
- }
- case 'pipelineRegressor': {
- for (let i = 0; i < model.pipelineRegressor.pipeline.models.length; i++) {
- this._loadModel(model.pipelineRegressor.pipeline.models[i], scope, (group ? (group + '/') : '') + 'pipelineRegressor[' + i.toString() + ']');
- }
- return 'Pipeline Regressor';
- }
- case 'glmClassifier': {
- this._createNode(scope, group, 'glmClassifier', null, description,
- {
- classEncoding: model.glmClassifier.classEncoding,
- offset: model.glmClassifier.offset,
- weights: model.glmClassifier.weights
- },
- [ model.description.input[0].name ],
- [ model.description.predictedProbabilitiesName ]);
- this._updateClassifierOutput(group, model.glmClassifier);
- return 'Generalized Linear Classifier';
- }
- case 'glmRegressor': {
- this._createNode(scope, group, 'glmRegressor', null, description,
- model.glmRegressor,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Generalized Linear Regressor';
- }
- case 'dictVectorizer': {
- this._createNode(scope, group, 'dictVectorizer', null, description,
- model.dictVectorizer,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Dictionary Vectorizer';
- }
- case 'featureVectorizer': {
- this._createNode(scope, group, 'featureVectorizer', null, description,
- model.featureVectorizer,
- coreml.Graph._formatFeatureDescriptionList(model.description.input),
- [ model.description.output[0].name ]);
- return 'Feature Vectorizer';
- }
- case 'treeEnsembleClassifier': {
- this._createNode(scope, group, 'treeEnsembleClassifier', null, description,
- model.treeEnsembleClassifier.treeEnsemble,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- this._updateClassifierOutput(group, model.treeEnsembleClassifier);
- return 'Tree Ensemble Classifier';
- }
- case 'treeEnsembleRegressor': {
- this._createNode(scope, group, 'treeEnsembleRegressor', null, description,
- model.treeEnsembleRegressor.treeEnsemble,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Tree Ensemble Regressor';
- }
- case 'supportVectorClassifier': {
- this._createNode(scope, group, 'supportVectorClassifier', null, description,
- {
- coefficients: model.supportVectorClassifier.coefficients,
- denseSupportVectors: model.supportVectorClassifier.denseSupportVectors,
- kernel: model.supportVectorClassifier.kernel,
- numberOfSupportVectorsPerClass: model.supportVectorClassifier.numberOfSupportVectorsPerClass,
- probA: model.supportVectorClassifier.probA,
- probB: model.supportVectorClassifier.probB,
- rho: model.supportVectorClassifier.rho,
- supportVectors: model.supportVectorClassifier.supportVectors
- },
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- this._updateClassifierOutput(group, model.supportVectorClassifier);
- return 'Support Vector Classifier';
- }
- case 'supportVectorRegressor': {
- this._createNode(scope, group, 'supportVectorRegressor', null, description,
- {
- coefficients: model.supportVectorRegressor.coefficients,
- kernel: model.supportVectorRegressor.kernel,
- rho: model.supportVectorRegressor.rho,
- supportVectors: model.supportVectorRegressor.supportVectors
- },
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Support Vector Regressor';
- }
- case 'arrayFeatureExtractor': {
- this._createNode(scope, group, 'arrayFeatureExtractor', null, description,
- { extractIndex: model.arrayFeatureExtractor.extractIndex },
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Array Feature Extractor';
- }
- case 'oneHotEncoder': {
- const categoryType = model.oneHotEncoder.CategoryType;
- const oneHotEncoderParams = { outputSparse: model.oneHotEncoder.outputSparse };
- oneHotEncoderParams[categoryType] = model.oneHotEncoder[categoryType];
- this._createNode(scope, group, 'oneHotEncoder', null, description,
- oneHotEncoderParams,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'One Hot Encoder';
- }
- case 'imputer': {
- const imputedValue = model.imputer.ImputedValue;
- const replaceValue = model.imputer.ReplaceValue;
- const imputerParams = {};
- imputerParams[imputedValue] = model.imputer[imputedValue];
- imputerParams[replaceValue] = model.imputer[replaceValue];
- this._createNode(scope, group, 'oneHotEncoder', null, description,
- imputerParams,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Imputer';
- }
- case 'normalizer': {
- this._createNode(scope, group, 'normalizer', null, description,
- model.normalizer,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Normalizer';
- }
- case 'wordTagger': {
- this._createNode(scope, group, 'wordTagger', null, description,
- model.wordTagger,
- [ model.description.input[0].name ],
- [
- model.wordTagger.tokensOutputFeatureName,
- model.wordTagger.tokenTagsOutputFeatureName,
- model.wordTagger.tokenLocationsOutputFeatureName,
- model.wordTagger.tokenLengthsOutputFeatureName
- ]);
- return 'Word Tagger';
- }
- case 'textClassifier': {
- this._createNode(scope, group, 'textClassifier', null, description,
- model.textClassifier,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Text Classifier';
- }
- case 'nonMaximumSuppression': {
- const nonMaximumSuppressionParams = {
- pickTop: model.nonMaximumSuppression.pickTop,
- stringClassLabels: model.nonMaximumSuppression.stringClassLabels,
- iouThreshold: model.nonMaximumSuppression.iouThreshold,
- confidenceThreshold: model.nonMaximumSuppression.confidenceThreshold
- };
- this._createNode(scope, group, 'nonMaximumSuppression', null, description,
- nonMaximumSuppressionParams,
- [
- model.nonMaximumSuppression.confidenceInputFeatureName,
- model.nonMaximumSuppression.coordinatesInputFeatureName,
- model.nonMaximumSuppression.iouThresholdInputFeatureName,
- model.nonMaximumSuppression.confidenceThresholdInputFeatureName,
- ],
- [
- model.nonMaximumSuppression.confidenceOutputFeatureName,
- model.nonMaximumSuppression.coordinatesOutputFeatureName
- ]);
- return 'Non Maximum Suppression';
- }
- case 'visionFeaturePrint': {
- const visionFeaturePrintParams = {
- scene: model.visionFeaturePrint.scene
- };
- this._createNode(scope, group, 'visionFeaturePrint', null, description,
- visionFeaturePrintParams,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Vision Feature Print';
- }
- case 'soundAnalysisPreprocessing': {
- this._createNode(scope, group, 'soundAnalysisPreprocessing', null, description,
- model.soundAnalysisPreprocessing,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Sound Analysis Preprocessing';
- }
- case 'kNearestNeighborsClassifier': {
- this._createNode(scope, group, 'kNearestNeighborsClassifier', null, description,
- model.kNearestNeighborsClassifier,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- this._updateClassifierOutput(group, model.kNearestNeighborsClassifier);
- return 'Nearest Neighbors Classifier';
- }
- case 'itemSimilarityRecommender': {
- this._createNode(scope, group, 'itemSimilarityRecommender', null, description,
- {
- itemStringIds: model.itemSimilarityRecommender.itemStringIds.vector,
- itemItemSimilarities: model.itemSimilarityRecommender.itemItemSimilarities
- },
- model.description.input.map((feature) => feature.name),
- model.description.output.map((feature) => feature.name));
- return 'Item Similarity Recommender';
- }
- case 'audioFeaturePrint': {
- this._createNode(scope, group, 'audioFeaturePrint', null, description,
- model.audioFeaturePrint,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Audio Feature Print';
- }
- case 'linkedModel': {
- this._createNode(scope, group, 'linkedModel', null, description,
- model.linkedModel.linkedModelFile,
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'Linked Model';
- }
- case 'customModel': {
- this._createNode(scope, group, 'customModel', null, description,
- { className: model.customModel.className, parameters: model.customModel.parameters },
- [ model.description.input[0].name ],
- [ model.description.output[0].name ]);
- return 'customModel';
- }
- case 'mlProgram': {
- return this._loadProgram(model.mlProgram, scope, group, weights);
- }
- default: {
- throw new coreml.Error("Unsupported model type '" + JSON.stringify(Object.keys(model)) + "'.");
- }
- }
- }
- _loadProgram(program, scope, group, weights) {
- // TODO: need to handle functions other than main?
- const main = program.functions.main;
- // TODO: need to handle more than one block specialization?
- const block = main.block_specializations.CoreML5;
- const convertValue = (value) => {
- switch (value.value) {
- case 'immediateValue': {
- const tensor = value.immediateValue.tensor;
- let values = null;
- switch (tensor.value) {
- case 'ints':
- values = tensor.ints.values;
- break;
- case 'strings':
- values = tensor.strings.values;
- break;
- case 'bools':
- values = tensor.bools.values;
- break;
- case 'floats':
- values = tensor.floats.values;
- break;
- case 'bytes':
- values = tensor.bytes.values;
- break;
- default:
- throw new coreml.Error("Unsupported tensor value '" + tensor.value + "'.");
- }
- return values;
- }
- case 'blobFileValue': {
- const type = coreml.Utility.valueType(value.type);
- const blob = value.blobFileValue;
- const offset = blob.offset.toNumber();
- const file = blob.fileName;
- let data = null;
- const stream = weights.get(file);
- if (stream) {
- stream.seek(offset);
- const buffer = stream.read(32);
- const reader = new base.BinaryReader(buffer);
- const signature = reader.uint32();
- if (signature == 0xdeadbeef) {
- reader.uint32(); // dataType
- const size = reader.uint64();
- stream.seek(reader.uint64());
- const length = (type.shape.dimensions || []).reduce((a, b) => a * b, 1);
- switch (type.dataType) {
- case 'float32': {
- const buffer = stream.read(size);
- data = new Float32Array(buffer.buffer, buffer.byteOffset, length).slice();
- break;
- }
- case 'float16': {
- data = stream.read(size);
- break;
- }
- default:
- throw new coreml.Error("Unsupported blob data type '" + type.dataType + "'.");
- }
- }
- }
- return new coreml.Tensor('Blob', type, data);
- }
- default: {
- throw new coreml.Error("Unsupported value '" + value.value + "'.");
- }
- }
- };
- const args = new Map();
- const arg = (name) => {
- if (!args.has(name)) {
- args.set(name, { name: name, to: [], from: [] });
- }
- return args.get(name);
- };
- const operations = block.operations.map((op) => {
- const operation = {
- type: op.type,
- attributes: {}
- };
- for (const entry of Object.entries(op.attributes)) {
- const key = entry[0];
- const value = entry[1];
- operation.attributes[key] = convertValue(value);
- }
- operation.inputs = Object.entries(op.inputs).map((entry) => {
- const key = entry[0];
- const input = entry[1];
- const args = input.arguments.map((argument) => {
- if (argument.name) {
- const value = arg(argument.name);
- value.to.push(operation);
- return value;
- }
- return { value: argument.value };
- });
- return {
- name: key,
- arguments: args
- };
- });
- operation.outputs = op.outputs.map((output) => {
- const value = arg(output.name);
- value.type = coreml.Utility.valueType(output.type);
- value.from.push(operation);
- return {
- name: 'output',
- arguments: [ value ]
- };
- });
- return operation;
- });
- for (const op of operations) {
- if (op.type === 'const' && op.inputs.length === 0 &&
- op.outputs.length === 1 && op.outputs[0].arguments.length === 1) {
- const argument = op.outputs[0].arguments[0];
- if (op.attributes && op.attributes.val) {
- const type = argument.type;
- const data = op.attributes.val;
- if (data instanceof Uint8Array && data.length === 2 &&
- type.dataType === 'float16' && type.shape.dimensions.length === 0) {
- const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
- argument.value = view.getFloat16(0, true);
- }
- else {
- argument.value = data;
- }
- argument.const = true;
- op.delete = true;
- }
- }
- }
- for (const op of operations) {
- for (const input of op.inputs) {
- if (input.arguments.length > 1 && input.arguments.some((argument) => argument.const)) {
- if (input.arguments.every((argument) => argument.value instanceof coreml.Tensor)) {
- continue;
- }
- for (const argument of input.arguments) {
- for (const from of argument.from) {
- from.delete = false;
- }
- delete argument.value;
- }
- }
- }
- }
- for (const op of operations) {
- if (op.delete) {
- continue;
- }
- op.inputs = op.inputs.filter((input) => {
- if (input.arguments.every((argument) => argument.value === undefined || argument.value instanceof coreml.Tensor)) {
- return true;
- }
- if (input.arguments.length === 1) {
- const argument = input.arguments[0];
- op.attributes[input.name] = argument.value;
- return false;
- }
- op.attributes[input.name] = input.arguments.map((argument) => argument.value[0]);
- return false;
- });
- }
- const tensors = new Map();
- const tensor = (arg) => {
- if (!tensors.has(arg.name)) {
- tensors.set(arg.name, new coreml.Argument(arg.name, arg.type, null, arg.value));
- }
- return tensors.get(arg.name);
- };
- for (const op of operations) {
- if (op.delete) {
- continue;
- }
- op.inputs = op.inputs.map((input) => new coreml.Parameter(input.name, true, input.arguments.map((argument) => tensor(argument))));
- op.outputs = op.outputs.map((output) => new coreml.Parameter(output.name, true, output.arguments.map((argument) => tensor(argument))));
- }
- for (const op of operations.filter((op) => !op.delete)) {
- const type = 'program:' + op.type;
- const metadata = this._metadata.type(type);
- if (metadata && Array.isArray(metadata.inputs)) {
- let index = 1;
- const map = new Map(metadata.inputs.map((input) => [ input.name, index++ ]));
- op.inputs.sort((a, b) => (map.get(a.name) || map.size) - (map.get(b.name) || map.size));
- }
- const node = new coreml.Node(this._metadata, group, type, null, null, op.attributes, op.inputs, op.outputs);
- this._nodes.push(node);
- }
- return 'ML Program';
- }
- _createNode(scope, group, type, name, description, data, inputs, outputs, outputTypes) {
- inputs = inputs.map((input) => scope[input] ? scope[input].argument : input);
- outputs = outputs.map((output) => {
- if (scope[output]) {
- scope[output].counter++;
- const next = output + '\n' + scope[output].counter.toString(); // custom argument id
- scope[output].argument = next;
- return next;
- }
- scope[output] = {
- argument: output,
- counter: 0
- };
- return output;
- });
- const initializers = [];
- const attributes = {};
- if (data) {
- const map = this._initialize(type, data, initializers);
- for (const key of Object.keys(data)) {
- if (map[key]) {
- continue;
- }
- attributes[key] = data[key];
- }
- }
- const inputParameters = this._metadata.getInputs(type, inputs).map((input) => {
- return new coreml.Parameter(input.name, true, input.arguments.map((argument) => {
- return new coreml.Argument(argument.name, argument.type, null, null);
- }));
- });
- inputParameters.push(...initializers);
- const outputParameters = outputs.map((output, index) => {
- const name = this._metadata.getOutputName(type, index);
- const outputType = outputTypes ? outputTypes[index] : null;
- return new coreml.Parameter(name, true, [ new coreml.Argument(output, outputType, null, null) ]);
- });
- const node = new coreml.Node(this._metadata, group, type, name, description, attributes, inputParameters, outputParameters);
- this._nodes.push(node);
- return node;
- }
- _initializer(type, initializers, kind, name, shape, data) {
- let dataType = '?';
- let quantization = null;
- let values = null;
- if (data) {
- if (data.floatValue && data.floatValue.length > 0) {
- values = data.floatValue;
- dataType = 'float32';
- }
- else if (data.float16Value && data.float16Value.length > 0) {
- values = data.float16Value; // byte[]
- dataType = 'float16';
- }
- else if (data.rawValue && data.rawValue.length > 0) {
- if (data.quantization) {
- values = data.rawValue;
- dataType = 'uint' + data.quantization.numberOfBits.toString();
- }
- else {
- shape = [];
- }
- }
- quantization = data.quantization || null;
- }
- const tensorType = new coreml.TensorType(dataType, new coreml.TensorShape(shape));
- const tensor = new coreml.Tensor(kind, tensorType, values, quantization);
- const argument = new coreml.Argument('', null, null, tensor);
- const visible = this._metadata.visible(type, name);
- initializers.push(new coreml.Parameter(name, visible, [ argument ]));
- }
- _initialize(type, data, initializers) {
- switch (type) {
- case 'convolution': {
- const weightsShape = [ data.outputChannels, data.kernelChannels, data.kernelSize[0], data.kernelSize[1] ];
- if (data.isDeconvolution) {
- weightsShape[0] = data.kernelChannels;
- weightsShape[1] = Math.floor(data.outputChannels / (data.nGroups != 0 ? data.nGroups : 1));
- }
- this._initializer(type, initializers, 'Weights', 'weights', weightsShape, data.weights);
- if (data.hasBias) {
- this._initializer(type, initializers, 'Weights', 'bias', [ data.outputChannels ], data.bias);
- }
- return { 'weights': true, 'bias': data.hasBias };
- }
- case 'innerProduct':
- this._initializer(type, initializers, 'Weights', 'weights', [ data.outputChannels, data.inputChannels ], data.weights);
- if (data.hasBias) {
- this._initializer(type, initializers, 'Weights', 'bias', [ data.outputChannels ], data.bias);
- }
- return { 'weights': true, 'bias': data.hasBias };
- case 'batchnorm':
- this._initializer(type, initializers, 'Weights', 'gamma', [ data.channels ], data.gamma);
- this._initializer(type, initializers, 'Weights', 'beta', [ data.channels ], data.beta);
- if (data.mean) {
- this._initializer(type, initializers, 'Weights', 'mean', [ data.channels ], data.mean);
- }
- if (data.variance) {
- this._initializer(type, initializers, 'Weights', 'variance', [ data.channels ], data.variance);
- }
- return { 'gamma': true, 'beta': true, 'mean': true, 'variance': true };
- case 'embedding':
- this._initializer(type, initializers, 'Weights', 'weights', [ data.inputDim, data.outputChannels ], data.weights);
- return { 'weights': true };
- case 'loadConstant':
- case 'loadConstantND':
- this._initializer(type, initializers, 'Weights', 'data', data.shape, data.data);
- return { 'data': true };
- case 'scale':
- this._initializer(type, initializers, 'Weights', 'scale', data.shapeScale, data.scale);
- if (data.hasBias) {
- this._initializer(type, initializers, 'Weights', 'bias', data.shapeBias, data.bias);
- }
- return { 'scale': true, 'bias': data.hasBias };
- case 'bias':
- this._initializer(type, initializers, 'Weights', 'bias', data.shape, data.bias);
- return { 'bias': true };
- case 'simpleRecurrent':
- this._initializer(type, initializers, 'Weights', 'weights', [ data.outputVectorSize, data.inputVectorSize ], data.weightMatrix);
- this._initializer(type, initializers, 'Weights', 'recurrent', [ data.outputVectorSize, data.inputVectorSize ], data.recursionMatrix);
- if (data.hasBiasVectors) {
- this._initializer(type, initializers, 'Weights', 'bias', [ data.outputVectorSize ], data.biasVector);
- }
- return { 'weightMatrix': true, 'recursionMatrix': true, 'biasVector': data.hasBiasVectors };
- case 'gru': {
- const recursionMatrixShape = [ data.outputVectorSize, data.outputVectorSize ];
- const weightMatrixShape = [ data.outputVectorSize, data.inputVectorSize ];
- const biasVectorShape = [ data.outputVectorSize ];
- this._initializer(type, initializers, 'Weights', 'updateGateWeightMatrix', weightMatrixShape, data.updateGateWeightMatrix);
- this._initializer(type, initializers, 'Weights', 'resetGateWeightMatrix', weightMatrixShape, data.resetGateWeightMatrix);
- this._initializer(type, initializers, 'Weights', 'outputGateWeightMatrix', weightMatrixShape, data.outputGateWeightMatrix);
- this._initializer(type, initializers, 'Weights', 'updateGateRecursionMatrix', recursionMatrixShape, data.updateGateRecursionMatrix);
- this._initializer(type, initializers, 'Weights', 'resetGateRecursionMatrix', recursionMatrixShape, data.resetGateRecursionMatrix);
- this._initializer(type, initializers, 'Weights', 'outputGateRecursionMatrix', recursionMatrixShape, data.outputGateRecursionMatrix);
- if (data.hasBiasVectors) {
- this._initializer(type, initializers, 'Weights', 'updateGateBiasVector', biasVectorShape, data.updateGateBiasVector);
- this._initializer(type, initializers, 'Weights', 'resetGateBiasVector', biasVectorShape, data.resetGateBiasVector);
- this._initializer(type, initializers, 'Weights', 'outputGateBiasVector', biasVectorShape, data.outputGateBiasVector);
- }
- return {
- 'updateGateWeightMatrix': true, 'resetGateWeightMatrix': true, 'outputGateWeightMatrix': true,
- 'updateGateRecursionMatrix': true, 'resetGateRecursionMatrix': true, 'outputGateRecursionMatrix': true,
- 'updateGateBiasVector': data.hasBiasVectors, 'resetGateBiasVector': data.hasBiasVectors, 'outputGateBiasVector': data.hasBiasVectors
- };
- }
- case 'uniDirectionalLSTM':
- case 'biDirectionalLSTM': {
- const count = (type == 'uniDirectionalLSTM') ? 1 : 2;
- const matrixShape = [ data.outputVectorSize, data.inputVectorSize ];
- const vectorShape = [ data.outputVectorSize ];
- for (let i = 0; i < count; i++) {
- const weights = count == 1 ? data.weightParams : data.weightParams[i];
- const suffix = (i == 0) ? '' : '_rev';
- this._initializer(type, initializers, 'Weights', 'inputGateWeightMatrix' + suffix, matrixShape, weights.inputGateWeightMatrix);
- this._initializer(type, initializers, 'Weights', 'forgetGateWeightMatrix' + suffix, matrixShape, weights.forgetGateWeightMatrix);
- this._initializer(type, initializers, 'Weights', 'blockInputWeightMatrix' + suffix, matrixShape, weights.blockInputWeightMatrix);
- this._initializer(type, initializers, 'Weights', 'outputGateWeightMatrix' + suffix, matrixShape, weights.outputGateWeightMatrix);
- this._initializer(type, initializers, 'Weights', 'inputGateRecursionMatrix' + suffix, matrixShape, weights.inputGateRecursionMatrix);
- this._initializer(type, initializers, 'Weights', 'forgetGateRecursionMatrix' + suffix, matrixShape,weights.forgetGateRecursionMatrix);
- this._initializer(type, initializers, 'Weights', 'blockInputRecursionMatrix' + suffix, matrixShape, weights.blockInputRecursionMatrix);
- this._initializer(type, initializers, 'Weights', 'outputGateRecursionMatrix' + suffix, matrixShape, weights.outputGateRecursionMatrix);
- if (data.params.hasBiasVectors) {
- this._initializer(type, initializers, 'Weights', 'inputGateBiasVector' + suffix, vectorShape, weights.inputGateBiasVector);
- this._initializer(type, initializers, 'Weights', 'forgetGateBiasVector' + suffix, vectorShape, weights.forgetGateBiasVector);
- this._initializer(type, initializers, 'Weights', 'blockInputBiasVector' + suffix, vectorShape, weights.blockInputBiasVector);
- this._initializer(type, initializers, 'Weights', 'outputGateBiasVector' + suffix, vectorShape, weights.outputGateBiasVector);
- }
- if (data.params.hasPeepholeVectors) {
- this._initializer(type, initializers, 'Weights', 'inputGatePeepholeVector' + suffix, vectorShape, weights.inputGatePeepholeVector);
- this._initializer(type, initializers, 'Weights', 'forgetGatePeepholeVector' + suffix, vectorShape, weights.forgetGatePeepholeVector);
- this._initializer(type, initializers, 'Weights', 'outputGatePeepholeVector' + suffix, vectorShape, weights.outputGatePeepholeVector);
- }
- }
- return { 'weightParams': true };
- }
- case 'dictVectorizer':
- data.stringToIndex = this._convertVector(data.stringToIndex);
- return {};
- case 'wordTagger':
- data.modelParameterData = Array.from(data.modelParameterData);
- data.stringTags = this._convertVector(data.stringTags);
- return { tokensOutputFeatureName: true, tokenTagsOutputFeatureName: true, tokenLengthsOutputFeatureName: true, tokenLocationsOutputFeatureName: true };
- case 'textClassifier':
- data.modelParameterData = Array.from(data.modelParameterData);
- data.stringClassLabels = this._convertVector(data.stringClassLabels);
- return {};
- case 'nonMaximumSuppression':
- data.stringClassLabels = this._convertVector(data.stringClassLabels);
- return {};
- default:
- return {};
- }
- }
- _convertVector(value) {
- if (value && Object.keys(value).length == 1 && value.vector) {
- return value.vector;
- }
- return value;
- }
- static _formatFeatureDescriptionList(list) {
- return list.map((item) => item.name);
- }
- };
- coreml.Parameter = class {
- constructor(name, visible, args) {
- this._name = name;
- this._visible = visible;
- this._arguments = args;
- }
- get name() {
- return this._name;
- }
- get visible() {
- return this._visible;
- }
- get arguments() {
- return this._arguments;
- }
- };
- coreml.Argument = class {
- constructor(name, type, description, initializer) {
- if (typeof name !== 'string') {
- throw new coreml.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
- }
- this._name = name;
- this._type = type;
- this._description = description || null;
- this._initializer = initializer || null;
- }
- get name() {
- return this._name;
- }
- set name(value) {
- this._name = value;
- }
- get type() {
- if (this._initializer) {
- return this._initializer.type;
- }
- return this._type;
- }
- get description() {
- return this._description;
- }
- get quantization() {
- if (this._initializer) {
- return this._initializer.quantization;
- }
- return null;
- }
- get initializer() {
- return this._initializer;
- }
- };
- coreml.Node = class {
- constructor(metadata, group, type, name, description, attributes, inputs, outputs) {
- if (!type) {
- throw new Error('Undefined node type.');
- }
- if (group) {
- this._group = group;
- }
- this._type = Object.assign({}, metadata.type(type) || { name: type });
- this._type.name = type.split(':').pop();
- this._name = name || '';
- this._description = description || '';
- this._inputs = inputs;
- this._outputs = outputs;
- this._attributes = [];
- if (attributes) {
- for (const key of Object.keys(attributes)) {
- const schema = metadata.attribute(type, key);
- const value = attributes[key];
- const attribute = new coreml.Attribute(schema, key, value);
- this._attributes.push(attribute);
- }
- }
- }
- get type() {
- return this._type;
- }
- get name() {
- return this._name;
- }
- get description() {
- return this._description;
- }
- get metadata() {
- return this._metadata;
- }
- get group() {
- return this._group ? this._group : null;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get attributes() {
- return this._attributes;
- }
- };
- coreml.Attribute = class {
- constructor(metadata, name, value) {
- this._name = name;
- this._value = value;
- if (this._value instanceof coreml.Tensor) {
- this._type = 'tensor';
- }
- if (metadata) {
- if (metadata.type) {
- this._type = metadata.type;
- }
- if (this._type && coreml.proto) {
- this._value = coreml.Utility.enum(this._type, this._value);
- }
- if (Object.prototype.hasOwnProperty.call(metadata, 'visible') && !metadata.visible) {
- this._visible = false;
- }
- else if (Object.prototype.hasOwnProperty.call(metadata, 'default')) {
- if (Array.isArray(value)) {
- value = value.map((item) => item.toNumber());
- }
- if (JSON.stringify(metadata.default) == JSON.stringify(value)) {
- this._visible = false;
- }
- }
- }
- }
- get name() {
- return this._name;
- }
- get type() {
- return this._type;
- }
- get value() {
- return this._value;
- }
- get visible() {
- return this._visible == false ? false : true;
- }
- };
- coreml.Tensor = class {
- constructor(kind, type, data, quantization) {
- this._kind = kind;
- this._type = type;
- this._data = data;
- this._quantization = quantization;
- }
- get kind() {
- return this._kind;
- }
- get type() {
- return this._type;
- }
- get quantization() {
- if (this._quantization) {
- if (this._quantization.lookupTableQuantization &&
- this._quantization.lookupTableQuantization.floatValue &&
- this._quantization.lookupTableQuantization.floatValue.length > 0) {
- const map = [];
- for (const key of Object.keys(this._quantization.lookupTableQuantization.floatValue)) {
- map.push(key.toString() + ' = ' + this._quantization.lookupTableQuantization.floatValue[key].toString());
- }
- return map.join('; ');
- }
- return '?';
- }
- return null;
- }
- get state() {
- return this._context().state;
- }
- 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 JSON.stringify(value, null, 4);
- }
- _context() {
- const context = {};
- context.state = null;
- context.index = 0;
- context.count = 0;
- context.dataType = this._type.dataType;
- context.dimensions = this._type.shape.dimensions;
- if (!this._data) {
- context.state = 'Tensor data is empty.';
- return context;
- }
- switch (context.dataType) {
- case 'float32':
- context.data = this._data;
- break;
- case 'float16':
- context.data = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength);
- break;
- default:
- if (this._quantization) {
- context.dataType = 'quantization';
- context.bits = this._quantization.numberOfBits.toNumber();
- context.data = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength);
- }
- else {
- context.state = 'Tensor data type is not implemented.';
- }
- break;
- }
- return context;
- }
- _decode(context, dimension) {
- const results = [];
- const size = context.dimensions[dimension];
- if (dimension == context.dimensions.length - 1) {
- for (let i = 0; i < size; i++) {
- if (context.count > context.limit) {
- results.push('...');
- return results;
- }
- switch (context.dataType) {
- case 'float32':
- results.push(this._data[context.index]);
- context.index++;
- break;
- case 'float16':
- results.push(context.data.getFloat16(context.index, true));
- context.index += 2;
- break;
- case 'quantization':
- results.push(context.data.getBits(context.index, context.bits));
- context.index++;
- break;
- default:
- break;
- }
- context.count++;
- }
- }
- else {
- for (let j = 0; j < size; j++) {
- if (context.count > context.limit) {
- results.push('...');
- return results;
- }
- results.push(this._decode(context, dimension + 1));
- }
- }
- return results;
- }
- };
- coreml.TensorType = class {
- constructor(dataType, shape) {
- this._dataType = dataType;
- this._shape = shape || new coreml.TensorShape([]);
- }
- get dataType() {
- return this._dataType;
- }
- get shape() {
- return this._shape;
- }
- toString() {
- return this.dataType + this._shape.toString();
- }
- };
- coreml.TensorShape = class {
- constructor(dimensions) {
- this._dimensions = dimensions;
- }
- get dimensions() {
- return this._dimensions;
- }
- toString() {
- if (!this._dimensions || this._dimensions.length == 0) {
- return '';
- }
- return '[' + this._dimensions.map((dimension) => dimension.toString()).join(',') + ']';
- }
- };
- coreml.ListType = class {
- constructor(elementType) {
- this._elementType = elementType;
- }
- toString() {
- return 'list<' + this._elementType.toString() + '>';
- }
- };
- coreml.MapType = class {
- constructor(keyType, valueType) {
- this._keyType = keyType;
- this._valueType = valueType;
- }
- get keyType() {
- return this._keyType;
- }
- get valueType() {
- return this._valueType;
- }
- toString() {
- return 'map<' + this._keyType + ',' + this._valueType.toString() + '>';
- }
- };
- coreml.SequenceType = class {
- constructor(type) {
- this._type = type;
- }
- get type() {
- return this._type;
- }
- toString() {
- return 'sequence<' + this._type + '>';
- }
- };
- coreml.ImageType = class {
- constructor(colorSpace, width, height) {
- this._width = width;
- this._height = height;
- switch (colorSpace) {
- case coreml.proto.ImageFeatureType.ColorSpace.GRAYSCALE:
- this._colorSpace = 'Grayscale';
- break;
- case coreml.proto.ImageFeatureType.ColorSpace.RGB:
- this._colorSpace = 'RGB';
- break;
- case coreml.proto.ImageFeatureType.ColorSpace.BGR:
- this._colorSpace = 'BGR';
- break;
- default:
- throw new coreml.Error("Unsupported image color space '" + colorSpace + "'.");
- }
- }
- toString() {
- return 'image<' + this._colorSpace + ',' + this._width. toString() + 'x' + this._height.toString() + '>';
- }
- };
- coreml.OptionalType = class {
- constructor(type) {
- this._type = type;
- }
- get type() {
- return this._type;
- }
- toString() {
- return 'optional<' + this._type.toString() + '>';
- }
- };
- coreml.Utility = class {
- static enum(name, value) {
- let type = coreml.proto;
- const parts = name.split('.');
- while (type && parts.length > 0) {
- type = type[parts.shift()];
- }
- if (type) {
- coreml.Utility._enumKeyMap = coreml.Utility._enumKeyMap || new Map();
- if (!coreml.Utility._enumKeyMap.has(name)) {
- const map = new Map(Object.entries(type).map((pair) => [ pair[1], pair[0] ]));
- coreml.Utility._enumKeyMap.set(name, map);
- }
- const map = coreml.Utility._enumKeyMap.get(name);
- if (map.has(value)) {
- return map.get(value);
- }
- }
- return value;
- }
- static featureType(type) {
- let result = '?';
- if (type) {
- switch (type.Type) {
- case 'multiArrayType': {
- let shape = new coreml.TensorShape([]);
- if (type.multiArrayType.shape && type.multiArrayType.shape.length > 0) {
- shape = new coreml.TensorShape(type.multiArrayType.shape);
- }
- let dataType = '?';
- switch (type.multiArrayType.dataType) {
- case coreml.proto.ArrayFeatureType.ArrayDataType.INVALID_ARRAY_DATA_TYPE:
- dataType = '?';
- break;
- case coreml.proto.ArrayFeatureType.ArrayDataType.FLOAT32:
- dataType = 'float32';
- break;
- case coreml.proto.ArrayFeatureType.ArrayDataType.INT32:
- dataType = 'int32';
- break;
- case coreml.proto.ArrayFeatureType.ArrayDataType.DOUBLE:
- dataType = 'float64';
- break;
- default:
- throw new coreml.Error("Unsupported array data type '" + type.multiArrayType.dataType + "'.");
- }
- result = new coreml.TensorType(dataType, shape);
- break;
- }
- case 'stringType': {
- result = new coreml.TensorType('string');
- break;
- }
- case 'doubleType': {
- result = new coreml.TensorType('float64');
- break;
- }
- case 'int64Type': {
- result = new coreml.TensorType('int64');
- break;
- }
- case 'dictionaryType': {
- result = new coreml.MapType(type.dictionaryType.KeyType.replace('KeyType', ''), 'float64');
- break;
- }
- case 'sequenceType': {
- result = new coreml.SequenceType(coreml.Utility.featureType(type[type.Type]));
- break;
- }
- case 'imageType': {
- result = new coreml.ImageType(type.imageType.colorSpace, type.imageType.width, type.imageType.height);
- break;
- }
- default: {
- throw new coreml.Error("Unsupported feature type '" + type.Type + "'.");
- }
- }
- if (type.isOptional) {
- result = new coreml.OptionalType(result);
- }
- }
- return result;
- }
- static tensorType(type) {
- if (!coreml.Utility._dataTypes) {
- coreml.Utility._dataTypes = new Map();
- const DataType = coreml.proto.MILSpec.DataType;
- for (const pair of Object.entries(DataType)) {
- if (pair[0] === 'UNUSED_TYPE') {
- continue;
- }
- const name = pair[0] === 'bool' ? 'boolean' : pair[0].toLowerCase();
- coreml.Utility._dataTypes.set(pair[1], name);
- }
- }
- const shape = (type.dimensions.map(dim => dim.constant ? dim.constant.size : '?'));
- const dataType = coreml.Utility._dataTypes.get(type.dataType);
- if (dataType === null) {
- throw new coreml.Error("Unsupported data type '" + type.dataType + "'.");
- }
- return new coreml.TensorType(dataType, new coreml.TensorShape(shape));
- }
- static valueType(type) {
- switch (type.type) {
- case 'tensorType':
- return coreml.Utility.tensorType(type.tensorType);
- case 'listType':
- return new coreml.ListType(coreml.Utility.valueType(type.listType.type));
- default:
- throw new coreml.Error("Unsupported value type '" + type.type + "'.");
- }
- }
- };
- coreml.Metadata = class {
- static open(context) {
- if (coreml.Metadata._metadata) {
- return Promise.resolve(coreml.Metadata._metadata);
- }
- return context.request('coreml-metadata.json', 'utf-8', null).then((data) => {
- coreml.Metadata._metadata = new coreml.Metadata(data);
- return coreml.Metadata._metadata;
- }).catch(() => {
- coreml.Metadata._metadata = new coreml.Metadata(null);
- return coreml.Metadata._metadata;
- });
- }
- constructor(data) {
- this._map = new Map();
- this._attributeCache = new Map();
- this._inputCache = new Map();
- if (data) {
- const metadata = JSON.parse(data);
- this._map = new Map(metadata.map((item) => [ item.name, item ]));
- }
- }
- type(name) {
- return this._map.get(name);
- }
- attribute(type, name) {
- const key = type + ':' + name;
- if (!this._attributeCache.has(key)) {
- this._attributeCache.set(key, null);
- const metadata = this.type(type);
- if (metadata && Array.isArray(metadata.attributes) && metadata.attributes.length > 0) {
- for (const attribute of metadata.attributes) {
- this._attributeCache.set(type + ':' + attribute.name, attribute);
- }
- }
- }
- return this._attributeCache.get(key);
- }
- visible(type, name) {
- const key = type + ':' + name;
- if (!this._inputCache.has(key)) {
- this._inputCache.set(key, null);
- const metadata = this.type(type);
- if (metadata && Array.isArray(metadata.inputs) && metadata.inputs.length > 0) {
- for (const input of metadata.inputs) {
- this._inputCache.set(type + ':' + input.name, input);
- }
- }
- }
- const input = this._inputCache.get(key);
- if (input) {
- return input.visible === false ? false : true;
- }
- return true;
- }
- getInputs(type, inputs) {
- const results = [];
- const schema = this._map.get(type);
- let index = 0;
- while (index < inputs.length) {
- const result = { arguments: [] };
- let count = 1;
- let name = null;
- if (schema && schema.inputs) {
- if (index < schema.inputs.length) {
- const input = schema.inputs[index];
- name = input.name;
- if (schema.inputs[index].option == 'variadic') {
- count = inputs.length - index;
- }
- }
- }
- else if (index == 0) {
- name = 'input';
- }
- result.name = name ? name : '(' + index.toString() + ')';
- const array = inputs.slice(index, index + count);
- for (let j = 0; j < array.length; j++) {
- result.arguments.push({ name: array[j] });
- }
- index += count;
- results.push(result);
- }
- return results;
- }
- getOutputName(type, index) {
- const schema = this._map.get(type);
- if (schema) {
- const outputs = schema.outputs;
- if (outputs && index < outputs.length) {
- const output = outputs[index];
- if (output) {
- const name = output.name;
- if (name) {
- return name;
- }
- }
- }
- }
- if (index == 0) {
- return 'output';
- }
- return '(' + index.toString() + ')';
- }
- };
- coreml.Error = class extends Error {
- constructor(message) {
- super(message);
- this.name = 'Error loading Core ML model.';
- }
- };
- if (typeof module !== 'undefined' && typeof module.exports === 'object') {
- module.exports.ModelFactory = coreml.ModelFactory;
- }
|