Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.

Commit dc31ec2

Browse files
committed
[Example] Iterator tool for image classification
1 parent 382e0cd commit dc31ec2

File tree

2 files changed

+90
-43
lines changed

2 files changed

+90
-43
lines changed

examples/deeplab/utils.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,49 +45,6 @@ class Utils {
4545
this.initialized = true;
4646
}
4747

48-
async iterateLayers(configs) {
49-
if (!this.initialized) return;
50-
51-
let iterators = [];
52-
for (let config of configs) {
53-
let model = await new TFliteModelImporter({
54-
rawModel: this.tfModel,
55-
backend: config.backend,
56-
prefer: config.prefer || null,
57-
});
58-
iterators.push(model.layerIterator([this.inputTensor]));
59-
}
60-
61-
while (true) {
62-
63-
let layerOutputs = [];
64-
for (let it of iterators) {
65-
layerOutputs.push(await it.next());
66-
}
67-
68-
let baselineOutput = layerOutputs[0];
69-
if (baselineOutput.done) {
70-
break;
71-
}
72-
73-
console.debug(`\n\n\nLayer ${baselineOutput.value.outputName}`);
74-
75-
for (let i = 0; i < configs.length; ++i) {
76-
console.debug(`\n${configs[i].backend}:`);
77-
console.debug(`\n${layerOutputs[i].value.tensor}`);
78-
79-
if (i > 0) {
80-
let sum = 0;
81-
for (let j = 0; j < baselineOutput.value.tensor.length; j++) {
82-
sum += Math.pow(layerOutputs[i].value.tensor[j] - baselineOutput.value.tensor[j], 2);
83-
}
84-
let variance = sum / baselineOutput.value.tensor.length;
85-
console.debug(`var with ${configs[0].backend}: ${variance}`);
86-
}
87-
}
88-
}
89-
}
90-
9148
async predict() {
9249
if (!this.initialized) return;
9350
let start = performance.now();
@@ -199,4 +156,48 @@ class Utils {
199156
this.outputTensor = new Float32Array(newModel.outputSize.reduce((x,y) => x*y));
200157
this.tfModel = null;
201158
}
159+
160+
// for debugging
161+
async iterateLayers(configs) {
162+
if (!this.initialized) return;
163+
164+
let iterators = [];
165+
for (let config of configs) {
166+
let model = await new TFliteModelImporter({
167+
rawModel: this.tfModel,
168+
backend: config.backend,
169+
prefer: config.prefer || null,
170+
});
171+
iterators.push(model.layerIterator([this.inputTensor]));
172+
}
173+
174+
while (true) {
175+
176+
let layerOutputs = [];
177+
for (let it of iterators) {
178+
layerOutputs.push(await it.next());
179+
}
180+
181+
let baselineOutput = layerOutputs[0];
182+
if (baselineOutput.done) {
183+
break;
184+
}
185+
186+
console.debug(`\n\n\nLayer ${baselineOutput.value.outputName}`);
187+
188+
for (let i = 0; i < configs.length; ++i) {
189+
console.debug(`\n${configs[i].backend}:`);
190+
console.debug(`\n${layerOutputs[i].value.tensor}`);
191+
192+
if (i > 0) {
193+
let sum = 0;
194+
for (let j = 0; j < baselineOutput.value.tensor.length; j++) {
195+
sum += Math.pow(layerOutputs[i].value.tensor[j] - baselineOutput.value.tensor[j], 2);
196+
}
197+
let variance = sum / baselineOutput.value.tensor.length;
198+
console.debug(`var with ${configs[0].backend}: ${variance}`);
199+
}
200+
}
201+
}
202+
}
202203
}

examples/image_classification/utils.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,50 @@ class Utils {
218218
this.model._compilation._preparedModel._deleteAll();
219219
}
220220
}
221+
222+
223+
// for debugging
224+
async iterateLayers(configs) {
225+
if (!this.initialized) return;
226+
227+
let iterators = [];
228+
for (let config of configs) {
229+
let importer = this.modelFile.split('.').pop() === 'tflite' ? TFliteModelImporter : OnnxModelImporter;
230+
let model = await new importer({
231+
rawModel: this.rawModel,
232+
backend: config.backend,
233+
prefer: config.prefer || null,
234+
});
235+
iterators.push(model.layerIterator([this.inputTensor]));
236+
}
237+
238+
while (true) {
239+
240+
let layerOutputs = [];
241+
for (let it of iterators) {
242+
layerOutputs.push(await it.next());
243+
}
244+
245+
let baselineOutput = layerOutputs[0];
246+
if (baselineOutput.done) {
247+
break;
248+
}
249+
250+
console.debug(`\n\n\nLayer ${baselineOutput.value.outputName}`);
251+
252+
for (let i = 0; i < configs.length; ++i) {
253+
console.debug(`\n${configs[i].backend}:`);
254+
console.debug(`\n${layerOutputs[i].value.tensor}`);
255+
256+
if (i > 0) {
257+
let sum = 0;
258+
for (let j = 0; j < baselineOutput.value.tensor.length; j++) {
259+
sum += Math.pow(layerOutputs[i].value.tensor[j] - baselineOutput.value.tensor[j], 2);
260+
}
261+
let variance = sum / baselineOutput.value.tensor.length;
262+
console.debug(`var with ${configs[0].backend}: ${variance}`);
263+
}
264+
}
265+
}
266+
}
221267
}

0 commit comments

Comments
 (0)