asyncfunctionpredict(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.browser.fromPixels(imgElement).toFloat(); 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 ".concat(Math.floor(totalTime1), " ms ") + "(not including preprocessing: ".concat(Math.floor(totalTime2), " ms)")); // Show the classes in the DOM. // showResults(imgElement, classes); return 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. */ asyncfunctiongetTopKClasses(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 = newFloat32Array(topK); const topkIndices = newInt32Array(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.IMAGENET_CLASSES[topkIndices[i]], probability: topkValues[i] }); } return topClassesAndProbs; }