Skip to content

Commit 6d36dbd

Browse files
committed
refactor and make things run
1 parent 739896b commit 6d36dbd

File tree

6 files changed

+140
-63
lines changed

6 files changed

+140
-63
lines changed

.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.DS_Store
2+
node_modules
3+
/build
4+
/dist
5+
distForTests
6+
tests-out
7+
dist_intermediate
8+
devdocs
9+
/_md_docs
10+
/docs/niivue*.js
11+
/demos/niivue*.js
12+
/demos/dist
13+
/tests/niivue*.js
14+
niivue.es.js
15+
niivue.umd.js
16+
/downloads
17+
__diff_output__
18+
/coverage
19+
# local env files
20+
.env.local
21+
.env.*.local
22+
23+
# Log files
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
pnpm-debug.log*
28+
29+
# Editor directories and files
30+
.idea
31+
.vscode
32+
*.suo
33+
*.ntvs*
34+
*.njsproj
35+
*.sln
36+
*.sw?
37+
.DS_Store
38+
/test-results/
39+
/playwright-report/
40+
/blob-report/
41+
/playwright/.cache/
42+
/playwright/e2e/index.js
43+
/tests/index.js

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ You can serve a hot-reloadable web page that allows you to interactively modify
88

99
```bash
1010
git [email protected]:neurolabusc/niivue-onnx.git
11-
cd niivue-neglect
11+
cd niivue-onnx
1212
npm install
1313
npm run dev
1414
```
1515

16+
#### to build and serve the built version
17+
18+
```bash
19+
npm run build
20+
npx http-server dist/
21+
```
22+

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta charset="UTF-8" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<link rel="stylesheet" href="./niivue.css" />
8-
<title>Niivue Neglect Predictions</title>
8+
<title>Niivue ONNX</title>
99
</head>
1010

1111
<body>

