forked from webmachinelearning/webnn-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet50v2_nchw.js
177 lines (161 loc) · 6.76 KB
/
resnet50v2_nchw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
import {buildConstantByNpy} from '../common/utils.js';
// ResNet50 V2 model with 'nchw' input layout
export class ResNet50V2Nchw {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/resnet50v2_nchw/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/resnet50v2_nchw/weights/';
}
this.inputOptions = {
mean: [0.485, 0.456, 0.406],
std: [0.229, 0.224, 0.225],
norm: true,
inputLayout: 'nchw',
labelUrl: './labels/labels1000.txt',
inputDimensions: [1, 3, 224, 224],
};
this.outputDimensions = [1, 1000];
}
async buildConv_(input, name, stageName, options = undefined) {
let prefix = '';
if (stageName !== '') {
prefix = this.weightsUrl_ + 'resnetv24_stage' + stageName + '_conv' +
name;
} else {
prefix = this.weightsUrl_ + 'resnetv24_conv' + name;
}
const weightName = prefix + '_weight.npy';
const weight = await buildConstantByNpy(this.builder_, weightName);
return this.builder_.conv2d(input, weight, options);
}
async buildBatchNorm_(input, name, stageName, relu = true) {
let prefix = '';
if (stageName !== '') {
prefix = this.weightsUrl_ + 'resnetv24_stage' + stageName +
'_batchnorm' + name;
} else {
prefix = this.weightsUrl_ + 'resnetv24_batchnorm' + name;
}
const scaleName = prefix + '_gamma.npy';
const biasName = prefix + '_beta.npy';
const meanName = prefix + '_running_mean.npy';
const varName = prefix + '_running_var.npy';
const scale = await buildConstantByNpy(this.builder_, scaleName);
const bias = await buildConstantByNpy(this.builder_, biasName);
const mean = await buildConstantByNpy(this.builder_, meanName);
const variance = await buildConstantByNpy(this.builder_, varName);
const options = {scale: scale, bias: bias};
if (relu) {
options.activation = this.builder_.relu();
}
return this.builder_.batchNormalization(input, mean, variance, options);
}
async buildGemm_(input, name) {
const prefix = this.weightsUrl_ + 'resnetv24_dense' + name;
const weightName = prefix + '_weight.npy';
const weight = await buildConstantByNpy(this.builder_, weightName);
const biasName = prefix + '_bias.npy';
const bias = await buildConstantByNpy(this.builder_, biasName);
const options =
{c: this.builder_.reshape(bias, [1, 1000]), bTranspose: true};
return this.builder_.gemm(input, weight, options);
}
async buildBottlenectV2_(
input, stageName, nameIndices, downsample = false, stride = 1) {
let residual = input;
let strides = [1, 1];
if (downsample) {
strides = [stride, stride];
}
const bn1 = await this.buildBatchNorm_(input, nameIndices[0], stageName);
const conv1 = await this.buildConv_(bn1, nameIndices[1], stageName);
const bn2 = await this.buildBatchNorm_(
conv1, parseInt(nameIndices[0]) + 1, stageName);
const conv2 = await this.buildConv_(
bn2, nameIndices[2], stageName, {padding: [1, 1, 1, 1], strides});
const bn3 = await this.buildBatchNorm_(
conv2, parseInt(nameIndices[0]) + 2, stageName);
const conv3 = await this.buildConv_(bn3, nameIndices[3], stageName);
if (downsample) {
residual = await this.buildConv_(
bn1, parseInt(nameIndices[0]) + 3, stageName, {strides});
}
return this.builder_.add(conv3, residual);
}
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const data = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const bn1 = await this.buildBatchNorm_(data, '0', '', false);
const conv0 = await this.buildConv_(
bn1, '0', '', {padding: [3, 3, 3, 3], strides: [2, 2]});
const bn2 = await this.buildBatchNorm_(conv0, '1', '');
const pool1 = await this.builder_.maxPool2d(bn2,
{windowDimensions: [3, 3], padding: [1, 1, 1, 1], strides: [2, 2]});
// Stage 1
const bottleneck1 = await this.buildBottlenectV2_(
pool1, '1', ['0', '0', '1', '2'], true);
const bottleneck2 = await this.buildBottlenectV2_(
bottleneck1, '1', ['3', '4', '5', '6']);
const bottleneck3 = await this.buildBottlenectV2_(
bottleneck2, '1', ['6', '7', '8', '9']);
// Stage 2
const bottleneck4 = await this.buildBottlenectV2_(
bottleneck3, '2', ['0', '0', '1', '2'], true, 2);
const bottleneck5 = await this.buildBottlenectV2_(
bottleneck4, '2', ['3', '4', '5', '6']);
const bottleneck6 = await this.buildBottlenectV2_(
bottleneck5, '2', ['6', '7', '8', '9']);
const bottleneck7 = await this.buildBottlenectV2_(
bottleneck6, '2', ['9', '10', '11', '12']);
// Stage 3
const bottleneck8 = await this.buildBottlenectV2_(
bottleneck7, '3', ['0', '0', '1', '2'], true, 2);
const bottleneck9 = await this.buildBottlenectV2_(
bottleneck8, '3', ['3', '4', '5', '6']);
const bottleneck10 = await this.buildBottlenectV2_(
bottleneck9, '3', ['6', '7', '8', '9']);
const bottleneck11 = await this.buildBottlenectV2_(
bottleneck10, '3', ['9', '10', '11', '12']);
const bottleneck12 = await this.buildBottlenectV2_(
bottleneck11, '3', ['12', '13', '14', '15']);
const bottleneck13 = await this.buildBottlenectV2_(
bottleneck12, '3', ['15', '16', '17', '18']);
// Stage 4
const bottleneck14 = await this.buildBottlenectV2_(
bottleneck13, '4', ['0', '0', '1', '2'], true, 2);
const bottleneck15 = await this.buildBottlenectV2_(
bottleneck14, '4', ['3', '4', '5', '6']);
const bottleneck16 = await this.buildBottlenectV2_(
bottleneck15, '4', ['6', '7', '8', '9']);
const bn3 = await this.buildBatchNorm_(bottleneck16, '2', '');
const pool2 = await this.builder_.averagePool2d(bn3);
const reshape = this.builder_.reshape(pool2, [1, 2048]);
const gemm = await this.buildGemm_(reshape, '0');
return this.builder_.softmax(gemm);
}
async build(outputOperand) {
this.graph_ = await this.builder_.build({'output': outputOperand});
}
// Release the constant tensors of a model
dispose() {
// dispose() is only available in webnn-polyfill
if (this.graph_ !== null && 'dispose' in this.graph_) {
this.graph_.dispose();
}
}
async compute(inputBuffer, outputBuffer) {
const inputs = {'input': inputBuffer};
const outputs = {'output': outputBuffer};
const results = await this.context_.compute(this.graph_, inputs, outputs);
return results;
}
}