forked from webmachinelearning/webnn-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deeplabv3_mnv2_nhwc.js
162 lines (152 loc) · 6.68 KB
/
deeplabv3_mnv2_nhwc.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
'use strict';
import {buildConstantByNpy} from '../common/utils.js';
// DeepLab V3 MobileNet V2 model with 'nhwc' input layout
export class DeepLabV3MNV2Nhwc {
constructor() {
this.context_ = null;
this.devicePreference_ = null;
this.builder_ = null;
this.graph_ = null;
this.weightsUrl_ = '../test-data/models/deeplabv3_mnv2_nhwc/weights/';
this.inputOptions = {
mean: [127.5, 127.5, 127.5],
std: [127.5, 127.5, 127.5],
scaledFlag: true,
inputLayout: 'nhwc',
labelUrl: './labels/labels.txt',
inputDimensions: [1, 513, 513, 3],
};
this.outputDimensions = [1, 513, 513, 21];
}
async buildConv_(
input, namePrefix, dwBiasSuffix = '', relu6 = true, options = {}) {
const prefix = this.weightsUrl_ + namePrefix;
let weightsName = prefix + '.npy';
let biasName = prefix + '_bn_offset.npy';
if (namePrefix.includes('depthwise')) {
weightsName = prefix + '_depthwise.npy';
biasName = `${prefix}_${dwBiasSuffix}.npy`;
} else if (namePrefix === 'logits_semantic') {
weightsName = prefix + '_Conv2D.npy';
biasName = prefix + '_biases.npy';
}
const weights = await buildConstantByNpy(this.builder_, weightsName);
const bias = await buildConstantByNpy(this.builder_, biasName);
options.inputLayout = 'nhwc';
options.autoPad = 'same-upper';
if (namePrefix.includes('depthwise')) {
options.filterLayout = 'ihwo';
} else {
options.filterLayout = 'ohwi';
}
options.bias = bias;
if (relu6) {
// TODO: Set clamp activation to options once it's supported in
// WebNN DML backend.
// Implement `clip` by `clamp` of WebNN API
if (this.devicePreference_ == 'gpu') {
return this.builder_.clamp(
this.builder_.conv2d(input, weights, options),
{minValue: 0, maxValue: 6});
} else {
options.activation = this.builder_.clamp({minValue: 0, maxValue: 6});
}
}
return this.builder_.conv2d(input, weights, options);
}
async buildLinearBottleneck_(
input, nameIndice, dwiseOptions, shortcut = true) {
const namePrefix = 'MobilenetV2_expanded_conv_' + nameIndice;
let dwBiasSuffix = 'depthwise_bn_offset';
if (Number.parseInt(nameIndice) > 6) {
dwBiasSuffix = 'BatchNorm_FusedBatchNorm';
}
const conv1x1Relu6 = await this.buildConv_(
input, `${namePrefix}_expand_Conv2D`);
const dwise3x3Relu6 = await this.buildConv_(
conv1x1Relu6, `${namePrefix}_depthwise`,
dwBiasSuffix, true, dwiseOptions);
const conv1x1Linear = await this.buildConv_(
dwise3x3Relu6, `${namePrefix}_project_Conv2D`, '', false);
if (shortcut) {
return this.builder_.add(input, conv1x1Linear);
}
return conv1x1Linear;
}
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.devicePreference_ = contextOptions.devicePreference;
this.builder_ = new MLGraphBuilder(this.context_);
const strides = [2, 2];
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const conv0 = await this.buildConv_(
input, 'MobilenetV2_Conv_Conv2D', '', true, {strides});
const conv1 = await this.buildConv_(
conv0, 'MobilenetV2_expanded_conv_depthwise',
'depthwise_bn_offset', true, {groups: 32});
const conv2 = await this.buildConv_(
conv1, 'MobilenetV2_expanded_conv_project_Conv2D', '', false);
const bottleneck0 = await this.buildLinearBottleneck_(
conv2, '1', {strides, groups: 96}, false);
const bottleneck1 = await this.buildLinearBottleneck_(
bottleneck0, '2', {groups: 144});
const bottleneck2 = await this.buildLinearBottleneck_(
bottleneck1, '3', {strides, groups: 144}, false);
const bottleneck3 = await this.buildLinearBottleneck_(
bottleneck2, '4', {groups: 192});
const bottleneck4 = await this.buildLinearBottleneck_(
bottleneck3, '5', {groups: 192});
const bottleneck5 = await this.buildLinearBottleneck_(
bottleneck4, '6', {groups: 192}, false);
const bottleneck6 = await this.buildLinearBottleneck_(
bottleneck5, '7', {dilations: [2, 2], groups: 384});
const bottleneck7 = await this.buildLinearBottleneck_(
bottleneck6, '8', {dilations: [2, 2], groups: 384});
const bottleneck8 = await this.buildLinearBottleneck_(
bottleneck7, '9', {dilations: [2, 2], groups: 384});
const bottleneck9 = await this.buildLinearBottleneck_(
bottleneck8, '10', {dilations: [2, 2], groups: 384}, false);
const bottleneck10 = await this.buildLinearBottleneck_(
bottleneck9, '11', {dilations: [2, 2], groups: 576});
const bottleneck11 = await this.buildLinearBottleneck_(
bottleneck10, '12', {dilations: [2, 2], groups: 576});
const bottleneck12 = await this.buildLinearBottleneck_(
bottleneck11, '13', {dilations: [2, 2], groups: 576}, false);
const bottleneck13 = await this.buildLinearBottleneck_(
bottleneck12, '14', {dilations: [4, 4], groups: 960});
const bottleneck14 = await this.buildLinearBottleneck_(
bottleneck13, '15', {dilations: [4, 4], groups: 960});
const bottleneck15 = await this.buildLinearBottleneck_(
bottleneck14, '16', {dilations: [4, 4], groups: 960}, false);
const conv3 = await this.buildConv_(bottleneck15, 'aspp0_Conv2D');
const averagePool2d = this.builder_.averagePool2d(bottleneck15,
{windowDimensions: [65, 65], strides: [65, 65], layout: 'nhwc'});
const conv4 = await this.buildConv_(averagePool2d, 'image_pooling_Conv2D');
const resample0 = this.builder_.resample2d(
conv4, {sizes: [65, 65], mode: 'linear', axes: [1, 2]});
const concat = this.builder_.concat([resample0, conv3], 3);
const conv5 = await this.buildConv_(concat, 'concat_projection_Conv2D');
const conv6 = await this.buildConv_(conv5, 'logits_semantic', '', false);
const resample1 = this.builder_.resample2d(
conv6, {sizes: [65, 65], mode: 'linear', axes: [1, 2]});
return this.builder_.resample2d(
resample1, {sizes: [513, 513], mode: 'linear', axes: [1, 2]});
}
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;
}
}