Esempio n. 1
0
def PredictModel():

    model = settings.options.predictmodel
    image = settings.options.predictimage
    outdir = settings.options.segmentation

    if (model != None and image != None and outdir != None):

        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # see issue #152

        imagepredict = nib.load(image)
        imageheader = imagepredict.header
        numpypredict = imagepredict.get_data().astype(settings.IMG_DTYPE)
        assert numpypredict.shape[0:2] == (settings._globalexpectedpixel,
                                           settings._globalexpectedpixel)
        nslice = numpypredict.shape[2]

        resizepredict = skimage.transform.resize(
            numpypredict, (settings.options.trainingresample,
                           settings.options.trainingresample, nslice),
            order=0,
            preserve_range=True,
            mode='constant').astype(settings.IMG_DTYPE).transpose(2, 1, 0)

        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])
        segout_int = (segout_float[..., 0] >=
                      settings.options.segthreshold).astype(settings.SEG_DTYPE)

        segout_float_resize = skimage.transform.resize(
            segout_float[..., 0], (nslice, settings._globalexpectedpixel,
                                   settings._globalexpectedpixel),
            order=0,
            preserve_range=True,
            mode='constant').transpose(2, 1, 0)
        segout_float_img = nib.Nifti1Image(segout_float_resize,
                                           None,
                                           header=imageheader)
        segout_float_img.to_filename(
            outdir.replace('.nii.gz', '-predtumorfloat.nii.gz'))

        segout_int_resize = skimage.transform.resize(
            segout_int[..., 0], (nslice, settings._globalexpectedpixel,
                                 settings._globalexpectedpixel),
            order=0,
            preserve_range=True,
            mode='constant').transpose(2, 1, 0)
        segout_int_img = nib.Nifti1Image(segout_int_resize,
                                         None,
                                         header=imageheader)
        segout_int_img.to_filename(
            outdir.replace('.nii.gz', '-predtumorseg.nii.gz'))

        return segout_int_resize
