示例#1
0
def Test():
    test_loader = LoadTestData(is_test=True)
    train_loader, validation_loader = LoadTVData(is_test=True)

    model = ResNet(Bottleneck, [3, 4, 6, 3]).to(device)
    model.load_state_dict(torch.load(model_path))

    fpr_list, tpr_list, auc_list = [], [], []

    name_list = ['Train', 'Validation', 'Test']
    loader_list = [train_loader, validation_loader, test_loader]

    model.eval()
    # with torch.no_grad():
    for name_num, loader in enumerate(loader_list):
        class_list, class_pred_list = [], []
        for i, (inputs, outputs) in enumerate(loader):
            t2, dwi, adc, roi, prostate = inputs[0], inputs[1], inputs[
                2], inputs[3], inputs[4]
            ece = np.argmax(outputs, axis=1)

            inputs = torch.cat([t2, dwi, adc, roi, prostate], axis=1)
            inputs = inputs.type(torch.FloatTensor).to(device)

            ece = ece.type(torch.LongTensor).to(device)

            class_out = model(inputs)
            class_out = torch.squeeze(class_out, dim=1)
            class_out_softmax = nn.functional.softmax(class_out, dim=1)

            class_list.extend(list(ece.cpu().numpy()))
            class_pred_list.extend(
                list(class_out_softmax.cpu().detach().numpy()[..., 1]))

        fpr, tpr, auc = get_auc(class_pred_list, class_list)
        fpr_list.append(fpr)
        tpr_list.append(tpr)
        auc_list.append(auc)
    draw_roc(fpr_list, tpr_list, auc_list, name_list)
示例#2
0
def Test():
    test_loader = LoadTestData(data_folder)
    train_loader, validation_loader = LoadTVData(data_folder, is_test=True)

    model = ResNeXt(5, 1, [3, 4, 6, 3]).to(device)
    model.load_state_dict(torch.load(model_path))

    fpr_list, tpr_list, auc_list = [], [], []

    name_list = ['Train', 'Validation', 'Test']
    loader_list = [train_loader, validation_loader, test_loader]

    # with torch.no_grad():
    model.eval()
    for name_num, loader in enumerate(loader_list):
        class_list, class_pred_list = [], []
        for i, (inputs, outputs) in enumerate(loader):
            t2, dwi, adc, pca, prostate = inputs[0], inputs[1], inputs[
                2], inputs[3], inputs[4]
            ece = outputs.to(device)

            inputs = torch.cat([t2, dwi, adc, pca, prostate], dim=1)
            inputs = inputs.float().to(device)

            class_out = model(inputs)
            class_out_sigmoid = class_out.sigmoid()

            class_list.extend(list(ece.cpu().numpy()))
            class_pred_list.extend(
                list(class_out_sigmoid.cpu().detach().numpy()))

        fpr, tpr, auc = get_auc(class_pred_list, class_list)
        fpr_list.append(fpr)
        tpr_list.append(tpr)
        auc_list.append(auc)
    draw_roc(fpr_list, tpr_list, auc_list, name_list)
