Example #1
0
    def evalute(self, validGenerator):
        self.model.eval()
        losses = []
        recalls = []
        mrrs = []
        with torch.no_grad():
            batchSize = validGenerator.batchSize
            hidden = self.model.initHidden(batchSize)
            for ii, (input, target, finishedMask,
                     validMask) in tqdm(enumerate(validGenerator),
                                        total=validGenerator.totalIters,
                                        miniters=1000,
                                        position=0,
                                        leave=True):
                input = input.to(self.device)
                target = target.to(self.device)
                hidden = self.model.resetHidden(hidden, finishedMask,
                                                validMask)
                logit, hidden = self.model(input, hidden)

                if (self.lossFunc is not None):
                    loss = self.lossFunc(logit, target)
                    if (~np.isnan(loss.item())):
                        losses.append(loss.item())

                recall, mrr = Metrics.calc(logit, target, k=self.topk)
                recalls.append(recall)
                mrrs.append(mrr.cpu().numpy())

        if (len(losses)):
            meanLoss = np.mean(losses)
        else:
            meanLoss = 0

        meanRecall = np.mean(recalls)
        meanMrr = np.mean(mrrs)

        return meanLoss, meanRecall, meanMrr
Example #2
0
def metrics_computation(input_channels, seq_len, features_order, kernel_size=[5, 5], strides=[1, 1], conv_paddings=[0, 0],
                        gpu=True, gpu_device="cuda:0", gen_path=P.GenDataDir(), alpha_=P.Alpha, lambda_=P.Lambda,
                        train_id="train", test_id="test", epoch=P.Epoch, addParamFn=P.ParamFunction, seed=42,
                        verbose=False):
    """
    """
    # Add restarting mechanism.
    metrics = Me.Metrics(input_channels=input_channels, seq_len=seq_len, kernel_sizes=kernel_size,
                         strides=strides, conv_paddings=conv_paddings, gpu=gpu, distance_metric=P.DistanceMetric,
                         gpu_device=gpu_device, prep_included=P.PreprocessingIncluded,
                         prep_excluded=P.PreprocessingExcluded, scale=P.Scale,
                         features_ordering=features_order, seed=seed, verbose=verbose,
                         data_fmt_class="MotionSenseDataset", window_overlap=P.Window_overlap)
    results = R.Results(resultDir=P.ResultsDir())
    sr = R.StopRestart(resultDir=P.ResultsDir())

    # Read data and compute metrics,
    # Baseline
    metric_epochs = 200
    metric_batch = 256
    def shaping(path):
        # Set the data as the same for all, the generated ones and the original ones such that we have the same
        # computation graph.
        data = D.MotionSenseDataset(path, window_overlap=P.Window_overlap)
        return data.__inverse_transform_conv__(sensor_tensor=data.sensor, phy=data.phy_data,
                                               act_tensor=data.activities, sens_tensor=data.sensitive,
                                               user_id_tensor=data.users_id, trials=data.trials, cpu_device=CPU_DEVICE)

    o_test = shaping(P.TestPath)
    if not sr.computed(epoch=0, alpha_=np.NaN, lambda_=np.NaN, Attribute="act") or \
        not sr.computed(epoch=0, alpha_=np.NaN, lambda_=np.NaN, Attribute="gender"):
        o_train = shaping(P.TrainPath)

        sp = metrics.sensitive_attribute(train_set=o_train, test_set=o_test,
                                         use_accuracy=False, act_name="act", drop=[], sens_name="gender", ms_act_name="act",
                                         ms_sens_name="sens", sklearn_data_process=None, use_phys=True, physNodes=3,
                                         phys_names=["height", "weight", "age"], ms_phys_name="phy", epoch=metric_epochs,
                                         batch_size=metric_batch, loss_fn=None)
        tp = metrics.task(train_set=o_train, test_set=o_test,
                          act_name="act", drop=[], sens_name="gender", ms_act_name="act", ms_sens_name="sens",
                          sklearn_data_process=None, use_phys=True, physNodes=3, phys_names=["height", "weight", "age"],
                          ms_phys_name="phy", epoch=metric_epochs, batch_size=metric_batch, loss_fn=None)
        di = metrics.distance(o_test, o_test)
        # Add and save results
        results.add_result(distance=di, s_acc=sp[0], s_ber=sp[1], t_acc=tp[0], t_ber=tp[1], sens_name="gender",
                           act_name="act", epoch=0, alpha_=np.NaN, lambda_=np.NaN)


    # Sanitization
    name = lambda n, e: "{}/{}_{}.csv".format(gen_path, n, addParamFn(e))
    for epoch in tqdm.tqdm(range(1, epoch+1)):
        if not sr.computed(epoch=epoch, alpha_=alpha_, lambda_=lambda_, Attribute="act") or \
            not sr.computed(epoch=epoch, alpha_=alpha_, lambda_=lambda_, Attribute="gender"):
            train = shaping(name(train_id, epoch))
            test = shaping(name(test_id, epoch))
            sp = metrics.sensitive_attribute(train_set=train, test_set=test,
                                             use_accuracy=False, act_name="act", drop=[], sens_name="gender", ms_act_name="act",
                                             ms_sens_name="sens", sklearn_data_process=None, use_phys=True, physNodes=3,
                                             phys_names=["height", "weight", "age"], ms_phys_name="phy", epoch=metric_epochs,
                                             batch_size=metric_batch, loss_fn=None, learning_rate=5e-4, weight_decay=0)
            tp = metrics.task(train_set=train, test_set=test,
                              act_name="act", drop=[], sens_name="gender", ms_act_name="act", ms_sens_name="sens",
                              sklearn_data_process=None, use_phys=True, physNodes=3, phys_names=["height", "weight", "age"],
                              ms_phys_name="phy", epoch=metric_epochs, batch_size=metric_batch, loss_fn=None,
                              learning_rate=5e-4, weight_decay=0)
            di = metrics.distance(o_test_set=o_test, test_set=test)
            # Add and save results
            results.add_result(distance=di, s_acc=sp[0], s_ber=sp[1], t_acc=tp[0], t_ber=tp[1], sens_name="gender",
                               act_name="act", epoch=epoch, alpha_=alpha_, lambda_=lambda_)
