-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
132 lines (128 loc) · 4.21 KB
/
main.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
import { Niivue } from '@niivue/niivue'
// IMPORTANT: we need to import this specific file.
import meshnet from "./net.js"
async function main() {
clipCheck.onchange = function () {
if (clipCheck.checked) {
nv1.setClipPlane([0, 0, 90])
} else {
nv1.setClipPlane([2, 0, 90])
}
}
opacitySlider0.oninput = function () {
nv1.setOpacity(0, opacitySlider0.value / 255)
nv1.updateGLVolume()
}
opacitySlider1.oninput = function () {
nv1.setOpacity(1, opacitySlider1.value / 255)
}
function doLoadImage() {
opacitySlider0.oninput()
}
async function fetchJSON(fnm) {
const response = await fetch(fnm)
const js = await response.json()
return js
}
saveImgBtn.onclick = function () {
nv1.volumes[1].saveToDisk('Custom.nii')
}
aboutBtn.onclick = function () {
const url = "https://github.com/niivue/niivue-tinygrad"
window.open(url, "_blank")
}
async function ensureConformed() {
const nii = nv1.volumes[0]
let isConformed = nii.dims[1] === 256 && nii.dims[2] === 256 && nii.dims[3] === 256
if (nii.permRAS[0] !== -1 || nii.permRAS[1] !== 3 || nii.permRAS[2] !== -2) {
isConformed = false
}
if (isConformed) {
return
}
const nii2 = await nv1.conform(nii, false)
await nv1.removeVolume(nv1.volumes[0])
await nv1.addVolume(nii2)
}
async function closeAllOverlays() {
while (nv1.volumes.length > 1) {
await nv1.removeVolume(nv1.volumes[1])
}
}
const getDevice = async () => {
if (!navigator.gpu) return false;
const requiredLimits = {};
const maxBufferSize = 335544320;
requiredLimits.maxStorageBufferBindingSize = maxBufferSize;
requiredLimits.maxBufferSize = maxBufferSize;
const adapter = await navigator.gpu.requestAdapter();
return await adapter.requestDevice({
requiredLimits: requiredLimits,
requiredFeatures: ["shader-f16"]
});
};
segmentBtn.onclick = async function () {
if (nv1.volumes.length < 1) {
window.alert('Please open a voxel-based image')
return
}
const startTime = Date.now();
loadingCircle.classList.remove('hidden')
await closeAllOverlays()
await ensureConformed()
let img32 = new Float32Array(nv1.volumes[0].img)
// normalize input data to range 0..1
let mx = img32[0]
let mn = mx
for (let i = 0; i < img32.length; i++) {
mx = Math.max(mx, img32[i])
mn = Math.min(mn, img32[i])
}
let scale32 = 1 / (mx - mn)
for (let i = 0; i < img32.length; i++) {
img32[i] = (img32[i] - mn) * scale32
}
// Setup tinygrad meshnet model: get a WebGPU device, load weights
const device = await getDevice();
const session = await meshnet.load(device, "./net.safetensors");
const shape = [1, 1, 256, 256, 256]
const nvox = shape.reduce((a, b) => a * b)
if (img32.length !== nvox) {
throw new Error(`img32 length (${img32.length}) does not match expected tensor length (${expectedLength})`)
}
// run tinygrad inference
const results = await session(img32);
// Can be multi-tensor output, but this model only produces a single output
const argMaxImg = results[0];
const segmentImg = nv1.cloneVolume(0)
segmentImg.img = argMaxImg
segmentImg.hdr.datatypeCode = 16 // = float32
segmentImg.hdr.dims[4] = 1
segmentImg.trustCalMinMax = false
// Add the output to niivue
const cmap = await fetchJSON('./colormap3.json')
segmentImg.setColormapLabel(cmap)
segmentImg.opacity = opacitySlider1.value / 255
await nv1.addVolume(segmentImg)
loadingCircle.classList.add('hidden')
const elapsedTime = Date.now() - startTime
document.getElementById("intensity").innerHTML = `${elapsedTime}ms to segment`
}
function handleLocationChange(data) {
document.getElementById("intensity").innerHTML = data.string
}
const defaults = {
backColor: [0.4, 0.4, 0.4, 1],
onLocationChange: handleLocationChange,
}
const nv1 = new Niivue(defaults)
nv1.attachToCanvas(gl1)
nv1.opts.multiplanarForceRender = true
nv1.opts.yoke3Dto2DZoom = true
nv1.opts.crosshairGap = 11
nv1.setInterpolation(true)
nv1.onImageLoaded = doLoadImage
await nv1.loadVolumes([{ url: './t1_crop.nii.gz' }])
segmentBtn.onclick()
}
main()