示例#3
0
def ModelTest(weights_list=None, data_type=None):
    if data_type is None:
        data_type = ['alltrain', 'test']
    from Metric.classification_statistics import get_auc, draw_roc
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    input_shape = (192, 192)
    batch_size = 2
    model_folder = model_root + '/PAGNet_MaxPred'
    bc = BinaryClassification()

    fpr_list, tpr_list, auc_list = [], [], []
    train_result, test_result = {}, {}
    for type in data_type:
        spliter = DataSpliter()
        data_folder = os.path.join(data_root, type)
        sub_list = spliter.LoadName(data_root + '/{}-name.csv'.format(type))

        data = DataManager(sub_list=sub_list)
        data.AddOne(Image2D(data_folder + '/T2Slice', shape=input_shape))
        data.AddOne(Image2D(data_folder + '/AdcSlice', shape=input_shape))
        data.AddOne(Image2D(data_folder + '/DwiSlice', shape=input_shape))
        data.AddOne(Image2D(data_folder + '/DistanceMap', shape=input_shape, is_roi=True))
        data.AddOne(Label(data_root + '/label.csv', label_tag='Positive'), is_input=False)
        data_loader = DataLoader(data, batch_size=batch_size, shuffle=False)

        cv_folder_list = [one for one in IterateCase(model_folder, only_folder=True, verbose=0)]
        cv_pred_list, cv_label_list = [], []

        for cv_index, cv_folder in enumerate(cv_folder_list):
            model = ResNeXt(3, 2).to(device)
            if weights_list is None:
                one_fold_weights_list = [one for one in IterateCase(cv_folder, only_folder=False, verbose=0) if
                                         one.is_file()]
                one_fold_weights_list = sorted(one_fold_weights_list, key=lambda x: os.path.getctime(str(x)))
                weights_path = one_fold_weights_list[-1]
            else:
                weights_path = weights_list[cv_index]

            print(weights_path.name)
            model.load_state_dict(torch.load(str(weights_path)))

            pred_list, label_list = [], []
            model.eval()
            for inputs, outputs in data_loader:
                inputs = MoveTensorsToDevice(inputs, device)
                outputs = MoveTensorsToDevice(outputs, device)

                preds = model(*inputs)[:, 1]
                pred_list.extend((1 - preds).cpu().data.numpy().squeeze().tolist())
                label_list.extend((1 - outputs).cpu().data.numpy().astype(int).squeeze().tolist())
                # pred_list.extend((preds).cpu().data.numpy().squeeze().tolist())
                # label_list.extend((outputs).cpu().data.numpy().astype(int).squeeze().tolist())

            cv_pred_list.append(pred_list)
            cv_label_list.append(label_list)

            del model, weights_path

        cv_pred = np.array(cv_pred_list)
        cv_label = np.array(cv_label_list)
        mean_pred = np.mean(cv_pred, axis=0)
        mean_label = np.mean(cv_label, axis=0)

        fpr, tpr, auc = get_auc(mean_pred, mean_label)
        fpr_list.append(fpr)
        tpr_list.append(tpr)
        auc_list.append(auc)

        if type == 'alltrain':
            train_result = bc.Run(mean_pred.tolist(), mean_label.astype(int).tolist(), threshould=None)
            print(train_result)
        elif type == 'test':
            test_result = bc.Run(mean_pred.tolist(), mean_label.astype(int).tolist(), threshould=train_result['Youden Index'])
            print(test_result)

    draw_roc(fpr_list, tpr_list, auc_list, name_list=['alltrian', 'test'])
示例#4
0
def Test():
    from Metric.Dice import Dice
    import matplotlib.pyplot as plt
    dice = Dice()
    test_loader = LoadTestData()
    train_loader, validation_loader = LoadTVData(is_test=True)

    model = MultiTaskModel(in_channels=3, out_channels=1).to(device)
    model.load_state_dict(torch.load(model_path))

    fpr_list, tpr_list, auc_list = [], [], []

    name_list = ['Train', 'Validation', 'Test']
    loader_list = [train_loader, validation_loader, test_loader]

    # with torch.no_grad():
    model.eval()
    for name_num, loader in enumerate(loader_list):
        class_list, class_pred_list = [], []
        prostate_list, prostate_pred_list = [], []
        roi_list, roi_pred_list = [], []
        prostate_dice, roi_dice = [], []
        for i, (inputs, outputs) in enumerate(loader):
            t2, dwi, adc = inputs[0], inputs[1], inputs[2],
            ece, roi, prostate = np.squeeze(
                outputs[0],
                axis=1), outputs[1].to(device), outputs[2].to(device)

            inputs = torch.cat([t2, dwi, adc], axis=1)
            inputs = inputs.type(torch.FloatTensor).to(device)

            ece = np.argmax(ece, axis=1)
            ece = ece.type(torch.LongTensor).to(device)

            roi_out, prostate_out, class_out, _ = model(inputs)
            class_out_softmax = nn.functional.softmax(class_out, dim=1)

            class_list.extend(list(ece.cpu().numpy()))
            class_pred_list.extend(
                list(class_out_softmax.cpu().detach().numpy()[..., 1]))

            prostate_out = BinaryPred(prostate_out).cpu().detach()
            prostate = prostate.cpu()
            prostate_pred_list.extend(list(prostate_out))
            prostate_list.extend(list(prostate))

            roi_out = BinaryPred(roi_out).cpu().detach()
            roi = roi.cpu()
            roi_pred_list.extend(list(roi_out))
            roi_list.extend(list(roi))

        for idx in range(len(roi_list)):
            roi_dice.append(dice(roi_list[idx], roi_pred_list[idx]).numpy())
            prostate_dice.append(
                dice(prostate_list[idx], prostate_pred_list[idx]).numpy())
        print('average dice of roi:', sum(roi_dice) / len(roi_dice))
        print('average dice of prostate:',
              sum(prostate_dice) / len(prostate_dice))
        plt.hist(roi_dice)
        plt.title('Dice of Prostate Cancer in ' + name_list[name_num])
        plt.show()

        plt.hist(prostate_dice)
        plt.title('Dice of Prostate in ' + name_list[name_num])
        plt.show()

        fpr, tpr, auc = get_auc(class_pred_list, class_list)
        fpr_list.append(fpr)
        tpr_list.append(tpr)
        auc_list.append(auc)
    draw_roc(fpr_list, tpr_list, auc_list, name_list)