Esempio n. 2
0
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
Esempio n. 3
0
def TrainModel(idfold=0):

    from setupmodel import GetSetupKfolds, GetCallbacks, GetOptimizer, GetLoss
    from buildmodel import get_unet, thick_slices

    ###
    ### load data
    ###

    kfolds = settings.options.kfolds

    print('loading memory map db for large dataset')
    numpydatabase = np.load(settings._globalnpfile)
    (train_index, test_index) = GetSetupKfolds(settings.options.dbfile, kfolds,
                                               idfold)

    print('copy data subsets into memory...')
    axialbounds = numpydatabase['axialtumorbounds']
    dataidarray = numpydatabase['dataid']
    dbtrainindex = np.isin(dataidarray, train_index)
    dbtestindex = np.isin(dataidarray, test_index)
    subsetidx_train = np.all(np.vstack((axialbounds, dbtrainindex)), axis=0)
    subsetidx_test = np.all(np.vstack((axialbounds, dbtestindex)), axis=0)
    if np.sum(subsetidx_train) + np.sum(subsetidx_test) != min(
            np.sum(axialbounds), np.sum(dbtrainindex)):
        raise ("data error: slice numbers dont match")

    print('copy memory map from disk to RAM...')
    trainingsubset = numpydatabase[subsetidx_train]

    np.random.seed(seed=0)
    np.random.shuffle(trainingsubset)
    totnslice = len(trainingsubset)

    if settings.options.D3:
        x_data = trainingsubset['imagedata']
        y_data = trainingsubset['truthdata']

        x_train = thick_slices(x_data, settings.options.thickness)
        y_train = thick_slices(y_data, settings.options.thickness)
    else:
        x_train = trainingsubset['imagedata']
        y_train = trainingsubset['truthdata']

    slicesplit = int(0.9 * totnslice)
    TRAINING_SLICES = slice(0, slicesplit)
    VALIDATION_SLICES = slice(slicesplit, totnslice)

    print("\nkfolds : ", kfolds)
    print("idfold : ", idfold)
    print("slices in kfold   : ", totnslice)
    print("slices training   : ", slicesplit)
    print("slices validation : ", totnslice - slicesplit)
    try:
        print("slices testing    : ", len(numpydatabase[subsetidx_test]))
    except:
        print("slices testing    : 0")

    ###
    ### data preprocessing : applying liver mask
    ###
    y_train_typed = y_train.astype(settings.SEG_DTYPE)

    liver_idx = y_train_typed > 0
    y_train_liver = np.zeros_like(y_train_typed)
    y_train_liver[liver_idx] = 1

    tumor_idx = y_train_typed > 1
    y_train_tumor = np.zeros_like(y_train_typed)
    y_train_tumor[tumor_idx] = 1

    x_masked = x_train * y_train_liver - 100.0 * (1.0 - y_train_liver)
    x_masked = x_masked.astype(settings.IMG_DTYPE)

    ###
    ### set up output, logging, and callbacks
    ###
    logfileoutputdir = '%s/%03d/%03d' % (settings.options.outdir, kfolds,
                                         idfold)
    os.system('mkdir -p ' + logfileoutputdir)
    os.system('mkdir -p ' + logfileoutputdir + '/nii')
    os.system('mkdir -p ' + logfileoutputdir + '/tumor')
    print("Output to\t", logfileoutputdir)

    ###
    ### create and run model
    ###
    opt = GetOptimizer()
    callbacks, modelloc = GetCallbacks(logfileoutputdir, "tumor")
    lss, met = GetLoss()
    model = get_unet()
    model.compile(loss=lss, metrics=met, optimizer=opt)

    print("\n\n\tlivermask training...\tModel parameters: {0:,}".format(
        model.count_params()))

    if settings.options.augment:
        train_datagen = ImageDataGenerator(
            brightness_range=[0.95, 1.05],
            width_shift_range=[-0.1, 0.1],
            height_shift_range=[-0.1, 0.1],
            horizontal_flip=True,
            vertical_flip=True,
            zoom_range=0.1,
            fill_mode='nearest',
        )
    else:
        train_datagen = ImageDataGenerator()

    test_datagen = ImageDataGenerator()

    if settings.options.D3:
        train_generator = train_datagen.flow(
            x_masked[TRAINING_SLICES, :, :, :, np.newaxis],
            y_train_tumor[TRAINING_SLICES, :, :, :, np.newaxis],
            batch_size=settings.options.trainingbatch)
        test_generator = test_datagen.flow(
            x_masked[TRAINING_SLICES, :, :, :, np.newaxis],
            y_train_tumor[TRAINING_SLICES, :, :, :, np.newaxis],
            batch_size=settings.options.validationbatch)
    else:
        train_generator = train_datagen.flow(
            x_masked[TRAINING_SLICES, :, :, np.newaxis],
            y_train_tumor[TRAINING_SLICES, :, :, np.newaxis],
            batch_size=settings.options.trainingbatch)
        test_generator = test_datagen.flow(
            x_masked[VALIDATION_SLICES, :, :, np.newaxis],
            y_train_tumor[VALIDATION_SLICES, :, :, np.newaxis],
            batch_size=settings.options.validationbatch)

    history_liver = model.fit_generator(
        train_generator,
        steps_per_epoch=slicesplit // settings.options.trainingbatch,
        validation_steps=(totnslice - slicesplit) //
        settings.options.validationbatch,
        epochs=settings.options.numepochs,
        validation_data=test_generator,
        callbacks=callbacks)

    ###
    ### make predicions on validation set
    ###
    print("\n\n\tapplying models...")
    if settings.options.D3:
        y_pred_float = model.predict(x_masked[VALIDATION_SLICES, :, :, :,
                                              np.newaxis])
    else:
        y_pred_float = model.predict(x_masked[VALIDATION_SLICES, :, :,
                                              np.newaxis])

    y_pred_seg = (y_pred_float[..., 0] >=
                  settings.options.segthreshold).astype(settings.SEG_DTYPE)

    print("\tsaving to file...")

    if settings.options.D3:
        trueinnii = nib.Nifti1Image(x_train[VALIDATION_SLICES, :, :, :], None)
        truesegnii = nib.Nifti1Image(y_train[VALIDATION_SLICES, :, :, :], None)
        truelivernii = nib.Nifti1Image(
            y_train_liver[VALIDATION_SLICES, :, :, :], None)
        truetumornii = nib.Nifti1Image(
            y_train_tumor[VALIDATION_SLICES, :, :, :], None)
    else:
        trueinnii = nib.Nifti1Image(x_train[VALIDATION_SLICES, :, :], None)
        truesegnii = nib.Nifti1Image(y_train[VALIDATION_SLICES, :, :], None)
        truelivernii = nib.Nifti1Image(y_train_liver[VALIDATION_SLICES, :, :],
                                       None)
        truetumornii = nib.Nifti1Image(y_train_tumor[VALIDATION_SLICES, :, :],
                                       None)

    predsegnii = nib.Nifti1Image(y_pred_seg, None)
    predfloatnii = nib.Nifti1Image(y_pred_float, None)

    trueinnii.to_filename(logfileoutputdir + '/nii/trueimg.nii.gz')
    truesegnii.to_filename(logfileoutputdir + '/nii/truseg.nii.gz')
    truelivernii.to_filename(logfileoutputdir + '/nii/trueliver.nii.gz')
    truetumornii.to_filename(logfileoutputdir + '/nii/truetumor.nii.gz')
    predsegnii.to_filename(logfileoutputdir + '/nii/predtumorseg.nii.gz')
    predfloatnii.to_filename(logfileoutputdir + '/nii/predtumorfloat.nii.gz')

    print("\done saving.")
    return modelloc
