def get_img(imgloc, segloc=None): npimg, _, npseg = preprocess.reorient(imgloc, segloc) npimg = preprocess.resize_to_nn(npimg).astype(np.float32) npimg = preprocess.window(npimg, -100, 400) npimg = preprocess.rescale(npimg, -100, 400) npseg = preprocess.resize_to_nn(npseg).astype(np.uint8) assert npimg.shape == npseg.shape return npimg[..., np.newaxis], npseg[..., np.newaxis]
def get_img(imgloc, segloc=None): npimg, _, npseg = preprocess.reorient(imgloc, segloc) npimg = preprocess.resize_to_nn(npimg).astype(np.float32) npimg = preprocess.window(npimg, hu_lb, hu_ub) npimg = preprocess.rescale(npimg, hu_lb, hu_ub) npseg = preprocess.resize_to_nn(npseg).astype(np.uint8) print(npimg.shape) assert npimg.shape == npseg.shape return npimg, npseg
def get_mda_imgs(dbfile='/rsrch1/ip/jacctor/livermask/trainingdata_small.csv', rootloc='/rsrch1/ip/jacctor/LiTS/LiTS'): nscans = 0 nvalid = 0 nslices = 0 with open(dbfile, 'r') as csvfile: myreader = csv.DictReader(csvfile, delimiter='\t') for row in myreader: imageloc = '%s/%s' % (rootloc, row['image']) truthloc = '%s/%s' % (rootloc, row['label']) print(imageloc, truthloc) nscans += 1 try: npimg, header, npseg = preprocess.reorient(imageloc, segloc=truthloc) nslices += header['dim'][3] nvalid += 1 except nib.filebasedimages.ImageFileError: print("could not read file") print('\ndone precomputing size: ', nslices, ' slices, from ', nvalid, ' scans out of ', nscans, ' scans.\n') imgs = np.empty((nslices, npx, npx)) segs = np.empty((nslices, npx, npx)) sidx = 0 with open(dbfile, 'r') as csvfile: myreader = csv.DictReader(csvfile, delimiter='\t') for row in myreader: imageloc = '%s/%s' % (rootloc, row['image']) truthloc = '%s/%s' % (rootloc, row['label']) print(imageloc, truthloc) try: npimg, header, npseg = preprocess.reorient(imageloc, segloc=truthloc) npimg = preprocess.resize_to_nn(npimg, transpose=True).astype( np.int16) npseg = preprocess.resize_to_nn(npseg, transpose=True).astype( np.uint8) sss = header['dim'][3] imgs[sidx:sidx + sss, ...] = npimg segs[sidx:sidx + sss, ...] = npseg sidx += sss except nib.filebasedimages.ImageFileError: print("ignoring the file I could't read earlier") return imgs, segs
def setup_training_from_file(): datacsv = settings.options.dbfile # create custom data frame database type os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' logfileoutputdir = settings.options.outdir os.system ('mkdir -p ' + logfileoutputdir) os.system ('mkdir -p ' + logfileoutputdir + '/data') os.system ('mkdir -p ' + logfileoutputdir + '/data/img') os.system ('mkdir -p ' + logfileoutputdir + '/data/seg') print("Output to\t", logfileoutputdir) imgdir = logfileoutputdir+'/data/img' segdir = logfileoutputdir+'/data/seg' imglist = [] seglist = [] dataidlist = [] with open(datacsv, 'r') as csvfile: myreader = csv.DictReader(csvfile, delimiter=',') for row in myreader: dataid = int(row['dataid']) imagelocation = '%s/%s' % (settings.options.rootlocation,row['image']) truthlocation = '%s/%s' % (settings.options.rootlocation,row['label']) print(imagelocation,truthlocation ) numpyimage, orig_header, numpytruth = preprocess.reorient(imagelocation, segloc=truthlocation) resimage = preprocess.resize_to_nn(numpyimage, transpose=False).astype(settings.IMG_DTYPE) resimage = preprocess.window(resimage, settings.options.hu_lb, settings.options.hu_ub) resimage = preprocess.rescale(resimage, settings.options.hu_lb, settings.options.hu_ub) restruth = preprocess.resize_to_nn(numpytruth, transpose=False).astype(settings.SEG_DTYPE) imgROI, segROI = isolate_ROI(resimage, restruth) Xloc = imgdir+'/volume-'+str(dataid) Yloc = segdir+'/segmentation-'+str(dataid) Xlist, Ylist = save_img_into_substacks(imgROI, segROI, Xloc, Yloc) imglist += Xlist seglist += Ylist dataidlist += [dataid]*len(Xlist) savelistsloc = logfileoutputdir+'/data/datalocations.txt' with open(savelistsloc, 'w') as csvfile: writer = csv.writer(csvfile, delimiter=',') writer.writerow(['dataid','imgloc','segloc']) for row in zip(dataidlist, imglist, seglist): writer.writerow(row) return savelistsloc
def get_img(imgloc, segloc=None): npimg, _, npseg = preprocess.reorient(imgloc, segloc) npimg = preprocess.resize_to_nn(npimg).astype(np.float32) npimg = preprocess.window(npimg, -100, 200) npimg = preprocess.rescale(npimg, -100, 200) npseg = preprocess.resize_to_nn(npseg).astype(np.uint8) print(npimg.shape) print(npseg.shape) assert npimg.shape == npseg.shape npimg = np.transpose(npimg, (2, 0, 1)) npseg = np.transpose(npseg, (2, 0, 1)) return npimg[..., np.newaxis], npseg[..., np.newaxis]
def get_img(imgloc): npimg, _, _ = preprocess.reorient(imgloc) npimg = preprocess.resize_to_nn(npimg, transpose=True).astype(np.int16) npimg = preprocess.window(npimg, -100,300) npimg = preprocess.rescale(npimg, -100,300) print(npimg.shape) midslice = npimg[int(npimg.shape[0] / 2),:,:] return midslice[np.newaxis,:,:,np.newaxis]
def PredictModel(model=settings.options.predictmodel, image=settings.options.predictimage, imageheader=None, outdir=settings.options.segmentation): if (model != None and image != None and outdir != None): os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152 numpypredict, origheader, _ = preprocess.reorient(image) assert numpypredict.shape[0:2] == (settings._globalexpectedpixel, settings._globalexpectedpixel) resizepredict = preprocess.resize_to_nn(numpypredict) resizepredict = preprocess.window(resizepredict, settings.options.hu_lb, settings.options.hu_ub) resizepredict = preprocess.rescale(resizepredict, settings.options.hu_lb, settings.options.hu_ub) opt = GetOptimizer() lss, met = GetLoss() loaded_model = get_unet() loaded_model.compile(loss=lss, metrics=met, optimizer=opt) loaded_model.load_weights(model) segout_float = loaded_model.predict(resizepredict[..., np.newaxis])[..., 0] segout_int = (segout_float >= settings.options.segthreshold).astype( settings.SEG_DTYPE) segin_windowed = preprocess.resize_to_original(resizepredict) segin_windowed_img = nib.Nifti1Image(segin_windowed, None, header=origheader) segin_windowed_img.to_filename( outdir.replace('.nii', '-imgin-windowed.nii')) segout_float_resize = preprocess.resize_to_original(segout_float) segout_float_img = nib.Nifti1Image(segout_float_resize, None, header=origheader) segout_float_img.to_filename(outdir.replace('.nii', '-pred-float.nii')) segout_int_resize = preprocess.resize_to_original(segout_int) segout_int_img = nib.Nifti1Image(segout_int_resize, None, header=origheader) segout_int_img.to_filename(outdir.replace('.nii', '-pred-seg.nii')) return segout_float_resize, segout_int_resize
def PredictNifti(model, saveloc, imageloc, segloc=None): print('loading data: ', imageloc) image, origheader, trueseg = preprocess.reorient(imageloc, segloc=segloc) image = preprocess.resize_to_nn(image, transpose=False).astype( settings.FLOAT_DTYPE) image = preprocess.window(image, settings.options.hu_lb, settings.options.hu_ub) image = preprocess.rescale(image, settings.options.hu_lb, settings.options.hu_ub) image_img = nib.Nifti1Image(image, None, header=origheader) image_img.to_filename(saveloc + '-img.nii') predseg_float, predseg = PredictNpy(model, image) if segloc: trueseg = preprocess.resize_to_nn(trueseg, transpose=False).astype( settings.SEG_DTYPE) names = ['DSC'] metrics = [dsc_l2_3D_npy] if settings.options.liver: tseg = (trueseg >= 1).astype(np.int32)[..., np.newaxis] elif settings.options.tumor: tseg = (trueseg > 1).astype(np.int32)[..., np.newaxis] print('pred ', predseg.shape, predseg.dtype, '\ttrue ', tseg.shape, tseg.dtype) scores = [ met(tseg.astype(np.int32), predseg.astype(np.float32)) for met in metrics ] print('DSC:\t', end='') for idx, s in enumerate(scores): print(names[idx], '\t', 1.0 - s, end='\t') print() print('saving data: ', saveloc) print(predseg_float.shape) for i in range(predseg_float.shape[-1]): segout_float_img = nib.Nifti1Image(predseg_float[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-float-' + str(i) + '.nii') trueseg_i = (trueseg == i).astype(settings.SEG_DTYPE) segout_float_img = nib.Nifti1Image(predseg_float[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-truth-' + str(i) + '.nii') trueseg_img = nib.Nifti1Image(trueseg, None, header=origheader) trueseg_img.to_filename(saveloc + '-truth.nii') predseg_img = nib.Nifti1Image(predseg, None, header=origheader) predseg_img.to_filename(saveloc + '-int.nii') if settings.options.ttdo and segloc: print('starting TTDO...') loaded_model = model.layers[-2] imagedata = np.transpose(image, (2, 0, 1))[..., np.newaxis] f = K.function([loaded_model.layers[0].input, K.learning_phase()], [loaded_model.layers[-1].output]) print('\tgenerating trials...') results = np.zeros(trueseg.shape + ( 1, settings.options.ntrials, )) nvalidslices = imagedata.shape[0] stride = 128 for jj in range(settings.options.ntrials): zidx = 0 while zidx < nvalidslices: maxz = min(zidx + stride, nvalidslices) # out = f([imagedata[zidx:maxz,...], 1])[0] # out = np.transpose(out, (1,2,0,3)) # results[...,zidx:maxz,0,jj] = out[...,0] results[..., zidx:maxz, 0, jj] = np.transpose( f([imagedata[zidx:maxz, ...], 1])[0], (1, 2, 0, 3))[..., 0] zidx += stride print('\tcalculating statistics...') pred_avg = results.mean(axis=-1) pred_var = results.var(axis=-1) pred_ent = np.zeros(pred_avg.shape) ent_idx0 = pred_avg > 0 ent_idx1 = pred_avg < 1 ent_idx = np.logical_and(ent_idx0, ent_idx1) pred_ent[ent_idx] = -1*np.multiply( pred_avg[ent_idx], np.log( pred_avg[ent_idx])) \ -1*np.multiply(1.0 - pred_avg[ent_idx], np.log(1.0 - pred_avg[ent_idx])) print('\tsaving statistics...') # save pred_avg for i in range(pred_avg.shape[-1]): segout_float_img = nib.Nifti1Image(pred_avg[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-avg-float-' + str(i) + '.nii') pred_avg_int = np.argmax(pred_avg, axis=-1) predseg_img = nib.Nifti1Image(pred_avg_int, None, header=origheader) predseg_img.to_filename(saveloc + '-avg-int.nii') # save pred_var for i in range(pred_var.shape[-1]): segout_float_img = nib.Nifti1Image(pred_var[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-var-float-' + str(i) + '.nii') # save pred_ent for i in range(pred_ent.shape[-1]): segout_float_img = nib.Nifti1Image(pred_ent[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-ent-float-' + str(i) + '.nii') return predseg, names, scores
def PredictModel(model=settings.options.predictmodel, image=settings.options.predictimage, imageheader=None, outdir=settings.options.segmentation, seg=None): if (model != None and image != None and outdir != None): os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152 numpypredict, origheader, origseg = preprocess.reorient(image, segloc=seg) assert numpypredict.shape[0:2] == (settings._globalexpectedpixel, settings._globalexpectedpixel) resizepredict = preprocess.resize_to_nn(numpypredict) resizepredict = preprocess.window(resizepredict, settings.options.hu_lb, settings.options.hu_ub) resizepredict = preprocess.rescale(resizepredict, settings.options.hu_lb, settings.options.hu_ub) if settings.options.D3 or settings.options.D25: dataid = np.ones((resizepredict.shape[0])) idx = np.array([1]) resizepredict2 = thick_slices(resizepredict, settings.options.thickness, dataid, idx) else: resizepredict2 = resizepredict if seg: origseg = preprocess.resize_to_nn(origseg) origseg = preprocess.livermask(origseg) if settings.options.D25: dataid_origseg = np.ones((origseg.shape[0])) origseg = thick_slices(origseg, 1, dataid_origseg, idx) origseg = origseg[..., 0] # if not settings.options.D25 and not settings.options.D3: # origseg = origseg.transpose((0,2,1)).astype(settings.FLOAT_DTYPE) origseg_img = nib.Nifti1Image( preprocess.resize_to_original(origseg), None) origseg_img.to_filename(outdir.replace('.nii', '-trueseg.nii')) ### ### set up model ### loaded_model = load_model(model, custom_objects={ 'dsc_l2': dsc_l2, 'l1': l1, 'dsc': dsc, 'dsc_int': dsc, 'ISTA': ISTA }, compile=False) #loaded_model.summary() if settings.options.D25: segout_float = loaded_model.predict(resizepredict2)[..., 0] else: segout_float = loaded_model.predict( resizepredict2[..., np.newaxis])[..., 0] segout_int = (segout_float >= settings.options.segthreshold).astype( settings.SEG_DTYPE) if settings.options.D3: segout_float = unthick_slices(segout_float, settings.options.thickness, dataid, idx) segout_int = unthick_slices(segout_int, settings.options.thickness) elif settings.options.D25: resizepredict = resizepredict.transpose((0, 2, 1)) #segout_int = preprocess.largest_connected_component(segout_int).astype(settings.SEG_DTYPE) segin_windowed = preprocess.resize_to_original(resizepredict) segin_windowed_img = nib.Nifti1Image(segin_windowed, None, header=origheader) segin_windowed_img.to_filename( outdir.replace('.nii', '-imgin-windowed.nii')) segout_float_resize = preprocess.resize_to_original(segout_float) segout_float_img = nib.Nifti1Image(segout_float_resize, None, header=origheader) segout_float_img.to_filename(outdir.replace('.nii', '-pred-float.nii')) segout_int_resize = preprocess.resize_to_original(segout_int) segout_int_img = nib.Nifti1Image(segout_int_resize, None, header=origheader) segout_int_img.to_filename(outdir.replace('.nii', '-pred-seg.nii')) if seg: #score = dsc_l2_3D(origseg, segout_int) score = dsc_l2_3D(origseg.astype(settings.FLOAT_DTYPE), segout_float) print('dsc:\t', 1.0 - score) return segout_float_resize, segout_int_resize
def PredictDropout(model=settings.options.predictmodel, image=settings.options.predictimage, outdir=settings.options.segmentation, seg=None): if not (model != None and image != None and outdir != None): return if model is None: model = settings.options.predictmodel if outdir is None: outdir = settings.options.segmentation os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152 numpypredict, origheader, origseg = preprocess.reorient(image, segloc=seg) assert numpypredict.shape[0:2] == (settings._globalexpectedpixel, settings._globalexpectedpixel) resizepredict = preprocess.resize_to_nn(numpypredict) resizepredict = preprocess.window(resizepredict, settings.options.hu_lb, settings.options.hu_ub) resizepredict = preprocess.rescale(resizepredict, settings.options.hu_lb, settings.options.hu_ub) if seg: origseg = preprocess.resize_to_nn(origseg) origseg = preprocess.livermask(origseg) # save unprocessed image_in img_in_nii = nib.Nifti1Image(image, None, header=origheader) img_in_nii.to_filename(outdir.replace('.nii', '-imgin.nii')) # save preprocessed image_in segin_windowed_img = nib.Nifti1Image(resizepredict, None) segin_windowed_img.to_filename( outdir.replace('.nii', '-imgin-windowed.nii')) # save true segmentation if seg: origseg_img = nib.Nifti1Image(origseg, None) origseg_img.to_filename(outdir.replace('.nii', '-seg.nii')) ### ### set up model ### loaded_model = load_model(model, custom_objects={ 'dsc_l2': dsc_l2, 'l1': l1, 'dsc': dsc, 'dsc_int': dsc, 'ISTA': ISTA }) ### ### making baseline prediction and saving to file ### print('\tmaking baseline predictions...') segout_float = loaded_model.predict(resizepredict[..., np.newaxis])[..., 0] segout_int = (segout_float >= settings.options.segthreshold).astype( settings.SEG_DTYPE) segout_int = preprocess.largest_connected_component(segout_int).astype( settings.SEG_DTYPE) segout_float_img = nib.Nifti1Image(segout_float, None) segout_float_img.to_filename(outdir.replace('.nii', '-pred-float.nii')) segout_int_img = nib.Nifti1Image(segout_int, None) segout_int_img.to_filename(outdir.replace('.nii', '-pred-seg.nii')) if seg: score = dsc_l2_3D(origseg, segout_int) print('\t\t\tdsc:\t', 1.0 - score) ### ### making predictions using different Bernoulli draws for dropout ### print('\tmaking predictions with different dropouts trials...') f = K.function([loaded_model.layers[0].input, K.learning_phase()], [loaded_model.layers[-1].output]) results = np.zeros(resizepredict.shape + (settings.options.ntrials, )) for jj in range(settings.options.ntrials): results[..., jj] = f([resizepredict[..., np.newaxis], 1])[0][..., 0] print('\tcalculating statistics...') pred_avg = results.mean(axis=-1) pred_var = results.var(axis=-1) pred_ent = np.zeros(pred_avg.shape) ent_idx0 = pred_avg > 0 ent_idx1 = pred_avg < 1 ent_idx = np.logical_and(ent_idx0, ent_idx1) pred_ent[ent_idx] = -1*np.multiply( pred_avg[ent_idx], np.log( pred_avg[ent_idx])) \ -1*np.multiply(1.0 - pred_avg[ent_idx], np.log(1.0 - pred_avg[ent_idx])) print('\tsaving trial statistics...') # save pred_avg pred_avg_img = nib.Nifti1Image(pred_avg, None) pred_avg_img.to_filename(outdir.replace('.nii', '-pred-avg.nii')) # save pred_var pred_var_img = nib.Nifti1Image(pred_var, None) pred_var_img.to_filename(outdir.replace('.nii', '-pred-var.nii')) # save pred_ent pred_ent_img = nib.Nifti1Image(pred_ent, None) pred_ent_img.to_filename(outdir.replace('.nii', '-pred-ent.nii')) print('\n') return segout_int, segout_float
def PredictNifti(model, saveloc, imageloc, segloc=None): print('loading data: ', imageloc) image, origheader, trueseg = preprocess.reorient(imageloc, segloc=segloc) image = preprocess.resize_to_nn(image, transpose=False).astype( settings.FLOAT_DTYPE) image = preprocess.window(image, settings.options.hu_lb, settings.options.hu_ub) image = preprocess.rescale(image, settings.options.hu_lb, settings.options.hu_ub) image_img = nib.Nifti1Image(image, None, header=origheader) image_img.to_filename(saveloc + '-img.nii') predseg_float, predseg = PredictNpy(model, image) if segloc: trueseg = preprocess.resize_to_nn(trueseg, transpose=False).astype( settings.SEG_DTYPE) # names = ['Background','Liver','Tumor'] # metrics = [dsc_l2_background_npy, dsc_l2_liver_npy, dsc_l2_tumor_npy] names = ['DSC'] metrics = [dsc_l2_npy] if settings.options.liver: tseg = (trueseg >= 1).astype(np.int32)[..., np.newaxis] elif settings.options.tumor: tseg = (trueseg > 1).astype(np.int32)[..., np.newaxis] print('pred ', predseg.shape, predseg.dtype, '\ttrue ', tseg.shape, tseg.dtype) scores = [ met(tseg.astype(np.int32), predseg.astype(np.float32)) for met in metrics ] print('DSC:\t', end='') for idx, s in enumerate(scores): print(names[idx], '\t', 1.0 - s, end='\t') print() print('saving data: ', saveloc) print(predseg_float.shape) for i in range(predseg_float.shape[-1]): segout_float_img = nib.Nifti1Image(predseg_float[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-float-' + str(i) + '.nii') trueseg_i = (trueseg == i).astype(settings.SEG_DTYPE) segout_float_img = nib.Nifti1Image(predseg_float[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-truth-' + str(i) + '.nii') trueseg_img = nib.Nifti1Image(trueseg, None, header=origheader) trueseg_img.to_filename(saveloc + '-truth.nii') predseg_img = nib.Nifti1Image(predseg, None, header=origheader) predseg_img.to_filename(saveloc + '-int.nii') if settings.options.ttdo and segloc: print('starting TTDO...') f = K.function([loaded_model.layers[0].input, K.learning_phase()], [loaded_model.layers[-1].output]) print('\tgenerating trials...') results = np.zeros(trueseg.shape + ( 3, settings.options.ntrials, )) for jj in range(settings.options.ntrials): segdata = np.zeros((*image.shape, 3)) nvalidslices = image.shape[2] - settings.options.thickness + 1 for z in range(nvalidslices): indata = image[np.newaxis, :, :, z:z + settings.options.thickness, np.newaxis] segdata[:, :, z:z + settings.options.thickness, :] += f([indata, 1])[0, ...] for i in range(settings.options.thickness): segdata[:, :, i, :] *= (settings.options.thickness) / (i + 1) for i in range(settings.options.thickness): segdata[:, :, -1 - i, :] *= (settings.options.thickness) / (i + 1) results[..., jj] = segdata / settings.options.thickness print('\tcalculating statistics...') pred_avg = results.mean(axis=-1) pred_var = results.var(axis=-1) pred_ent = np.zeros(pred_avg.shape) ent_idx0 = pred_avg > 0 ent_idx1 = pred_avg < 1 ent_idx = np.logical_and(ent_idx0, ent_idx1) pred_ent[ent_idx] = -1*np.multiply( pred_avg[ent_idx], np.log( pred_avg[ent_idx])) \ -1*np.multiply(1.0 - pred_avg[ent_idx], np.log(1.0 - pred_avg[ent_idx])) print('\tsaving statistics...') # save pred_avg for i in range(pred_avg.shape[-1]): segout_float_img = nib.Nifti1Image(pred_avg[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-avg-float-' + str(i) + '.nii') pred_avg_int = np.argmax(pred_avg, axis=-1) predseg_img = nib.Nifti1Image(pred_avg_int, None, header=origheader) predseg_img.to_filename(saveloc + '-avg-int.nii') # save pred_var for i in range(pred_var.shape[-1]): segout_float_img = nib.Nifti1Image(pred_var[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-var-float-' + str(i) + '.nii') # save pred_ent for i in range(pred_ent.shape[-1]): segout_float_img = nib.Nifti1Image(pred_ent[..., i], None, header=origheader) segout_float_img.to_filename(saveloc + '-ent-float-' + str(i) + '.nii') return predseg, names, scores
imgloc = 'C:/Users/sofia/OneDrive/Documents/GitHub/unlinked_livermask/data/LiTS/TrainingBatch2/volume-130.nii' segloc = 'C:/Users/sofia/OneDrive/Documents/GitHub/unlinked_livermask/data/LiTS/TrainingBatch2/segmentation-130.nii' #img, seg = get_img(imgloc, segloc) modelloclist = ['C:/Users/sofia/OneDrive/Documents/GitHub/convswap_shallow.h5'] outloclist = [ 'C:/Users/sofia/OneDrive/Documents/GitHub/convswap_images/D2_shallow_test_imgs2/' ] # preprocess image for prediction img, origheader, origseg = preprocess.reorient(imgloc, segloc=segloc) assert img.shape[0:2] == (settings._globalexpectedpixel, settings._globalexpectedpixel) img = preprocess.resize_to_nn(img) img = preprocess.window(img, settings.options.hu_lb, settings.options.hu_ub) img = preprocess.rescale(img, settings.options.hu_lb, settings.options.hu_ub) img = img[..., np.newaxis] # run functions for j in range(len(outloclist)): modelloc = modelloclist[j] outloc = outloclist[j] vzm, names, mdict = make_viz_model(modelloc) predict_viz_model(vzm, img, names, mdict, outloc) ## histogram of image pixel values #def plot_histogram(data, b=100, r=(-110,410)): # counts, bin_edges = np.histogram(data, bins=b, range=r) # plt.bar(bin_edges[:-1], counts, width=[0.8*(bin_edges[i+1]-bin_edges[i]) for i in range(len(bin_edges)-1)])
def BuildDB(): # create custom data frame database type mydatabasetype = [ ('dataid', int), ('axialliverbounds', bool), ('axialtumorbounds', bool), ('imagepath', 'S128'), ('imagedata', '(%d,%d)int16' % (settings.options.trainingresample, settings.options.trainingresample)), ('truthpath', 'S128'), ('truthdata', '(%d,%d)uint8' % (settings.options.trainingresample, settings.options.trainingresample)) ] # initialize empty dataframe numpydatabase = np.empty(0, dtype=mydatabasetype) # load all data from csv totalnslice = 0 with open(settings.options.dbfile, 'r') as csvfile: myreader = csv.DictReader(csvfile, delimiter=',') for row in myreader: imagelocation = '%s/%s' % (settings.options.rootlocation, row['image']) truthlocation = '%s/%s' % (settings.options.rootlocation, row['label']) print(imagelocation, truthlocation) numpyimage, orig_header, numpytruth = preprocess.reorient( imagelocation, segloc=truthlocation) # # load nifti file # imagedata = nib.load(imagelocation) # numpyimage= imagedata.get_data().astype(settings.IMG_DTYPE ) # error check assert numpyimage.shape[0:2] == (settings._globalexpectedpixel, settings._globalexpectedpixel) nslice = numpyimage.shape[2] assert numpytruth.shape[0:2] == (settings._globalexpectedpixel, settings._globalexpectedpixel) assert nslice == numpytruth.shape[2] resimage = preprocess.resize_to_nn( numpyimage, transpose=False).astype(settings.IMG_DTYPE) restruth = preprocess.resize_to_nn( numpytruth, transpose=False).astype(settings.SEG_DTYPE) # bounding box for each label if (np.max(restruth) == 1): (liverboundingbox, ) = ndimage.find_objects(restruth) tumorboundingbox = None else: (liverboundingbox, tumorboundingbox) = ndimage.find_objects(restruth) if (nslice == restruth.shape[2]): # custom data type to subset datamatrix = np.zeros(nslice, dtype=mydatabasetype) # custom data type to subset datamatrix['dataid'] = np.repeat(row['dataid'], nslice) # id the slices within the bounding box axialliverbounds = np.repeat(False, nslice) axialtumorbounds = np.repeat(False, nslice) axialliverbounds[liverboundingbox[2]] = True if (tumorboundingbox != None): axialtumorbounds[tumorboundingbox[2]] = True datamatrix['axialliverbounds'] = axialliverbounds datamatrix['axialtumorbounds'] = axialtumorbounds datamatrix['imagepath'] = np.repeat(imagelocation, nslice) datamatrix['truthpath'] = np.repeat(truthlocation, nslice) datamatrix['imagedata'] = resimage.transpose(2, 1, 0) datamatrix['truthdata'] = restruth.transpose(2, 1, 0) numpydatabase = np.hstack((numpydatabase, datamatrix)) # count total slice for QA totalnslice = totalnslice + nslice else: print('training data error image[2] = %d , truth[2] = %d ' % (nslice, restruth.shape[2])) # save numpy array to disk np.save(settings._globalnpfile, numpydatabase)