Skip to content

Commit fc45148

Browse files
author
Kevin Scott
committed
Update callback methods and update readme and accept alternate image formats
1 parent 19638ae commit fc45148

File tree

8 files changed

+273
-35
lines changed

8 files changed

+273
-35
lines changed

README.md

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,58 @@ const mlClassifier = new MLClassifier();
6767

6868
This will begin loading the pretrained model and provide you with an object onto which to add data and train.
6969

70+
### `constructor`
71+
72+
`MLClassifier` accepts a number of callbacks when initialized:
73+
74+
#### Parameters
75+
76+
* **onLoadStart** (`Function`) *Optional* - A callback for when `load` (loading the pre-trained model) is first called.
77+
* **onLoadComplete** (`Function`) *Optional* - A callback for when `load` (loading the pre-trained model) is complete.
78+
* **onAddDataStart** (`Function`) *Optional* - A callback for when `addData` is first called.
79+
* **onAddDataComplete** (`Function`) *Optional* - A callback for when `addData` is complete.
80+
* **onClearDataStart** (`Function`) *Optional* - A callback for when `clearData` is first called.
81+
* **onClearDataComplete** (`Function`) *Optional* - A callback for when `clearData` is complete.
82+
* **onTrainStart** (`Function`) *Optional* - A callback for when `train` is first called.
83+
* **onTrainComplete** (`Function`) *Optional* - A callback for when `train` is complete.
84+
* **onEvaluateStart** (`Function`) *Optional* - A callback for when `evaluate` is first called.
85+
* **onEvaluateComplete** (`Function`) *Optional* - A callback for when `evaluate` is complete.
86+
* **onPredictStart** (`Function`) *Optional* - A callback for when `predict` is first called.
87+
* **onPredictComplete** (`Function`) *Optional* - A callback for when `predict` is complete.
88+
* **onSaveStart** (`Function`) *Optional* - A callback for when `save` is first called.
89+
* **onSaveComplete** (`Function`) *Optional* - A callback for when `save` is complete.
90+
91+
92+
#### Example
93+
```
94+
import MLClassifier from 'ml-classifier';
95+
const mlClassifier = new MLClassifier({
96+
onLoadStart: () => console.log('onLoadStart'),
97+
onLoadComplete: () => console.log('onLoadComplete'),
98+
onAddDataStart: () => console.log('onAddDataStart'),
99+
onAddDataComplete: () => console.log('onAddDataComplete'),
100+
onClearDataStart: () => console.log('onClearDataStart'),
101+
onClearDataComplete: () => console.log('onClearDataComplete'),
102+
onTrainStart: () => console.log('onTrainStart'),
103+
onTrainComplete: () => console.log('onTrainComplete'),
104+
onEvaluateStart: () => console.log('onEvaluateStart'),
105+
onEvaluateComplete: () => console.log('onEvaluateComplete'),
106+
onPredictStart: () => console.log('onPredictStart'),
107+
onPredictComplete: () => console.log('onPredictComplete'),
108+
onSaveStart: () => console.log('onSaveStart'),
109+
onSaveComplete: () => console.log('onSaveComplete'),
110+
});
111+
```
112+
70113
### `addData`
71114

72115
This method takes an array of incoming images, an optional array of labels, and an optional dataType.
73116

74117
#### Example
75118

76119
```
77-
import MLClassifier, { DataType } from 'ml-classifier';
120+
import MLClassifier from 'ml-classifier';
121+
const mlClassifier = new MLClassifier();
78122
mlClassifier.addData(images, labels, 'train');
79123
```
80124

@@ -95,7 +139,8 @@ Nothing.
95139
#### Example
96140

97141
```
98-
import MLClassifier, { DataType } from 'ml-classifier';
142+
import MLClassifier from 'ml-classifier';
143+
const mlClassifier = new MLClassifier();
99144
mlClassifier.addData(images, labels, DataType.TRAIN);
100145
mlClassifier.train({
101146
callbacks: {
@@ -121,7 +166,8 @@ mlClassifier.train({
121166
#### Example
122167

123168
```
124-
import MLClassifier, { DataType } from 'ml-classifier';
169+
import MLClassifier from 'ml-classifier';
170+
const mlClassifier = new MLClassifier();
125171
mlClassifier.addData(images, labels, DataType.TRAIN);
126172
mlClassifier.train();
127173
mlClassifier.addData(evaluationImages, labels, DataType.EVALUATE);
@@ -143,7 +189,8 @@ mlClassifier.evaluate();
143189
#### Example
144190

145191
```
146-
import MLClassifier, { DataType } from 'ml-classifier';
192+
import MLClassifier from 'ml-classifier';
193+
const mlClassifier = new MLClassifier();
147194
mlClassifier.addData(images, labels, DataType.TRAIN);
148195
mlClassifier.train();
149196
mlClassifier.predict(imageToPredict);
@@ -164,7 +211,8 @@ mlClassifier.predict(imageToPredict);
164211
#### Example
165212

166213
```
167-
import MLClassifier, { DataType } from 'ml-classifier';
214+
import MLClassifier from 'ml-classifier';
215+
const mlClassifier = new MLClassifier();
168216
mlClassifier.addData(images, labels, DataType.TRAIN);
169217
mlClassifier.train();
170218
mlClassifier.save(('path-to-save');
@@ -183,7 +231,8 @@ mlClassifier.save(('path-to-save');
183231
#### Example
184232

185233
```
186-
import MLClassifier, { DataType } from 'ml-classifier';
234+
import MLClassifier from 'ml-classifier';
235+
const mlClassifier = new MLClassifier();
187236
mlClassifier.addData(images, labels, DataType.TRAIN);
188237
mlClassifier.train();
189238
mlClassifier.getModel();
@@ -203,7 +252,8 @@ The saved Tensorflow.js model.
203252

204253
#### Example
205254
```
206-
import MLClassifier, { DataType } from 'ml-classifier';
255+
import MLClassifier from 'ml-classifier';
256+
const mlClassifier = new MLClassifier();
207257
mlClassifier.addData(images, labels, DataType.TRAIN);
208258
mlClassifier.clearData(DataType.TRAIN);
209259
```

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "ml-classifier",
3-
"version": "0.3.8",
3+
"version": "0.3.9",
44
"description": "A machine learning engine for quickly training image classification models in your browser",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
77
"repository": "https://github.com/thekevinscott/ml-classifier",
88
"scripts": {
99
"clean": "rimraf dist/*",
1010
"watch": "npm run build -- --watch",
11-
"build": "npm run clean && rollup -c",
11+
"clean-build": "npm run clean && npm run build",
12+
"build": "rollup -c",
1213
"test": "jest"
1314
},
1415
"keywords": [

src/cropAndResizeImage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const crop = (img: tf.Tensor3D) => {
1010
}
1111

1212
// convert pixel data into a tensor
13-
const cropAndResizeImage = async (img: tf.Tensor3D, dims: [number, number] = [224, 224]): Promise<tf.Tensor3D> => {
13+
const cropAndResizeImage = async (img: tf.Tensor3D, dims: [number, number]): Promise<tf.Tensor3D> => {
1414
return tf.tidy(() => {
1515
const croppedImage = crop(tf.image.resizeBilinear(img, dims));
1616
return croppedImage.expandDims(0).toFloat().div(tf.scalar(127)).sub(tf.scalar(1));

0 commit comments

Comments
 (0)