Esempio n. 4
0
def TrainModel(idfold=0):

    from setupmodel import GetSetupKfolds, GetCallbacks, GetOptimizer, GetLoss
    from buildmodel import get_unet

    ###
    ### set up output, logging, and callbacks
    ###
    kfolds = settings.options.kfolds

    logfileoutputdir = '%s/%03d/%03d' % (settings.options.outdir, kfolds,
                                         idfold)
    os.system('mkdir -p ' + logfileoutputdir)
    os.system('mkdir -p ' + logfileoutputdir + '/nii')
    os.system('mkdir -p ' + logfileoutputdir + '/liver')
    print("Output to\t", logfileoutputdir)

    ###
    ### load data
    ###

    print('loading memory map db for large dataset')
    numpydatabase = np.load(settings._globalnpfile)
    (train_index, test_index,
     valid_index) = GetSetupKfolds(settings.options.dbfile, kfolds, idfold)

    print('copy data subsets into memory...')
    axialbounds = numpydatabase['axialliverbounds']
    dataidarray = numpydatabase['dataid']
    dbtrainindex = np.isin(dataidarray, train_index)
    dbtestindex = np.isin(dataidarray, test_index)
    dbvalidindex = np.isin(dataidarray, valid_index)
    subsetidx_train = np.all(np.vstack((axialbounds, dbtrainindex)), axis=0)
    subsetidx_test = np.all(np.vstack((axialbounds, dbtestindex)), axis=0)
    subsetidx_valid = np.all(np.vstack((axialbounds, dbvalidindex)), axis=0)
    if np.sum(subsetidx_train) + np.sum(subsetidx_test) + np.sum(
            subsetidx_valid) != min(np.sum(axialbounds), np.sum(dbtrainindex)):
        raise ("data error: slice numbers dont match")

    print('copy memory map from disk to RAM...')
    trainingsubset = numpydatabase[subsetidx_train]
    validsubset = numpydatabase[subsetidx_valid]
    testsubset = numpydatabase[subsetidx_test]

    #  trimg = trainingsubset['imagedata']
    #  trseg = trainingsubset['truthdata']
    #  vaimg = validsubset['imagedata']
    #  vaseg = validsubset['truthdata']
    #  teimg = testsubset['imagedata']
    #  teseg = testsubset['truthdata']

    #  trimg_img = nib.Nifti1Image(trimg, None)
    #  trimg_img.to_filename( logfileoutputdir+'/nii/train-img.nii.gz')
    #  vaimg_img = nib.Nifti1Image(vaimg, None)
    #  vaimg_img.to_filename( logfileoutputdir+'/nii/valid-img.nii.gz')
    #  teimg_img = nib.Nifti1Image(teimg, None)
    #  teimg_img.to_filename( logfileoutputdir+'/nii/test-img.nii.gz')
    #
    #  trseg_img = nib.Nifti1Image(trseg, None)
    #  trseg_img.to_filename( logfileoutputdir+'/nii/train-seg.nii.gz')
    #  vaseg_img = nib.Nifti1Image(vaseg, None)
    #  vaseg_img.to_filename( logfileoutputdir+'/nii/valid-seg.nii.gz')
    #  teseg_img = nib.Nifti1Image(teseg, None)
    #  teseg_img.to_filename( logfileoutputdir+'/nii/test-seg.nii.gz')

    np.random.seed(seed=0)
    np.random.shuffle(trainingsubset)

    ntrainslices = len(trainingsubset)
    nvalidslices = len(validsubset)

    x_train = trainingsubset['imagedata']
    y_train = trainingsubset['truthdata']

    x_valid = validsubset['imagedata']
    y_valid = validsubset['truthdata']

    print("\nkfolds : ", kfolds)
    print("idfold : ", idfold)
    print("slices training   : ", ntrainslices)
    print("slices validation : ", nvalidslices)

    ###
    ### data preprocessing : applying liver mask
    ###
    y_train_typed = y_train.astype(settings.SEG_DTYPE)
    y_train_liver = preprocess.livermask(y_train_typed)

    x_train_typed = x_train
    x_train_typed = preprocess.window(x_train_typed, settings.options.hu_lb,
                                      settings.options.hu_ub)
    x_train_typed = preprocess.rescale(x_train_typed, settings.options.hu_lb,
                                       settings.options.hu_ub)

    y_valid_typed = y_valid.astype(settings.SEG_DTYPE)
    y_valid_liver = preprocess.livermask(y_valid_typed)

    x_valid_typed = x_valid
    x_valid_typed = preprocess.window(x_valid_typed, settings.options.hu_lb,
                                      settings.options.hu_ub)
    x_valid_typed = preprocess.rescale(x_valid_typed, settings.options.hu_lb,
                                       settings.options.hu_ub)

    ###
    ### create and run model
    ###
    opt = GetOptimizer()
    callbacks, modelloc = GetCallbacks(logfileoutputdir, "liver")
    lss, met = GetLoss()
    model = get_unet()
    model.compile(loss=lss, metrics=met, optimizer=opt)

    print("\n\n\tlivermask training...\tModel parameters: {0:,}".format(
        model.count_params()))

    if settings.options.augment:
        train_datagen = ImageDataGenerator(
            brightness_range=[0.9, 1.1],
            preprocessing_function=preprocess.post_augment,
        )
        train_maskgen = ImageDataGenerator()
    else:
        train_datagen = ImageDataGenerator()
        train_maskgen = ImageDataGenerator()

    sd = 2  # arbitrary but fixed seed for ImageDataGenerators()
    dataflow = train_datagen.flow(x_train_typed[..., np.newaxis],
                                  batch_size=settings.options.trainingbatch,
                                  seed=sd,
                                  shuffle=True)
    maskflow = train_maskgen.flow(y_train_liver[..., np.newaxis],
                                  batch_size=settings.options.trainingbatch,
                                  seed=sd,
                                  shuffle=True)
    train_generator = zip(dataflow, maskflow)

    #  train_generator = train_datagen.flow(x_train_typed[...,np.newaxis],
    #          y=y_train_liver[...,np.newaxis],
    #          batch_size=settings.options.trainingbatch,
    #          seed=sd,
    #          shuffle=True)

    valid_datagen = ImageDataGenerator()
    valid_maskgen = ImageDataGenerator()

    validdataflow = valid_datagen.flow(
        x_valid_typed[..., np.newaxis],
        batch_size=settings.options.validationbatch,
        seed=sd,
        shuffle=True)
    validmaskflow = valid_maskgen.flow(
        y_valid_liver[..., np.newaxis],
        batch_size=settings.options.validationbatch,
        seed=sd,
        shuffle=True)
    valid_generator = zip(validdataflow, validmaskflow)

    ###
    ### visualize augmentation
    ###
    #
    #  import matplotlib
    #  matplotlib.use('TkAgg')
    #  from matplotlib import pyplot as plt
    #  for i in range(8):
    #      plt.subplot(4,4,2*i + 1)
    #      imbatch = dataflow.next()
    #      sgbatch = maskflow.next()
    #      imaug = imbatch[0][:,:,0]
    #      sgaug = sgbatch[0][:,:,0]
    #      plt.imshow(imaug)
    #      plt.subplot(4,4,2*i + 2)
    #      plt.imshow(sgaug)
    #  plt.show()
    #  return
    #

    history_liver = model.fit_generator(
        train_generator,
        steps_per_epoch=ntrainslices / settings.options.trainingbatch,
        epochs=settings.options.numepochs,
        validation_data=valid_generator,
        callbacks=callbacks,
        shuffle=True,
        validation_steps=nvalidslices / settings.options.validationbatch,
    )

    ###
    ### make predicions on validation set
    ###
    print("\n\n\tapplying models...")
    y_pred_float = model.predict(x_valid_typed[..., np.newaxis])
    y_pred_seg = (y_pred_float[..., 0] >=
                  settings.options.segthreshold).astype(settings.SEG_DTYPE)

    print("\tsaving to file...")
    trueinnii = nib.Nifti1Image(x_valid, None)
    truesegnii = nib.Nifti1Image(y_valid, None)
    #  windownii     = nib.Nifti1Image(x_valid_typed, None)
    truelivernii = nib.Nifti1Image(y_valid_liver, None)
    predsegnii = nib.Nifti1Image(y_pred_seg, None)
    predfloatnii = nib.Nifti1Image(y_pred_float, None)

    trueinnii.to_filename(logfileoutputdir + '/nii/trueimg.nii.gz')
    truesegnii.to_filename(logfileoutputdir + '/nii/trueseg.nii.gz')
    #  windownii.to_filename(    logfileoutputdir+'/nii/windowedimg.nii.gz')
    truelivernii.to_filename(logfileoutputdir + '/nii/trueliver.nii.gz')
    predsegnii.to_filename(logfileoutputdir + '/nii/predtumorseg.nii.gz')
    predfloatnii.to_filename(logfileoutputdir + '/nii/predtumorfloat.nii.gz')

    print("\done saving.")
    return modelloc
