-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrainchop-mainthread.js
2159 lines (1870 loc) · 76.7 KB
/
brainchop-mainthread.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as tf from '@tensorflow/tfjs'
import { BWLabeler } from './bwlabels.js'
import { inferenceModelsList } from "./brainchop-parameters.js"
export { runInference}
async function getModelNumParameters(modelObj) {
let numParameters = 0
for (let layerIdx = 0; layerIdx < modelObj.layers.length; layerIdx++) {
numParameters += modelObj.layers[layerIdx].countParams()
}
return numParameters
}
async function getModelNumLayers(modelObj) {
return modelObj.layers.length
}
async function isModelChnlLast(modelObj) {
for (let layerIdx = 0; layerIdx < modelObj.layers.length; layerIdx++) {
if (modelObj.layersByDepth[layerIdx][0].dataFormat) {
return modelObj.layersByDepth[layerIdx][0].dataFormat === 'channelsLast'
}
}
}
async function load_model(modelUrl) {
console.log('main thread load_model', modelUrl)
return await tf.loadLayersModel(modelUrl)
}
async function getAllSlices2D(allSlices, slice_height, slice_width) {
const allSlices_2D = []
for (let sliceIdx = 0; sliceIdx < allSlices.length; sliceIdx++) {
allSlices_2D.push(tf.tensor(allSlices[sliceIdx], [slice_height, slice_width]))
}
return allSlices_2D
}
async function getSlices3D(allSlices_2D) {
return tf.stack(allSlices_2D)
}
async function getAllSlicesData1D(num_of_slices, niftiHeader, niftiImage) {
// Get nifti dimensions
const cols = niftiHeader.dims[1] // Slice width
const rows = niftiHeader.dims[2] // Slice height
let typedData
if (niftiHeader.datatypeCode === 2) {
// enum from nvimage/utils DT_UINT8 = 2
typedData = new Uint8Array(niftiImage)
} else if (niftiHeader.datatypeCode === 4) {
// DT_INT16 = 4
typedData = new Int16Array(niftiImage)
} else if (niftiHeader.datatypeCode === 8) {
// DT_INT32 = 8
typedData = new Int32Array(niftiImage)
} else if (niftiHeader.datatypeCode === 16) {
// DT_FLOAT32 = 16
typedData = new Float32Array(niftiImage)
} else if (niftiHeader.datatypeCode === 64) {
// DT_FLOAT64 = 64
typedData = new Float64Array(niftiImage)
} else if (niftiHeader.datatypeCode === 256) {
// DT_INT8 = 256
typedData = new Int8Array(niftiImage)
} else if (niftiHeader.datatypeCode === 512) {
// DT_UINT16 = 512
typedData = new Uint16Array(niftiImage)
} else if (niftiHeader.datatypeCode === 768) {
// DT_UINT32 = 768
typedData = new Uint32Array(niftiImage)
} else {
return
}
const allSlices = []
let offset3D = 0
// Draw pixels
for (let slice = 0; slice < num_of_slices; slice++) {
const slice = new Array(rows * cols)
let offset2D = 0
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const value = typedData[offset3D++]
// Create 1Dim Array of pixel value, this 1 dim represents one channel
slice[offset2D++] = value & 0xff
}
}
allSlices.push(slice)
}
return allSlices
}
async function calculateQuantiles(tensor, lowerQuantile = 0.01, upperQuantile = 0.99) {
// Flatten the tensor
const flatTensor = tensor.flatten()
// Convert the flattened tensor to an array to sort it
const flatArray = await flatTensor.array()
flatArray.sort((a, b) => a - b) // Sort the array in ascending order
// Convert the sorted array back to a tensor
const sortedTensor = tf.tensor1d(flatArray)
// Calculate the indices for the quantiles
const numElements = sortedTensor.shape[0]
const lowIndex = Math.floor(numElements * lowerQuantile)
const highIndex = Math.ceil(numElements * upperQuantile) - 1 // Subtract 1 because indices are 0-based
// Slice the sorted tensor to get qmin and qmax
const qmin = sortedTensor.slice(lowIndex, 1) // Get the value at the low index
const qmax = sortedTensor.slice(highIndex, 1) // Get the value at the high index
// Get the actual values from the tensors
const qminValue = (await qmin.array())[0]
const qmaxValue = (await qmax.array())[0]
// Clean up tensors to free memory
flatTensor.dispose()
sortedTensor.dispose()
qmin.dispose()
qmax.dispose()
return { qmin: qminValue, qmax: qmaxValue }
}
async function quantileNormalizeVolumeData(tensor, lowerQuantile = 0.05, upperQuantile = 0.95) {
// Call calculateQuantiles and wait for the result
const { qmin, qmax } = await calculateQuantiles(tensor, lowerQuantile, upperQuantile)
// Convert qmin and qmax back to scalars
const qminScalar = tf.scalar(qmin)
const qmaxScalar = tf.scalar(qmax)
// Perform the operation: (tensor - qmin) / (qmax - qmin)
const resultTensor = tensor.sub(qminScalar).div(qmaxScalar.sub(qminScalar))
// Dispose of the created scalars to free memory
qminScalar.dispose()
qmaxScalar.dispose()
// Return the resulting tensor
return resultTensor
}
async function minMaxNormalizeVolumeData(volumeData) {
// Normalize the data to the range 0 - 1 using min-max scaling
const volumeData_Max = volumeData.max()
const volumeData_Min = volumeData.min()
const normalizedSlices_3d = await volumeData.sub(volumeData_Min).div(volumeData_Max.sub(volumeData_Min))
return normalizedSlices_3d
}
async function inferenceFullVolumeSeqCovLayer(
model,
slices_3d,
input_shape,
isChannelLast,
num_of_slices,
slice_height,
slice_width
) {
window.alert('inferenceFullVolumeSeqCovLayer() is not dead code?')
}
async function inferenceFullVolume(
model,
slices_3d,
input_shape,
isChannelLast,
num_of_slices,
slice_height,
slice_width
) {
window.alert('inferenceFullVolume() is not dead code?')
}
async function inferenceSubVolumes(model, slices_3d, num_of_slices, slice_height, slice_width, pipeline1_out = null) {
window.alert('inferenceSubVolumes() is not dead code?')
}
async function tensor2LightBuffer(tensor, dtype) {
window.alert('tensor2LightBuffer() is not dead code?')
// return new Buffer(tensor.shape, dtype, Array.from(tensor.dataSync()) );
}
async function draw3dObjBoundingVolume(unstackOutVolumeTensor) {
window.alert('draw3dObjBoundingVolume() is not dead code?')
}
async function argMaxLarge(outVolumeBuffer, num_of_slices, slice_height, slice_width, numOfClasses, dtype = 'float32') {
window.alert('argMaxLarge() is not dead code?')
}
async function addZeroPaddingTo3dTensor(tensor3d, rowPadArr = [1, 1], colPadArr = [1, 1], depthPadArr = [1, 1]) {
if (tensor3d.rank !== 3) {
throw new Error('Tensor must be 3D')
}
return tensor3d.pad([rowPadArr, colPadArr, depthPadArr])
}
async function removeZeroPaddingFrom3dTensor(tensor3d, rowPad = 1, colPad = 1, depthPad = 1) {
if (tensor3d.rank !== 3) {
throw new Error('Tensor must be 3D')
}
let h, w, d
;[h, w, d] = tensor3d.shape
return tensor3d.slice([rowPad, colPad, depthPad], [h - 2 * rowPad, w - 2 * colPad, d - 2 * depthPad])
}
async function resizeWithZeroPadding(croppedTensor3d, newDepth, newHeight, newWidth, refVoxel, boundVolSizeArr) {
const row_pad_befor = refVoxel[0]
const col_pad_befor = refVoxel[1]
const depth_pad_befor = refVoxel[2]
// last and lower volume voxel
const row_max = row_pad_befor + boundVolSizeArr[0] - 1 // size [2, 2, 2] means 2 voxels total in each dim
const col_max = col_pad_befor + boundVolSizeArr[1] - 1
const depth_max = depth_pad_befor + boundVolSizeArr[2] - 1
const row_pad_after = newHeight - row_max - 1 > 0 ? newHeight - row_max - 1 : 0
const col_pad_after = newWidth - col_max - 1 > 0 ? newWidth - col_max - 1 : 0
const depth_pad_after = newDepth - depth_max - 1 > 0 ? newDepth - depth_max - 1 : 0
return croppedTensor3d.pad([
[row_pad_befor, row_pad_after],
[col_pad_befor, col_pad_after],
[depth_pad_befor, depth_pad_after]
])
}
async function applyMriThreshold(tensor, percentage) {
// Perform asynchronous operations outside of tf.tidy
const maxTensor = tensor.max()
const thresholdTensor = maxTensor.mul(percentage)
const threshold = await thresholdTensor.data() // Extracts the threshold value
// Dispose tensors not needed anymore
maxTensor.dispose()
thresholdTensor.dispose()
// Use tf.tidy for synchronous operations
return tf.tidy(() => {
const dataForProcessing = tensor.clone()
// Thresholding (assuming background has very low values compared to the head)
const mask = dataForProcessing.greater(threshold[0])
// -- const denoisedMriData = dataForProcessing.mul(mask);
// No need to manually dispose dataForProcessing and mask, as tf.tidy() will dispose them auto.
return mask
})
// -- return denoisedMriData;
}
async function binarizeVolumeDataTensor(volumeDataTensor) {
const alpha = 0
// element-wise: (x > 0 ? 1 : alpha * x ); e.g. Tenosr [0, 0.9, 0.8, -3] => Tensor [0, 1, 1, 0]
return volumeDataTensor.step(alpha)
}
async function generateBrainMask(
unstackOutVolumeTensor,
num_of_slices,
slice_height,
slice_width,
modelEntry,
opts,
callbackUI,
callbackImg,
isFinalImage = true
) {
console.log('Generate Brain Masking ... ')
// Convert all slices into 1 Dim array to download
let allOutputSlices3DCC = []
// const allOutputSlices3DContours = []
// dataSync() using to flatten array. Takes around 1.5 s
for (let sliceTensorIdx = 0; sliceTensorIdx < unstackOutVolumeTensor.length; sliceTensorIdx++) {
allOutputSlices3DCC[sliceTensorIdx] = Array.from(unstackOutVolumeTensor[sliceTensorIdx].dataSync())
}
const isPreModelPostProcessEnable = modelEntry.preModelPostProcess
// let isPreModelPostProcessEnable = inferenceModelsList[$$("selectModel").getValue() - 1]["preModelPostProcess"];
if (isPreModelPostProcessEnable) {
console.log('Phase-1 Post processing enabled ... ')
allOutputSlices3DCC = tf.tidy(() => {
// Remove noisy regions using 3d CC
// const sliceWidth = niftiHeader.dims[1]
// const sliceHeight = niftiHeader.dims[2]
// return postProcessSlices3D(allOutputSlices3DCC, slice_height, slice_width)
const errTxt = 'postProcessSlices3D() should be upgraded to BWLabeler'
callbackUI(errTxt, -1, errTxt)
})
console.log('Post processing done ')
} else {
console.log('Phase-1 Post processing disabled ... ')
}
// Use this conversion to download output slices as nii file. Takes around 30 ms
// does not use `push` to avoid stack overflows. In future: consider .set() with typed arrays
const allOutputSlices3DCC1DimArray = new Array(allOutputSlices3DCC[0].length * allOutputSlices3DCC.length)
let index = 0;
for (let sliceIdx = 0; sliceIdx < allOutputSlices3DCC.length; sliceIdx++) {
for (let i = 0; i < allOutputSlices3DCC[sliceIdx].length; i++) {
allOutputSlices3DCC1DimArray[index++] = allOutputSlices3DCC[sliceIdx][i];
}
}
let brainOut = []
if (opts.isBrainCropMaskBased) {
// Mask-based
const brainMaskTensor1d = await binarizeVolumeDataTensor(tf.tensor1d(allOutputSlices3DCC1DimArray))
brainOut = Array.from(brainMaskTensor1d.dataSync())
} else {
// Brain tissue
window.alert('getAllSlicesData1D() is not dead code? niftiHeader and niftiImage required by getAllSlicesData1D')
}
if (isFinalImage || opts.showPhase1Output) {//all done
callbackImg(brainOut, opts, modelEntry)
callbackUI('Segmentation finished', 0)
}
return tf.tensor(brainOut, [num_of_slices, slice_height, slice_width])
}
async function convByOutputChannelAndInputSlicing(input, filter, biases, stride, pad, dilationRate, sliceSize) {
// const batchSize = input.shape[0]
// const depth = input.shape[1]
// const height = input.shape[2]
// const width = input.shape[3]
const inChannels = input.shape[4]
const outChannels = filter.shape[4]
// Create an empty array to hold the output channels
let outputChannels = null
// Slice the input tensor and process one output channel at a time
for (let channel = 0; channel < outChannels; channel++) {
const numSlices = Math.ceil(inChannels / sliceSize)
const biasesSlice = biases.slice([channel], [1])
let outputChannel = null
for (let i = 0; i < numSlices; i++) {
const startChannel = i * sliceSize
const endChannel = Math.min((i + 1) * sliceSize, inChannels)
// Only proceed if there are channels to process
if (startChannel < inChannels) {
const resultSlice = tf.tidy(() => {
const inputSlice = input.slice([0, 0, 0, 0, startChannel], [-1, -1, -1, -1, endChannel - startChannel])
const filterSlice = filter.slice([0, 0, 0, startChannel, channel], [-1, -1, -1, endChannel - startChannel, 1])
// Perform the convolution for the current slice and output channel
return tf.conv3d(inputSlice, filterSlice, stride, pad, 'NDHWC', dilationRate)
})
if (outputChannel === null) {
outputChannel = resultSlice
} else {
const updatedOutputChannel = outputChannel.add(resultSlice)
outputChannel.dispose()
resultSlice.dispose()
outputChannel = updatedOutputChannel
}
}
}
// Add the biases to the accumulated convolutions for this channel
const biasedOutputChannel = outputChannel.add(biasesSlice)
outputChannel.dispose()
biasesSlice.dispose()
// Accumulate the channel to the output array
if (outputChannels == null) {
outputChannels = biasedOutputChannel
} else {
const updatedOutputChannels = await tf.concat([outputChannels, biasedOutputChannel], 4)
biasedOutputChannel.dispose()
outputChannels.dispose()
outputChannels = updatedOutputChannels
}
}
return outputChannels
}
function processTensorInChunks(inputTensor, filterWeights, chunkSize) {
// Assuming inputTensor's shape: [batch, depth, height, width, inChannels]
// and filterWeights's shape: [filterDepth, filterHeight, filterWidth, inChannels, outChannels]
const stride = 1
const pad = 0
const dilationRate = 1
const inChannels = inputTensor.shape[4]
const numSlices = Math.ceil(inChannels / chunkSize)
let accumulatedResult = null
for (let i = 0; i < numSlices; i++) {
const startChannel = i * chunkSize
const endChannel = Math.min((i + 1) * chunkSize, inChannels)
const channels = endChannel - startChannel
const inputSlice = tf.tidy(() => {
// Slice the input tensor to get the current chunk
return inputTensor.slice([0, 0, 0, 0, startChannel], [-1, -1, -1, -1, channels])
})
const filterSlice = tf.tidy(() => {
// Slice the filter weights to match the input tensor's current chunk
return filterWeights.slice([0, 0, 0, startChannel, 0], [-1, -1, -1, channels, -1])
})
const resultSlice = tf.conv3d(inputSlice, filterSlice, stride, pad, 'NDHWC', dilationRate)
// Clean up the slices to free memory
inputSlice.dispose()
filterSlice.dispose()
// Squeeze the result slice to remove dimensions of size 1
const squeezedResultSlice = tf.squeeze(resultSlice)
resultSlice.dispose() // Dispose of the original resultSlice after squeezing
if (accumulatedResult === null) {
accumulatedResult = squeezedResultSlice
} else {
// Accumulate the result by adding the new result slice to it
const newAccumulatedResult = accumulatedResult.add(squeezedResultSlice)
// Dispose of the previous accumulatedResult and squeezedResultSlice
accumulatedResult.dispose()
// Dispose of squeezedResultSlice only if it wasn't assigned to accumulatedResult
if (accumulatedResult !== squeezedResultSlice) {
squeezedResultSlice.dispose()
}
// Update accumulatedResult with the new result
accumulatedResult = newAccumulatedResult
}
tf.tidy(() => {
tf.matMul(tf.zeros([1, 1]), tf.zeros([1, 1]))
})
}
return accumulatedResult
}
class SequentialConvLayer {
constructor(model, chunkSize, isChannelLast, callbackUI) {
this.model = model
this.outChannels = model.outputLayers[0].kernel.shape[4]
this.chunkSize = chunkSize
this.isChannelLast = isChannelLast
this.callbackUI = callbackUI // fork
}
/**
* Apply sequential convolution layer
* @since 3.0.0
* @member SequentialConvLayer
* @param {tf.Tensor} inputTensor e.g. [ 1, 256, 256, 256, 5 ]
* @return {promise}
*
* convLayer.rank -> 3
* typeof(convLayer) -> "object"
* convLayer: Object { dataFormat: "channelsLast", dilationRate: Array(3) [ 1, 1, 1 ], inputSpec: Array [ {…} ],
* name: "output", padding: "same", strides: Array(3) [ 1, 1, 1 ], ...}
*
* weights.shape -> Array(5) [ 1, 1, 1, 5, 3 ]
* weights.print()
* //=> Tensor
* [[[[[0.146999 , -1.4474995, -2.8961499],
* [1.1067894, 0.6897876 , -0.7573005],
* [-0.38512 , -0.2812168, -0.8637539],
* [0.9341159, -0.0344299, -2.3668685],
* [0.1052373, 1.266812 , 0.6542516 ]]]]]
*
* biases.shape -> Array [ 3 ]
* biases.print()
* //=> Tensor
* [-0.7850812, -2.3238883, 2.1639345]
*
* for idx = 0 -> filterWeights.shape -> Array(5) [ 1, 1, 1, 5, 1 ]
* filterWeights.print()
* //=> Tensor
* [[[[[0.146999 ],
* [1.1067894],
* [-0.38512 ],
* [0.9341159],
* [0.1052373]]]]]
*
* for idx = 0 -> filterBiases.shape -> Array [1]
* filterBiases.print()
* //=> Tensor
* [-0.7850812]
*/
async apply(inputTensor) {
const oldDeleteTextureThreshold = tf.ENV.get('WEBGL_DELETE_TEXTURE_THRESHOLD')
tf.ENV.set('WEBGL_DELETE_TEXTURE_THRESHOLD', 0)
const self = this
// Important to avoid "undefined" class var members inside the timer.
// "this" has another meaning inside the timer.
// document.getElementById("progressBarChild").parentElement.style.visibility = "visible";
return new Promise((resolve) => {
const startTime = performance.now()
const convLayer = self.model.layers[self.model.layers.length - 1]
const weights = convLayer.getWeights()[0] //
const biases = convLayer.getWeights()[1]
const outputShape = self.isChannelLast ? inputTensor.shape.slice(1, -1) : inputTensor.shape.slice(2)
// -- e.g. outputShape : [256,256,256] or cropped Dim
// -- if inputTensor [ 1, D, H, W, 50 ], channelLast true -> outputShape : outputShape [D, H, W]
// -- if inputTensor [ 1, 50, D, H, W ], channelLast false -> outputShape : outputShape [D, H, W]
let outB = tf.mul(tf.ones(outputShape), -10000)
// -- e.g. outB.shape [256,256,256]
let outC = tf.zeros(outputShape)
// -- e.g. outC.shape [256,256,256]
let chIdx = 0
// console.log("---------------------------------------------------------");
console.log(' channel loop')
const seqTimer = window.setInterval(async function () {
tf.engine().startScope() // Start TensorFlow.js scope
console.log('=======================')
const memoryInfo0 = await tf.memory()
console.log(`| Number of Tensors: ${memoryInfo0.numTensors}`)
console.log(`| Number of Data Buffers: ${memoryInfo0.numDataBuffers}`)
console.log('Channel : ', chIdx)
const result = await tf.tidy(() => {
const filterWeights = weights.slice([0, 0, 0, 0, chIdx], [-1, -1, -1, -1, 1])
// -- e.g. filterWeights.shape [ 1, 1, 1, 5, 1 ]
const filterBiases = biases.slice([chIdx], [1])
// -- e.g. filterBiases.shape [1] -> Tensor [-0.7850812]
const outA = processTensorInChunks(
inputTensor,
filterWeights,
Math.min(self.chunkSize, self.outChannels)
).add(filterBiases)
const greater = tf.greater(outA, outB)
const newoutB = tf.where(greater, outA, outB)
const newoutC = tf.where(greater, tf.fill(outC.shape, chIdx), outC)
// Dispose the old tensors before reassigning
tf.dispose([outB, outC, filterWeights, filterBiases, outA, greater])
// Dummy operation to trigger cleanup
tf.tidy(() => tf.matMul(tf.ones([1, 1]), tf.ones([1, 1])))
return [newoutC, newoutB]
})
console.log('=======================')
const memoryInfo = await tf.memory()
self.callbackUI(`Iteration ${chIdx}`, chIdx / self.outChannels)
console.log(`Number of Tensors: ${memoryInfo.numTensors}`)
console.log(`Number of Data Buffers: ${memoryInfo.numDataBuffers}`)
console.log(`Megabytes In Use: ${(memoryInfo.numBytes / 1048576).toFixed(3)} MB`)
if (memoryInfo.unreliable) {
console.log(`Unreliable: ${memoryInfo.unreliable}`)
}
// Dispose of previous values before assigning new tensors to outC and outB
if (typeof outC !== 'undefined') {
outC.dispose()
}
if (typeof outB !== 'undefined') {
outB.dispose()
}
// Assign the new values to outC and outB
outC = tf.keep(result[0])
outB = tf.keep(result[1])
// // Assign the new values to outC and outB
// outC = result[0];
// outB = result[1];
tf.engine().endScope()
if (chIdx === self.outChannels - 1) {
window.clearInterval(seqTimer)
// document.getElementById("progressBarChild").style.width = 0 + "%";
tf.dispose(outB)
const endTime = performance.now()
const executionTime = endTime - startTime
console.log(`Execution time for output layer: ${executionTime} milliseconds`)
tf.ENV.set('WEBGL_DELETE_TEXTURE_THRESHOLD', oldDeleteTextureThreshold)
resolve(outC)
} else {
chIdx++
// the seemingly strange sequence of operations
// below prevents tfjs from uncontrolably
// grabbing buffers, even when all tensors have
// already been disposed
const outCShape = outC.shape
const outCdata = outC.dataSync()
const outBShape = outC.shape
const outBdata = outB.dataSync()
outC.dispose()
outB.dispose()
// tf.disposeVariables()
outC = tf.tensor(outCdata, outCShape)
outB = tf.tensor(outBdata, outBShape)
// document.getElementById("progressBarChild").style.width = (chIdx + 1) * 100 / self.outChannels + "%";
}
// Artificially introduce a pause to allow for garbage collection to catch up
await new Promise((resolve) => setTimeout(resolve, 300))
}, 0)
})
}
} // <<<< End of class
async function generateOutputSlicesV2(
img,
OutVolumeTensorShape,
OutVolumeTensorType,
num_of_slices,
numSegClasses,
slice_height,
slice_width,
modelEntry,
opts,
niftiImage
) {
// Convert all slices into 1 Dim array
// const allOutputSlices3DContours = []
if (opts.isPostProcessEnable) {
const BWInstance = new BWLabeler()
const dim = new Uint32Array(OutVolumeTensorShape)
const conn = 26 // Example connectivity
const binarize = true
const onlyLargestClusterPerClass = true
const [labelCount, labeledImage] = BWInstance.bwlabel(img, dim, conn, binarize, onlyLargestClusterPerClass)
for (let i = 0; i < img.length; i++) {
img[i] *= labeledImage[i]
}
} // if isPostProcessEnable
const typedArrayConstructor = {
float32: Float32Array,
int32: Int32Array
// Add other cases as needed for different dtypes
}[OutVolumeTensorType]
// Create a new TypedArray from img with the same type as outLabelVolume
const allOutputSlices3DCC1DimArray = new Uint8Array(img)
const modelType = modelEntry.type
// return img
switch (modelType) {
case 'Brain_Masking': {
const brainMask = new Uint8Array(allOutputSlices3DCC1DimArray.length)
for (let i = 0; i < allOutputSlices3DCC1DimArray.length; i++) {
brainMask[i] = allOutputSlices3DCC1DimArray[i] !== 0 ? 1 : 0
}
// labelArrayBuffer = createNiftiOutArrayBuffer(rawNiftiData, brainMask);
// allOutputSlices3DCC1DimArray = brainMask;
// --labelsHistogramMap = null;
// maskBrainExtraction = true;
return brainMask
// break;
}
case 'Brain_Extraction': {
const maskedData = new Uint8Array(allOutputSlices3DCC1DimArray.length)
// const brainData = nifti2data(rawNiftiData);
for (let i = 0; i < allOutputSlices3DCC1DimArray.length; i++) {
// Create the mask - 1 where the value is non-zero, 0 where it is zero.
const maskValue = allOutputSlices3DCC1DimArray[i] !== 0 ? 1 : 0
// Apply the mask to the data - multiply by the mask value.
maskedData[i] = niftiImage[i] * maskValue
}
// labelArrayBuffer = createNiftiOutArrayBuffer(rawNiftiData, maskedData);
// Update `allOutputSlices3DCC1DimArray` if needed.
// allOutputSlices3DCC1DimArray = maskedData;
// Other operations...
// maskBrainExtraction = true;
return maskedData
// break;
}
}
return img
}
async function inferenceFullVolumeSeqCovLayerPhase2(
opts,
modelEntry,
model,
slices_3d,
num_of_slices,
slice_height,
slice_width,
pipeline1_out,
callbackUI,
callbackImg,
statData,
niftiImage
) {
// --Phase-2, After remove the skull try to allocate brain volume and make inferece
console.log(' ---- Start FullVolume Inference with Sequential Conv Layer for phase-II ---- ')
// console.log("BOB", callbackUI); console.log("UNCLE",callbackImg); return
const quantileNorm = modelEntry.enableQuantileNorm
if (quantileNorm) {
// Quantile normalize function needs specific models to be used
console.log('preModel Quantile normalization enabled')
slices_3d = await quantileNormalizeVolumeData(slices_3d)
} else {
// Min Max Nomalize MRI data to be from 0 to 1
console.log('preModel Min Max normalization enabled')
slices_3d = await minMaxNormalizeVolumeData(slices_3d)
}
let mask_3d
if (pipeline1_out == null) {
// preModel is null
// Check if thresholding the MRI to remove noisy voxels for better cropping is needed.
const autoThresholdValue = modelEntry.autoThreshold
if (autoThresholdValue > 0 && autoThresholdValue <= 1) {
// Filtered MRI from noisy voxel below autoThresholdValue
mask_3d = await applyMriThreshold(slices_3d, autoThresholdValue)
} else {
console.log('No valid crop threshold value')
// binarize original image
mask_3d = await slices_3d.greater([0]).asType('bool')
}
} else {
mask_3d = await pipeline1_out.greater([0]).asType('bool')
// -- pipeline1_out.dispose();
}
console.log(' mask_3d shape : ', mask_3d.shape)
const coords = await tf.whereAsync(mask_3d)
// -- Get each voxel coords (x, y, z)
mask_3d.dispose()
const coordsArr = coords.arraySync()
let row_min = slice_height
let row_max = 0
let col_min = slice_width
let col_max = 0
let depth_min = num_of_slices
let depth_max = 0
for (let i = 0; i < coordsArr.length; i++) {
if (row_min > coordsArr[i][0]) {
row_min = coordsArr[i][0]
} else if (row_max < coordsArr[i][0]) {
row_max = coordsArr[i][0]
}
if (col_min > coordsArr[i][1]) {
col_min = coordsArr[i][1]
} else if (col_max < coordsArr[i][1]) {
col_max = coordsArr[i][1]
}
if (depth_min > coordsArr[i][2]) {
depth_min = coordsArr[i][2]
} else if (depth_max < coordsArr[i][2]) {
depth_max = coordsArr[i][2]
}
}
console.log('row min and max :', row_min, row_max)
console.log('col min and max :', col_min, col_max)
console.log('depth min and max :', depth_min, depth_max)
// -- Reference voxel that cropped volume started slice with it
const refVoxel = [row_min, col_min, depth_min]
// -- Starting form refVoxel, size of bounding volume
const boundVolSizeArr = [row_max - row_min + 1, col_max - col_min + 1, depth_max - depth_min + 1]
coords.dispose()
// -- Extract 3d object (e.g. brain)
const cropped_slices_3d = await slices_3d.slice(
[row_min, col_min, depth_min],
[row_max - row_min + 1, col_max - col_min + 1, depth_max - depth_min + 1]
)
slices_3d.dispose()
// -- Padding size add to cropped brain
const pad = modelEntry.cropPadding
// Create margin around the bounding volume
let cropped_slices_3d_w_pad = await addZeroPaddingTo3dTensor(cropped_slices_3d, [pad, pad], [pad, pad], [pad, pad])
console.log(' cropped slices_3d with padding shape: ', cropped_slices_3d_w_pad.shape)
cropped_slices_3d.dispose()
if (opts.drawBoundingVolume) {
let testVol = await removeZeroPaddingFrom3dTensor(cropped_slices_3d_w_pad, pad, pad, pad)
console.log(' outLabelVolume without padding shape : ', testVol.shape)
testVol = await resizeWithZeroPadding(testVol, num_of_slices, slice_height, slice_width, refVoxel, boundVolSizeArr)
console.log(' outLabelVolume final shape after resizing : ', testVol.shape)
draw3dObjBoundingVolume(tf.unstack(testVol))
testVol.dispose()
return 0
}
statData.Brainchop_Ver = 'FullVolume'
// model.then(function (res) {
// console.log("--->>>>", opts.drawBoundingVolume); return
const res = await model
try {
let startTime = performance.now()
const inferenceStartTime = performance.now()
// maxLabelPredicted in whole volume of the brain
let maxLabelPredicted = 0
const transpose = modelEntry.enableTranspose
const delay = modelEntry.inferenceDelay
console.log('Inference delay :', delay)
if (transpose) {
cropped_slices_3d_w_pad = await cropped_slices_3d_w_pad.transpose()
console.log('Input transposed for pre-model')
} else {
console.log('Transpose not enabled for pre-model')
}
let i = 1
const layersLength = res.layers.length
console.log('res.layers.length ', layersLength)
const isChannelLast = isModelChnlLast(res)
const batchSize = opts.batchSize
const numOfChan = opts.numOfChan
let adjusted_input_shape
// -- Adjust model input shape
if (isChannelLast) {
res.layers[0].batchInputShape[1] = cropped_slices_3d_w_pad.shape[0]
res.layers[0].batchInputShape[2] = cropped_slices_3d_w_pad.shape[1]
res.layers[0].batchInputShape[3] = cropped_slices_3d_w_pad.shape[2]
adjusted_input_shape = [
batchSize,
res.layers[0].batchInputShape[1],
res.layers[0].batchInputShape[2],
res.layers[0].batchInputShape[3],
numOfChan
]
} else {
res.layers[0].batchInputShape[2] = cropped_slices_3d_w_pad.shape[0]
res.layers[0].batchInputShape[3] = cropped_slices_3d_w_pad.shape[1]
res.layers[0].batchInputShape[4] = cropped_slices_3d_w_pad.shape[2]
adjusted_input_shape = [
batchSize,
numOfChan,
res.layers[0].batchInputShape[2],
res.layers[0].batchInputShape[3],
res.layers[0].batchInputShape[4]
]
}
console.log(' Model batch input shape : ', res.layers[0].batchInputShape)
// -- batchInputShape {Array} input_shape - e.g. [?, D, H, W, Ch] or [?, Ch, D, H, W]
statData.Input_Shape = JSON.stringify(res.layers[0].batchInputShape)
statData.Output_Shape = JSON.stringify(res.output.shape)
statData.Channel_Last = isChannelLast
statData.Model_Param = await getModelNumParameters(res)
statData.Model_Layers = await getModelNumLayers(res)
statData.Model = modelEntry.modelName
statData.Seq_Conv = modelEntry.enableSeqConv
statData.Extra_Info = null
// Determine the number of output channels in the last layer of the model
// e.g. 3, 50, 104
const outputLayer = res.layers[res.layers.length - 1]
console.log('Output Layer : ', outputLayer)
const expected_Num_labels = isChannelLast
? outputLayer.outputShape[outputLayer.outputShape.length - 1]
: outputLayer.outputShape[1]
console.log('Num of output channels : ', expected_Num_labels)
const curTensor = []
curTensor[0] = await cropped_slices_3d_w_pad.reshape(adjusted_input_shape)
// console.log("curTensor[0] :", curTensor[0].dataSync());
// let curProgBar = parseInt(document.getElementById("progressBar").style.width);
const timer = window.setInterval(async function () {
try {
if (res.layers[i].activation.getClassName() !== 'linear') {
curTensor[i] = await res.layers[i].apply(curTensor[i - 1])
} else {
curTensor[i] = await convByOutputChannelAndInputSlicing(
curTensor[i - 1],
res.layers[i].getWeights()[0],
res.layers[i].getWeights()[1],
res.layers[i].strides,
res.layers[i].padding,
res.layers[i].dilationRate,
3
) // important for memory use
}
tf.dispose(curTensor[i - 1])
} catch (err) {
const errTxt = 'Your graphics card (e.g. Intel) may not be compatible with WebGL. ' + err.message
callbackUI(errTxt, -1, errTxt)
window.clearInterval(timer)
tf.engine().endScope()
tf.engine().disposeVariables()
statData.Inference_t = Infinity
statData.Postprocess_t = Infinity
statData.Status = 'Fail'
statData.Error_Type = err.message
statData.Extra_Err_Info = 'Failed while model layer ' + i + ' apply'
callbackUI('', -1, '', statData)
return 0
}
console.log('layer output Tensor shape : ', curTensor[i].shape)
console.log('layer count params ', res.layers[i].countParams())
res.layers[i].dispose()
curTensor[i - 1].dispose()
// bork
callbackUI('Layer ' + i.toString(), (i + 1) / layersLength)
if (tf.memory().unreliable) {
const unreliableReasons = 'unreliable reasons :' + tf.memory().reasons
callbackUI(unreliableReasons, NaN, unreliableReasons)
}
if (i === layersLength - 2) {
// Stop before the last layer or classification layer.
window.clearInterval(timer)
// // Create an instance of SequentialConvLayer
// The second parameter is important for memory,
// the larger it is, the more memory it uses
// it was 8, but I set it to 3, got a different error
// let seqConvLayer = new SequentialConvLayer(res, 10, isChannelLast);
const seqConvLayer = await new SequentialConvLayer(res, 10, isChannelLast, callbackUI)
// Apply the last output tensor to the seq. instance
let outputTensor = await seqConvLayer.apply(curTensor[i])
// -- document.getElementById("progressBarChild").style.width = 0 + "%";;
// Dispose the previous layer input tensor
tf.dispose(curTensor[i])
// delete the used class
// ? delete seqConvLayer;
// You can now use 'outputTensor' as needed
console.log(' Output tensor', outputTensor)
console.log(' Output tensor shape : ', outputTensor.shape)
// Array(3) [ 256, 256, 256 ]
if (outputTensor.shape.length !== 3) {
const msg = 'Output tensor shape should be 3 dims but it is ' + outputTensor.shape.length
callbackUI(msg, -1, msg)
}
const Inference_t = ((performance.now() - startTime) / 1000).toFixed(4)
console.log(' find array max ')
const curBatchMaxLabel = await outputTensor.max().dataSync()[0]
if (maxLabelPredicted < curBatchMaxLabel) {
maxLabelPredicted = curBatchMaxLabel
}
const numSegClasses = maxLabelPredicted + 1
console.log('Predicted num of segmentation classes', numSegClasses)
statData.Actual_Labels = numSegClasses
statData.Expect_Labels = expected_Num_labels
statData.NumLabels_Match = numSegClasses === expected_Num_labels
if (numSegClasses !== expected_Num_labels) {
const msg = 'expected ' + expected_Num_labels + ' labels, but the predicted are ' + numSegClasses
callbackUI(msg, -1, msg)
}
// -- Transpose back to fit Papaya display settings
let outLabelVolume = outputTensor.reshape([
cropped_slices_3d_w_pad.shape[0],
cropped_slices_3d_w_pad.shape[1],
cropped_slices_3d_w_pad.shape[2]
])