Create tflite_time_Inferance.js
Browse files- tflite_time_Inferance.js +143 -0
tflite_time_Inferance.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import * as tflite from '@tensorflow/tfjs-tflite';
|
| 3 |
+
|
| 4 |
+
function TFLiteObjectDetection() {
|
| 5 |
+
const [averageTime, setAverageTime] = useState(null);
|
| 6 |
+
const [loading, setLoading] = useState(false);
|
| 7 |
+
const [images, setImages] = useState([]);
|
| 8 |
+
const [model, setModel] = useState(null);
|
| 9 |
+
|
| 10 |
+
const handleFileChange = (event) => {
|
| 11 |
+
const files = Array.from(event.target.files);
|
| 12 |
+
setImages(files.slice(0, 10)); // Limit to the first 10 images
|
| 13 |
+
};
|
| 14 |
+
|
| 15 |
+
const loadModel = async () => {
|
| 16 |
+
try {
|
| 17 |
+
// Load the TFLite model
|
| 18 |
+
const loadedModel = await tflite.loadTFLiteModel('./model.tflite');
|
| 19 |
+
setModel(loadedModel);
|
| 20 |
+
console.log('Model loaded successfully!');
|
| 21 |
+
} catch (error) {
|
| 22 |
+
console.error('Error loading TFLite model:', error);
|
| 23 |
+
}
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
const runBenchmark = async () => {
|
| 27 |
+
if (!model || images.length === 0) {
|
| 28 |
+
alert('Please load the model and upload 10 images.');
|
| 29 |
+
return;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
setLoading(true);
|
| 33 |
+
const repetitions = 50; // Number of repetitions for benchmarking
|
| 34 |
+
let totalInferenceTime = 0;
|
| 35 |
+
|
| 36 |
+
try {
|
| 37 |
+
for (let rep = 0; rep < repetitions; rep++) {
|
| 38 |
+
console.log(`Repetition ${rep + 1} of ${repetitions}`);
|
| 39 |
+
|
| 40 |
+
for (const imageFile of images) {
|
| 41 |
+
const startTime = performance.now();
|
| 42 |
+
|
| 43 |
+
// Preprocess the image to create a tensor
|
| 44 |
+
const inputTensor = await preprocessImage(imageFile);
|
| 45 |
+
|
| 46 |
+
// Run inference using the TFLite model
|
| 47 |
+
const output = model.predict(inputTensor);
|
| 48 |
+
|
| 49 |
+
const endTime = performance.now();
|
| 50 |
+
totalInferenceTime += endTime - startTime;
|
| 51 |
+
|
| 52 |
+
// Log output for debugging (optional)
|
| 53 |
+
console.log('Inference output:', output);
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// Calculate average inference time
|
| 58 |
+
const avgInferenceTime = totalInferenceTime / (repetitions * images.length);
|
| 59 |
+
setAverageTime(avgInferenceTime);
|
| 60 |
+
} catch (error) {
|
| 61 |
+
console.error('Error during inference:', error);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
setLoading(false);
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
const preprocessImage = async (imageFile) => {
|
| 68 |
+
return new Promise((resolve) => {
|
| 69 |
+
const img = new Image();
|
| 70 |
+
const reader = new FileReader();
|
| 71 |
+
|
| 72 |
+
reader.onload = () => {
|
| 73 |
+
img.src = reader.result;
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
img.onload = () => {
|
| 77 |
+
const canvas = document.createElement('canvas');
|
| 78 |
+
const context = canvas.getContext('2d');
|
| 79 |
+
|
| 80 |
+
// Resize to match model input size
|
| 81 |
+
const modelInputWidth = 320; // Replace with your model's input width
|
| 82 |
+
const modelInputHeight = 320; // Replace with your model's input height
|
| 83 |
+
canvas.width = modelInputWidth;
|
| 84 |
+
canvas.height = modelInputHeight;
|
| 85 |
+
|
| 86 |
+
context.drawImage(img, 0, 0, modelInputWidth, modelInputHeight);
|
| 87 |
+
|
| 88 |
+
const imageData = context.getImageData(0, 0, modelInputWidth, modelInputHeight);
|
| 89 |
+
|
| 90 |
+
// Normalize pixel values to [0, 1] and convert to Float32Array
|
| 91 |
+
const floatData = new Float32Array(imageData.data.length / 4);
|
| 92 |
+
for (let i = 0, j = 0; i < imageData.data.length; i += 4) {
|
| 93 |
+
floatData[j++] = imageData.data[i] / 255; // R
|
| 94 |
+
floatData[j++] = imageData.data[i + 1] / 255; // G
|
| 95 |
+
floatData[j++] = imageData.data[i + 2] / 255; // B
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// Create a tensor with shape [1, 320, 320, 3]
|
| 99 |
+
resolve(new tflite.Tensor(floatData, [1, modelInputHeight, modelInputWidth, 3]));
|
| 100 |
+
};
|
| 101 |
+
|
| 102 |
+
reader.readAsDataURL(imageFile);
|
| 103 |
+
});
|
| 104 |
+
};
|
| 105 |
+
|
| 106 |
+
return React.createElement(
|
| 107 |
+
'div',
|
| 108 |
+
null,
|
| 109 |
+
React.createElement('h1', null, 'Object Detection Benchmark (TFLite)'),
|
| 110 |
+
React.createElement('button', { onClick: loadModel, disabled: model !== null }, 'Load Model'),
|
| 111 |
+
React.createElement('input', {
|
| 112 |
+
type: 'file',
|
| 113 |
+
multiple: true,
|
| 114 |
+
accept: 'image/*',
|
| 115 |
+
onChange: handleFileChange,
|
| 116 |
+
}),
|
| 117 |
+
React.createElement(
|
| 118 |
+
'button',
|
| 119 |
+
{ onClick: runBenchmark, disabled: loading || !model || images.length === 0 },
|
| 120 |
+
loading ? 'Running Benchmark...' : 'Start Benchmark'
|
| 121 |
+
),
|
| 122 |
+
React.createElement(
|
| 123 |
+
'div',
|
| 124 |
+
null,
|
| 125 |
+
averageTime !== null
|
| 126 |
+
? React.createElement(
|
| 127 |
+
'h2',
|
| 128 |
+
null,
|
| 129 |
+
`Average Inference Time: ${averageTime.toFixed(2)} ms`
|
| 130 |
+
)
|
| 131 |
+
: null
|
| 132 |
+
),
|
| 133 |
+
React.createElement(
|
| 134 |
+
'ul',
|
| 135 |
+
null,
|
| 136 |
+
images.map((img, index) =>
|
| 137 |
+
React.createElement('li', { key: index }, img.name)
|
| 138 |
+
)
|
| 139 |
+
)
|
| 140 |
+
);
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
export default TFLiteObjectDetection;
|