Esempio n. 5
0
def TrainModel(idfold=0, saveloclist=None):

    from setupmodel import GetSetupKfolds, GetCallbacks, GetOptimizer, GetLoss
    from buildmodel import get_unet

    os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
    os.environ['HDF5_USE_FILE_LOCKING'] = 'FALSE'

    ###
    ### set up output, logging, and callbacks
    ###
    kfolds = settings.options.kfolds

    logfileoutputdir = '%s/%03d/%03d' % (settings.options.outdir, kfolds,
                                         idfold)
    os.system('mkdir -p ' + logfileoutputdir)
    os.system('mkdir -p ' + logfileoutputdir + '/nii')
    if settings.options.liver:
        os.system('mkdir -p ' + logfileoutputdir + '/liver')
    elif settings.options.tumor:
        os.system('mkdir -p ' + logfileoutputdir + '/tumor')
    else:
        print('need to choose one of {liver,tumor}')
        raise ValueError(
            'need to choose to perform liver or tumor segmentation')
    print("Output to\t", logfileoutputdir)

    ###
    ### load data
    ###

    (train_index, test_index,
     valid_index) = GetSetupKfolds(settings.options.dbfile, kfolds, idfold)
    #    if not saveloclist:
    #        saveloclist = setup_training_from_file()

    if settings.options.liver:
        saveloclist = settings.options.datafiles_liver
    elif settings.options.tumor:
        saveloclist = settings.options.datafiles_tumor
    else:
        saveloclist = settings.options.datafiles_liver

    loclist = np.genfromtxt(saveloclist, delimiter=',', dtype='str')[1:]
    trainingsubset = [row for row in loclist if int(row[0]) in train_index]
    testingsubset = [row for row in loclist if int(row[0]) in test_index]
    validsubset = [row for row in loclist if int(row[0]) in valid_index]

    ###
    ### create and run model
    ###
    opt = GetOptimizer()
    callbacks, modelloc = GetCallbacks(logfileoutputdir, "liver")
    lss, met = GetLoss()
    model = get_unet()
    model.summary()
    model.compile(loss=lss, metrics=met, optimizer=opt)
    print("\n\n\tlivermask training...\tModel parameters: {0:,}".format(
        model.count_params()))

    train_xlist = [row[1] for row in trainingsubset]
    train_ylist = [row[2] for row in trainingsubset]

    valid_xlist = [row[1] for row in validsubset]
    valid_ylist = [row[2] for row in validsubset]

    training_generator = NpyDataGenerator(train_xlist, train_ylist)
    validation_generator = NpyDataGenerator(valid_xlist, valid_ylist)
    history_liver = model.fit_generator( \
                        verbose=2-int(settings.options.verbose),
                        generator=training_generator,
                        validation_data=validation_generator,
                        use_multiprocessing=True,
                        workers=8,
                        # steps_per_epoch  = ntrainslices / settings.options.trainingbatch,
                        epochs           = settings.options.numepochs,
                        callbacks        = callbacks,
                        shuffle          = True,
                        # validation_steps = nvalidslices / settings.options.validationbatch,
                        )
    return modelloc
