Skip to content

Commit

Permalink
fix optimized bwlabel, bump tfjs to current (4.18)
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Apr 29, 2024
1 parent d414c24 commit 17d1950
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 105 deletions.
12 changes: 1 addition & 11 deletions brainchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -1682,12 +1682,7 @@ async function inferenceFullVolumeSeqCovLayerPhase2(
const seqConvLayer = await new SequentialConvLayer(res, 10, isChannelLast, callbackUI)

// Apply the last output tensor to the seq. instance
let outputTensor = null
const profileInfo = await tf.profile(async () => {
// Your tensor operations here
outputTensor = await seqConvLayer.apply(curTensor[i])
})
console.log('profileInfo : ', profileInfo)
let outputTensor = await seqConvLayer.apply(curTensor[i])

// -- document.getElementById("progressBarChild").style.width = 0 + "%";;

Expand Down Expand Up @@ -2048,7 +2043,6 @@ async function inferenceFullVolumePhase2(
// ???? subsequent await are required
const timer = window.setInterval(async function () {
try {
// -- curTensor[i] = res.layers[i].apply( curTensor[i-1])
curTensor[i] = res.layers[i].apply(curTensor[i - 1])
} catch (err) {
callbackUI(err.message, -1, err.message)
Expand Down Expand Up @@ -2082,10 +2076,6 @@ async function inferenceFullVolumePhase2(
if (i === layersLength - 1) {
window.clearInterval(timer)

// prediction = res.layers[res.layers.length-1].apply(curTensor[i])
// curTensor[i].print()
// outputDataBeforArgmx = Array.from(curTensor[i].dataSync())

const axis = isChannelLast ? -1 : 1
console.log(' find argmax ')
console.log('last Tensor shape : ', curTensor[i].shape)
Expand Down
13 changes: 7 additions & 6 deletions bwlabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export class BWLabeler {

// provisionally label all voxels in volume
do_initial_labelling(bw, dim, conn) {
const naboPS = new Uint32Array(27)
const tn = new Uint32Array(27 + 5)
const naboPS = new Uint32Array(32)
const tn = new Uint32Array(32)
let label = 1
const kGrowArrayBy = 8192
let ttn = kGrowArrayBy
Expand Down Expand Up @@ -157,19 +157,20 @@ export class BWLabeler {

// translation table unifies a region that has been assigned multiple classes
fill_tratab(tt, nabo, nr_set, tn) {
let cntr = 0
// let cntr = 0
tn.fill(0)
const INT_MAX = 2147483647
let ltn = INT_MAX
for (let i = 0; i < nr_set; i++) {
let j = nabo[i]
cntr = 0
// cntr = 0
while (tt[j - 1] !== j) {
j = tt[j - 1]
cntr++
/* cntr++
if (cntr > 100) {
console.log('\nOoh no!!')
break
}
} */
}
tn[i] = j
ltn = Math.min(ltn, j)
Expand Down
13 changes: 7 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ async function main() {
nv1.opts.multiplanarForceRender = true
nv1.opts.yoke3Dto2DZoom = true
await nv1.loadVolumes([{ url: "./t1_crop.nii.gz" }])

aboutBtn.onclick = function () {
window.alert("BrainChop models https://github.com/neuroneural/brainchop")
}
Expand All @@ -32,14 +31,18 @@ async function main() {
nv1.removeVolume(nv1.volumes[0])
nv1.addVolume(nii2)
}

async function closeAllOverlays() {
while (nv1.volumes.length > 1) {
await nv1.removeVolume(nv1.volumes[1])
}
}
modelSelect.onchange = async function () {
await closeAllOverlays()
await ensureConformed()
let model = inferenceModelsList[this.selectedIndex]
let opts = brainChopOpts
runInference(opts, model, nv1.volumes[0].hdr, nv1.volumes[0].img, callbackImg, callbackUI)
}

saveBtn.onclick = function () {
nv1.volumes[1].saveToDisk("Custom.nii")
}
Expand All @@ -49,9 +52,7 @@ async function main() {
return js
}
async function callbackImg(img, opts, modelEntry) {
while (nv1.volumes.length > 1) {
await nv1.removeVolume(nv1.volumes[1])
}
closeAllOverlays()
let overlayVolume = await nv1.volumes[0].clone()
overlayVolume.zeroImage()
overlayVolume.hdr.scl_inter = 0
Expand Down
172 changes: 91 additions & 81 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@niivue/niivue":"^0.42.0",
"@tensorflow/tfjs": "3.9.0",
"@tensorflow/tfjs": "^4.18.0",
"gl-matrix": "^3.4.3"
},
"devDependencies": {
Expand Down

0 comments on commit 17d1950

Please sign in to comment.