-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.js
194 lines (161 loc) · 6.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tf from '@tensorflow/tfjs';
import {IMAGENET_CLASSES} from './imagenet_classes';
const MOBILENET_MODEL_PATH =
// tslint:disable-next-line:max-line-length
'https://tfhub.dev/google/tfjs-model/imagenet/mobilenet_v2_100_224/classification/3/default/1';
const IMAGE_SIZE = 224;
const TOPK_PREDICTIONS = 10;
let mobilenet;
const mobilenetDemo = async () => {
status('Loading model...');
mobilenet = await tf.loadGraphModel(MOBILENET_MODEL_PATH, {fromTFHub: true});
// Warmup the model. This isn't necessary, but makes the first prediction
// faster. Call `dispose` to release the WebGL memory allocated for the return
// value of `predict`.
mobilenet.predict(tf.zeros([1, IMAGE_SIZE, IMAGE_SIZE, 3])).dispose();
status('');
// Make a prediction through the locally hosted cat.jpg.
const catElement = document.getElementById('cat');
if (catElement.complete && catElement.naturalHeight !== 0) {
predict(catElement);
catElement.style.display = '';
} else {
catElement.onload = () => {
predict(catElement);
catElement.style.display = '';
}
}
document.getElementById('file-container').style.display = '';
};
/**
* Given an image element, makes a prediction through mobilenet returning the
* probabilities of the top K classes.
*/
async function predict(imgElement) {
status('Predicting...');
// The first start time includes the time it takes to extract the image
// from the HTML and preprocess it, in additon to the predict() call.
const startTime1 = performance.now();
// The second start time excludes the extraction and preprocessing and
// includes only the predict() call.
let startTime2;
const logits = tf.tidy(() => {
// tf.browser.fromPixels() returns a Tensor from an image element.
const img = tf.cast(tf.browser.fromPixels(imgElement), 'float32');
const offset = tf.scalar(127.5);
// Normalize the image from [0, 255] to [-1, 1].
const normalized = img.sub(offset).div(offset);
// Reshape to a single-element batch so we can pass it to predict.
const batched = normalized.reshape([1, IMAGE_SIZE, IMAGE_SIZE, 3]);
startTime2 = performance.now();
// Make a prediction through mobilenet.
return mobilenet.predict(batched);
});
// Convert logits to probabilities and class names.
const classes = await getTopKClasses(logits, TOPK_PREDICTIONS);
const totalTime1 = performance.now() - startTime1;
const totalTime2 = performance.now() - startTime2;
status(`Done in ${Math.floor(totalTime1)} ms ` +
`(not including preprocessing: ${Math.floor(totalTime2)} ms)`);
// Show the classes in the DOM.
showResults(imgElement, classes);
}
/**
* Computes the probabilities of the topK classes given logits by computing
* softmax to get probabilities and then sorting the probabilities.
* @param logits Tensor representing the logits from MobileNet.
* @param topK The number of top predictions to show.
*/
export async function getTopKClasses(logits, topK) {
const values = await logits.data();
const valuesAndIndices = [];
for (let i = 0; i < values.length; i++) {
valuesAndIndices.push({value: values[i], index: i});
}
valuesAndIndices.sort((a, b) => {
return b.value - a.value;
});
const topkValues = new Float32Array(topK);
const topkIndices = new Int32Array(topK);
for (let i = 0; i < topK; i++) {
topkValues[i] = valuesAndIndices[i].value;
topkIndices[i] = valuesAndIndices[i].index;
}
const topClassesAndProbs = [];
for (let i = 0; i < topkIndices.length; i++) {
topClassesAndProbs.push({
className: IMAGENET_CLASSES[topkIndices[i]],
probability: topkValues[i]
})
}
return topClassesAndProbs;
}
//
// UI
//
function showResults(imgElement, classes) {
const predictionContainer = document.createElement('div');
predictionContainer.className = 'pred-container';
const imgContainer = document.createElement('div');
imgContainer.appendChild(imgElement);
predictionContainer.appendChild(imgContainer);
const probsContainer = document.createElement('div');
for (let i = 0; i < classes.length; i++) {
const row = document.createElement('div');
row.className = 'row';
const classElement = document.createElement('div');
classElement.className = 'cell';
classElement.innerText = classes[i].className;
row.appendChild(classElement);
const probsElement = document.createElement('div');
probsElement.className = 'cell';
probsElement.innerText = classes[i].probability.toFixed(3);
row.appendChild(probsElement);
probsContainer.appendChild(row);
}
predictionContainer.appendChild(probsContainer);
predictionsElement.insertBefore(
predictionContainer, predictionsElement.firstChild);
}
const filesElement = document.getElementById('files');
filesElement.addEventListener('change', evt => {
let files = evt.target.files;
// Display thumbnails & issue call to predict each image.
for (let i = 0, f; f = files[i]; i++) {
// Only process image files (skip non image files)
if (!f.type.match('image.*')) {
continue;
}
let reader = new FileReader();
reader.onload = e => {
// Fill the image & call predict.
let img = document.createElement('img');
img.src = e.target.result;
img.width = IMAGE_SIZE;
img.height = IMAGE_SIZE;
img.onload = () => predict(img);
};
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
});
const demoStatusElement = document.getElementById('status');
const status = msg => demoStatusElement.innerText = msg;
const predictionsElement = document.getElementById('predictions');
mobilenetDemo();