learning_model 1.0.24 → 1.0.26

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.
@@ -12,7 +12,7 @@ interface LearningInterface {
12
12
  addData(label: string, data: any): Promise<void>;
13
13
  train(): Promise<tf.History>;
14
14
  infer(data: any): Promise<any>;
15
- saveModel(): void;
15
+ saveModel(): Promise<void>;
16
16
  running(): boolean;
17
17
  ready(): boolean;
18
18
  }
@@ -5,21 +5,24 @@ declare class LearningImage implements LearningInterface {
5
5
  epochs: number;
6
6
  batchSize: number;
7
7
  learningRate: number;
8
+ validateRate: number;
8
9
  labels: string[];
9
10
  isRunning: boolean;
10
11
  isReady: boolean;
12
+ isTrainedDone: boolean;
11
13
  limitSize: number;
12
14
  trainImages: tf.Tensor3D[];
13
15
  readonly MOBILE_NET_INPUT_WIDTH = 224;
14
16
  readonly MOBILE_NET_INPUT_HEIGHT = 224;
15
17
  readonly MOBILE_NET_INPUT_CHANNEL = 3;
16
18
  readonly IMAGE_NORMALIZATION_FACTOR = 255;
17
- constructor({ epochs, batchSize, limitSize, learningRate, }?: {
19
+ constructor({ epochs, batchSize, limitSize, learningRate, validateRate, }?: {
18
20
  modelURL?: string;
19
21
  epochs?: number;
20
22
  batchSize?: number;
21
23
  limitSize?: number;
22
24
  learningRate?: number;
25
+ validateRate?: number;
23
26
  });
24
27
  onProgress: (progress: number) => void;
25
28
  onLoss: (loss: number) => void;
@@ -29,7 +32,7 @@ declare class LearningImage implements LearningInterface {
29
32
  addData(label: string, data: any): Promise<void>;
30
33
  train(): Promise<tf.History>;
31
34
  infer(data: any): Promise<Map<string, number>>;
32
- saveModel(): void;
35
+ saveModel(): Promise<void>;
33
36
  running(): boolean;
34
37
  ready(): boolean;
35
38
  private _preprocessedTargetData;
@@ -34,7 +34,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  const tf = __importStar(require("@tensorflow/tfjs"));
36
36
  class LearningImage {
37
- constructor({ epochs = 10, batchSize = 16, limitSize = 2, learningRate = 0.001, } = {}) {
37
+ constructor({ epochs = 10, batchSize = 16, limitSize = 2, learningRate = 0.001, validateRate = 0.2, } = {}) {
38
38
  this.trainImages = [];
39
39
  this.MOBILE_NET_INPUT_WIDTH = 224;
40
40
  this.MOBILE_NET_INPUT_HEIGHT = 224;
@@ -49,9 +49,11 @@ class LearningImage {
49
49
  this.epochs = epochs;
50
50
  this.batchSize = batchSize;
51
51
  this.learningRate = learningRate;
52
+ this.validateRate = validateRate;
52
53
  this.labels = [];
53
54
  this.isRunning = false;
54
55
  this.isReady = false;
56
+ this.isTrainedDone = false;
55
57
  this.limitSize = 2;
56
58
  }
57
59
  // 학습 데이타 등록
@@ -82,10 +84,12 @@ class LearningImage {
82
84
  // 콜백 정의
83
85
  const customCallback = {
84
86
  onTrainBegin: (log) => {
87
+ this.isTrainedDone = false;
85
88
  this.onTrainBegin(log);
86
89
  console.log('Training has started.');
87
90
  },
88
91
  onTrainEnd: (log) => {
92
+ this.isTrainedDone = true;
89
93
  this.onTrainEnd(log);
90
94
  console.log('Training has ended.');
91
95
  this.isRunning = false;
@@ -120,6 +124,7 @@ class LearningImage {
120
124
  const history = yield this.model.fit(inputData, targetData, {
121
125
  epochs: this.epochs,
122
126
  batchSize: this.batchSize,
127
+ validationSplit: this.validateRate,
123
128
  callbacks: customCallback
124
129
  });
125
130
  console.log('Model training completed', history);
@@ -166,7 +171,14 @@ class LearningImage {
166
171
  }
167
172
  // 모델 저장
168
173
  saveModel() {
169
- console.log('saved model');
174
+ var _a;
175
+ return __awaiter(this, void 0, void 0, function* () {
176
+ console.log('saved model');
177
+ if (!this.isTrainedDone) {
178
+ return Promise.reject(new Error('Train is not done status'));
179
+ }
180
+ yield ((_a = this.model) === null || _a === void 0 ? void 0 : _a.save('localstorage://my-model'));
181
+ });
170
182
  }
171
183
  // 진행중 여부
172
184
  running() {
@@ -5,10 +5,12 @@ declare class LearningMobilenetImage implements LearningInterface {
5
5
  epochs: number;
6
6
  batchSize: number;
7
7
  learningRate: number;
8
+ validateRate: number;
8
9
  labels: string[];
9
10
  modelURL: string;
10
11
  isRunning: boolean;
11
12
  isReady: boolean;
13
+ isTrainedDone: boolean;
12
14
  limitSize: number;
13
15
  trainImages: tf.Tensor3D[];
14
16
  readonly MOBILE_NET_INPUT_WIDTH = 224;
@@ -16,12 +18,13 @@ declare class LearningMobilenetImage implements LearningInterface {
16
18
  readonly MOBILE_NET_INPUT_CHANNEL = 3;
17
19
  readonly IMAGE_NORMALIZATION_FACTOR = 255;
18
20
  constructor({ modelURL, // 디폴트 mobilenet 이미지
19
- epochs, batchSize, limitSize, learningRate, }?: {
21
+ epochs, batchSize, limitSize, learningRate, validateRate, }?: {
20
22
  modelURL?: string;
21
23
  epochs?: number;
22
24
  batchSize?: number;
23
25
  limitSize?: number;
24
26
  learningRate?: number;
27
+ validateRate?: number;
25
28
  });
26
29
  onProgress: (progress: number) => void;
27
30
  onLoss: (loss: number) => void;
@@ -31,7 +34,7 @@ declare class LearningMobilenetImage implements LearningInterface {
31
34
  addData(label: string, data: any): Promise<void>;
32
35
  train(): Promise<tf.History>;
33
36
  infer(data: any): Promise<Map<string, number>>;
34
- saveModel(): void;
37
+ saveModel(): Promise<void>;
35
38
  running(): boolean;
36
39
  ready(): boolean;
37
40
  private _preprocessedTargetData;
@@ -41,7 +41,7 @@ const tf = __importStar(require("@tensorflow/tfjs"));
41
41
  const util_1 = require("./util");
42
42
  class LearningMobilenetImage {
43
43
  constructor({ modelURL = 'https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json', // 디폴트 mobilenet 이미지
44
- epochs = 10, batchSize = 16, limitSize = 2, learningRate = 0.001, } = {}) {
44
+ epochs = 10, batchSize = 16, limitSize = 2, learningRate = 0.001, validateRate = 0.2, } = {}) {
45
45
  this.trainImages = [];
46
46
  this.MOBILE_NET_INPUT_WIDTH = 224;
47
47
  this.MOBILE_NET_INPUT_HEIGHT = 224;
@@ -57,10 +57,12 @@ class LearningMobilenetImage {
57
57
  this.epochs = epochs;
58
58
  this.batchSize = batchSize;
59
59
  this.learningRate = learningRate;
60
+ this.validateRate = validateRate;
60
61
  this.labels = [];
61
62
  this.modelURL = modelURL;
62
63
  this.isRunning = false;
63
64
  this.isReady = false;
65
+ this.isTrainedDone = false;
64
66
  this.limitSize = limitSize;
65
67
  }
66
68
  // 학습 데이타 등록
@@ -91,10 +93,12 @@ class LearningMobilenetImage {
91
93
  // 콜백 정의
92
94
  const customCallback = {
93
95
  onTrainBegin: (log) => {
96
+ this.isTrainedDone = false;
94
97
  this.onTrainBegin(log);
95
98
  console.log('Training has started.');
96
99
  },
97
100
  onTrainEnd: (log) => {
101
+ this.isTrainedDone = true;
98
102
  this.onTrainEnd(log);
99
103
  console.log('Training has ended.');
100
104
  this.isRunning = false;
@@ -127,6 +131,7 @@ class LearningMobilenetImage {
127
131
  const history = yield this.model.fit(inputData, targetData, {
128
132
  epochs: this.epochs,
129
133
  batchSize: this.batchSize,
134
+ validationSplit: this.validateRate,
130
135
  callbacks: customCallback
131
136
  });
132
137
  console.log('Model training completed', history);
@@ -173,7 +178,14 @@ class LearningMobilenetImage {
173
178
  }
174
179
  // 모델 저장
175
180
  saveModel() {
176
- console.log('saved model');
181
+ var _a;
182
+ return __awaiter(this, void 0, void 0, function* () {
183
+ console.log('saved model');
184
+ if (!this.isTrainedDone) {
185
+ return Promise.reject(new Error('Train is not done status'));
186
+ }
187
+ yield ((_a = this.model) === null || _a === void 0 ? void 0 : _a.save('localstorage://my-model'));
188
+ });
177
189
  }
178
190
  // 진행중 여부
179
191
  running() {
@@ -12,7 +12,7 @@ interface LearningInterface {
12
12
  addData(label: string, data: any): Promise<void>;
13
13
  train(): Promise<tf.History>;
14
14
  infer(data: any): Promise<any>;
15
- saveModel(): void;
15
+ saveModel(): Promise<void>;
16
16
  running(): boolean;
17
17
  ready(): boolean;
18
18
  }
@@ -5,21 +5,24 @@ declare class LearningImage implements LearningInterface {
5
5
  epochs: number;
6
6
  batchSize: number;
7
7
  learningRate: number;
8
+ validateRate: number;
8
9
  labels: string[];
9
10
  isRunning: boolean;
10
11
  isReady: boolean;
12
+ isTrainedDone: boolean;
11
13
  limitSize: number;
12
14
  trainImages: tf.Tensor3D[];
13
15
  readonly MOBILE_NET_INPUT_WIDTH = 224;
14
16
  readonly MOBILE_NET_INPUT_HEIGHT = 224;
15
17
  readonly MOBILE_NET_INPUT_CHANNEL = 3;
16
18
  readonly IMAGE_NORMALIZATION_FACTOR = 255;
17
- constructor({ epochs, batchSize, limitSize, learningRate, }?: {
19
+ constructor({ epochs, batchSize, limitSize, learningRate, validateRate, }?: {
18
20
  modelURL?: string;
19
21
  epochs?: number;
20
22
  batchSize?: number;
21
23
  limitSize?: number;
22
24
  learningRate?: number;
25
+ validateRate?: number;
23
26
  });
24
27
  onProgress: (progress: number) => void;
25
28
  onLoss: (loss: number) => void;
@@ -29,7 +32,7 @@ declare class LearningImage implements LearningInterface {
29
32
  addData(label: string, data: any): Promise<void>;
30
33
  train(): Promise<tf.History>;
31
34
  infer(data: any): Promise<Map<string, number>>;
32
- saveModel(): void;
35
+ saveModel(): Promise<void>;
33
36
  running(): boolean;
34
37
  ready(): boolean;
35
38
  private _preprocessedTargetData;
@@ -5,10 +5,12 @@ declare class LearningMobilenetImage implements LearningInterface {
5
5
  epochs: number;
6
6
  batchSize: number;
7
7
  learningRate: number;
8
+ validateRate: number;
8
9
  labels: string[];
9
10
  modelURL: string;
10
11
  isRunning: boolean;
11
12
  isReady: boolean;
13
+ isTrainedDone: boolean;
12
14
  limitSize: number;
13
15
  trainImages: tf.Tensor3D[];
14
16
  readonly MOBILE_NET_INPUT_WIDTH = 224;
@@ -16,12 +18,13 @@ declare class LearningMobilenetImage implements LearningInterface {
16
18
  readonly MOBILE_NET_INPUT_CHANNEL = 3;
17
19
  readonly IMAGE_NORMALIZATION_FACTOR = 255;
18
20
  constructor({ modelURL, // 디폴트 mobilenet 이미지
19
- epochs, batchSize, limitSize, learningRate, }?: {
21
+ epochs, batchSize, limitSize, learningRate, validateRate, }?: {
20
22
  modelURL?: string;
21
23
  epochs?: number;
22
24
  batchSize?: number;
23
25
  limitSize?: number;
24
26
  learningRate?: number;
27
+ validateRate?: number;
25
28
  });
26
29
  onProgress: (progress: number) => void;
27
30
  onLoss: (loss: number) => void;
@@ -31,7 +34,7 @@ declare class LearningMobilenetImage implements LearningInterface {
31
34
  addData(label: string, data: any): Promise<void>;
32
35
  train(): Promise<tf.History>;
33
36
  infer(data: any): Promise<Map<string, number>>;
34
- saveModel(): void;
37
+ saveModel(): Promise<void>;
35
38
  running(): boolean;
36
39
  ready(): boolean;
37
40
  private _preprocessedTargetData;
@@ -36,7 +36,7 @@ interface LearningInterface {
36
36
  infer(data: any): Promise<any>;
37
37
 
38
38
  // 모델 저장
39
- saveModel(): void;
39
+ saveModel(): Promise<void>;
40
40
 
41
41
  // 학습 진행 여부
42
42
  running(): boolean;
@@ -6,9 +6,11 @@ class LearningImage implements LearningInterface {
6
6
  epochs: number;
7
7
  batchSize: number;
8
8
  learningRate: number;
9
+ validateRate: number;
9
10
  labels: string[];
10
11
  isRunning: boolean;
11
12
  isReady: boolean;
13
+ isTrainedDone: boolean;
12
14
  limitSize: number;
13
15
  trainImages: tf.Tensor3D[] = [];
14
16
 
@@ -22,14 +24,17 @@ class LearningImage implements LearningInterface {
22
24
  batchSize = 16,
23
25
  limitSize = 2,
24
26
  learningRate = 0.001,
25
- }: { modelURL?: string, epochs?: number, batchSize?: number, limitSize?: number, learningRate?: number} = {}) {
27
+ validateRate = 0.2,
28
+ }: { modelURL?: string, epochs?: number, batchSize?: number, limitSize?: number, learningRate?: number, validateRate?: number} = {}) {
26
29
  this.model = null;
27
30
  this.epochs = epochs;
28
31
  this.batchSize = batchSize;
29
32
  this.learningRate = learningRate;
33
+ this.validateRate = validateRate;
30
34
  this.labels = [];
31
35
  this.isRunning = false;
32
36
  this.isReady = false;
37
+ this.isTrainedDone = false;
33
38
  this.limitSize = 2;
34
39
  }
35
40
 
@@ -69,10 +74,12 @@ class LearningImage implements LearningInterface {
69
74
  // 콜백 정의
70
75
  const customCallback = {
71
76
  onTrainBegin: (log: any) => {
77
+ this.isTrainedDone = false;
72
78
  this.onTrainBegin(log);
73
79
  console.log('Training has started.');
74
80
  },
75
81
  onTrainEnd: (log: any) => {
82
+ this.isTrainedDone = true;
76
83
  this.onTrainEnd(log);
77
84
  console.log('Training has ended.');
78
85
  this.isRunning = false;
@@ -108,6 +115,7 @@ class LearningImage implements LearningInterface {
108
115
  const history = await this.model.fit(inputData, targetData, {
109
116
  epochs: this.epochs,
110
117
  batchSize: this.batchSize,
118
+ validationSplit: this.validateRate, // 검증 데이터의 비율 설정
111
119
  callbacks: customCallback
112
120
  });
113
121
  console.log('Model training completed', history);
@@ -151,8 +159,12 @@ class LearningImage implements LearningInterface {
151
159
 
152
160
 
153
161
  // 모델 저장
154
- public saveModel(): void {
162
+ public async saveModel(): Promise<void> {
155
163
  console.log('saved model');
164
+ if (!this.isTrainedDone) {
165
+ return Promise.reject(new Error('Train is not done status'));
166
+ }
167
+ await this.model?.save('localstorage://my-model');
156
168
  }
157
169
 
158
170
 
@@ -13,10 +13,12 @@ class LearningMobilenetImage implements LearningInterface {
13
13
  epochs: number;
14
14
  batchSize: number;
15
15
  learningRate: number;
16
+ validateRate: number;
16
17
  labels: string[];
17
18
  modelURL: string;
18
19
  isRunning: boolean;
19
20
  isReady: boolean;
21
+ isTrainedDone: boolean;
20
22
  limitSize: number;
21
23
  trainImages: tf.Tensor3D[] = [];
22
24
 
@@ -31,15 +33,18 @@ class LearningMobilenetImage implements LearningInterface {
31
33
  batchSize = 16,
32
34
  limitSize = 2,
33
35
  learningRate = 0.001,
34
- }: { modelURL?: string, epochs?: number, batchSize?: number, limitSize?: number, learningRate?: number} = {}) {
36
+ validateRate = 0.2,
37
+ }: { modelURL?: string, epochs?: number, batchSize?: number, limitSize?: number, learningRate?: number, validateRate?: number} = {}) {
35
38
  this.model = null;
36
39
  this.epochs = epochs;
37
40
  this.batchSize = batchSize;
38
41
  this.learningRate = learningRate;
42
+ this.validateRate = validateRate;
39
43
  this.labels = [];
40
44
  this.modelURL = modelURL;
41
45
  this.isRunning = false;
42
46
  this.isReady = false;
47
+ this.isTrainedDone = false;
43
48
  this.limitSize = limitSize;
44
49
  }
45
50
 
@@ -81,10 +86,12 @@ class LearningMobilenetImage implements LearningInterface {
81
86
  // 콜백 정의
82
87
  const customCallback = {
83
88
  onTrainBegin: (log: any) => {
89
+ this.isTrainedDone = false;
84
90
  this.onTrainBegin(log);
85
91
  console.log('Training has started.');
86
92
  },
87
93
  onTrainEnd: (log: any) => {
94
+ this.isTrainedDone = true;
88
95
  this.onTrainEnd(log);
89
96
  console.log('Training has ended.');
90
97
  this.isRunning = false;
@@ -118,6 +125,7 @@ class LearningMobilenetImage implements LearningInterface {
118
125
  const history = await this.model.fit(inputData, targetData, {
119
126
  epochs: this.epochs,
120
127
  batchSize: this.batchSize,
128
+ validationSplit: this.validateRate, // 검증 데이터의 비율 설정
121
129
  callbacks: customCallback
122
130
  });
123
131
  console.log('Model training completed', history);
@@ -158,14 +166,19 @@ class LearningMobilenetImage implements LearningInterface {
158
166
  throw error;
159
167
  }
160
168
  }
161
-
162
169
 
170
+
163
171
  // 모델 저장
164
- public saveModel(): void {
172
+ public async saveModel(): Promise<void> {
165
173
  console.log('saved model');
174
+ if (!this.isTrainedDone) {
175
+ return Promise.reject(new Error('Train is not done status'));
176
+ }
177
+ await this.model?.save('localstorage://my-model');
166
178
  }
167
179
 
168
180
 
181
+
169
182
  // 진행중 여부
170
183
  public running(): boolean {
171
184
  return this.isRunning;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "learning_model",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "learning model develop",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",