Esempio n. 6
0
def TrainModel(idfold=0):

    from setupmodel import GetSetupKfolds, GetCallbacks, GetOptimizer, GetLoss
    from buildmodel import get_unet

    os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'

    ###
    ### load data
    ###

    kfolds = settings.options.kfolds

    logfileoutputdir = '%s/%03d/%03d' % (settings.options.outdir, kfolds,
                                         idfold)
    os.system('mkdir -p ' + logfileoutputdir)
    os.system('mkdir -p ' + logfileoutputdir + '/nii')
    os.system('mkdir -p ' + logfileoutputdir + '/tumor')
    print("Output to\t", logfileoutputdir)

    print('loading memory map db for large dataset')
    numpydatabase = np.load(settings._globalnpfile)
    (train_index, test_index,
     valid_index) = GetSetupKfolds(settings.options.dbfile, kfolds, idfold)

    print('copy data subsets into memory...')
    axialbounds = numpydatabase['axialtumorbounds']
    dataidarray = numpydatabase['dataid']
    dbtrainindex = np.isin(dataidarray, train_index)
    dbtestindex = np.isin(dataidarray, test_index)
    dbvalidindex = np.isin(dataidarray, valid_index)
    subsetidx_train = np.all(np.vstack((axialbounds, dbtrainindex)), axis=0)
    subsetidx_test = np.all(np.vstack((axialbounds, dbtestindex)), axis=0)
    subsetidx_valid = np.all(np.vstack((axialbounds, dbvalidindex)), axis=0)
    if np.sum(subsetidx_train) + np.sum(subsetidx_test) + np.sum(
            subsetidx_valid) != min(np.sum(axialbounds), np.sum(dbtrainindex)):
        raise ("data error: slice numbers dont match")

    print('copy memory map from disk to RAM...')
    trainingsubset = numpydatabase[subsetidx_train]
    validsubset = numpydatabase[subsetidx_valid]
    testsubset = numpydatabase[subsetidx_test]

    del numpydatabase
    del axialbounds
    del dataidarray
    del dbtrainindex
    del dbtestindex
    del dbvalidindex
    del subsetidx_train
    del subsetidx_test
    del subsetidx_valid

    np.random.seed(seed=0)
    np.random.shuffle(trainingsubset)
    ntrainslices = len(trainingsubset)
    nvalidslices = len(validsubset)

    x_train = trainingsubset['imagedata']
    y_train = trainingsubset['truthdata']

    x_valid = validsubset['imagedata']
    y_valid = validsubset['truthdata']

    print('\nkfolds : ', kfolds)
    print("idfold : ", idfold)
    print("slices training   : ", ntrainslices)
    print("slices validation : ", nvalidslices)

    ###
    ### data preprocessing : applying liver mask
    ###
    y_train_typed = y_train.astype(settings.SEG_DTYPE)
    y_train_liver = preprocess.livermask(y_train_typed)
    y_train_tumor = preprocess.tumormask(y_train_typed)

    x_train_typed = x_train
    x_train_typed = preprocess.window(x_train_typed, settings.options.hu_lb,
                                      settings.options.hu_ub)
    x_train_typed = preprocess.rescale(x_train_typed, settings.options.hu_lb,
                                       settings.options.hu_ub)

    x_train_masked = x_train_typed * y_train_liver.astype(
        settings.IMG_DTYPE) - (1.0 - y_train_liver.astype(settings.IMG_DTYPE))

    y_valid_typed = y_valid.astype(settings.SEG_DTYPE)
    y_valid_liver = preprocess.livermask(y_valid_typed)
    y_valid_tumor = preprocess.tumormask(y_valid_typed)

    x_valid_typed = x_valid
    x_valid_typed = preprocess.window(x_valid_typed, settings.options.hu_lb,
                                      settings.options.hu_ub)
    x_valid_typed = preprocess.rescale(x_valid_typed, settings.options.hu_lb,
                                       settings.options.hu_ub)

    x_valid_masked = x_valid_typed * y_valid_liver.astype(
        settings.IMG_DTYPE) - (1.0 - y_valid_liver.astype(settings.IMG_DTYPE))

    ###
    ### create and run model
    ###
    opt = GetOptimizer()
    callbacks, modelloc = GetCallbacks(logfileoutputdir, "tumor")
    lss, met = GetLoss()
    model = get_unet()
    model.compile(loss=lss, metrics=met, optimizer=opt)

    print("\n\n\tlivermask training...\tModel parameters: {0:,}".format(
        model.count_params()))

    if settings.options.augment:
        train_datagen = ImageDataGenerator(
            brightness_range=[0.9, 1.1],
            fill_mode='nearest',
            preprocessing_function=preprocess.post_augment)
        train_maskgen = ImageDataGenerator()
    else:
        train_datagen = ImageDataGenerator()
        train_maskgen = ImageDataGenerator()

    test_datagen = ImageDataGenerator()

    sd = 2
    #  dataflow = train_datagen.flow(x_train_typed[...,np.newaxis],
    dataflow = train_datagen.flow(x_train_masked[..., np.newaxis],
                                  batch_size=settings.options.trainingbatch,
                                  seed=sd,
                                  shuffle=True)
    maskflow = train_maskgen.flow(y_train_tumor[..., np.newaxis],
                                  batch_size=settings.options.trainingbatch,
                                  seed=sd,
                                  shuffle=True)
    train_generator = zip(dataflow, maskflow)

    valid_datagen = ImageDataGenerator()
    valid_maskgen = ImageDataGenerator()

    valid_dataflow = valid_datagen.flow(
        x_valid_masked[..., np.newaxis],
        batch_size=settings.options.validationbatch,
        seed=sd,
        shuffle=True)
    valid_maskflow = valid_maskgen.flow(
        y_valid_tumor[..., np.newaxis],
        batch_size=settings.options.validationbatch,
        seed=sd,
        shuffle=True)
    valid_generator = zip(valid_dataflow, valid_maskflow)

    history_tumor = model.fit_generator(
        train_generator,
        steps_per_epoch=ntrainslices / settings.options.trainingbatch,
        epochs=settings.options.numepochs,
        validation_data=valid_generator,
        callbacks=callbacks,
        shuffle=True,
        validation_steps=nvalidslices / settings.options.validationbatch,
    )

    del x_train
    del y_train
    del x_train_typed
    del y_train_typed
    del y_train_liver
    del y_train_tumor
    del x_train_masked

    ###
    ### make predicions on validation set
    ###
    print("\n\n\tapplying models...")
    y_pred_float = model.predict(x_valid_masked[..., np.newaxis])
    y_pred_seg = (y_pred_float[..., 0] >=
                  settings.options.segthreshold).astype(settings.SEG_DTYPE)

    print("\tsaving to file...")
    trueinnii = nib.Nifti1Image(x_valid, None)
    truesegnii = nib.Nifti1Image(y_valid, None)
    truelivernii = nib.Nifti1Image(y_valid_liver, None)
    truetumornii = nib.Nifti1Image(y_valid_tumor, None)
    windownii = nib.Nifti1Image(x_valid_typed, None)
    maskednii = nib.Nifti1Image(x_valid_masked, None)
    predsegnii = nib.Nifti1Image(y_pred_seg, None)
    predfloatnii = nib.Nifti1Image(y_pred_float, None)

    trueinnii.to_filename(logfileoutputdir + '/nii/trueimg.nii.gz')
    truesegnii.to_filename(logfileoutputdir + '/nii/trueseg.nii.gz')
    truetumornii.to_filename(logfileoutputdir + '/nii/truetumor.nii.gz')
    truelivernii.to_filename(logfileoutputdir + '/nii/trueliver.nii.gz')
    windownii.to_filename(logfileoutputdir + '/nii/windowedimg.nii.gz')
    maskednii.to_filename(logfileoutputdir + '/nii/masked.nii.gz')
    predsegnii.to_filename(logfileoutputdir + '/nii/predtumorseg.nii.gz')
    predfloatnii.to_filename(logfileoutputdir + '/nii/predtumorfloat.nii.gz')

    del x_valid
    del y_valid
    del x_valid_typed
    del y_valid_typed
    del y_valid_liver
    del y_valid_tumor
    del x_valid_masked

    print("\done saving.")
    return modelloc
