-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathface_landmark_nchw.js
129 lines (111 loc) · 4.09 KB
/
face_landmark_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
'use strict';
import {buildConstantByNpy, weightsOrigin} from '../common/utils.js';
// SimpleCNN model with 'nchw' layout.
export class FaceLandmarkNchw {
constructor() {
this.context_ = null;
this.builder_ = null;
this.graph_ = null;
this.inputTensor_ = null;
this.outputTensor_ = null;
this.weightsUrl_ = weightsOrigin() +
'/test-data/models/face_landmark_nchw/weights';
this.inputOptions = {
inputLayout: 'nchw',
inputShape: [1, 3, 128, 128],
};
this.outputShape_ = [1, 136];
}
async buildMaxPool2d(input, options) {
return this.builder_.maxPool2d(await input, options);
}
async buildConv_(input, indice) {
const prefix = `${this.weightsUrl_}/conv2d`;
let weightSuffix = '_kernel.npy';
let biasSuffix = `_Conv2D_bias.npy`;
if (indice > 0) {
weightSuffix = `_${indice}${weightSuffix}`;
biasSuffix = `_${indice + 1}${biasSuffix}`;
}
const weightsName = prefix + weightSuffix;
const weights = buildConstantByNpy(this.builder_, weightsName);
const biasName = prefix + biasSuffix;
const bias = buildConstantByNpy(this.builder_, biasName);
const options = {
bias: await bias,
};
const conv2d = this.builder_.conv2d(await input, await weights, options);
return this.builder_.relu(conv2d);
}
async buildGemm_(input, namePrefix, relu = false, reshapeSize) {
const weights = buildConstantByNpy(this.builder_,
`${this.weightsUrl_}/${namePrefix}_kernel_transpose.npy`);
const bias = buildConstantByNpy(this.builder_,
`${this.weightsUrl_}/${namePrefix}_MatMul_bias.npy`);
const options = {
aTranspose: false,
bTranspose: true,
c: await bias,
};
let gemm;
if (reshapeSize !== undefined) {
gemm = this.builder_.gemm(this.builder_.reshape(
this.builder_.transpose(await input, {permutation: [0, 2, 3, 1]}),
[1, reshapeSize]), await weights, options);
} else {
gemm = this.builder_.gemm(await input, await weights, options);
}
if (relu) {
gemm = this.builder_.relu(gemm);
}
return gemm;
}
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 input = 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 poolOptions =
{windowDimensions: [2, 2], strides: [2, 2]};
const conv0 = this.buildConv_(input, 0);
const pool0 = this.buildMaxPool2d(conv0, poolOptions);
const conv1 = this.buildConv_(pool0, 1);
const conv2 = this.buildConv_(conv1, 2);
const pool1 = this.buildMaxPool2d(conv2, poolOptions);
const conv3 = this.buildConv_(pool1, 3);
const conv4 = this.buildConv_(conv3, 4);
const pool2 = this.buildMaxPool2d(conv4, poolOptions);
const conv5 = this.buildConv_(pool2, 5);
const conv6 = this.buildConv_(conv5, 6);
const pool3 = this.buildMaxPool2d(conv6, {windowDimensions: [2, 2]});
const conv7 = this.buildConv_(pool3, 7);
const gemm0 = this.buildGemm_(conv7, 'dense', true, 6400);
const gemm1 = this.buildGemm_(gemm0, 'logits');
return await gemm1;
}
async build(outputOperand) {
this.graph_ = await this.builder_.build({'output': outputOperand});
}
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);
}
}