Skip to content

Commit 4a1f0cf

Browse files
authored
[mobilent] Add timing number without image extraction and preprocessing (#254)
It turns out that image extraction and preprocessing takes a big fraction of the time. A typical result (with the new code) looks like: Done in 76 ms (not including image preprocessing: 21 ms)
1 parent 614ba81 commit 4a1f0cf

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

mobilenet/index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ const mobilenetDemo = async () => {
6161
async function predict(imgElement) {
6262
status('Predicting...');
6363

64-
const startTime = performance.now();
64+
// The first start time includes the time it takes to extract the image
65+
// from the HTML and preprocess it, in additon to the predict() call.
66+
const startTime1 = performance.now();
67+
// The second start time excludes the extraction and preprocessing and
68+
// includes only the predict() call.
69+
let startTime2;
6570
const logits = tf.tidy(() => {
6671
// tf.browser.fromPixels() returns a Tensor from an image element.
6772
const img = tf.browser.fromPixels(imgElement).toFloat();
@@ -73,14 +78,17 @@ async function predict(imgElement) {
7378
// Reshape to a single-element batch so we can pass it to predict.
7479
const batched = normalized.reshape([1, IMAGE_SIZE, IMAGE_SIZE, 3]);
7580

81+
startTime2 = performance.now();
7682
// Make a prediction through mobilenet.
7783
return mobilenet.predict(batched);
7884
});
7985

8086
// Convert logits to probabilities and class names.
8187
const classes = await getTopKClasses(logits, TOPK_PREDICTIONS);
82-
const totalTime = performance.now() - startTime;
83-
status(`Done in ${Math.floor(totalTime)}ms`);
88+
const totalTime1 = performance.now() - startTime1;
89+
const totalTime2 = performance.now() - startTime2;
90+
status(`Done in ${Math.floor(totalTime1)} ms ` +
91+
`(not including preprocessing: ${Math.floor(totalTime2)} ms)`);
8492

8593
// Show the classes in the DOM.
8694
showResults(imgElement, classes);

0 commit comments

Comments
 (0)