Esempio n. 7
0
def TrainModel(idfold=0):

  from setupmodel import GetSetupKfolds, GetCallbacks, GetOptimizer, GetLoss
  from buildmodel import get_unet, thick_slices, unthick_slices, unthick

  ###
  ### set up output, logging and callbacks 
  ###

  kfolds = settings.options.kfolds

  logfileoutputdir= '%s/%03d/%03d' % (settings.options.outdir, kfolds, idfold)
  os.system ('mkdir -p ' + logfileoutputdir)
  os.system ('mkdir -p ' + logfileoutputdir + '/nii')
  os.system ('mkdir -p ' + logfileoutputdir + '/liver')
  print("Output to\t", logfileoutputdir)
  
  
   ###
   ### load data
   ###

  print('loading memory map db for large dataset')
  numpydatabase = np.load(settings._globalnpfile)
  (train_index,test_index,valid_index) = GetSetupKfolds(settings.options.dbfile, kfolds, idfold)

  print('copy data subsets into memory...')
  axialbounds = numpydatabase['axialliverbounds']
  dataidarray = numpydatabase['dataid']
  
  dbtrainindex = np.isin(dataidarray, train_index )
  dbtestindex  = np.isin(dataidarray, test_index  )
  dbvalidindex = np.isin(dataidarray, valid_index ) 
  
  subsetidx_train  = np.all( np.vstack((axialbounds , dbtrainindex)) , axis=0 )
  subsetidx_test   = np.all( np.vstack((axialbounds , dbtestindex )) , axis=0 )
  subsetidx_valid  = np.all( np.vstack((axialbounds , dbvalidindex)) , axis=0 )
  
  print(np.sum(subsetidx_train) + np.sum(subsetidx_test) + np.sum(subsetidx_valid))
  print(min(np.sum(axialbounds ),np.sum(dbtrainindex )))
  
  if np.sum(subsetidx_train) + np.sum(subsetidx_test) + np.sum(subsetidx_valid) != min(np.sum(axialbounds ),np.sum(dbtrainindex )) :
      raise("data error: slice numbers dont match")

  print('copy memory map from disk to RAM...')
  trainingsubset = numpydatabase[subsetidx_train]
  validsubset    = numpydatabase[subsetidx_valid]
  testsubset     = numpydatabase[subsetidx_test]