main.js

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Niivue } from '@niivue/niivue'
2-
import * as ort from 'onnxruntime-web';
2+
// IMPORTANT: we need to import this specific file.
3+
import * as ort from "./node_modules/onnxruntime-web/dist/ort.all.mjs"
4+
console.log(ort);
35
async function main() {
46
aboutBtn.onclick = function () {
57
let url = "https://github.com/axinging/mlmodel-convension-demo/blob/main/onnx/onnx-brainchop.html"
@@ -17,79 +19,103 @@ async function main() {
1719
const nv1 = new Niivue(defaults)
1820
nv1.attachToCanvas(gl1)
1921
await nv1.loadVolumes([{ url: './t1_crop.nii.gz' }])
22+
// FIXME: Do we want to conform?
23+
const conformed = await nv1.conform(
24+
nv1.volumes[0],
25+
false,
26+
true,
27+
true
28+
)
29+
nv1.removeVolume(nv1.volumes[0])
30+
nv1.addVolume(conformed)
2031

2132
let feedsInfo = [];
2233
function getFeedInfo(feed, type, data, dims) {
23-
const warmupTimes = 0;
24-
const runTimes = 1;
25-
for (let i = 0; i < warmupTimes + runTimes; i++) {
26-
let typedArray;
27-
let typeBytes;
28-
if (type === 'bool') {
29-
data = [data];
30-
dims = [1];
31-
typeBytes = 1;
32-
} else if (type === 'int8') {
33-
typedArray = Int8Array;
34-
} else if (type === 'float16') {
35-
typedArray = Uint16Array;
36-
} else if (type === 'int32') {
37-
typedArray = Int32Array;
38-
} else if (type === 'uint32') {
39-
typedArray = Uint32Array;
40-
} else if (type === 'float32') {
41-
typedArray = Float32Array;
42-
} else if (type === 'int64') {
43-
typedArray = BigInt64Array;
44-
}
45-
if (typeBytes === undefined) {
46-
typeBytes = typedArray.BYTES_PER_ELEMENT;
47-
}
34+
const warmupTimes = 0;
35+
const runTimes = 1;
36+
for (let i = 0; i < warmupTimes + runTimes; i++) {
37+
let typedArray;
38+
let typeBytes;
39+
if (type === 'bool') {
40+
data = [data];
41+
dims = [1];
42+
typeBytes = 1;
43+
} else if (type === 'int8') {
44+
typedArray = Int8Array;
45+
} else if (type === 'float16') {
46+
typedArray = Uint16Array;
47+
} else if (type === 'int32') {
48+
typedArray = Int32Array;
49+
} else if (type === 'uint32') {
50+
typedArray = Uint32Array;
51+
} else if (type === 'float32') {
52+
typedArray = Float32Array;
53+
} else if (type === 'int64') {
54+
typedArray = BigInt64Array;
55+
}
56+
if (typeBytes === undefined) {
57+
typeBytes = typedArray.BYTES_PER_ELEMENT;
58+
}
4859

49-
let size, _data;
50-
if (Array.isArray(data) || ArrayBuffer.isView(data)) {
51-
size = data.length;
52-
_data = data;
53-
} else {
54-
size = dims.reduce((a, b) => a * b);
55-
if (data === 'random') {
56-
_data = typedArray.from({ length: size }, () => getRandom(type));
57-
} else {
58-
_data = typedArray.from({ length: size }, () => data);
59-
}
60-
}
60+
let size, _data;
61+
if (Array.isArray(data) || ArrayBuffer.isView(data)) {
62+
size = data.length;
63+
_data = data;
64+
} else {
65+
size = dims.reduce((a, b) => a * b);
66+
if (data === 'random') {
67+
_data = typedArray.from({ length: size }, () => getRandom(type));
68+
} else {
69+
_data = typedArray.from({ length: size }, () => data);
70+
}
71+
}
6172

62-
if (i > feedsInfo.length - 1) {
63-
feedsInfo.push(new Map());
64-
}
65-
feedsInfo[i].set(feed, [type, _data, dims, Math.ceil(size * typeBytes / 16) * 16]);
73+
if (i > feedsInfo.length - 1) {
74+
feedsInfo.push(new Map());
6675
}
67-
return feedsInfo;
76+
feedsInfo[i].set(feed, [type, _data, dims, Math.ceil(size * typeBytes / 16) * 16]);
77+
}
78+
return feedsInfo;
6879
}
6980
const option = {
70-
executionProviders: [
71-
{
72-
//name: 'webgpu',
73-
name: 'webgl',
74-
},
75-
],
76-
graphOptimizationLevel: 'extended',
77-
optimizedModelFilepath: 'opt.onnx'
81+
executionProviders: [
82+
{
83+
name: 'webgpu',
84+
},
85+
],
86+
graphOptimizationLevel: 'extended',
87+
optimizedModelFilepath: 'opt.onnx'
7888
};
7989

8090
const session = await ort.InferenceSession.create('./model_5_channels.onnx', option);
8191
const shape = [1, 1, 256, 256, 256];
82-
const temp = getFeedInfo("input.1", "float32", 0, shape);
92+
// FIXME: Do we want to use a real image for inference?
93+
const imgData = nv1.volumes[0].img;
94+
const expectedLength = shape.reduce((a, b) => a * b);
95+
// FIXME: Do we need want this?
96+
if (imgData.length !== expectedLength) {
97+
throw new Error(`imgData length (${imgData.length}) does not match expected tensor length (${expectedLength})`);
98+
}
99+
100+
const temp = getFeedInfo("input.1", "float32", imgData, shape);
83101
let dataA = temp[0].get('input.1')[1];
84-
// let dataTemp = await loadJSON("./onnx-branchchop-input64.jsonc");
85-
// dataA = dataTemp['data'];
86102
const tensorA = new ort.Tensor('float32', dataA, shape);
87-
103+
88104
const feeds = { "input.1": tensorA };
89105
// feed inputs and run
90106
console.log("before run");
91107
const results = await session.run(feeds);
92-
console.log("after run");
108+
console.log(results);
109+
console.log("after run")
110+
// FIXME: is this really the output data? It doesn't make sense when rendered,
111+
// but then again, maybe the input was wrong?
112+
const outData = results[39].data
113+
const newImg = nv1.cloneVolume(0);
114+
newImg.img = outData
115+
// Add the output to niivue
116+
nv1.addVolume(newImg)
117+
nv1.setColormap(newImg.id, "red")
118+
nv1.setOpacity(1, 0.5)
93119
}
94120

95121
main()

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
8-
"build": "vite build",
8+
"build": "vite build && npm run copyMJS && npm run copyWASM",
9+
"copyMJS": "cp ./node_modules/onnxruntime-web/dist/*.mjs ./dist/assets/",
10+
"copyWASM": "cp ./node_modules/onnxruntime-web/dist/*.wasm ./dist/assets/",
911
"preview": "vite preview"
1012
},
1113
"dependencies": {
12-
"@niivue/niivue":"^0.43.3",
13-
"onnxruntime-web": "1.19.0-dev.20240713-281ed8c12d"
14+
"@niivue/niivue": "^0.43.3",
15+
"onnxruntime-web": "^1.19.0-dev.20240713-281ed8c12d"
1416
},
1517
"devDependencies": {
1618
"vite": "^5.2.0"

0 commit comments

Comments
 (0)