@@ -119,8 +119,12 @@ function getPredictFnForModel(model, input) {
119
119
/**
120
120
* Executes the predict function for `model` (`model.predict` for tf.LayersModel
121
121
* and `model.executeAsync` for tf.GraphModel) and times the inference process
122
- * for `numRuns` rounds. Then returns a promise that resolves with an array of
123
- * inference times for each inference process.
122
+ * for `numRuns` rounds. Then returns a promise that resolves with information
123
+ * about the model's inference time:
124
+ * - `times`: an array of inference time for each inference
125
+ * - `averageTime`: the average time of all inferences
126
+ * - `minTime`: the minimum time of all inferences
127
+ * - `maxTime`: the maximum time of all inferences
124
128
*
125
129
* The inference time contains the time spent by both `predict()` and `data()`
126
130
* called by tensors in the prediction.
@@ -130,10 +134,13 @@ function getPredictFnForModel(model, input) {
130
134
* 'https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/2';
131
135
* const model = await tf.loadGraphModel(modelUrl, {fromTFHub: true});
132
136
* const zeros = tf.zeros([1, 224, 224, 3]);
133
- * const elapsedTimeArray =
137
+ * const timeInfo =
134
138
* await profileInferenceTimeForModel(model, zeros, 2);
135
139
*
136
- * console.log(`Elapsed time array: ${elapsedTimeArray}`);
140
+ * console.log(`Elapsed time array: ${timeInfo.times}`);
141
+ * console.log(`Average time: ${timeInfo.averageTime}`);
142
+ * console.log(`Minimum time: ${timeInfo.minTime}`);
143
+ * console.log(`Maximum time: ${timeInfo.maxTime}`);
137
144
* ```
138
145
*
139
146
* @param model An instance of tf.GraphModel or tf.LayersModel for timing the
@@ -148,8 +155,12 @@ async function profileInferenceTimeForModel(model, input, numRuns = 1) {
148
155
149
156
/**
150
157
* Executes `predict()` and times the inference process for `numRuns` rounds.
151
- * Then returns a promise that resolves with an array of inference time for each
152
- * inference process.
158
+ * Then returns a promise that resolves with information about the inference
159
+ * time:
160
+ * - `times`: an array of inference time for each inference
161
+ * - `averageTime`: the average time of all inferences
162
+ * - `minTime`: the minimum time of all inferences
163
+ * - `maxTime`: the maximum time of all inferences
153
164
*
154
165
* The inference time contains the time spent by both `predict()` and `data()`
155
166
* called by tensors in the prediction.
@@ -159,10 +170,13 @@ async function profileInferenceTimeForModel(model, input, numRuns = 1) {
159
170
* 'https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/2';
160
171
* const model = await tf.loadGraphModel(modelUrl, {fromTFHub: true});
161
172
* const zeros = tf.zeros([1, 224, 224, 3]);
162
- * const elapsedTimeArray =
173
+ * const timeInfo =
163
174
* await profileInferenceTime(() => model.predict(zeros), 2);
164
175
*
165
- * console.log(`Elapsed time array: ${elapsedTimeArray}`);
176
+ * console.log(`Elapsed time array: ${timeInfo.times}`);
177
+ * console.log(`Average time: ${timeInfo.averageTime}`);
178
+ * console.log(`Minimum time: ${timeInfo.minTime}`);
179
+ * console.log(`Maximum time: ${timeInfo.maxTime}`);
166
180
* ```
167
181
*
168
182
* @param predict The predict function to execute and time.
@@ -175,7 +189,7 @@ async function profileInferenceTime(predict, numRuns = 1) {
175
189
`a(n) ${ typeof predict } is found.` ) ;
176
190
}
177
191
178
- const elapsedTimeArray = [ ] ;
192
+ const times = [ ] ;
179
193
for ( let i = 0 ; i < numRuns ; i ++ ) {
180
194
const start = performance . now ( ) ;
181
195
const res = await predict ( ) ;
@@ -184,9 +198,20 @@ async function profileInferenceTime(predict, numRuns = 1) {
184
198
const elapsedTime = performance . now ( ) - start ;
185
199
186
200
tf . dispose ( res ) ;
187
- elapsedTimeArray . push ( elapsedTime ) ;
201
+ times . push ( elapsedTime ) ;
188
202
}
189
- return elapsedTimeArray ;
203
+
204
+ const averageTime = times . reduce ( ( acc , curr ) => acc + curr , 0 ) / times . length ;
205
+ const minTime = Math . min ( ...times ) ;
206
+ const maxTime = Math . max ( ...times ) ;
207
+ const timeInfo = {
208
+ times,
209
+ averageTime,
210
+ minTime,
211
+ maxTime
212
+
213
+ } ;
214
+ return timeInfo ;
190
215
}
191
216
192
217
/**
0 commit comments