Ejemplo n.º 1
0
def compute_dsc_scores(y_true, y_pred):
    dims = y_true.shape
    dsc_l2_2D  = [None]*dims[0]
    dsc_int_2D = [None]*dims[0]

    dsc3D_l2  = dsc_l2_3D(y_true, y_pred)
    dsc3D_int = dsc_int_3D(y_true, y_pred)

    for iii in range(dims[0]):
        this_y_true = y_true[iii,...]
        this_y_pred = y_pred[iii,...]
        dsc_l2_2D[iii]  = dsc_l2(this_y_true[np.newaxis,...], this_y_pred[np.newaxis,...])
        dsc_int_2D[iii] = dsc_int(this_y_true[np.newaxis,...], this_y_pred[np.newaxis,...])

    return dsc3D_l2, dsc3D_int, dsc_l2_2D, dsc_int_2D
Ejemplo n.º 2
0
def OneKfold(i=0, datadict=None):

    k = settings.options.kfolds

    modelloc = TrainModel(idfold=i)
    (train_set, test_set) = GetSetupKfolds(settings.options.dbfile, k, i)
    for idtest in test_set:
        baseloc = '%s/%03d/%03d' % (settings.options.outdir, k, i)
        imgloc = '%s/%s' % (settings.options.rootlocation,
                            datadict[idtest]['image'])
        segloc = '%s/%s' % (settings.options.rootlocation,
                            datadict[idtest]['label'])
        outloc = '%s/label-%04d.nii.gz' % (baseloc, idtest)
        sumscore = 0
        if settings.options.numepochs > 0 and settings.options.makepredictions:  # doing K-fold prediction as I train

            imagepredict = nib.load(imgloc)
            imageheader = imagepredict.header
            numpypredict = imagepredict.get_data().astype(settings.IMG_DTYPE)
            allseg = nib.load(segloc).get_data().astype(settings.SEG_DTYPE)

            liver_idx = allseg > 0
            tumor_idx = allseg > 1

            seg_liver = np.zeros_like(allseg)
            seg_liver[liver_idx] = 1

            seg_tumor = np.zeros_like(allseg)
            seg_tumor[tumor_idx] = 1

            image_liver = seg_liver * numpypredict - 100.0 * (1.0 - seg_liver)
            image_liver = image_liver.astype(settings.IMG_DTYPE)

            predseg = PredictModelFromNumpy(model=modelloc,
                                            image=image_liver,
                                            imageheader=imageheader,
                                            outdir=outloc)
            score = dsc_int_3D(seg_tumor, predseg)
            sumscore += score
            print(idtest, "\t", score)

    print(k, " avg dice:\t", sumscore / len(test_set))