Example #3
0
    xTestBrute = testBrute.loc[:, 'rotationRate.x':'userAcceleration.z']
    xTestBrute = xTestBrute.as_matrix()
    tempDistance = []
    for j in range(len(xTestBrute[0])):
        tempDistance.append(Di.euclidean(xTestOriginal[:, j], xTestBrute[:,
                                                                         j]))
    distance.append(np.mean(tempDistance))

    epoch.append(e)

    for i in range(numberClass):

        clf = tc[i]
        clf.fit(xTrain, yTrain1)
        predictions = clf.predict(xTest)
        accuAct[i].append(Me.Metrics().accuracy(predictions, yTest1))

        clf = tc[i]
        clf.fit(xTrain, yTrain2)
        predictions = clf.predict(xTest)
        accuGender[i].append(Me.Metrics().accuracy(predictions, yTest2))

    train_prep = D.Preprocessing(adressTrain,
                                 prep_excluded=P.PreprocessingExcluded,
                                 scale=P.Scale,
                                 prep_included=P.PreprocessingIncluded,
                                 numeric_as_categoric_max_thr=0)
    train_prep.set_features_ordering(None)
    test_prep = D.Preprocessing(adressTest,
                                prep_excluded=P.PreprocessingExcluded,
                                scale=P.Scale,
Example #4
0
                            prep_included=P.PreprocessingIncluded)
test_prep.set_features_ordering(None)
test_prep.fit_transform()
train_ds = D.MotionSenseDataset(train_prep)
test_ds = D.MotionSenseDataset(test_prep)


