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

Commit

Permalink
[Example] Iterator tool for image classification
Browse files Browse the repository at this point in the history
  • Loading branch information
pinzhenx committed Jan 28, 2019
1 parent 382e0cd commit dc31ec2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 43 deletions.
87 changes: 44 additions & 43 deletions examples/deeplab/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,6 @@ class Utils {
this.initialized = true;
}

async iterateLayers(configs) {
if (!this.initialized) return;

let iterators = [];
for (let config of configs) {
let model = await new TFliteModelImporter({
rawModel: this.tfModel,
backend: config.backend,
prefer: config.prefer || null,
});
iterators.push(model.layerIterator([this.inputTensor]));
}

while (true) {

let layerOutputs = [];
for (let it of iterators) {
layerOutputs.push(await it.next());
}

let baselineOutput = layerOutputs[0];
if (baselineOutput.done) {
break;
}

console.debug(`\n\n\nLayer ${baselineOutput.value.outputName}`);

for (let i = 0; i < configs.length; ++i) {
console.debug(`\n${configs[i].backend}:`);
console.debug(`\n${layerOutputs[i].value.tensor}`);

if (i > 0) {
let sum = 0;
for (let j = 0; j < baselineOutput.value.tensor.length; j++) {
sum += Math.pow(layerOutputs[i].value.tensor[j] - baselineOutput.value.tensor[j], 2);
}
let variance = sum / baselineOutput.value.tensor.length;
console.debug(`var with ${configs[0].backend}: ${variance}`);
}
}
}
}

async predict() {
if (!this.initialized) return;
let start = performance.now();
Expand Down Expand Up @@ -199,4 +156,48 @@ class Utils {
this.outputTensor = new Float32Array(newModel.outputSize.reduce((x,y) => x*y));
this.tfModel = null;
}

// for debugging
async iterateLayers(configs) {
if (!this.initialized) return;

let iterators = [];
for (let config of configs) {
let model = await new TFliteModelImporter({
rawModel: this.tfModel,
backend: config.backend,
prefer: config.prefer || null,
});
iterators.push(model.layerIterator([this.inputTensor]));
}

while (true) {

let layerOutputs = [];
for (let it of iterators) {
layerOutputs.push(await it.next());
}

let baselineOutput = layerOutputs[0];
if (baselineOutput.done) {
break;
}

console.debug(`\n\n\nLayer ${baselineOutput.value.outputName}`);

for (let i = 0; i < configs.length; ++i) {
console.debug(`\n${configs[i].backend}:`);
console.debug(`\n${layerOutputs[i].value.tensor}`);

if (i > 0) {
let sum = 0;
for (let j = 0; j < baselineOutput.value.tensor.length; j++) {
sum += Math.pow(layerOutputs[i].value.tensor[j] - baselineOutput.value.tensor[j], 2);
}
let variance = sum / baselineOutput.value.tensor.length;
console.debug(`var with ${configs[0].backend}: ${variance}`);
}
}
}
}
}
46 changes: 46 additions & 0 deletions examples/image_classification/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,50 @@ class Utils {
this.model._compilation._preparedModel._deleteAll();
}
}


// for debugging
async iterateLayers(configs) {
if (!this.initialized) return;

let iterators = [];
for (let config of configs) {
let importer = this.modelFile.split('.').pop() === 'tflite' ? TFliteModelImporter : OnnxModelImporter;
let model = await new importer({
rawModel: this.rawModel,
backend: config.backend,
prefer: config.prefer || null,
});
iterators.push(model.layerIterator([this.inputTensor]));
}

while (true) {

let layerOutputs = [];
for (let it of iterators) {
layerOutputs.push(await it.next());
}

let baselineOutput = layerOutputs[0];
if (baselineOutput.done) {
break;
}

console.debug(`\n\n\nLayer ${baselineOutput.value.outputName}`);

for (let i = 0; i < configs.length; ++i) {
console.debug(`\n${configs[i].backend}:`);
console.debug(`\n${layerOutputs[i].value.tensor}`);

if (i > 0) {
let sum = 0;
for (let j = 0; j < baselineOutput.value.tensor.length; j++) {
sum += Math.pow(layerOutputs[i].value.tensor[j] - baselineOutput.value.tensor[j], 2);
}
let variance = sum / baselineOutput.value.tensor.length;
console.debug(`var with ${configs[0].backend}: ${variance}`);
}
}
}
}
}

0 comments on commit dc31ec2

Please sign in to comment.