learning_model 1.0.50 → 1.0.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -105,6 +105,23 @@
105
105
  * =============================================================================
106
106
  */
107
107
 
108
+ /**
109
+ * @license
110
+ * Copyright 2019 Google LLC. All Rights Reserved.
111
+ * Licensed under the Apache License, Version 2.0 (the 'License');
112
+ * you may not use this file except in compliance with the License.
113
+ * You may obtain a copy of the License at
114
+ *
115
+ * http://www.apache.org/licenses/LICENSE-2.0
116
+ *
117
+ * Unless required by applicable law or agreed to in writing, software
118
+ * distributed under the License is distributed on an 'AS IS' BASIS,
119
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120
+ * See the License for the specific language governing permissions and
121
+ * limitations under the License.
122
+ * =============================================================================
123
+ */
124
+
108
125
  /**
109
126
  * @license
110
127
  * Copyright 2020 Google Inc. All Rights Reserved.
@@ -1,4 +1,5 @@
1
1
  import * as tf from '@tensorflow/tfjs';
2
+ import '@tensorflow/tfjs-backend-wasm';
2
3
  import { io } from '@tensorflow/tfjs-core';
3
4
  import LearningInterface from './base';
4
5
  declare class LearningMobilenet implements LearningInterface {
@@ -39,6 +40,8 @@ declare class LearningMobilenet implements LearningInterface {
39
40
  private _convertToTfDataset;
40
41
  addData(label: string, data: any): Promise<void>;
41
42
  init(): Promise<void>;
43
+ private setupBackend;
44
+ private checkWasmSupport;
42
45
  train(): Promise<tf.History>;
43
46
  infer(data: any): Promise<Map<string, number>>;
44
47
  saveModel(handlerOrURL: io.IOHandler | string, config?: io.SaveConfig): Promise<void>;
@@ -38,6 +38,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
38
38
  };
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
40
  const tf = __importStar(require("@tensorflow/tfjs"));
41
+ require("@tensorflow/tfjs-backend-wasm"); // WebAssembly 백엔드 추가
41
42
  const tfjs_1 = require("@tensorflow/tfjs");
42
43
  const tf_1 = require("../utils/tf");
43
44
  const canvas_1 = require("../utils/canvas");
@@ -172,6 +173,8 @@ class LearningMobilenet {
172
173
  init() {
173
174
  return __awaiter(this, void 0, void 0, function* () {
174
175
  try {
176
+ console.log('init call');
177
+ yield this.setupBackend();
175
178
  this.mobilenetModule = yield (0, tf_1.loadModel)();
176
179
  }
177
180
  catch (error) {
@@ -180,6 +183,34 @@ class LearningMobilenet {
180
183
  }
181
184
  });
182
185
  }
186
+ setupBackend() {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ const isWasmSupported = yield this.checkWasmSupport();
189
+ if (isWasmSupported) {
190
+ yield tf.setBackend('wasm');
191
+ yield tf.ready();
192
+ console.log('Backend is set to WebAssembly');
193
+ }
194
+ else {
195
+ yield tf.setBackend('cpu');
196
+ yield tf.ready();
197
+ console.log('Backend is set to CPU');
198
+ }
199
+ });
200
+ }
201
+ checkWasmSupport() {
202
+ return __awaiter(this, void 0, void 0, function* () {
203
+ try {
204
+ yield tf.setBackend('wasm');
205
+ yield tf.ready();
206
+ return true;
207
+ }
208
+ catch (error) {
209
+ console.warn('WASM backend is not supported in this environment.', error);
210
+ return false;
211
+ }
212
+ });
213
+ }
183
214
  // 모델 학습 처리
184
215
  train() {
185
216
  return __awaiter(this, void 0, void 0, function* () {
@@ -1,4 +1,5 @@
1
1
  import * as tf from '@tensorflow/tfjs';
2
+ import '@tensorflow/tfjs-backend-wasm';
2
3
  import { io } from '@tensorflow/tfjs-core';
3
4
  import LearningInterface from './base';
4
5
  declare class LearningMobilenet implements LearningInterface {
@@ -39,6 +40,8 @@ declare class LearningMobilenet implements LearningInterface {
39
40
  private _convertToTfDataset;
40
41
  addData(label: string, data: any): Promise<void>;
41
42
  init(): Promise<void>;
43
+ private setupBackend;
44
+ private checkWasmSupport;
42
45
  train(): Promise<tf.History>;
43
46
  infer(data: any): Promise<Map<string, number>>;
44
47
  saveModel(handlerOrURL: io.IOHandler | string, config?: io.SaveConfig): Promise<void>;
@@ -5,6 +5,7 @@
5
5
  ///////////////////////////////////////////////////////////////////////////
6
6
 
7
7
  import * as tf from '@tensorflow/tfjs';
8
+ import '@tensorflow/tfjs-backend-wasm'; // WebAssembly 백엔드 추가
8
9
  import { dispose } from '@tensorflow/tfjs';
9
10
  import { io } from '@tensorflow/tfjs-core';
10
11
  import LearningInterface from './base';
@@ -188,6 +189,8 @@ class LearningMobilenet implements LearningInterface {
188
189
 
189
190
  public async init() {
190
191
  try {
192
+ console.log('init call')
193
+ await this.setupBackend();
191
194
  this.mobilenetModule = await loadModel();
192
195
  } catch(error) {
193
196
  console.log('init Error', error);
@@ -195,6 +198,29 @@ class LearningMobilenet implements LearningInterface {
195
198
  }
196
199
  }
197
200
 
201
+ private async setupBackend() {
202
+ const isWasmSupported = await this.checkWasmSupport();
203
+ if (isWasmSupported) {
204
+ await tf.setBackend('wasm');
205
+ await tf.ready();
206
+ console.log('Backend is set to WebAssembly');
207
+ } else {
208
+ await tf.setBackend('cpu');
209
+ await tf.ready();
210
+ console.log('Backend is set to CPU');
211
+ }
212
+ }
213
+
214
+ private async checkWasmSupport(): Promise<boolean> {
215
+ try {
216
+ await tf.setBackend('wasm');
217
+ await tf.ready();
218
+ return true;
219
+ } catch (error) {
220
+ console.warn('WASM backend is not supported in this environment.', error);
221
+ return false;
222
+ }
223
+ }
198
224
 
199
225
  // 모델 학습 처리
200
226
  public async train(): Promise<tf.History> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "learning_model",
3
- "version": "1.0.50",
3
+ "version": "1.0.51",
4
4
  "description": "learning model develop",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,6 +20,7 @@
20
20
  "@tensorflow-models/mobilenet": "^2.1.0",
21
21
  "@tensorflow/tfjs": "^4.6.0",
22
22
  "@tensorflow/tfjs-layers": "^4.6.0",
23
+ "@tensorflow/tfjs-backend-wasm": "^4.20.0",
23
24
  "canvas": "^2.11.2",
24
25
  "learning_model": "^1.0.0"
25
26
  },