forked from webmachinelearning/webnn-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssd_mobilenetv2_face_nchw.js
256 lines (234 loc) · 9.34 KB
/
ssd_mobilenetv2_face_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
'use strict';
import {buildConstantByNpy, computePadding2DForAutoPad} from '../common/utils.js';
// SSD MobileNet V2 Face model with 'nchw' layout.
export class SsdMobilenetV2FaceNchw {
constructor() {
this.context_ = null;
this.deviceType_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/ssd_mobilenetv2_face_nchw/weights/';
if (location.hostname.toLowerCase().indexOf('github.io') > -1) {
this.weightsUrl_ = 'https://d3i5xkfad89fac.cloudfront.net/test-data/models/ssd_mobilenetv2_face_nchw/weights/';
}
this.inputOptions = {
inputLayout: 'nchw',
margin: [1.2, 1.2, 0.8, 1.1],
mean: [127.5, 127.5, 127.5],
std: [127.5, 127.5, 127.5],
boxSize: 4,
numClasses: 2,
numBoxes: [1083, 600, 150, 54, 24, 6],
inputDimensions: [1, 3, 300, 300],
};
this.outputsInfo = {
'biasAdd0': [1, 12, 19, 19],
'biasAdd3': [1, 6, 19, 19],
'biasAdd6': [1, 24, 10, 10],
'biasAdd9': [1, 12, 10, 10],
'biasAdd12': [1, 24, 5, 5],
'biasAdd15': [1, 12, 5, 5],
'biasAdd18': [1, 24, 3, 3],
'biasAdd21': [1, 12, 3, 3],
'biasAdd24': [1, 24, 2, 2],
'biasAdd27': [1, 12, 2, 2],
'biasAdd30': [1, 24, 1, 1],
'biasAdd33': [1, 12, 1, 1],
};
}
async buildConv_(input, nameArray, clip = true, options = {}) {
// nameArray: 0: keyword, 1: indice or suffix
let prefix = this.weightsUrl_;
const weightSuffix = '_weights.npy';
let biasSuffix = '_Conv2D_bias.npy';
if (nameArray[0].includes('expanded')) {
prefix += 'FeatureExtractor_MobilenetV2_expanded_conv_';
if (nameArray[0].includes('depthwise')) {
prefix += nameArray[1] === '0' ?
'depthwise_depthwise' : `${nameArray[1]}_depthwise_depthwise`;
biasSuffix = '_bias.npy';
} else if (nameArray[0].includes('project')) {
prefix += nameArray[1] === '0' ? 'project' : `${nameArray[1]}_project`;
} else {
prefix += `${nameArray[1]}_expand`;
}
} else if (nameArray[0] === 'Class' || nameArray[0] === 'BoxEncoding') {
prefix += `BoxPredictor_${nameArray[1]}_${nameArray[0]}Predictor`;
} else if (nameArray[0].includes('layer')) { // layer_19_1 or layer_19_2
prefix += `FeatureExtractor_MobilenetV2_${nameArray[0]}_Conv2d_\
${nameArray[1]}`;
} else {
prefix += `${nameArray[0]}`;
}
const weightsName = prefix + weightSuffix;
const weights = buildConstantByNpy(this.builder_, weightsName);
const biasName = prefix + biasSuffix;
const bias = buildConstantByNpy(this.builder_, biasName);
const inputShape = (await input).shape();
const weightsShape = (await weights).shape();
options.padding = computePadding2DForAutoPad(
/* nchw */[inputShape[2], inputShape[3]],
/* oihw */[weightsShape[2], weightsShape[3]],
options.strides, options.dilations, 'same-upper');
options.bias = await bias;
if (clip) {
// TODO: Set clamp activation to options once it's supported in
// WebNN DML backend.
// Implement `clip` by `clamp` of WebNN API
if (this.deviceType_ == 'gpu') {
return this.builder_.clamp(
this.builder_.conv2d(await input, await weights, options),
{minValue: 0, maxValue: 6});
} else {
options.activation = this.builder_.clamp({minValue: 0, maxValue: 6});
}
}
return this.builder_.conv2d(await input, await weights, options);
}
async buildLinearBottleneck_(
input, indice, shortcut = true, groups, stridesNode) {
let convOptions;
const dwiseOptions = {groups};
const strides = [2, 2];
if (stridesNode === 'convRelu6') {
convOptions = {strides};
}
if (stridesNode === 'dwiseRelu6') {
dwiseOptions.strides = strides;
}
const convRelu6Keyword = indice === '0' ?
'FeatureExtractor_MobilenetV2_Conv' : 'expanded';
const convRelu6 = this.buildConv_(
input, [convRelu6Keyword, indice], true, convOptions);
const dwiseRelu6 = this.buildConv_(
convRelu6, ['expanded_depthwise', indice], true, dwiseOptions);
const convLinear = this.buildConv_(
dwiseRelu6, ['expanded_project', indice], false);
if (shortcut) {
return this.builder_.add(await input, await convLinear);
}
return await convLinear;
}
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.deviceType_ = contextOptions.deviceType;
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const bottleneck0 = this.buildLinearBottleneck_(
input, '0', false, 32, 'convRelu6');
const bottleneck1 = this.buildLinearBottleneck_(
bottleneck0, '1', false, 96, 'dwiseRelu6');
const bottleneck2 = this.buildLinearBottleneck_(
bottleneck1, '2', true, 144);
const bottleneck3 = this.buildLinearBottleneck_(
bottleneck2, '3', false, 144, 'dwiseRelu6');
const bottleneck4 = this.buildLinearBottleneck_(
bottleneck3, '4', true, 192);
const bottleneck5 = this.buildLinearBottleneck_(
bottleneck4, '5', true, 192);
const bottleneck6 = this.buildLinearBottleneck_(
bottleneck5, '6', false, 192, 'dwiseRelu6');
const bottleneck7 = this.buildLinearBottleneck_(
bottleneck6, '7', true, 384);
const bottleneck8 = this.buildLinearBottleneck_(
bottleneck7, '8', true, 384);
const bottleneck9 = this.buildLinearBottleneck_(
bottleneck8, '9', true, 384);
const bottleneck10 = this.buildLinearBottleneck_(
bottleneck9, '10', false, 384);
const bottleneck11 = this.buildLinearBottleneck_(
bottleneck10, '11', true, 576);
const bottleneck12 = this.buildLinearBottleneck_(
bottleneck11, '12', true, 576);
const conv13Relu6 = this.buildConv_(
bottleneck12, ['expanded', '13']);
const dwise13Relu6 = this.buildConv_(
conv13Relu6,
['expanded_depthwise', '13'],
true,
{groups: 576, strides: [2, 2]});
const convLinear13 = this.buildConv_(
dwise13Relu6, ['expanded_project', '13'], false);
const biasAdd0 = this.buildConv_(
conv13Relu6, ['BoxEncoding', '0'], false);
const biasAdd3 = this.buildConv_(
conv13Relu6, ['Class', '0'], false);
const bottleneck14 = this.buildLinearBottleneck_(
convLinear13, '14', true, 960);
const bottleneck15 = this.buildLinearBottleneck_(
bottleneck14, '15', true, 960);
const bottleneck16 = this.buildLinearBottleneck_(
bottleneck15, '16', false, 960);
const conv17Relu6 = this.buildConv_(
bottleneck16, ['FeatureExtractor_MobilenetV2_Conv_1']);
const biasAdd6 = this.buildConv_(
conv17Relu6, ['BoxEncoding', '1'], false);
const biasAdd9 = this.buildConv_(
conv17Relu6, ['Class', '1'], false);
const conv18Relu6 = this.buildConv_(
conv17Relu6, ['layer_19_1', '2_1x1_256']);
const conv19Relu6 = this.buildConv_(
conv18Relu6, ['layer_19_2', '2_3x3_s2_512'], true, {strides: [2, 2]});
const biasAdd12 = this.buildConv_(
conv19Relu6, ['BoxEncoding', '2'], false);
const biasAdd15 = this.buildConv_(
conv19Relu6, ['Class', '2'], false);
const conv20Relu6 = this.buildConv_(
conv19Relu6, ['layer_19_1', '3_1x1_128']);
const conv21Relu6 = this.buildConv_(
conv20Relu6, ['layer_19_2', '3_3x3_s2_256'], true, {strides: [2, 2]});
const biasAdd18 = this.buildConv_(
conv21Relu6, ['BoxEncoding', '3'], false);
const biasAdd21 = this.buildConv_(
conv21Relu6, ['Class', '3'], false);
const conv22Relu6 = this.buildConv_(
conv21Relu6, ['layer_19_1', '4_1x1_128']);
const conv23Relu6 = this.buildConv_(
conv22Relu6, ['layer_19_2', '4_3x3_s2_256'], true, {strides: [2, 2]});
const biasAdd24 = this.buildConv_(
conv23Relu6, ['BoxEncoding', '4'], false);
const biasAdd27 = this.buildConv_(
conv23Relu6, ['Class', '4'], false);
const conv24Relu6 = this.buildConv_(
conv23Relu6, ['layer_19_1', '5_1x1_64']);
const conv25Relu6 = this.buildConv_(
conv24Relu6, ['layer_19_2', '5_3x3_s2_128'], true, {strides: [2, 2]});
const biasAdd30 = this.buildConv_(
conv25Relu6, ['BoxEncoding', '5'], false);
const biasAdd33 = this.buildConv_(
conv25Relu6, ['Class', '5'], false);
return {
biasAdd0: await biasAdd0,
biasAdd3: await biasAdd3,
biasAdd6: await biasAdd6,
biasAdd9: await biasAdd9,
biasAdd12: await biasAdd12,
biasAdd15: await biasAdd15,
biasAdd18: await biasAdd18,
biasAdd21: await biasAdd21,
biasAdd24: await biasAdd24,
biasAdd27: await biasAdd27,
biasAdd30: await biasAdd30,
biasAdd33: await biasAdd33,
};
}
async build(outputOperand) {
this.graph_ = await this.builder_.build(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, outputs) {
const inputs = {'input': inputBuffer};
const results = await this.context_.compute(this.graph_, inputs, outputs);
return results;
}
}