Skip to content

Commit

Permalink
Update to align with latest spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Honry committed Nov 2, 2023
1 parent 7d41ac7 commit 01a0801
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
11 changes: 7 additions & 4 deletions image_classification/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ async function renderCamStream() {
const inputCanvas = utils.getVideoFrame(camElement);
console.log('- Computing... ');
const start = performance.now();
await netInstance.compute(inputBuffer, outputBuffer);
const results = await netInstance.compute(inputBuffer, outputBuffer);
computeTime = (performance.now() - start).toFixed(2);
outputBuffer = results.outputs.output;
console.log(` done in ${computeTime} ms.`);
drawInput(inputCanvas, 'camInCanvas');
showPerfResult();
Expand Down Expand Up @@ -256,7 +257,7 @@ async function main() {
// UI shows model loading progress
await ui.showProgressComponent('current', 'pending', 'pending');
console.log('- Loading weights... ');
const contextOptions = {type: 'webnn', devicePreference: devicePreference};
const contextOptions = {type: 'webnn', deviceType: devicePreference};
if (powerPreference) {
contextOptions['powerPreference'] = powerPreference;
}
Expand All @@ -281,11 +282,12 @@ async function main() {
let medianComputeTime;

// Do warm up
await netInstance.compute(inputBuffer, outputBuffer);
let results = await netInstance.compute(inputBuffer, outputBuffer);

for (let i = 0; i < numRuns; i++) {
start = performance.now();
await netInstance.compute(inputBuffer, outputBuffer);
results = await netInstance.compute(
results.inputs.input, results.outputs.output);
computeTime = (performance.now() - start).toFixed(2);
console.log(` compute time ${i+1}: ${computeTime} ms`);
computeTimeArray.push(Number(computeTime));
Expand All @@ -295,6 +297,7 @@ async function main() {
medianComputeTime = medianComputeTime.toFixed(2);
console.log(` median compute time: ${medianComputeTime} ms`);
}
outputBuffer = results.outputs.output;
console.log('outputBuffer: ', outputBuffer);
await ui.showProgressComponent('done', 'done', 'done');
ui.readyShowResultComponents();
Expand Down
11 changes: 6 additions & 5 deletions image_classification/mobilenetv2_7_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export class MobileNetV27Nchw {
}
if (clip) {
// implement `clip` by `clamp` of WebNN API
options.activation = this.builder_.clamp({minValue: 0, maxValue: 6});
return this.builder_.clamp(this.builder_.conv2d(input, weights, options), {minValue: 0, maxValue: 6});
} else {
options.activation = undefined;
return this.builder_.conv2d(input, weights, options);
}
return this.builder_.conv2d(input, weights, options);
}

async buildGemm_(input, name) {
Expand Down Expand Up @@ -122,14 +122,14 @@ export class MobileNetV27Nchw {
const conv3 = await this.buildConv_(bottleneck15, '95', true);
const conv4 = await this.buildConv_(conv3, '97', false, {groups: 1280, strides: [7, 7]});
const conv5 = await this.buildConv_(conv4, '104', false);
const reshape = this.builder_.reshape(conv5, [1, -1]);
const reshape = this.builder_.reshape(conv5, [1, null]);
// return reshape;
// const gemm = await this.buildGemm_(reshape, '104');
return this.builder_.softmax(reshape);
}

async build(outputOperand) {
this.graph_ = await this.builder_.buildAsync({'output': outputOperand});
this.graph_ = await this.builder_.build({'output': outputOperand});
}

// Release the constant tensors of a model
Expand All @@ -143,6 +143,7 @@ export class MobileNetV27Nchw {
async compute(inputBuffer, outputBuffer) {
const inputs = {'input': inputBuffer};
const outputs = {'output': outputBuffer};
await this.context_.compute(this.graph_, inputs, outputs);
const results = await this.context_.compute(this.graph_, inputs, outputs);
return results;
}
}

0 comments on commit 01a0801

Please sign in to comment.