Ejemplo n.º 3
0
def make_all_noisy_images(imgloc,
                          segloc,
                          epslist,
                          sparsity,
                          loaded_net,
                          loaded_net_reg,
                          dlist,
                          rootsaveloc='./',
                          ns=5):

    img, seg = get_img(imgloc, segloc)

    os.makedirs(rootsaveloc, exist_ok=True)
    os.makedirs(rootsaveloc + '/clean', exist_ok=True)
    save_as_nii(img, rootsaveloc + '/clean/img.nii.gz')
    save_as_nii(seg, rootsaveloc + '/clean/seg.nii.gz')

    print('\n\n')
    print(imgloc)
    print(segloc)
    print('\t making clean prediction...')
    clean_pred = loaded_net.predict(img, batch_size=16)
    clean_pred_seg = (clean_pred >= 0.5).astype(np.uint8)
    clean_pred_seg = preprocess.largest_connected_component(
        clean_pred_seg).astype(np.uint8)
    clean_dsc_int = dsc_int_3D(clean_pred_seg, seg)
    clean_dsc_l2 = dsc_l2_3D(clean_pred, seg)
    save_as_nii(clean_pred, rootsaveloc + '/clean/pred.nii.gz')
    save_as_nii(clean_pred_seg, rootsaveloc + '/clean/pred-seg.nii.gz')

    print('\t making clean regularized prediction...')
    clean_pred_reg = loaded_net_reg.predict(img, batch_size=16)
    clean_pred_seg_reg = (clean_pred_reg >= 0.5).astype(np.uint8)
    clean_pred_seg_reg = preprocess.largest_connected_component(
        clean_pred_seg_reg).astype(np.uint8)
    clean_dsc_int_reg = dsc_int_3D(clean_pred_seg_reg, seg)
    clean_dsc_l2_reg = dsc_l2_3D(clean_pred_reg, seg)
    save_as_nii(clean_pred_reg, rootsaveloc + '/clean/pred-reg.nii.gz')
    save_as_nii(clean_pred_seg_reg, rootsaveloc + '/clean/pred-seg-reg.nii.gz')

    print('\t generating noise structures...')
    adv = fgsm(img, seg, loaded_net, eps=1.0, loss=dsc_l2)
    noisemaker = set_params(epslist=epslist,
                            seg=seg,
                            sparsity=sparsity,
                            loaded_net=loaded_net,
                            dlist=dlist,
                            ns=ns,
                            adv=adv)

    for distr in noisemaker:

        f = noisemaker[distr]['maker']
        e_vals = noisemaker[distr]['list']

        for ie, e in enumerate(e_vals):
            t1 = time.time()
            print(noisemaker[distr]['name'], e)
            if e == 0.0:
                # update values in noisemaker dict
                noisemaker[distr]['eps_list'].append(e)
                noisemaker[distr]['dsc_l2'].append(clean_dsc_l2)
                noisemaker[distr]['dsc_int'].append(clean_dsc_int)
                noisemaker[distr]['del_l2'].append(0.0)
                noisemaker[distr]['del_int'].append(0.0)
                noisemaker[distr]['dsc_l2_reg'].append(clean_dsc_l2_reg)
                noisemaker[distr]['dsc_int_reg'].append(clean_dsc_int_reg)
                noisemaker[distr]['del_l2_reg'].append(0.0)
                noisemaker[distr]['del_int_reg'].append(0.0)


