[Ml][Trainer] Add JS stubs for ML Trainer API 68/266468/1
authorRafal Walczyna <r.walczyna@samsung.com>
Mon, 24 May 2021 13:42:29 +0000 (15:42 +0200)
committerPiotr Kosko/Tizen API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Fri, 12 Nov 2021 10:05:45 +0000 (11:05 +0100)
original change: https://review.tizen.org/gerrit/258708

Change-Id: I25df0e3e8a5a50dc8f2b3020c07519f1317d466b
Signed-off-by: Rafal Walczyna <r.walczyna@samsung.com>
src/ml/js/ml_manager.js
src/ml/js/ml_trainer.js [new file with mode: 0755]

index b094dfb..d9a46e4 100755 (executable)
@@ -25,6 +25,11 @@ var MachineLearningManager = function() {
             enumerable: true,
             writable: false,
             value: new MachineLearningPipeline()
+        },
+        trainer: {
+            enumerable: true,
+            writable: false,
+            value: new MachineLearningTrainer()
         }
     });
 };
diff --git a/src/ml/js/ml_trainer.js b/src/ml/js/ml_trainer.js
new file mode 100755 (executable)
index 0000000..685899c
--- /dev/null
@@ -0,0 +1,409 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+var MachineLearningTrainer = function() {};
+
+var OptimizerType = {
+    OPTIMIZER_ADAM: 'OPTIMIZER_ADAM',
+    OPTIMIZER_SGD: 'OPTIMIZER_SGD',
+    OPTIMIZER_UNKNOWN: 'OPTIMIZER_UNKNOWN'
+};
+
+var DatasetType = {
+    DATASET_GENERATOR: 'DATASET_GENERATOR',
+    DATASET_FILE: 'DATASET_FILE',
+    DATASET_UNKNOWN: 'DATASET_UNKNOWN'
+};
+
+var LayerType = {
+    LAYER_IN: 'LAYER_IN',
+    LAYER_FC: 'LAYER_FC',
+    LAYER_BN: 'LAYER_BN',
+    LAYER_CONV2D: 'LAYER_CONV2D',
+    LAYER_POOLING2D: 'LAYER_POOLING2D',
+    LAYER_FLATTEN: 'LAYER_FLATTEN',
+    LAYER_ACTIVATION: 'LAYER_ACTIVATION',
+    LAYER_ADDITION: 'LAYER_ADDITION',
+    LAYER_CONCAT: 'LAYER_CONCAT',
+    LAYER_MULTIOUT: 'LAYER_MULTIOUT',
+    LAYER_LOSS: 'LAYER_LOSS',
+    LAYER_BACKBONE_NNSTREAMER: 'LAYER_BACKBONE_NNSTREAMER',
+    LAYER_BACKBONE_TFLITE: 'LAYER_BACKBONE_TFLITE',
+    LAYER_EMBEDDING: 'LAYER_EMBEDDING',
+    LAYER_RNN: 'LAYER_RNN',
+    LAYER_UNKNOWN: 'LAYER_UNKNOWN'
+};
+
+var VerbosityLevel = {
+    SUMMARY_MODEL: 'SUMMARY_MODEL',
+    SUMMARY_LAYER: 'SUMMARY_LAYER',
+    SUMMARY_TENSOR: 'SUMMARY_TENSOR'
+};
+
+var Layer = function(id) {
+    Object.defineProperties(this, {
+        name: {
+            enumerable: true,
+            get: function() {
+                // TODO
+            }
+        },
+        type: {
+            enumerable: true,
+            get: function() {
+                // TODO
+            }
+        },
+        _id: { value: id, writable: false, enumerable: false }
+    });
+};
+
+var ValidSetPropertyExceptions = [
+    'InvalidValuesError',
+    'TypeMismatchError',
+    'AbortError'
+];
+
+Layer.prototype.setProperty = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'name',
+            type: types_.STRING
+        },
+        {
+            name: 'value',
+            type: types_.STRING
+        }
+    ]);
+
+    if (!args.has.name || !args.has.value) {
+        throw new WebAPIException(
+            WebAPIException.TYPE_MISMATCH_ERR,
+            'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
+        );
+    }
+
+    var callArgs = {
+        id: this._id,
+        name: args.name,
+        value: args.value
+    };
+
+    var result = native_.callSync('MLTrainerLayerSetProperty', callArgs);
+
+    if (native_.isFailure(result)) {
+        throw native_.getErrorObjectAndValidate(
+            result,
+            ValidSetPropertyExceptions,
+            AbortError
+        );
+    }
+};
+
+var Optimizer = function(id) {
+    Object.defineProperties(this, {
+        type: {
+            enumerable: true,
+            get: function() {
+                // TODO
+            }
+        },
+        _id: { value: id, writable: false, enumerable: false }
+    });
+};
+
+Optimizer.prototype.setProperty = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'name',
+            type: types_.STRING
+        },
+        {
+            name: 'value',
+            type: types_.STRING
+        }
+    ]);
+
+    if (!args.has.name || !args.has.value) {
+        throw new WebAPIException(
+            WebAPIException.TYPE_MISMATCH_ERR,
+            'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
+        );
+    }
+
+    var callArgs = {
+        id: this._id,
+        name: args.name,
+        value: args.value
+    };
+
+    var result = native_.callSync('MLTrainerOptimizerSetProperty', callArgs);
+
+    if (native_.isFailure(result)) {
+        throw native_.getErrorObjectAndValidate(
+            result,
+            ValidSetPropertyExceptions,
+            AbortError
+        );
+    }
+};
+
+var Dataset = function(id) {
+    Object.defineProperties(this, {
+        type: {
+            enumerable: true,
+            get: function() {
+                // TODO
+            }
+        },
+        _id: { value: id, writable: false, enumerable: false }
+    });
+};
+
+Dataset.prototype.setProperty = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'name',
+            type: types_.STRING
+        },
+        {
+            name: 'value',
+            type: types_.STRING
+        }
+    ]);
+
+    if (!args.has.name || !args.has.value) {
+        throw new WebAPIException(
+            WebAPIException.TYPE_MISMATCH_ERR,
+            'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
+        );
+    }
+
+    var callArgs = {
+        id: this._id,
+        name: args.name,
+        value: args.value
+    };
+
+    var result = native_.callSync('MLTrainerDatasetSetProperty', callArgs);
+
+    if (native_.isFailure(result)) {
+        throw native_.getErrorObjectAndValidate(
+            result,
+            ValidSetPropertyExceptions,
+            AbortError
+        );
+    }
+};
+
+var Model = function(id) {
+    Object.defineProperties(this, {
+        _id: { value: id, writable: false, enumerable: false }
+    });
+};
+
+function ValidateCompileOptions(options) {
+    // TODO:
+}
+
+Model.prototype.compile = function() {
+    var args = validator.validateArgs(arguments, [
+        {
+            name: 'options',
+            type: validator.Types.DICTIONARY,
+            optional: true,
+            nullable: true
+        }
+    ]);
+    if (args.has.options) {
+        ValidateCompileOptions(args.options);
+    }
+    // TODO:
+};
+
+function ValidateRunOptions(options) {
+    // TODO:
+}
+
+Model.prototype.run = function() {
+    var args = validator.validateArgs(arguments, [
+        {
+            name: 'options',
+            type: validator.Types.DICTIONARY,
+            optional: true,
+            nullable: true
+        }
+    ]);
+    if (args.has.options) {
+        ValidateRunOptions(args.options);
+    }
+    // TODO
+};
+
+Model.prototype.summarize = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'level',
+            type: types_.ENUM,
+            values: Object.values(VerbosityLevel),
+            optional: false
+        }
+    ]);
+    // TODO
+};
+
+Model.prototype.addLayer = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'layer',
+            type: types_.PLATFORM_OBJECT,
+            values: Layer
+        }
+    ]);
+    // TODO
+};
+
+Model.prototype.setDataset = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'dataset',
+            type: types_.PLATFORM_OBJECT,
+            values: Dataset
+        }
+    ]);
+    // TODO
+};
+
+Model.prototype.setOptimizer = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'optimizer',
+            type: types_.PLATFORM_OBJECT,
+            values: Optimizer
+        }
+    ]);
+    // TODO
+};
+
+var ValidCreateLayerExceptions = ['NotSupportedError', 'TypeMismatchError', 'AbortError'];
+
+var NO_ID = -1;
+MachineLearningTrainer.prototype.createLayer = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'type',
+            type: types_.ENUM,
+            values: Object.values(LayerType),
+            optional: false
+        }
+    ]);
+
+    // TODO
+    return new Layer(NO_ID);
+};
+
+function ValidateDatasetPaths(train, validate, test) {
+    // TODO
+}
+
+MachineLearningTrainer.prototype.createGeneratorDataset = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'train',
+            type: types_.STRING
+        },
+        {
+            name: 'validate',
+            type: types_.STRING,
+            optional: true,
+            nullable: true
+        },
+        {
+            name: 'test',
+            type: types_.STRING,
+            optional: true,
+            nullable: true
+        }
+    ]);
+    ValidateDatasetPaths(args.train, args.validate.args.test);
+
+    // TODO
+    return new Dataset(NO_ID);
+};
+
+MachineLearningTrainer.prototype.createFileDataset = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'train',
+            type: types_.FUNCTION
+        },
+        {
+            name: 'validate',
+            type: types_.FUNCTION,
+            optional: true,
+            nullable: true
+        },
+        {
+            name: 'test',
+            type: types_.FUNCTION,
+            optional: true,
+            nullable: true
+        }
+    ]);
+    ValidateDatasetPaths(args.train, args.validate.args.test);
+
+    // TODO
+    return new Dataset(NO_ID);
+};
+
+MachineLearningTrainer.prototype.createOptimizer = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'optimizer',
+            type: types_.ENUM,
+            values: Object.values(OptimizerType),
+            optional: false
+        }
+    ]);
+
+    // TODO
+    return new Optimizer(NO_ID);
+};
+
+MachineLearningTrainer.prototype.constructModelWithConfiguration = function() {
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'configPath',
+            type: types_.STRING,
+            optional: true
+        }
+    ]);
+    if (args.has.configPath) {
+        try {
+            args.configPath = tizen.filesystem.toURI(args.configPath);
+        } catch (e) {
+            throw new WebAPIException(WebAPIException.NOT_FOUND_ERR, 'Path is invalid');
+        }
+    }
+
+    // TODO
+    return new Model(NO_ID);
+};
+
+MachineLearningTrainer.prototype.constructModel = function() {
+    // TODO
+    return new Model(NO_ID);
+};