# build data loaders
batch_size = 256
# Defining model Predicting activities.
activities = np.unique(train_ds.activities)
phys_shape = train_ds.phy_data.shape[1]


metrics = Me.Metrics(input_channels=train_ds.input_channels, seq_len=train_ds.seq_len, kernel_sizes=[5,5],
                     strides=[1, 1], conv_paddings=[0, 0], gpu=True,
                     gpu_device="cuda:0", prep_included=P.PreprocessingIncluded, prep_excluded=P.PreprocessingExcluded,
                     scale=P.Scale, features_ordering=None, seed=42, verbose=True)

# sp = metrics.sensitive_attribute(train_set=pd.read_csv("../" + P.TrainPath), test_set=pd.read_csv("../" + P.TestPath),
#                                  use_accuracy=False, drop=[], sens_name="sens", phys_names="phy", epoch=200, batch_size=batch_size,
#                                  loss_fn=None, verbose=False)
# tp = metrics.sensitive_attribute(train_set="../" + P.TrainPath, test_set="../" + P.TestPath, use_accuracy=False,
#                                  drop=[], sens_name="sens", phys_names="phy", epoch=200, batch_size=batch_size, loss_fn=Cl.AccuracyLoss(),
#                                  verbose=False)
tp = metrics.sensitive_attribute(train_set=pd.read_csv("../" + P.TrainPath), test_set=pd.read_csv("../" + P.TestPath),
                                 use_accuracy=False, act_name="act", drop=[], sens_name="gender", ms_act_name="act",
                                 ms_sens_name="sens", sklearn_data_process=None, use_phys=True, physNodes=3,
                                 phys_names=["height", "weight", "age"], ms_phys_name="phy", epoch=200, batch_size=256,
                                 loss_fn=None)