#                # save image files as nii
#                print('\t saving nii files...')
#                saveloc = rootsaveloc+'/'+noisemaker[distr]['name']+'/'+str(e)+'/'
#                os.makedirs(saveloc, exist_ok=True)
#                noise = np.zeros_like(img)
#                save_as_nii(noise,              saveloc+'perturbation.nii.gz')
#                save_as_nii(img,                saveloc+'noisy-img.nii.gz')
#                save_as_nii(clean_pred,         saveloc+'noisy-pred-float.nii.gz')
#                save_as_nii(clean_pred_reg,     saveloc+'noisy-pred-float-reg.nii.gz')
#                save_as_nii(clean_pred_seg,     saveloc+'noisy-pred-seg.nii.gz')
#                save_as_nii(clean_pred_seg_reg, saveloc+'noisy-pred-seg-reg.nii.gz')
            else:
                for sss in range(noisemaker[distr]['nsamples']):

                    # make noise
                    print('\t', e, sss)
                    print('\t making noise...')
                    noisy = f(img, e)

                    # predict on the noisy images, no regularization
                    print('\t making noisy prediction...')
                    noisy_pred = loaded_net.predict(noisy, batch_size=16)
                    noisy_pred_seg = (noisy_pred >= 0.5).astype(np.uint8)
                    noisy_pred_seg = preprocess.largest_connected_component(
                        noisy_pred_seg).astype(np.uint8)

                    # predict on the noisy images, with regularization
                    print('\t making regularized prediction...')
                    noisy_pred_reg = loaded_net_reg.predict(noisy,
                                                            batch_size=16)
                    noisy_pred_seg_reg = (noisy_pred_reg >= 0.5).astype(
                        np.uint8)
                    noisy_pred_seg_reg = preprocess.largest_connected_component(
                        noisy_pred_seg_reg).astype(np.uint8)

                    # calculate and print DSC scores, no regularization
                    print('\t calculating dsc scores...')
                    noisy_dsc_int = dsc_int_3D(noisy_pred_seg, seg)
                    noisy_dsc_l2 = dsc_l2_3D(noisy_pred, seg)
                    print('\t\t clean     DSC(int) : ', clean_dsc_int,
                          '\t noisy     DSC(int) : ', noisy_dsc_int,
                          '\t delta : ', noisy_dsc_int - clean_dsc_int)
                    print('\t\t clean     DSC(l2)  : ', clean_dsc_l2,
                          '\t noisy     DSC(l2)  : ', noisy_dsc_l2,
                          '\t delta : ', noisy_dsc_l2 - clean_dsc_l2)

                    # calculate and print DSC scores, with regularization
                    print('\t calculating regularized dsc scores...')
                    noisy_dsc_int_reg = dsc_int_3D(noisy_pred_seg_reg, seg)
                    noisy_dsc_l2_reg = dsc_l2_3D(noisy_pred_reg, seg)
                    print('\t\t clean reg DSC(int) : ', clean_dsc_int_reg,
                          '\t noisy reg DSC(int) : ', noisy_dsc_int_reg,
                          '\t delta : ', noisy_dsc_int_reg - clean_dsc_int_reg)
                    print('\t\t clean reg DSC(l2)  : ', clean_dsc_l2_reg,
                          '\t noisy reg DSC(l2)  : ', noisy_dsc_l2_reg,
                          '\t delta : ', noisy_dsc_l2_reg - clean_dsc_l2_reg)

                    # update values in noisemaker dict
                    print('\t storing data into dict...')
                    noisemaker[distr]['eps_list'].append(e)
                    noisemaker[distr]['dsc_l2'].append(noisy_dsc_l2)
                    noisemaker[distr]['dsc_int'].append(noisy_dsc_int)
                    noisemaker[distr]['del_l2'].append(noisy_dsc_l2 -
                                                       clean_dsc_l2)
                    noisemaker[distr]['del_int'].append(noisy_dsc_int -
                                                        clean_dsc_int)
                    noisemaker[distr]['dsc_l2_reg'].append(noisy_dsc_l2_reg)
                    noisemaker[distr]['dsc_int_reg'].append(noisy_dsc_int_reg)
                    noisemaker[distr]['del_l2_reg'].append(noisy_dsc_l2_reg -
                                                           clean_dsc_l2_reg)
                    noisemaker[distr]['del_int_reg'].append(noisy_dsc_int_reg -
                                                            clean_dsc_int_reg)

                    # save image files as nii
                    saveloc = rootsaveloc + '/' + noisemaker[distr][
                        'name'] + '/' + str(e) + '/'
                    os.makedirs(saveloc, exist_ok=True)
                    print('\t saving nii files...')
                    save_as_nii(
                        noisy - img,
                        saveloc + 'perturbation-' + str(sss) + '.nii.gz')
                    save_as_nii(noisy,
                                saveloc + 'noisy-img-' + str(sss) + '.nii.gz')
                    save_as_nii(
                        noisy_pred,
                        saveloc + 'noisy-pred-float-' + str(sss) + '.nii.gz')
                    save_as_nii(
                        noisy_pred_reg, saveloc + 'noisy-pred-float-reg-' +
                        str(sss) + '.nii.gz')
                    save_as_nii(
                        noisy_pred_seg,
                        saveloc + 'noisy-pred-seg-' + str(sss) + '.nii.gz')
                    save_as_nii(
                        noisy_pred_seg_reg,
                        saveloc + 'noisy-pred-seg-reg-' + str(sss) + '.nii.gz')

                    print('\t deleting files...')
                    del noisy
                    del noisy_pred
                    del noisy_pred_reg
                    del noisy_pred_seg
                    del noisy_pred_seg_reg

            t2 = time.time()
            print('\t time : ', t2 - t1)

    print('\n')
    return noisemaker