#  np.random.seed(seed=0)
#  np.random.shuffle(trainingsubset)
  
  ntrainslices = len(trainingsubset)
  nvalidslices = len(validsubset)

  if settings.options.D3:
      x_data  = trainingsubset['imagedata']
      y_data  = trainingsubset['truthdata']
      x_valid = validsubset['imagedata']
      y_valid = validsubset['truthdata']
      
      x_train = thick_slices(x_data, settings.options.thickness, trainingsubset['dataid'], train_index)
      y_train = thick_slices(y_data, settings.options.thickness, trainingsubset['dataid'], train_index)
      
      x_valid = thick_slices(x_valid, settings.options.thickness, validsubset['dataid'], valid_index)
      y_valid = thick_slices(y_valid, settings.options.thickness, validsubset['dataid'], valid_index)
      
      np.random.seed(seed=0)
      train_shuffle = np.random.permutation(x_train.shape[0])
      valid_shuffle = np.random.permutation(x_valid.shape[0])
      x_train = x_train[train_shuffle,...]
      y_train = y_train[train_shuffle,...]
      x_valid = x_valid[valid_shuffle,...]
      y_valid = y_valid[valid_shuffle,...]
      
  elif settings.options.D25: 
      x_data  = trainingsubset['imagedata']
      y_data  = trainingsubset['truthdata']
      x_valid = validsubset['imagedata']
      y_valid = validsubset['truthdata']
      
      x_train = thick_slices(x_data, settings.options.thickness, trainingsubset['dataid'], train_index)
      x_valid = thick_slices(x_valid, settings.options.thickness, validsubset['dataid'], valid_index)
      
      y_train = thick_slices(y_data, 1, trainingsubset['dataid'], train_index)
      y_valid = thick_slices(y_valid, 1, validsubset['dataid'], valid_index)
      
      np.random.seed(seed=0)
      train_shuffle = np.random.permutation(x_train.shape[0])
      valid_shuffle = np.random.permutation(x_valid.shape[0])
      x_train = x_train[train_shuffle,...]
      y_train = y_train[train_shuffle,...]
      x_valid = x_valid[valid_shuffle,...]
      y_valid = y_valid[valid_shuffle,...]
  
  else: 
      np.random.seed(seed=0)
      np.random.shuffle(trainingsubset)
      
      x_train=trainingsubset['imagedata']
      y_train=trainingsubset['truthdata']
      x_valid=validsubset['imagedata']
      y_valid=validsubset['truthdata']
  

#  slicesplit        = int(0.9 * totnslice)
#  TRAINING_SLICES   = slice(         0, slicesplit)
#  VALIDATION_SLICES = slice(slicesplit, totnslice )


  print("\nkfolds : ", kfolds)
  print("idfold : ", idfold)
  print("slices training   : ", ntrainslices)
  print("slices validation : ", nvalidslices)
  try:
      print("slices testing    : ", len(testsubset))
  except:
      print("slices testing    : 0")


  ###
  ### data preprocessing : applying liver mask
  ###
  y_train_typed = y_train.astype(settings.SEG_DTYPE)
  y_train_liver = preprocess.livermask(y_train_typed)
  
  x_train_typed = x_train
  x_train_typed = preprocess.window(x_train_typed, settings.options.hu_lb, settings.options.hu_ub)
  x_train_typed = preprocess.rescale(x_train_typed, settings.options.hu_lb, settings.options.hu_ub)

  y_valid_typed = y_valid.astype(settings.SEG_DTYPE)
  y_valid_liver = preprocess.livermask(y_valid_typed)
  
  x_valid_typed = x_valid
  x_valid_typed = preprocess.window(x_valid_typed, settings.options.hu_lb, settings.options.hu_ub)
  x_valid_typed = preprocess.rescale(x_valid_typed, settings.options.hu_lb, settings.options.hu_ub)

