-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathresnet50v2_nchw.js
186 lines (171 loc) · 6.95 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
178
179
180
181
182
183
184
185
186
'use strict';
import {buildConstantByNpy, weightsOrigin} 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.inputTensor_ = null;
this.outputTensor_ = null;
this.weightsUrl_ = weightsOrigin() +
'/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',
inputShape: [1, 3, 224, 224],
};
this.outputShape_ = [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 = buildConstantByNpy(this.builder_, weightName);
return this.builder_.conv2d(await input, await 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 = buildConstantByNpy(this.builder_, scaleName);
const bias = buildConstantByNpy(this.builder_, biasName);
const mean = buildConstantByNpy(this.builder_, meanName);
const variance = buildConstantByNpy(this.builder_, varName);
const options = {scale: await scale, bias: await bias};
const batchnorm = this.builder_.batchNormalization(
await input,
await mean,
await variance,
options,
);
return relu ? this.builder_.relu(batchnorm) : batchnorm;
}
async buildGemm_(input, name) {
const prefix = this.weightsUrl_ + 'resnetv24_dense' + name;
const weightName = prefix + '_weight.npy';
const weight = buildConstantByNpy(this.builder_, weightName);
const biasName = prefix + '_bias.npy';
const bias = buildConstantByNpy(this.builder_, biasName);
const options =
{c: this.builder_.reshape(await bias, [1, 1000]), bTranspose: true};
return this.builder_.gemm(await input, await 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 = this.buildBatchNorm_(input, nameIndices[0], stageName);
const conv1 = this.buildConv_(bn1, nameIndices[1], stageName);
const bn2 = this.buildBatchNorm_(
conv1, parseInt(nameIndices[0]) + 1, stageName);
const conv2 = this.buildConv_(
bn2, nameIndices[2], stageName, {padding: [1, 1, 1, 1], strides});
const bn3 = this.buildBatchNorm_(
conv2, parseInt(nameIndices[0]) + 2, stageName);
const conv3 = this.buildConv_(bn3, nameIndices[3], stageName);
if (downsample) {
residual = this.buildConv_(
bn1, parseInt(nameIndices[0]) + 3, stageName, {strides});
}
return this.builder_.add(await conv3, await residual);
}
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const inputDesc = {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
};
const data = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
const bn1 = this.buildBatchNorm_(data, '0', '', false);
const conv0 = this.buildConv_(
bn1, '0', '', {padding: [3, 3, 3, 3], strides: [2, 2]});
const bn2 = this.buildBatchNorm_(conv0, '1', '');
const pool1 = this.builder_.maxPool2d(await bn2,
{windowDimensions: [3, 3], padding: [1, 1, 1, 1], strides: [2, 2]});
// Stage 1
const bottleneck1 = this.buildBottlenectV2_(
pool1, '1', ['0', '0', '1', '2'], true);
const bottleneck2 = this.buildBottlenectV2_(
bottleneck1, '1', ['3', '4', '5', '6']);
const bottleneck3 = this.buildBottlenectV2_(
bottleneck2, '1', ['6', '7', '8', '9']);
// Stage 2
const bottleneck4 = this.buildBottlenectV2_(
bottleneck3, '2', ['0', '0', '1', '2'], true, 2);
const bottleneck5 = this.buildBottlenectV2_(
bottleneck4, '2', ['3', '4', '5', '6']);
const bottleneck6 = this.buildBottlenectV2_(
bottleneck5, '2', ['6', '7', '8', '9']);
const bottleneck7 = this.buildBottlenectV2_(
bottleneck6, '2', ['9', '10', '11', '12']);
// Stage 3
const bottleneck8 = this.buildBottlenectV2_(
bottleneck7, '3', ['0', '0', '1', '2'], true, 2);
const bottleneck9 = this.buildBottlenectV2_(
bottleneck8, '3', ['3', '4', '5', '6']);
const bottleneck10 = this.buildBottlenectV2_(
bottleneck9, '3', ['6', '7', '8', '9']);
const bottleneck11 = this.buildBottlenectV2_(
bottleneck10, '3', ['9', '10', '11', '12']);
const bottleneck12 = this.buildBottlenectV2_(
bottleneck11, '3', ['12', '13', '14', '15']);
const bottleneck13 = this.buildBottlenectV2_(
bottleneck12, '3', ['15', '16', '17', '18']);
// Stage 4
const bottleneck14 = this.buildBottlenectV2_(
bottleneck13, '4', ['0', '0', '1', '2'], true, 2);
const bottleneck15 = this.buildBottlenectV2_(
bottleneck14, '4', ['3', '4', '5', '6']);
const bottleneck16 = this.buildBottlenectV2_(
bottleneck15, '4', ['6', '7', '8', '9']);
const bn3 = this.buildBatchNorm_(bottleneck16, '2', '');
const pool2 = this.builder_.averagePool2d(await bn3);
const reshape = this.builder_.reshape(await pool2, [1, 2048]);
const gemm = this.buildGemm_(await reshape, '0');
return this.builder_.softmax(await gemm);
}
async build(outputOperand) {
this.graph_ = await this.builder_.build({'output': outputOperand});
}
// Release the constant tensors of a model
async compute(inputBuffer) {
this.context_.writeTensor(this.inputTensor_, inputBuffer);
const inputs = {'input': this.inputTensor_};
const outputs = {'output': this.outputTensor_};
this.context_.dispatch(this.graph_, inputs, outputs);
const results = await this.context_.readTensor(this.outputTensor_);
return new Float32Array(results);
}
}