2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var MachineLearningTrainer = function() {};
20 OPTIMIZER_ADAM: 'OPTIMIZER_ADAM',
21 OPTIMIZER_SGD: 'OPTIMIZER_SGD',
22 OPTIMIZER_UNKNOWN: 'OPTIMIZER_UNKNOWN'
26 DATASET_GENERATOR: 'DATASET_GENERATOR',
27 DATASET_FILE: 'DATASET_FILE',
28 DATASET_UNKNOWN: 'DATASET_UNKNOWN'
35 LAYER_CONV2D: 'LAYER_CONV2D',
36 LAYER_POOLING2D: 'LAYER_POOLING2D',
37 LAYER_FLATTEN: 'LAYER_FLATTEN',
38 LAYER_ACTIVATION: 'LAYER_ACTIVATION',
39 LAYER_ADDITION: 'LAYER_ADDITION',
40 LAYER_CONCAT: 'LAYER_CONCAT',
41 LAYER_MULTIOUT: 'LAYER_MULTIOUT',
42 LAYER_LOSS: 'LAYER_LOSS',
43 LAYER_BACKBONE_NNSTREAMER: 'LAYER_BACKBONE_NNSTREAMER',
44 LAYER_BACKBONE_TFLITE: 'LAYER_BACKBONE_TFLITE',
45 LAYER_EMBEDDING: 'LAYER_EMBEDDING',
46 LAYER_RNN: 'LAYER_RNN',
47 LAYER_UNKNOWN: 'LAYER_UNKNOWN'
50 var VerbosityLevel = {
51 SUMMARY_MODEL: 'SUMMARY_MODEL',
52 SUMMARY_LAYER: 'SUMMARY_LAYER',
53 SUMMARY_TENSOR: 'SUMMARY_TENSOR'
56 var Layer = function(id) {
57 Object.defineProperties(this, {
70 _id: { value: id, writable: false, enumerable: false }
74 var ValidSetPropertyExceptions = [
80 Layer.prototype.setProperty = function() {
81 var args = validator_.validateArgs(arguments, [
92 if (!args.has.name || !args.has.value) {
93 throw new WebAPIException(
94 WebAPIException.TYPE_MISMATCH_ERR,
95 'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
105 var result = native_.callSync('MLTrainerLayerSetProperty', callArgs);
107 if (native_.isFailure(result)) {
108 throw native_.getErrorObjectAndValidate(
110 ValidSetPropertyExceptions,
116 var Optimizer = function(id) {
117 Object.defineProperties(this, {
124 _id: { value: id, writable: false, enumerable: false }
128 Optimizer.prototype.setProperty = function() {
129 var args = validator_.validateArgs(arguments, [
140 if (!args.has.name || !args.has.value) {
141 throw new WebAPIException(
142 WebAPIException.TYPE_MISMATCH_ERR,
143 'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
153 var result = native_.callSync('MLTrainerOptimizerSetProperty', callArgs);
155 if (native_.isFailure(result)) {
156 throw native_.getErrorObjectAndValidate(
158 ValidSetPropertyExceptions,
164 var Dataset = function(id) {
165 Object.defineProperties(this, {
172 _id: { value: id, writable: false, enumerable: false }
176 Dataset.prototype.setProperty = function() {
177 var args = validator_.validateArgs(arguments, [
188 if (!args.has.name || !args.has.value) {
189 throw new WebAPIException(
190 WebAPIException.TYPE_MISMATCH_ERR,
191 'Invalid parameter: ' + (args.has.name ? 'value' : 'name') + ' is undefined'
201 var result = native_.callSync('MLTrainerDatasetSetProperty', callArgs);
203 if (native_.isFailure(result)) {
204 throw native_.getErrorObjectAndValidate(
206 ValidSetPropertyExceptions,
212 var Model = function(id) {
213 Object.defineProperties(this, {
214 _id: { value: id, writable: false, enumerable: false }
218 function ValidateCompileOptions(options) {
222 Model.prototype.compile = function() {
223 var args = validator.validateArgs(arguments, [
226 type: validator.Types.DICTIONARY,
231 if (args.has.options) {
232 ValidateCompileOptions(args.options);
237 function ValidateRunOptions(options) {
241 Model.prototype.run = function() {
242 var args = validator.validateArgs(arguments, [
245 type: validator.Types.DICTIONARY,
250 if (args.has.options) {
251 ValidateRunOptions(args.options);
256 Model.prototype.summarize = function() {
257 var args = validator_.validateArgs(arguments, [
261 values: Object.values(VerbosityLevel),
268 Model.prototype.addLayer = function() {
269 var args = validator_.validateArgs(arguments, [
272 type: types_.PLATFORM_OBJECT,
279 Model.prototype.setDataset = function() {
280 var args = validator_.validateArgs(arguments, [
283 type: types_.PLATFORM_OBJECT,
290 Model.prototype.setOptimizer = function() {
291 var args = validator_.validateArgs(arguments, [
294 type: types_.PLATFORM_OBJECT,
301 var ValidCreateLayerExceptions = ['NotSupportedError', 'TypeMismatchError', 'AbortError'];
304 MachineLearningTrainer.prototype.createLayer = function() {
305 var args = validator_.validateArgs(arguments, [
309 values: Object.values(LayerType),
315 return new Layer(NO_ID);
318 function ValidateDatasetPaths(train, validate, test) {
322 MachineLearningTrainer.prototype.createGeneratorDataset = function() {
323 var args = validator_.validateArgs(arguments, [
341 ValidateDatasetPaths(args.train, args.validate.args.test);
344 return new Dataset(NO_ID);
347 MachineLearningTrainer.prototype.createFileDataset = function() {
348 var args = validator_.validateArgs(arguments, [
351 type: types_.FUNCTION
355 type: types_.FUNCTION,
361 type: types_.FUNCTION,
366 ValidateDatasetPaths(args.train, args.validate.args.test);
369 return new Dataset(NO_ID);
372 MachineLearningTrainer.prototype.createOptimizer = function() {
373 var args = validator_.validateArgs(arguments, [
377 values: Object.values(OptimizerType),
383 return new Optimizer(NO_ID);
386 MachineLearningTrainer.prototype.constructModelWithConfiguration = function() {
387 var args = validator_.validateArgs(arguments, [
394 if (args.has.configPath) {
396 args.configPath = tizen.filesystem.toURI(args.configPath);
398 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR, 'Path is invalid');
403 return new Model(NO_ID);
406 MachineLearningTrainer.prototype.constructModel = function() {
408 return new Model(NO_ID);