learning_model 1.0.3 → 1.0.5

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.
@@ -36,48 +36,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
36
36
  step((generator = generator.apply(thisArg, _arguments || [])).next());
37
37
  });
38
38
  };
39
- var __generator = (this && this.__generator) || function (thisArg, body) {
40
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
41
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
42
- function verb(n) { return function (v) { return step([n, v]); }; }
43
- function step(op) {
44
- if (f) throw new TypeError("Generator is already executing.");
45
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
46
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
47
- if (y = 0, t) op = [op[0] & 2, t.value];
48
- switch (op[0]) {
49
- case 0: case 1: t = op; break;
50
- case 4: _.label++; return { value: op[1], done: false };
51
- case 5: _.label++; y = op[1]; op = [0]; continue;
52
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
53
- default:
54
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
55
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
56
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
57
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
58
- if (t[2]) _.ops.pop();
59
- _.trys.pop(); continue;
60
- }
61
- op = body.call(thisArg, _);
62
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
63
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
64
- }
65
- };
66
39
  Object.defineProperty(exports, "__esModule", { value: true });
67
- var tf = __importStar(require("@tensorflow/tfjs"));
68
- var LearningMobilenetImage = /** @class */ (function () {
69
- function LearningMobilenetImage(_a) {
70
- var _b = _a === void 0 ? {} : _a, _c = _b.modelURL, modelURL = _c === void 0 ? 'https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json' : _c, // 디폴트 mobilenet 이미지
71
- _d = _b.epochs, // 디폴트 mobilenet 이미지
72
- epochs = _d === void 0 ? 10 : _d, _e = _b.batchSize, batchSize = _e === void 0 ? 16 : _e;
40
+ const tf = __importStar(require("@tensorflow/tfjs"));
41
+ class LearningMobilenetImage {
42
+ constructor({ modelURL = 'https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json', // 디폴트 mobilenet 이미지
43
+ epochs = 10, batchSize = 16, limitSize = 2, } = {}) {
73
44
  this.trainImages = [];
74
45
  this.MOBILE_NET_INPUT_WIDTH = 224;
75
46
  this.MOBILE_NET_INPUT_HEIGHT = 224;
76
47
  this.MOBILE_NET_INPUT_CHANNEL = 3;
77
48
  this.IMAGE_NORMALIZATION_FACTOR = 255.0;
78
49
  // 진행 상태를 나타내는 이벤트를 정의합니다.
79
- this.onProgress = function () { };
80
- this.onLoss = function () { };
50
+ this.onProgress = () => { };
51
+ this.onLoss = () => { };
52
+ this.onTrainBegin = () => { };
53
+ this.onTrainEnd = () => { };
81
54
  this.model = null;
82
55
  this.epochs = epochs;
83
56
  this.batchSize = batchSize;
@@ -85,12 +58,12 @@ var LearningMobilenetImage = /** @class */ (function () {
85
58
  this.modelURL = modelURL;
86
59
  this.isRunning = false;
87
60
  this.isReady = false;
88
- this.limitSize = 2;
61
+ this.limitSize = limitSize;
89
62
  }
90
63
  // 학습 데이타 등록
91
- LearningMobilenetImage.prototype.addData = function (label, data) {
64
+ addData(label, data) {
92
65
  try {
93
- var tensor = tf.browser.fromPixels(data);
66
+ const tensor = tf.browser.fromPixels(data);
94
67
  console.log('addData', tensor);
95
68
  this.trainImages.push(tensor);
96
69
  this.labels.push(label);
@@ -103,162 +76,139 @@ var LearningMobilenetImage = /** @class */ (function () {
103
76
  console.error('Model training failed', error);
104
77
  throw error;
105
78
  }
106
- };
79
+ }
107
80
  // 모델 학습 처리
108
- LearningMobilenetImage.prototype.train = function () {
109
- return __awaiter(this, void 0, void 0, function () {
110
- var customCallback, _a, inputData, targetData, history_1, error_1;
111
- var _this = this;
112
- return __generator(this, function (_b) {
113
- switch (_b.label) {
114
- case 0:
115
- if (this.isRunning) {
116
- return [2 /*return*/, Promise.reject(new Error('Training is already in progress.'))];
117
- }
118
- customCallback = {
119
- onTrainBegin: function () {
120
- console.log('Training has started.');
121
- },
122
- onTrainEnd: function () {
123
- console.log('Training has ended.');
124
- _this.isRunning = false;
125
- },
126
- onBatchBegin: function (batch, logs) {
127
- console.log("Batch ".concat(batch, " is starting."));
128
- },
129
- onBatchEnd: function (batch, logs) {
130
- console.log("Batch ".concat(batch, " has ended."));
131
- },
132
- onEpochBegin: function (epoch, logs) {
133
- var progress = Math.floor(((epoch + 1) / _this.epochs) * 100);
134
- _this.onProgress(progress);
135
- console.log("Epoch ".concat(epoch + 1, " is starting."));
136
- },
137
- onEpochEnd: function (epoch, logs) {
138
- console.log("Epoch ".concat(epoch + 1, " has ended."));
139
- console.log('Loss:', logs.loss);
140
- }
141
- };
142
- _b.label = 1;
143
- case 1:
144
- _b.trys.push([1, 4, , 5]);
145
- this.isRunning = true;
146
- if (this.labels.length < this.limitSize) {
147
- return [2 /*return*/, Promise.reject(new Error('Please train Data need over 2 data length'))];
148
- }
149
- _a = this;
150
- return [4 /*yield*/, this._createModel(this.labels.length)];
151
- case 2:
152
- _a.model = _b.sent();
153
- inputData = this._preprocessedInputData(this.model);
154
- targetData = this._preprocessedTargetData();
155
- return [4 /*yield*/, this.model.fit(inputData, targetData, {
156
- epochs: this.epochs,
157
- batchSize: this.batchSize,
158
- callbacks: customCallback
159
- })];
160
- case 3:
161
- history_1 = _b.sent();
162
- console.log('Model training completed', history_1);
163
- return [2 /*return*/, history_1];
164
- case 4:
165
- error_1 = _b.sent();
166
- this.isRunning = false;
167
- console.error('Model training failed', error_1);
168
- throw error_1;
169
- case 5: return [2 /*return*/];
81
+ train() {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ if (this.isRunning) {
84
+ return Promise.reject(new Error('Training is already in progress.'));
85
+ }
86
+ // 콜백 정의
87
+ const customCallback = {
88
+ onTrainBegin: (log) => {
89
+ this.onTrainBegin(log);
90
+ console.log('Training has started.');
91
+ },
92
+ onTrainEnd: (log) => {
93
+ this.onTrainEnd(log);
94
+ console.log('Training has ended.');
95
+ this.isRunning = false;
96
+ },
97
+ onBatchBegin: (batch, logs) => {
98
+ console.log(`Batch ${batch} is starting.`);
99
+ },
100
+ onBatchEnd: (batch, logs) => {
101
+ console.log(`Batch ${batch} has ended.`);
102
+ },
103
+ onEpochBegin: (epoch, logs) => {
104
+ //const progress = Math.floor(((epoch + 1) / this.epochs) * 100);
105
+ this.onProgress(epoch + 1);
106
+ console.log(`Epoch ${epoch + 1} is starting.`);
107
+ },
108
+ onEpochEnd: (epoch, logs) => {
109
+ console.log(`Epoch ${epoch + 1} has ended.`);
110
+ console.log('Loss:', logs.loss);
111
+ }
112
+ };
113
+ try {
114
+ this.isRunning = true;
115
+ if (this.labels.length < this.limitSize) {
116
+ return Promise.reject(new Error('Please train Data need over 2 data length'));
170
117
  }
171
- });
118
+ this.model = yield this._createModel(this.labels.length);
119
+ const inputData = this._preprocessedInputData(this.model);
120
+ const targetData = this._preprocessedTargetData();
121
+ const history = yield this.model.fit(inputData, targetData, {
122
+ epochs: this.epochs,
123
+ batchSize: this.batchSize,
124
+ callbacks: customCallback
125
+ });
126
+ console.log('Model training completed', history);
127
+ return history;
128
+ }
129
+ catch (error) {
130
+ this.isRunning = false;
131
+ console.error('Model training failed', error);
132
+ throw error;
133
+ }
172
134
  });
173
- };
135
+ }
174
136
  // 추론하기
175
- LearningMobilenetImage.prototype.infer = function (data) {
176
- return __awaiter(this, void 0, void 0, function () {
177
- var tensor, resizedTensor, reshapedTensor, predictions, predictionsData, classProbabilities, i, className, probability, existingProbability, error_2;
178
- return __generator(this, function (_a) {
179
- switch (_a.label) {
180
- case 0:
181
- if (this.model === null) {
182
- throw new Error('Model is null');
183
- }
184
- _a.label = 1;
185
- case 1:
186
- _a.trys.push([1, 3, , 4]);
187
- tensor = tf.browser.fromPixels(data);
188
- resizedTensor = tf.image.resizeBilinear(tensor, [this.MOBILE_NET_INPUT_WIDTH, this.MOBILE_NET_INPUT_HEIGHT]);
189
- reshapedTensor = resizedTensor.expandDims(0);
190
- predictions = this.model.predict(reshapedTensor);
191
- return [4 /*yield*/, predictions.data()];
192
- case 2:
193
- predictionsData = _a.sent();
194
- classProbabilities = new Map();
195
- for (i = 0; i < predictionsData.length; i++) {
196
- className = this.labels[i];
197
- probability = predictionsData[i];
198
- existingProbability = classProbabilities.get(className);
199
- if (existingProbability !== undefined) {
200
- classProbabilities.set(className, existingProbability + probability);
201
- }
202
- else {
203
- classProbabilities.set(className, probability);
204
- }
205
- }
206
- console.log('Class Probabilities:', classProbabilities);
207
- return [2 /*return*/, classProbabilities];
208
- case 3:
209
- error_2 = _a.sent();
210
- throw error_2;
211
- case 4: return [2 /*return*/];
137
+ infer(data) {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ if (this.model === null) {
140
+ throw new Error('Model is null');
141
+ }
142
+ try {
143
+ const tensor = tf.browser.fromPixels(data);
144
+ const resizedTensor = tf.image.resizeBilinear(tensor, [this.MOBILE_NET_INPUT_WIDTH, this.MOBILE_NET_INPUT_HEIGHT]);
145
+ const reshapedTensor = resizedTensor.expandDims(0); // 배치 크기 1을 추가하여 4차원으로 변환
146
+ const predictions = this.model.predict(reshapedTensor);
147
+ const predictionsData = yield predictions.data(); // 예측 텐서의 데이터를 비동기로 가져옴
148
+ const classProbabilities = new Map(); // 클래스별 확률 누적값을 저장할 맵
149
+ for (let i = 0; i < predictionsData.length; i++) {
150
+ const className = this.labels[i]; // 클래스 이름
151
+ const probability = predictionsData[i];
152
+ const existingProbability = classProbabilities.get(className);
153
+ if (existingProbability !== undefined) {
154
+ classProbabilities.set(className, existingProbability + probability);
155
+ }
156
+ else {
157
+ classProbabilities.set(className, probability);
158
+ }
212
159
  }
213
- });
160
+ console.log('Class Probabilities:', classProbabilities);
161
+ return classProbabilities;
162
+ }
163
+ catch (error) {
164
+ throw error;
165
+ }
214
166
  });
215
- };
167
+ }
216
168
  // 모델 저장
217
- LearningMobilenetImage.prototype.saveModel = function () {
169
+ saveModel() {
218
170
  console.log('saved model');
219
- };
171
+ }
220
172
  // 진행중 여부
221
- LearningMobilenetImage.prototype.running = function () {
173
+ running() {
222
174
  return this.isRunning;
223
- };
224
- LearningMobilenetImage.prototype.ready = function () {
175
+ }
176
+ ready() {
225
177
  return this.isReady;
226
- };
178
+ }
227
179
  // target 라벨 데이타
228
- LearningMobilenetImage.prototype._preprocessedTargetData = function () {
229
- var _this = this;
180
+ _preprocessedTargetData() {
230
181
  // 라벨 unique 처리 & 배열 리턴
231
182
  console.log('uniqueLabels.length', this.labels, this.labels.length);
232
- var labelIndices = this.labels.map(function (label) { return _this.labels.indexOf(label); });
183
+ const labelIndices = this.labels.map((label) => this.labels.indexOf(label));
233
184
  console.log('labelIndices', labelIndices);
234
- var oneHotEncode = tf.oneHot(tf.tensor1d(labelIndices, 'int32'), this.labels.length);
185
+ const oneHotEncode = tf.oneHot(tf.tensor1d(labelIndices, 'int32'), this.labels.length);
235
186
  console.log('oneHotEncode', oneHotEncode);
236
187
  return oneHotEncode;
237
- };
188
+ }
238
189
  // 입력 이미지 데이타
239
- LearningMobilenetImage.prototype._preprocessedInputData = function (model) {
240
- var _this = this;
190
+ _preprocessedInputData(model) {
241
191
  // 이미지 배열을 배치로 변환 - [null, 224, 224, 3]
242
- var inputShape = model.inputs[0].shape;
192
+ const inputShape = model.inputs[0].shape;
243
193
  console.log('inputShape', inputShape);
244
194
  // inputShape를 이와 같이 포멧 맞춘다. for reshape to [224, 224, 3]
245
- var inputShapeArray = inputShape.slice(1);
195
+ const inputShapeArray = inputShape.slice(1);
246
196
  console.log('inputShapeArray', inputShapeArray);
247
- var inputBatch = tf.stack(this.trainImages.map(function (image) {
197
+ const inputBatch = tf.stack(this.trainImages.map((image) => {
248
198
  // 이미지 전처리 및 크기 조정 등을 수행한 후에
249
199
  // 모델의 입력 형태로 변환하여 반환
250
- var xs = _this._preprocessData(image); // 전처리 함수는 사용자 정의해야 함
200
+ const xs = this._preprocessData(image); // 전처리 함수는 사용자 정의해야 함
251
201
  return tf.reshape(xs, inputShapeArray);
252
202
  }));
253
203
  return inputBatch;
254
- };
204
+ }
255
205
  // 모델 학습하기 위한 데이타 전처리 단계
256
- LearningMobilenetImage.prototype._preprocessData = function (tensor) {
206
+ _preprocessData(tensor) {
257
207
  try {
258
208
  // mobilenet model summary를 하면 위와 같이 224,224 사이즈의 입력값 설정되어 있다. ex) input_1 (InputLayer) [null,224,224,3]
259
- var resizedImage = tf.image.resizeBilinear(tensor, [this.MOBILE_NET_INPUT_WIDTH, this.MOBILE_NET_INPUT_HEIGHT]);
209
+ const resizedImage = tf.image.resizeBilinear(tensor, [this.MOBILE_NET_INPUT_WIDTH, this.MOBILE_NET_INPUT_HEIGHT]);
260
210
  // 이미지를 [0,1] 범위로 정규화 255로 나뉜 픽셀값
261
- var normalizedImage = resizedImage.div(this.IMAGE_NORMALIZATION_FACTOR);
211
+ const normalizedImage = resizedImage.div(this.IMAGE_NORMALIZATION_FACTOR);
262
212
  // expandDims(0)을 하여 차원을 추가하여 4D텐서 반환
263
213
  return normalizedImage.expandDims(0);
264
214
  }
@@ -266,50 +216,41 @@ var LearningMobilenetImage = /** @class */ (function () {
266
216
  console.error('Failed to _preprocessData data', error);
267
217
  throw error;
268
218
  }
269
- };
219
+ }
270
220
  // 모델 저장
271
- LearningMobilenetImage.prototype._createModel = function (numClasses) {
272
- return __awaiter(this, void 0, void 0, function () {
273
- var load_model, truncatedModel, _i, _a, layer, model, error_3;
274
- return __generator(this, function (_b) {
275
- switch (_b.label) {
276
- case 0:
277
- _b.trys.push([0, 2, , 3]);
278
- return [4 /*yield*/, tf.loadLayersModel(this.modelURL)];
279
- case 1:
280
- load_model = _b.sent();
281
- truncatedModel = tf.model({
282
- inputs: load_model.inputs,
283
- outputs: load_model.layers[load_model.layers.length - 2].output
284
- });
285
- // 모델을 학습 가능하게 설정하고 선택한 레이어까지 고정
286
- for (_i = 0, _a = truncatedModel.layers; _i < _a.length; _i++) {
287
- layer = _a[_i];
288
- layer.trainable = false;
289
- }
290
- model = tf.sequential();
291
- model.add(truncatedModel);
292
- model.add(tf.layers.flatten()); // 필요한 경우 Flatten 레이어 추가
293
- model.add(tf.layers.dense({
294
- units: numClasses,
295
- activation: 'softmax'
296
- }));
297
- model.compile({
298
- loss: (numClasses === 2) ? 'binaryCrossentropy' : 'categoricalCrossentropy',
299
- optimizer: tf.train.adam(),
300
- metrics: ['accuracy']
301
- });
302
- model.summary();
303
- return [2 /*return*/, model];
304
- case 2:
305
- error_3 = _b.sent();
306
- console.error('Failed to load model', error_3);
307
- throw error_3;
308
- case 3: return [2 /*return*/];
221
+ _createModel(numClasses) {
222
+ return __awaiter(this, void 0, void 0, function* () {
223
+ try {
224
+ const load_model = yield tf.loadLayersModel(this.modelURL);
225
+ // 기존 MobileNet 모델에서 마지막 레이어 제외
226
+ const truncatedModel = tf.model({
227
+ inputs: load_model.inputs,
228
+ outputs: load_model.layers[load_model.layers.length - 2].output
229
+ });
230
+ // 모델을 학습 가능하게 설정하고 선택한 레이어까지 고정
231
+ for (let layer of truncatedModel.layers) {
232
+ layer.trainable = false;
309
233
  }
310
- });
234
+ const model = tf.sequential();
235
+ model.add(truncatedModel);
236
+ model.add(tf.layers.flatten()); // 필요한 경우 Flatten 레이어 추가
237
+ model.add(tf.layers.dense({
238
+ units: numClasses,
239
+ activation: 'softmax'
240
+ }));
241
+ model.compile({
242
+ loss: (numClasses === 2) ? 'binaryCrossentropy' : 'categoricalCrossentropy',
243
+ optimizer: tf.train.adam(),
244
+ metrics: ['accuracy']
245
+ });
246
+ model.summary();
247
+ return model;
248
+ }
249
+ catch (error) {
250
+ console.error('Failed to load model', error);
251
+ throw error;
252
+ }
311
253
  });
312
- };
313
- return LearningMobilenetImage;
314
- }());
254
+ }
255
+ }
315
256
  exports.default = LearningMobilenetImage;