From 17d19505374d309759f8384f1eb90fb39e44af02 Mon Sep 17 00:00:00 2001 From: neurolabusc Date: Mon, 29 Apr 2024 08:45:29 -0400 Subject: [PATCH] fix optimized bwlabel, bump tfjs to current (4.18) --- brainchop.js | 12 +--- bwlabels.js | 13 ++-- main.js | 13 ++-- package-lock.json | 172 ++++++++++++++++++++++++---------------------- package.json | 2 +- 5 files changed, 107 insertions(+), 105 deletions(-) diff --git a/brainchop.js b/brainchop.js index 3d90056..c7806d5 100644 --- a/brainchop.js +++ b/brainchop.js @@ -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 + "%";; @@ -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) @@ -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) diff --git a/bwlabels.js b/bwlabels.js index 55cf46a..61343aa 100644 --- a/bwlabels.js +++ b/bwlabels.js @@ -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 @@ -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) diff --git a/main.js b/main.js index 2028be0..9ed628a 100644 --- a/main.js +++ b/main.js @@ -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") } @@ -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") } @@ -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 diff --git a/package-lock.json b/package-lock.json index 52562e5..290232c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.1.0", "dependencies": { "@niivue/niivue": "^0.42.0", - "@tensorflow/tfjs": "3.9.0", + "@tensorflow/tfjs": "^4.18.0", "gl-matrix": "^3.4.3" }, "devDependencies": { @@ -629,19 +629,19 @@ ] }, "node_modules/@tensorflow/tfjs": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/@tensorflow/tfjs/-/tfjs-3.9.0.tgz", - "integrity": "sha512-TyykXiZ6r9rMoXbQZaAkOKJJUrJHQVAjH/K6XRCPpOG//Hf15ZW97ZODskEByj77yNMw4smFUWCFhprhY2PgDQ==", + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@tensorflow/tfjs/-/tfjs-4.18.0.tgz", + "integrity": "sha512-MjA2M91wSkTkpFSuyQAuZm+DT9Y7DFu3rsTaO1BjCbf1S7o8DgJPKMmh6hk6uEn6/SBBbZVXPu+ZTQ/SyrP2Pw==", "dependencies": { - "@tensorflow/tfjs-backend-cpu": "3.9.0", - "@tensorflow/tfjs-backend-webgl": "3.9.0", - "@tensorflow/tfjs-converter": "3.9.0", - "@tensorflow/tfjs-core": "3.9.0", - "@tensorflow/tfjs-data": "3.9.0", - "@tensorflow/tfjs-layers": "3.9.0", + "@tensorflow/tfjs-backend-cpu": "4.18.0", + "@tensorflow/tfjs-backend-webgl": "4.18.0", + "@tensorflow/tfjs-converter": "4.18.0", + "@tensorflow/tfjs-core": "4.18.0", + "@tensorflow/tfjs-data": "4.18.0", + "@tensorflow/tfjs-layers": "4.18.0", "argparse": "^1.0.10", "chalk": "^4.1.0", - "core-js": "3", + "core-js": "3.29.1", "regenerator-runtime": "^0.13.5", "yargs": "^16.0.3" }, @@ -650,98 +650,87 @@ } }, "node_modules/@tensorflow/tfjs-backend-cpu": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-backend-cpu/-/tfjs-backend-cpu-3.9.0.tgz", - "integrity": "sha512-PUv5B3wdQsA8cysk+oUhA0NqMoo/lwP8EazC/axQc8/72Dc6kU8uw/5qZtE5P4xXSqkNSlh2ifFm+8nH/6B+iA==", + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-backend-cpu/-/tfjs-backend-cpu-4.18.0.tgz", + "integrity": "sha512-zk6NyGGsv0mCDnc5xWxLB5Zi8GaHVI2gg7KC0FSw/r5wW9SfA3ZIqUHVZhbIVhNqZSWy6L/+EIJa7l6q1XCkrg==", "dependencies": { - "@types/seedrandom": "2.4.27", - "seedrandom": "2.4.3" + "@types/seedrandom": "^2.4.28", + "seedrandom": "^3.0.5" }, "engines": { "yarn": ">= 1.3.2" }, "peerDependencies": { - "@tensorflow/tfjs-core": "3.9.0" + "@tensorflow/tfjs-core": "4.18.0" } }, - "node_modules/@tensorflow/tfjs-backend-cpu/node_modules/seedrandom": { - "version": "2.4.3", - "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-2.4.3.tgz", - "integrity": "sha512-2CkZ9Wn2dS4mMUWQaXLsOAfGD+irMlLEeSP3cMxpGbgyOOzJGFa+MWCOMTOCMyZinHRPxyOj/S/C57li/1to6Q==" - }, "node_modules/@tensorflow/tfjs-backend-webgl": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-backend-webgl/-/tfjs-backend-webgl-3.9.0.tgz", - "integrity": "sha512-oUnyQFF9aCnNZpul9AnJwrt8noDJdMmxgq2+e/0DpEMBERcywtVj9qkKCccMaVFsdQV1lQxpV3kjC3vbFMDWKg==", + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-backend-webgl/-/tfjs-backend-webgl-4.18.0.tgz", + "integrity": "sha512-ZLQ8MnWnIttMDqHepEzPL0Y4HkTePl8+AaYm9AcNnDUK7VF2jn+BXcTHCUOGfiwKu/OMwRc6osDrjxr0cCqNrA==", "dependencies": { - "@tensorflow/tfjs-backend-cpu": "3.9.0", + "@tensorflow/tfjs-backend-cpu": "4.18.0", "@types/offscreencanvas": "~2019.3.0", - "@types/seedrandom": "2.4.27", - "@types/webgl-ext": "0.0.30", - "@types/webgl2": "0.0.5", - "seedrandom": "2.4.3" + "@types/seedrandom": "^2.4.28", + "seedrandom": "^3.0.5" }, "engines": { "yarn": ">= 1.3.2" }, "peerDependencies": { - "@tensorflow/tfjs-core": "3.9.0" + "@tensorflow/tfjs-core": "4.18.0" } }, - "node_modules/@tensorflow/tfjs-backend-webgl/node_modules/seedrandom": { - "version": "2.4.3", - "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-2.4.3.tgz", - "integrity": "sha512-2CkZ9Wn2dS4mMUWQaXLsOAfGD+irMlLEeSP3cMxpGbgyOOzJGFa+MWCOMTOCMyZinHRPxyOj/S/C57li/1to6Q==" - }, "node_modules/@tensorflow/tfjs-converter": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-converter/-/tfjs-converter-3.9.0.tgz", - "integrity": "sha512-ftegwQlGkyDCxZGhAVfMyWWXqpNhnyESvNY3oFAUV4eN6i/mmBTCSOQ5AX5VR5lr7PNYPWGO5sJ10Q5HeTPfgw==", + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-converter/-/tfjs-converter-4.18.0.tgz", + "integrity": "sha512-b7utNMvmI986OPaQvDyHoiHKiIyip7ubSjG4KzTcFOWEKTsirtHxRp/6QKY/XSuH3AJK9Switu1/B5QC7xR2Zg==", "peerDependencies": { - "@tensorflow/tfjs-core": "3.9.0" + "@tensorflow/tfjs-core": "4.18.0" } }, "node_modules/@tensorflow/tfjs-core": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-core/-/tfjs-core-3.9.0.tgz", - "integrity": "sha512-wQ+VMsbvCne2OsogiNtRP8Mc01LnRGvAYQ0SGaDa4+1uwY2jsMk5GZjG66JQvf/Ppw8wyvKF170eh0yyCBgfcg==", + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-core/-/tfjs-core-4.18.0.tgz", + "integrity": "sha512-wjyq+F8AZJjhtr5xUBAON4VrNBXr9Poows/LnRh1K7wS+w41sCk0EQfeo6NfgUZXbHE/jgcqU6+fZDUKt3qRrg==", "dependencies": { "@types/long": "^4.0.1", - "@types/offscreencanvas": "~2019.3.0", - "@types/seedrandom": "2.4.27", - "@types/webgl-ext": "0.0.30", + "@types/offscreencanvas": "~2019.7.0", + "@types/seedrandom": "^2.4.28", + "@webgpu/types": "0.1.38", "long": "4.0.0", "node-fetch": "~2.6.1", - "seedrandom": "2.4.3" + "seedrandom": "^3.0.5" }, "engines": { "yarn": ">= 1.3.2" } }, - "node_modules/@tensorflow/tfjs-core/node_modules/seedrandom": { - "version": "2.4.3", - "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-2.4.3.tgz", - "integrity": "sha512-2CkZ9Wn2dS4mMUWQaXLsOAfGD+irMlLEeSP3cMxpGbgyOOzJGFa+MWCOMTOCMyZinHRPxyOj/S/C57li/1to6Q==" + "node_modules/@tensorflow/tfjs-core/node_modules/@types/offscreencanvas": { + "version": "2019.7.3", + "resolved": "https://registry.npmjs.org/@types/offscreencanvas/-/offscreencanvas-2019.7.3.tgz", + "integrity": "sha512-ieXiYmgSRXUDeOntE1InxjWyvEelZGP63M+cGuquuRLuIKKT1osnkXjxev9B7d1nXSug5vpunx+gNlbVxMlC9A==" }, "node_modules/@tensorflow/tfjs-data": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-data/-/tfjs-data-3.9.0.tgz", - "integrity": "sha512-1/H9VlYlfEX/LflzobSB5sx3FCavWGmzqRnAyyn5ChjgCzIUa+RtJ7nYgK2+6RC2MIDgKt1jmu36mkKZrwPD3w==", + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-data/-/tfjs-data-4.18.0.tgz", + "integrity": "sha512-JGfEwq8a0nEXwwU04/PW7O6kZ3FdqAd6ofMboM9qwGTv47QdeO/G1657sy4N21Q/qiz5qFDbGLhim3Nq9gZm9g==", "dependencies": { "@types/node-fetch": "^2.1.2", - "node-fetch": "~2.6.1" + "node-fetch": "~2.6.1", + "string_decoder": "^1.3.0" }, "peerDependencies": { - "@tensorflow/tfjs-core": "3.9.0", - "seedrandom": "~2.4.3" + "@tensorflow/tfjs-core": "4.18.0", + "seedrandom": "^3.0.5" } }, "node_modules/@tensorflow/tfjs-layers": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-layers/-/tfjs-layers-3.9.0.tgz", - "integrity": "sha512-25I20Oy17YZ3y0x/pabeiN6/vai0vqMQ85/Bp0GLOpcN2kmOLcItdWOAqFW5YPI2nrTqnpNQyk9zhmIh8f6X4w==", + "version": "4.18.0", + "resolved": "https://registry.npmjs.org/@tensorflow/tfjs-layers/-/tfjs-layers-4.18.0.tgz", + "integrity": "sha512-9AyxUdvjMDbDOkNzdkMRToJZF/le5ia8X3lVfGRGxzsuyoWDBGlV9V1ICufD+Z8sODXVyu+cgE8kAMtVK4aHFA==", "peerDependencies": { - "@tensorflow/tfjs-core": "3.9.0" + "@tensorflow/tfjs-core": "4.18.0" } }, "node_modules/@types/estree": { @@ -778,19 +767,9 @@ "integrity": "sha512-esIJx9bQg+QYF0ra8GnvfianIY8qWB0GBx54PK5Eps6m+xTj86KLavHv6qDhzKcu5UUOgNfJ2pWaIIV7TRUd9Q==" }, "node_modules/@types/seedrandom": { - "version": "2.4.27", - "resolved": "https://registry.npmjs.org/@types/seedrandom/-/seedrandom-2.4.27.tgz", - "integrity": "sha512-YvMLqFak/7rt//lPBtEHv3M4sRNA+HGxrhFZ+DQs9K2IkYJbNwVIb8avtJfhDiuaUBX/AW0jnjv48FV8h3u9bQ==" - }, - "node_modules/@types/webgl-ext": { - "version": "0.0.30", - "resolved": "https://registry.npmjs.org/@types/webgl-ext/-/webgl-ext-0.0.30.tgz", - "integrity": "sha512-LKVgNmBxN0BbljJrVUwkxwRYqzsAEPcZOe6S2T6ZaBDIrFp0qu4FNlpc5sM1tGbXUYFgdVQIoeLk1Y1UoblyEg==" - }, - "node_modules/@types/webgl2": { - "version": "0.0.5", - "resolved": "https://registry.npmjs.org/@types/webgl2/-/webgl2-0.0.5.tgz", - "integrity": "sha512-oGaKsBbxQOY5+aJFV3KECDhGaXt+yZJt2y/OZsnQGLRkH6Fvr7rv4pCt3SRH1somIHfej/c4u7NSpCyd9x+1Ow==" + "version": "2.4.34", + "resolved": "https://registry.npmjs.org/@types/seedrandom/-/seedrandom-2.4.34.tgz", + "integrity": "sha512-ytDiArvrn/3Xk6/vtylys5tlY6eo7Ane0hvcx++TKo6RxQXuVfW0AF/oeWqAj9dN29SyhtawuXstgmPlwNcv/A==" }, "node_modules/@ungap/structured-clone": { "version": "1.2.0", @@ -805,6 +784,11 @@ "node": ">=10" } }, + "node_modules/@webgpu/types": { + "version": "0.1.38", + "resolved": "https://registry.npmjs.org/@webgpu/types/-/types-0.1.38.tgz", + "integrity": "sha512-7LrhVKz2PRh+DD7+S+PVaFd5HxaWQvoMqBbsV9fNJO1pjUs1P8bM2vQVNfk+3URTqbuTI7gkXi0rfsN0IadoBA==" + }, "node_modules/ansi-regex": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", @@ -906,9 +890,9 @@ "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" }, "node_modules/core-js": { - "version": "3.37.0", - "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.37.0.tgz", - "integrity": "sha512-fu5vHevQ8ZG4og+LXug8ulUtVxjOcEYvifJr7L5Bfq9GOztVqsKd9/59hUk2ZSbCrS3BqUr3EpaYGIYzq7g3Ug==", + "version": "3.29.1", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.29.1.tgz", + "integrity": "sha512-+jwgnhg6cQxKYIIjGtAHq2nwUOolo9eoFZ4sHfUH09BLXBgxnH4gA0zEd+t+BO2cNB8idaBtZFcFTRjQJRJmAw==", "hasInstallScript": true, "funding": { "type": "opencollective", @@ -1221,11 +1205,29 @@ "tslib": "^2.1.0" } }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/seedrandom": { - "version": "2.4.4", - "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-2.4.4.tgz", - "integrity": "sha512-9A+PDmgm+2du77B5i0Ip2cxOqqHjgNxnBgglxLcX78A2D6c2rTo61z4jnVABpF4cKeDMDG+cmXXvdnqse2VqMA==", - "peer": true + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz", + "integrity": "sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==" }, "node_modules/source-map-js": { "version": "1.2.0", @@ -1241,6 +1243,14 @@ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "integrity": "sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==" }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", diff --git a/package.json b/package.json index 24c8b06..db90b01 100644 --- a/package.json +++ b/package.json @@ -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": {