-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
138 lines (118 loc) · 3.78 KB
/
test.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
function get_data(window, name) {
return cy
.exec(
`cargo run --bin test_reference -- ${name} ../quality/tests/inputs.json ../quality/tests/outputs.json`
)
.then(() => {
return Promise.all([
fetch("/quality/tests/inputs.json").then((response) => response.json()),
fetch("/quality/tests/outputs.json").then((response) =>
response.json()
),
]);
});
}
function round(x) {
return Math.round(x * 200) / 200;
}
function predictAndCompare(model, inputs, refOutputs) {
cy.wrap(
model.predict(inputs).then((outputs) => {
assert.equal(outputs.length, refOutputs.length);
for (let i = 0; i < outputs.length; i++) {
assert.equal(outputs[i].data.length, refOutputs[i].data.length);
assert.deepEqual(outputs[i].shape, refOutputs[i].shape);
for (let j = 0; j < outputs[i].data; j++) {
assert.equal(round(outputs[i].data[j], round(refOutputs[i].data[j])));
}
}
})
);
}
it("can run ONNX model (no options)", () => {
cy.visit("/quality/tests/index.html");
cy.window().then((window) => {
const tractjs = window.tractjs;
get_data(window, "simple_onnx").then(([refInputs, refOutputs]) => {
tractjs
.load("/quality/models/data/squeezenet_1_1/model.onnx")
.then((model) => {
const inputs = refInputs.map((refInput) => {
return new tractjs.Tensor(
new Float32Array(refInput.data),
refInput.shape
);
});
predictAndCompare(model, inputs, refOutputs);
});
});
});
});
it("can run TF model (with input facts)", () => {
cy.visit("/quality/tests/index.html");
cy.window().then((window) => {
const tractjs = window.tractjs;
get_data(window, "simple_tf").then(([refInputs, refOutputs]) => {
tractjs
.load("/quality/models/data/squeezenet_1_1/model.pb", {
inputFacts: {
0: ["float32", [1, 227, 227, 3]],
},
})
.then((model) => {
const inputs = refInputs.map((refInput) => {
return new tractjs.Tensor(
new Float32Array(refInput.data),
refInput.shape
);
});
predictAndCompare(model, inputs, refOutputs);
});
});
});
});
it("can run ONNX model (with custom outputs)", () => {
cy.visit("/quality/tests/index.html");
cy.window().then((window) => {
const tractjs = window.tractjs;
get_data(window, "custom_output_onnx").then(([refInputs, refOutputs]) => {
tractjs
.load("/quality/models/data/squeezenet_1_1/model.onnx", {
outputs: ["squeezenet0_conv8_fwd", "squeezenet0_conv9_fwd"],
})
.then((model) => {
const inputs = refInputs.map((refInput) => {
return new tractjs.Tensor(
new Float32Array(refInput.data),
refInput.shape
);
});
predictAndCompare(model, inputs, refOutputs);
});
});
});
});
it("can run TF model (with custom inputs)", () => {
cy.visit("/quality/tests/index.html");
cy.window().then((window) => {
const tractjs = window.tractjs;
get_data(window, "custom_input_tf").then(([refInputs, refOutputs]) => {
tractjs
.load("/quality/models/data/squeezenet_1_1/model.pb", {
inputFacts: {
0: ["float32", [1, 227, 227, 3]],
},
inputs: ["fire5/relu_expand1x1/Relu", "fire5/relu_expand3x3/Relu"],
})
.then((model) => {
const inputs = refInputs.map((refInput) => {
return new tractjs.Tensor(
new Float32Array(refInput.data),
refInput.shape
);
});
predictAndCompare(model, inputs, refOutputs);
});
});
});
});