sp = metrics.task(train_set=pd.read_csv("../" + P.TrainPath), test_set=pd.read_csv("../" + P.TestPath),
Example #5
0
def model_test(use_existing):

    cm.mkdir(cm.workingPath.testingResults_path)

    # Loading test data:

    filename = cm.filename
    modelname = cm.modellist[0]

    # Single CT:
    originFile_list = sorted(
        glob(cm.workingPath.originTestingSet_path + filename))
    maskAortaFile_list = sorted(
        glob(cm.workingPath.aortaTestingSet_path + filename))
    maskPulFile_list = sorted(
        glob(cm.workingPath.pulTestingSet_path + filename))

    # Zahra CTs:
    # originFile_list = sorted(glob(cm.workingPath.originTestingSet_path + "vol*.dcm"))
    # maskAortaFile_list = sorted(glob(cm.workingPath.aortaTestingSet_path + "vol*.dcm"))
    # maskPulFile_list = sorted(glob(cm.workingPath.pulTestingSet_path + "vol*.dcm"))

    # Lidia CTs:
    # originFile_list = sorted(glob(cm.workingPath.originLidiaTestingSet_path + "vol*.dcm"))[61:]
    # maskAortaFile_list = sorted(glob(cm.workingPath.originLidiaTestingSet_path + "vol*.dcm"))[61:]
    # maskPulFile_list = sorted(glob(cm.workingPath.originLidiaTestingSet_path + "vol*.dcm"))[61:]

    # Abnormal CTs:
    # originFile_list = sorted(glob(cm.workingPath.originAbnormalTestingSet_path + "vol126*.dcm"))
    # maskAortaFile_list = sorted(glob(cm.workingPath.originAbnormalTestingSet_path + "vol126*.dcm"))
    # maskPulFile_list = sorted(glob(cm.workingPath.originAbnormalTestingSet_path + "vol126*.dcm"))

    for i in range(len(originFile_list)):

        # Show runtime:
        starttime = datetime.datetime.now()

        vol_slices = []
        out_test_images = []
        out_test_masks = []

        current_file = originFile_list[i].split('/')[-1]
        current_dir = cm.workingPath.testingResults_path + str(
            current_file[:-17])
        cm.mkdir(current_dir)
        cm.mkdir(current_dir + '/Plots/')
        cm.mkdir(current_dir + '/Surface_Distance/')
        cm.mkdir(current_dir + '/DICOM/')
        cm.mkdir(current_dir + '/mhd/')

        stdout_backup = sys.stdout
        log_file = open(current_dir + "/logs.txt", "w")
        sys.stdout = log_file

        print('-' * 30)
        print('Loading test data %04d/%04d...' % (i + 1, len(originFile_list)))

        originVol, originVol_num, originVolwidth, originVolheight = dp.loadFile(
            originFile_list[i])
        maskAortaVol, maskAortaVol_num, maskAortaVolwidth, maskAortaVolheight = dp.loadFile(
            maskAortaFile_list[i])
        maskPulVol, maskPulVol_num, maskPulVolwidth, maskPulVolheight = dp.loadFile(
            maskPulFile_list[i])
        maskVol = maskAortaVol

        for j in range(len(maskAortaVol)):
            maskAortaVol[j] = np.where(maskAortaVol[j] != 0, 1, 0)
        for j in range(len(maskPulVol)):
            maskPulVol[j] = np.where(maskPulVol[j] != 0, 2, 0)

        maskVol = maskVol + maskPulVol

        for j in range(len(maskVol)):
            maskVol[j] = np.where(maskVol[j] > 2, 0, maskVol[j])
            # maskVol[j] = np.where(maskVol[j] != 0, 0, maskVol[j])

        # Make the Vessel class
        for j in range(len(maskVol)):
            maskVol[j] = np.where(maskVol[j] != 0, 1, 0)

        for i in range(originVol.shape[0]):
            img = originVol[i, :, :]

            out_test_images.append(img)
        for i in range(maskVol.shape[0]):
            img = maskVol[i, :, :]

            out_test_masks.append(img)

        vol_slices.append(originVol.shape[0])

        maskAortaVol = None
        maskPulVol = None
        maskVol = None
        originVol = None
        nb_class = 2
        outmasks_onehot = to_categorical(out_test_masks, num_classes=nb_class)
        final_test_images = np.ndarray([sum(vol_slices), 512, 512, 1],
                                       dtype=np.int16)
        final_test_masks = np.ndarray([sum(vol_slices), 512, 512, nb_class],
                                      dtype=np.int8)

        for i in range(len(out_test_images)):
            final_test_images[i, :, :, 0] = out_test_images[i]
            final_test_masks[i, :, :, :] = outmasks_onehot[i]

        outmasks_onehot = None
        out_test_masks = None
        out_test_images = None

        row = cm.img_rows_3d
        col = cm.img_cols_3d
        row_1 = int((512 - row) / 2)
        row_2 = int(512 - (512 - row) / 2)
        col_1 = int((512 - col) / 2)
        col_2 = int(512 - (512 - col) / 2)
        slices = cm.slices_3d
        gaps = cm.gaps_3d

        final_images_crop = final_test_images[:, row_1:row_2, col_1:col_2, :]
        final_masks_crop = final_test_masks[:, row_1:row_2, col_1:col_2, :]

        sitk.WriteImage(
            sitk.GetImageFromArray(np.uint16(final_test_masks[:, :, :, 1])),
            current_dir + '/DICOM/masksAortaGroundTruth.dcm')
        # sitk.WriteImage(sitk.GetImageFromArray(np.uint16(final_test_masks[:, :, :, 2])), current_dir + '/DICOM/masksPulGroundTruth.dcm')

        sitk.WriteImage(
            sitk.GetImageFromArray(np.uint16(final_test_masks[:, :, :, 1])),
            current_dir + '/mhd/masksAortaGroundTruth.mhd')
        # sitk.WriteImage(sitk.GetImageFromArray(np.uint16(final_test_masks[:, :, :, 2])), current_dir + '/mhd/masksPulGroundTruth.mhd')

        # clear the masks for the final step:
        final_test_masks = np.where(final_test_masks == 0, 0, 0)

        num_patches = int((sum(vol_slices) - slices) / gaps)

        test_image = np.ndarray([1, slices, row, col, 1], dtype=np.int16)

        predicted_mask_volume = np.ndarray(
            [sum(vol_slices), row, col, nb_class], dtype=np.float32)

        # model = DenseUNet_3D.get_3d_denseunet()
        # model = UNet_3D.get_3d_unet_bn()
        # model = RSUNet_3D.get_3d_rsunet(opti)
        # model = UNet_3D.get_3d_wnet(opti)
        model = UNet_3D.get_3d_unet()
        # model = RSUNet_3D_Gerda.get_3d_rsunet_Gerdafeature(opti)

        using_start_end = 1
        start_slice = cm.start_slice
        end_slice = -1

        if use_existing:
            model.load_weights(modelname)

        for i in range(num_patches):
            count1 = i * gaps
            count2 = i * gaps + slices
            test_image[0] = final_images_crop[count1:count2]

            predicted_mask = model.predict(test_image)

            if i == int(num_patches * 0.63):
                vs.visualize_activation_in_layer_one_plot(
                    model, test_image, current_dir)
            else:
                pass

            predicted_mask_volume[count1:count2] += predicted_mask[
                0, :, :, :, :]

        t = len(predicted_mask_volume)
        for i in range(0, slices, gaps):
            predicted_mask_volume[i:(
                i +
                gaps)] = predicted_mask_volume[i:(i + gaps)] / (i / gaps + 1)

        for i in range(0, slices, gaps):
            predicted_mask_volume[(t - i -
                                   gaps):(t - i)] = predicted_mask_volume[
                                       (t - i - gaps):(t - i)] / (i / gaps + 1)

        for i in range(slices, (len(predicted_mask_volume) - slices)):
            predicted_mask_volume[i] = predicted_mask_volume[i] / (slices /
                                                                   gaps)

        np.save(cm.workingPath.testingSet_path + 'testImages.npy',
                final_images_crop)
        np.save(cm.workingPath.testingSet_path + 'testMasks.npy',
                final_masks_crop)
        np.save(cm.workingPath.testingSet_path + 'masksTestPredicted.npy',
                predicted_mask_volume)

        final_images_crop = None
        final_masks_crop = None
        predicted_mask_volume = None

        imgs_origin = np.load(cm.workingPath.testingSet_path +
                              'testImages.npy').astype(np.int16)
        imgs_true = np.load(cm.workingPath.testingSet_path +
                            'testMasks.npy').astype(np.int8)
        imgs_predict = np.load(cm.workingPath.testingSet_path +
                               'masksTestPredicted.npy').astype(np.float32)
        imgs_predict_threshold = np.load(cm.workingPath.testingSet_path +
                                         'masksTestPredicted.npy').astype(
                                             np.float32)

        ########## ROC curve aorta

        actual = imgs_true[:, :, :, 1].reshape(-1)
        predictions = imgs_predict[:, :, :, 1].reshape(-1)
        # predictions = np.where(predictions < (0.7), 0, 1)

        false_positive_rate_aorta, true_positive_rate_aorta, thresholds_aorta = roc_curve(
            actual, predictions, pos_label=1)
        roc_auc_aorta = auc(false_positive_rate_aorta,
                            true_positive_rate_aorta)
        plt.figure(1, figsize=(6, 6))
        plt.figure(1)
        plt.title('ROC of Aorta')
        plt.plot(false_positive_rate_aorta, true_positive_rate_aorta, 'b')
        label = 'AUC = %0.2f' % roc_auc_aorta
        plt.legend(loc='lower right')
        plt.plot([0, 1], [0, 1], 'r--')
        plt.xlim([-0.0, 1.0])
        plt.ylim([-0.0, 1.0])
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        # plt.show()
        saveName = '/Plots/ROC_Aorta_curve.png'
        plt.savefig(current_dir + saveName)
        plt.close()
        ########## ROC curve pul

        # actual = imgs_true[:, :, :, 2].reshape(-1)
        # predictions = imgs_predict[:, :, :, 2].reshape(-1)
        #
        # false_positive_rate_pul, true_positive_rate_pul, thresholds_pul = roc_curve(actual, predictions, pos_label=1)
        # roc_auc_pul = auc(false_positive_rate_pul, true_positive_rate_pul)
        # plt.figure(2, figsize=(6, 6))
        # plt.figure(2)
        # plt.title('ROC of pul')
        # plt.plot(false_positive_rate_pul, true_positive_rate_pul, 'b')
        # label = 'AUC = %0.2f' % roc_auc_pul
        # plt.legend(loc='lower right')
        # plt.plot([0, 1], [0, 1], 'r--')
        # plt.xlim([-0.0, 1.0])
        # plt.ylim([-0.0, 1.0])
        # plt.xlabel('False Positive Rate')
        # plt.ylabel('True Positive Rate')
        # # plt.show()
        # saveName = '/Plots/ROC_Pul_curve.png'
        # plt.savefig(current_dir + saveName)
        # plt.close()

        false_positive_rate_aorta = None
        true_positive_rate_aorta = None
        false_positive_rate_pul = None
        true_positive_rate_pul = None

        imgs_predict_threshold = np.where(imgs_predict_threshold < (0.5), 0, 1)

        if using_start_end == 1:
            aortaMean = lf.dice_coef_np(
                imgs_predict_threshold[start_slice:end_slice, :, :, 1],
                imgs_true[start_slice:end_slice, :, :, 1])
            # pulMean = lf.dice_coef_np(imgs_predict_threshold[start_slice:end_slice, :, :, 2],
            #                           imgs_true[start_slice:end_slice, :, :, 2])
        else:
            aortaMean = lf.dice_coef_np(imgs_predict_threshold[:, :, :, 1],
                                        imgs_true[:, :, :, 1])
            # pulMean = lf.dice_coef_np(imgs_predict_threshold[:, :, :, 2], imgs_true[:, :, :, 2])

        np.savetxt(current_dir + '/Plots/AortaDicemean.txt',
                   np.array(aortaMean).reshape(1, ),
                   fmt='%.5f')
        # np.savetxt(current_dir + '/Plots/PulDicemean.txt', np.array(pulMean).reshape(1, ), fmt='%.5f')

        print('Model file:', modelname)
        print('-' * 30)
        print('Aorta Dice Coeff', aortaMean)
        # print('Pul Dice Coeff', pulMean)
        print('-' * 30)

        # Draw the subplots of figures:

        color1 = 'gray'  # ***
        color2 = 'viridis'  # ******
        # color = 'plasma'  # **
        # color = 'magma'  # ***
        # color2 = 'RdPu'  # ***
        # color = 'gray'  # ***
        # color = 'gray'  # ***

        transparent1 = 1.0
        transparent2 = 0.5

        # Slice parameters:

        #################################### Aorta
        # Automatically:

        steps = 40
        slice = range(0, len(imgs_origin), steps)
        plt_row = 3
        plt_col = int(len(imgs_origin) / steps)

        plt.figure(3, figsize=(25, 12))
        plt.figure(3)

        for i in slice:
            if i == 0:
                plt_num = int(i / steps) + 1
            else:
                plt_num = int(i / steps)

            if plt_num <= plt_col:

                ax1 = plt.subplot(plt_row, plt_col, plt_num)
                title = 'slice=' + str(i)
                plt.title(title)
                ax1.imshow(imgs_origin[i, :, :, 0],
                           cmap=color1,
                           alpha=transparent1)
                ax1.imshow(imgs_true[i, :, :, 1],
                           cmap=color2,
                           alpha=transparent2)

                ax2 = plt.subplot(plt_row, plt_col, plt_num + plt_col)
                title = 'slice=' + str(i)
                plt.title(title)
                ax2.imshow(imgs_origin[i, :, :, 0],
                           cmap=color1,
                           alpha=transparent1)
                ax2.imshow(imgs_predict[i, :, :, 1],
                           cmap=color2,
                           alpha=transparent2)

                ax3 = plt.subplot(plt_row, plt_col, plt_num + 2 * plt_col)
                title = 'slice=' + str(i)
                plt.title(title)
                ax3.imshow(imgs_origin[i, :, :, 0],
                           cmap=color1,
                           alpha=transparent1)
                ax3.imshow(imgs_predict_threshold[i, :, :, 1],
                           cmap=color2,
                           alpha=transparent2)
            else:
                pass

        modelname = cm.modellist[0]

        imageName = re.findall(r'\d+\.?\d*', modelname)
        epoch_num = int(imageName[0]) + 1
        accuracy = float(
            np.loadtxt(current_dir + '/Plots/AortaDicemean.txt', float))

        # saveName = 'epoch_' + str(epoch_num) + '_dice_' +str(accuracy) + '.png'
        saveName = '/Plots/epoch_Aorta_%02d_dice_%.3f.png' % (epoch_num - 1,
                                                              accuracy)

        plt.subplots_adjust(left=0.0,
                            bottom=0.05,
                            right=1.0,
                            top=0.95,
                            hspace=0.3,
                            wspace=0.3)
        plt.savefig(current_dir + saveName)
        plt.close()
        # plt.show()

        ################################ Pulmonary
        # steps = 40
        # slice = range(0, len(imgs_origin), steps)
        # plt_row = 3
        # plt_col = int(len(imgs_origin) / steps)
        #
        # plt.figure(4, figsize=(25, 12))
        # plt.figure(4)
        # for i in slice:
        #   if i == 0:
        #     plt_num = int(i / steps) + 1
        #   else:
        #     plt_num = int(i / steps)
        #
        #   if plt_num <= plt_col:
        #
        #     ax1 = plt.subplot(plt_row, plt_col, plt_num)
        #     title = 'slice=' + str(i)
        #     plt.title(title)
        #     ax1.imshow(imgs_origin[i, :, :, 0], cmap=color1, alpha=transparent1)
        #     ax1.imshow(imgs_true[i, :, :, 2], cmap=color2, alpha=transparent2)
        #
        #     ax2 = plt.subplot(plt_row, plt_col, plt_num + plt_col)
        #     title = 'slice=' + str(i)
        #     plt.title(title)
        #     ax2.imshow(imgs_origin[i, :, :, 0], cmap=color1, alpha=transparent1)
        #     ax2.imshow(imgs_predict[i, :, :, 2], cmap=color2, alpha=transparent2)
        #
        #     ax3 = plt.subplot(plt_row, plt_col, plt_num + 2 * plt_col)
        #     title = 'slice=' + str(i)
        #     plt.title(title)
        #     ax3.imshow(imgs_origin[i, :, :, 0], cmap=color1, alpha=transparent1)
        #     ax3.imshow(imgs_predict_threshold[i, :, :, 2], cmap=color2, alpha=transparent2)
        #   else:
        #     pass
        #
        # modelname = cm.modellist[0]
        #
        # imageName = re.findall(r'\d+\.?\d*', modelname)
        # epoch_num = int(imageName[0]) + 1
        # accuracy = float(np.loadtxt(current_dir + '/Plots/PulDicemean.txt', float))
        #
        # # saveName = 'epoch_' + str(epoch_num) + '_dice_' +str(accuracy) + '.png'
        # saveName = '/Plots/epoch_Pul_%02d_dice_%.3f.png' % (epoch_num - 1, accuracy)
        #
        # plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95, hspace=0.3, wspace=0.3)
        # plt.savefig(current_dir + saveName)
        # plt.close()
        # # plt.show()

        print('Images saved')
        # Save npy as dcm files:

        final_test_aorta_predicted_threshold = final_test_masks[:, :, :, 1]
        # final_test_pul_predicted_threshold = final_test_masks[:, :, :, 2]

        final_test_aorta_predicted_threshold[:, row_1:row_2, col_1:
                                             col_2] = imgs_predict_threshold[:, :, :,
                                                                             1]
        # final_test_pul_predicted_threshold[:, row_1:row_2, col_1:col_2] = imgs_predict_threshold[:, :, :, 2]

        new_imgs_dcm = sitk.GetImageFromArray(
            np.uint16(final_test_images + 4000))
        new_imgs_aorta_predict_dcm = sitk.GetImageFromArray(
            np.uint16(final_test_aorta_predicted_threshold))
        # new_imgs_pul_predict_dcm = sitk.GetImageFromArray(np.uint16(final_test_pul_predicted_threshold))

        sitk.WriteImage(new_imgs_dcm,
                        current_dir + '/DICOM/imagesPredicted.dcm')
        sitk.WriteImage(new_imgs_aorta_predict_dcm,
                        current_dir + '/DICOM/masksAortaPredicted.dcm')
        # sitk.WriteImage(new_imgs_pul_predict_dcm, current_dir + '/DICOM/masksPulPredicted.dcm')

        sitk.WriteImage(new_imgs_dcm, current_dir + '/mhd/imagesPredicted.mhd')
        sitk.WriteImage(new_imgs_aorta_predict_dcm,
                        current_dir + '/mhd/masksAortaPredicted.mhd')
        # sitk.WriteImage(new_imgs_pul_predict_dcm, current_dir + '/mhd/masksPulPredicted.mhd')

        mt.SegmentDist(current_dir + '/mhd/masksAortaPredicted.mhd',
                       current_dir + '/mhd/masksAortaGroundTruth.mhd',
                       current_dir + '/Surface_Distance/Aorta')
        # mt.SegmentDist(current_dir + '/mhd/masksPulPredicted.mhd',current_dir + '/mhd/masksPulGroundTruth.mhd', current_dir + '/Surface_Distance/Pul')

        # ds1 = dicom.read_file(maskAortaFile_list[0])
        # ds2 = dicom.read_file(cm.workingPath.testingSet_path + 'masksAortaPredicted.dcm')
        # ds1.PixelData = ds2.PixelData
        # ds1.pop('pixel_array')
        # ds1["pixel_array"] = ds2["pixel_array"]
        # ds1.save_as(cm.workingPath.testingSet_path + 'masksAortaPredicted.dcm')
        # ds1.save_as(cm.workingPath.testingSet_path + 'masksAortaPredicted.mhd')
        #
        # ds1 = dicom.read_file(maskPulFile_list[0])
        # ds2 = dicom.read_file(cm.workingPath.testingSet_path + 'masksPulPredicted.dcm')
        # ds1.PixelData = ds2.PixelData
        # ds1["pixelarray"] = ds2["pixelarray"]
        # ds1.save_as(cm.workingPath.testingSet_path + 'masksPulPredicted.dcm')
        # ds1.save_as(cm.workingPath.testingSet_path + 'masksPulPredicted.mhd')
        print('DICOM saved')

        # Clear memory for the next testing sample:

        final_test_aorta_predicted_threshold = None
        final_test_pul_predicted_threshold = None
        imgs_predict_threshold = None
        new_imgs_dcm = None
        new_imgs_aorta_predict_dcm = None
        new_imgs_pul_predict_dcm = None
        final_test_images = None
        final_test_masks = None
        imgs_origin = None
        imgs_predict = None
        imgs_true = None
        predicted_mask = None
        predictions = None

        endtime = datetime.datetime.now()
        print('-' * 30)
        print('running time:', endtime - starttime)

        log_file.close()
        sys.stdout = stdout_backup