#  liver_idx = y_train_typed > 0
#  y_train_liver = np.zeros_like(y_train_typed)
#  y_train_liver[liver_idx] = 1
#
#  tumor_idx = y_train_typed > 1
#  y_train_tumor = np.zeros_like(y_train_typed)
#  y_train_tumor[tumor_idx] = 1
#
#  x_masked = x_train * y_train_liver - 100.0*(1.0 - y_train_liver)
#  x_masked = x_masked.astype(settings.IMG_DTYPE)



  ###
  ### create and run model   tf.keras.losses.mean_squared_error,
  ###
  opt                 = GetOptimizer()
  callbacks, modelloc = GetCallbacks(logfileoutputdir, "liver")
  lss, met            = GetLoss()
  model               = get_unet()
  model.compile(loss       = lss,
                metrics    = met,
                optimizer  = opt)

  print("\n\n\tlivermask training...\tModel parameters: {0:,}".format(model.count_params()))

  if settings.options.D3: 
      if settings.options.augment:
          train_datagen = ImageDataGenerator3D(
              brightness_range=[0.9,1.1],
              width_shift_range=[-0.1,0.1],
              height_shift_range=[-0.1,0.1],
              horizontal_flip=True,
              vertical_flip=True,
              zoom_range=0.1,
              fill_mode='nearest',
              preprocessing_function=preprocess.post_augment
              )
          train_maskgen = ImageDataGenerator3D()
      else:
          train_datagen = ImageDataGenerator3D()
          train_maskgen = ImageDataGenerator3D()
          
      valid_datagen = ImageDataGenerator3D()
      valid_maskgen = ImageDataGenerator3D()
  else:
      if settings.options.augment:
          train_datagen = ImageDataGenerator2D(
              brightness_range=[0.9,1.1],
              width_shift_range=[-0.1,0.1],
              height_shift_range=[-0.1,0.1],
              horizontal_flip=True,
              vertical_flip=True,
              zoom_range=0.1,
              fill_mode='nearest',
              preprocessing_function=preprocess.post_augment
              )
          train_maskgen = ImageDataGenerator2D()
      else:
          train_datagen = ImageDataGenerator2D()
          train_maskgen = ImageDataGenerator2D()
          
      valid_datagen = ImageDataGenerator2D()
      valid_maskgen = ImageDataGenerator2D()
      
 
  sd = 2  # arbitrary but fixed seed for ImageDataGenerators()
  
  if settings.options.D25:
      dataflow = train_datagen.flow(x_train_typed,
                                    batch_size=settings.options.trainingbatch,
                                    seed=sd,
                                    shuffle=True)
      maskflow = train_maskgen.flow(y_train_liver,
                                    batch_size=settings.options.trainingbatch,
                                    seed=sd,
                                    shuffle=True)
      
      validdataflow = valid_datagen.flow(x_valid_typed,
                                         batch_size=settings.options.validationbatch,
                                         seed=sd,
                                         shuffle=True)
      validmaskflow = valid_maskgen.flow(y_valid_liver,
                                         batch_size=settings.options.validationbatch,
                                         seed=sd,
                                         shuffle=True)
  else: 
      dataflow = train_datagen.flow(x_train_typed[...,np.newaxis],
                                    batch_size=settings.options.trainingbatch,
                                    seed=sd,
                                    shuffle=True)
      maskflow = train_maskgen.flow(y_train_liver[...,np.newaxis],
                                    batch_size=settings.options.trainingbatch,
                                    seed=sd,
                                    shuffle=True)
      
      validdataflow = valid_datagen.flow(x_valid_typed[...,np.newaxis],
                                         batch_size=settings.options.validationbatch,
                                         seed=sd,
                                         shuffle=True)
      validmaskflow = valid_maskgen.flow(y_valid_liver[...,np.newaxis],
                                         batch_size=settings.options.validationbatch,
                                         seed=sd,
                                         shuffle=True)
   
  train_generator = zip(dataflow, maskflow)  
  valid_generator = zip(validdataflow, validmaskflow)
      
  history_liver = model.fit_generator(
                        train_generator,
                        steps_per_epoch= ntrainslices // settings.options.trainingbatch,
                        validation_steps = nvalidslices // settings.options.validationbatch,
                        epochs=settings.options.numepochs,
                        validation_data=valid_generator,
                        callbacks=callbacks,
                        shuffle=True)



  ###
  ### make predicions on validation set
  ###
  print("\n\n\tapplying models...")
  
  if settings.options.D25:
      y_pred_float = model.predict( x_valid_typed )[...,0] #[...,settings.options.thickness] )
  else: 
      y_pred_float = model.predict( x_valid_typed[...,np.newaxis] )[...,0]
      
  y_pred_seg   = (y_pred_float >= settings.options.segthreshold).astype(settings.SEG_DTYPE)    

  if settings.options.D3:
      x_valid       = unthick(x_valid, settings.options.thickness, validsubset['dataid'], valid_index)
      y_valid       = unthick(y_valid, settings.options.thickness, validsubset['dataid'], valid_index)
      
      y_valid_liver = unthick(y_valid_liver, settings.options.thickness, validsubset['dataid'], valid_index)
      y_pred_float  = unthick(y_pred_float, settings.options.thickness, validsubset['dataid'], valid_index)
      y_pred_seg    = unthick(y_pred_seg, settings.options.thickness, validsubset['dataid'], valid_index)

  print("\tsaving to file...")
  
  trueinnii     = nib.Nifti1Image(x_valid,       None)
  truesegnii    = nib.Nifti1Image(y_valid,       None)
#  windownii     = nib.Nifti1Image(x_valid_typed, None)
  truelivernii  = nib.Nifti1Image(y_valid_liver, None)
  predsegnii    = nib.Nifti1Image(y_pred_seg, None )
  predfloatnii  = nib.Nifti1Image(y_pred_float, None)
  
  trueinnii.to_filename(    logfileoutputdir+'/nii/trueimg.nii.gz')
  truesegnii.to_filename(   logfileoutputdir+'/nii/truseg.nii.gz')
#  windownii.to_filename(    logfileoutputdir+'/nii/windowedimg.nii.gz')
  truelivernii.to_filename( logfileoutputdir+'/nii/trueliver.nii.gz')
  predsegnii.to_filename(   logfileoutputdir+'/nii/predtumorseg.nii.gz')
  predfloatnii.to_filename( logfileoutputdir+'/nii/predtumorfloat.nii.gz')

  print("t\done saving.")
  return modelloc