示例#5
0
def ModelTest(weights_list=None, data_type=None):
    if data_type is None:
        data_type = ['alltrain', 'test']
    from Metric.classification_statistics import get_auc, draw_roc
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    input_shape = (192, 192)
    batch_size = 2
    model_folder = model_root + '/ResNeXt_CBAM_CV_20201116_dis_five_lf'
    bc = BinaryClassification()

    fpr_list, tpr_list, auc_list = [], [], []
    for type in data_type:
        spliter = DataSpliter()
        sub_list = spliter.LoadName(data_root + '/{}-name.csv'.format(type))

        if type == 'test':
            data = DataManager(sub_list=sub_list)
            data.AddOne(Image2D(data_root + '/T2Slice/Test',
                                shape=input_shape))
            data.AddOne(
                Image2D(data_root + '/AdcSlice/Test', shape=input_shape))
            data.AddOne(
                Image2D(data_root + '/DwiSlice/Test', shape=input_shape))
            data.AddOne(
                Image2D(data_root + '/DistanceMap/Test',
                        shape=input_shape,
                        is_roi=True))
            data.AddOne(Feature(data_root + '/FiveClinicalbGS.csv'),
                        is_input=True)
            data.AddOne(Label(data_root + '/label.csv', label_tag='Positive'),
                        is_input=False)
            data_loader = DataLoader(data,
                                     batch_size=batch_size,
                                     shuffle=False)
        else:
            data = DataManager(sub_list=sub_list)
            data.AddOne(Image2D(data_root + '/T2Slice', shape=input_shape))
            data.AddOne(Image2D(data_root + '/AdcSlice', shape=input_shape))
            data.AddOne(Image2D(data_root + '/DwiSlice/', shape=input_shape))
            data.AddOne(
                Image2D(data_root + '/DistanceMap',
                        shape=input_shape,
                        is_roi=True))
            data.AddOne(Feature(data_root + '/FiveClinicalbGS.csv'),
                        is_input=True)
            data.AddOne(Label(data_root + '/label.csv', label_tag='Positive'),
                        is_input=False)
            data_loader = DataLoader(data,
                                     batch_size=batch_size,
                                     shuffle=False)

        cv_folder_list = [
            one
            for one in IterateCase(model_folder, only_folder=True, verbose=0)
        ]
        cv_pred_list, cv_label_list = [], []

        for cv_index, cv_folder in enumerate(cv_folder_list):
            model = ResNeXt(3, 2).to(device)
            if weights_list is None:
                one_fold_weights_list = [
                    one for one in IterateCase(
                        cv_folder, only_folder=False, verbose=0)
                    if one.is_file()
                ]
                one_fold_weights_list = sorted(
                    one_fold_weights_list,
                    key=lambda x: os.path.getctime(str(x)))
                weights_path = one_fold_weights_list[-1]
            else:
                weights_path = weights_list[cv_index]

            print(weights_path.name)
            model.load_state_dict(torch.load(str(weights_path)))

            pred_list, label_list = [], []
            model.eval()
            for inputs, outputs in data_loader:
                inputs = MoveTensorsToDevice(inputs, device)
                outputs = MoveTensorsToDevice(outputs, device)

                preds = model(*inputs)[:, 1]
                pred_list.extend((preds).cpu().data.numpy().squeeze().tolist())
                label_list.extend((
                    outputs).cpu().data.numpy().astype(int).squeeze().tolist())

            cv_pred_list.append(pred_list)
            cv_label_list.append(label_list)

            del model, weights_path

        cv_pred = np.array(cv_pred_list)
        cv_label = np.array(cv_label_list)
        mean_pred = np.mean(cv_pred, axis=0)
        mean_label = np.mean(cv_label, axis=0)
        np.save(
            os.path.join(
                r'/home/zhangyihong/Documents/ProstateECE/Result/NPY/AddClinical/FiveFeatureLFbGS',
                type + 'JSPH_FiveFeature_label.npy'), mean_label)
        np.save(
            os.path.join(
                r'/home/zhangyihong/Documents/ProstateECE/Result/NPY/AddClinical/FiveFeatureLFbGS',
                type + 'JSPH_FiveFeature_pred.npy'), mean_pred)

        fpr, tpr, auc = get_auc(mean_pred, mean_label)
        fpr_list.append(fpr)
        tpr_list.append(tpr)
        auc_list.append(auc)

        print(type)
        bc.Run(mean_pred.tolist(), mean_label.astype(int).tolist())

    draw_roc(fpr_list, tpr_list, auc_list, name_list=['alltrian', 'test'])