-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (41 loc) · 1.79 KB
/
Copy pathindex.js
File metadata and controls
58 lines (41 loc) · 1.79 KB
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
async function readJSON (event) {
let str = event.target.result;
let json = JSON.parse(str);
const session = await ort.InferenceSession.create('./colNet.onnx');
//const session = await ort.InferenceSession.create('https://github.com/Entenzahn/adl_ws22/releases/download/v0.1-beta/keyNet.onnx');
/*j = await fetch("./tensor.json")
.then(res => res.json())
.then(json => {
// Do whatever you want
// console.log(json)
return json;
});*/
console.log(json)
// prepare inputs. a tensor need its corresponding TypedArray as data
const x_tensor = new ort.Tensor('float32', json.flat(), [1,1,192,646])
console.log(x_tensor)
// prepare feeds. use model input names as keys.
const feeds = { 'x': x_tensor };
// feed inputs and run
const results = await session.run(feeds);
// read from results
const pred = results.y_hat.data;
const max_class = pred.indexOf(Math.max.apply(Math, pred));
const keys = ['A major', 'A minor', 'Ab major', 'Ab minor', 'B major', 'B minor',
'Bb major', 'Bb minor', 'C major', 'C minor', 'D major', 'D minor',
'Db major', 'Db minor', 'E major', 'E minor', 'Eb major',
'Eb minor', 'F major', 'F minor', 'G major', 'G minor', 'Gb major',
'Gb minor']
document.getElementById('predictions').innerHTML = 'Predicted Key: '+ keys[max_class];
}
async function predictKey(event) {
try {
event.preventDefault();
if (!file.value.length) return;
let reader = new FileReader();
reader.onload = readJSON;
reader.readAsText(file.files[0]);
} catch (e) {
document.getElementById('predictions').innerHTML = 'failed to inference ONNX model:'+e;
}
}