Exemplo n.º 1
0
def test():
    print('loading test data......')
    args = DefaultConfig()
    model = generate_model(args)

    for dataset in tqdm.tqdm(
            args.testdataset,
            desc='Total TestSet',
            total=len(args.testdataset),
            position=0,
            bar_format='{desc:<30}{percentage:3.0f}%|{bar:50}{r_bar}'):

        dataset_path = os.path.join(args.data, dataset)
        dataset_test = TestDataset(dataset_path,
                                   scale=(args.crop_height, args.crop_width),
                                   mode='val')
        dataloader_test = DataLoader(dataset_test,
                                     batch_size=1,
                                     shuffle=False,
                                     num_workers=args.num_workers,
                                     pin_memory=True,
                                     drop_last=False)
        save_path = os.path.join(args.data, dataset, "output/")
        # 判断结果
        if not os.path.exists(save_path):
            os.makedirs(save_path)

        model.eval()

        with torch.no_grad():
            for i, (img, gt, name) in tqdm.tqdm(
                    enumerate(dataloader_test),
                    desc=dataset + ' - Test',
                    total=len(dataloader_test),
                    position=1,
                    leave=False,
                    bar_format='{desc:<30}{percentage:3.0f}%|{bar:50}{r_bar}'):
                if torch.cuda.is_available() and args.use_gpu:
                    img = img.cuda()
                    gt = gt.cuda()

                output = model(img)
                out = F.upsample(output,
                                 size=gt.shape[2:],
                                 mode='bilinear',
                                 align_corners=False)
                out = out.data.sigmoid().cpu().numpy().squeeze()
                out = (out - out.min()) / (out.max() - out.min() + 1e-8)
                #save_path = os.path.join(save_path, name)
                path = save_path + "".join(name)
                imageio.imwrite(path, out)
Exemplo n.º 2
0
def main():
    args = DefaultConfig()
    """
    create dataset and dataloader
    """

    dataset_train = Dataset(args.train_data_path, scale=(args.trainsize, args.trainsize),augmentations=args.augmentations)
    dataloader_train = DataLoader(
        dataset_train,
        batch_size=args.batch_size,
        shuffle=True,
        num_workers=args.num_workers,
        pin_memory=True,
        drop_last=True
    )


    # build model
    os.environ['CUDA_VISIBLE_DEVICES'] = args.cuda

    """
    load model
    """

    model_all={'MyNet':MyNet()}

    model=model_all[args.net_work]
    print(args.net_work)
    cudnn.benchmark = True
    # model._initialize_weights()
    if torch.cuda.is_available() and args.use_gpu:
        model = torch.nn.DataParallel(model).cuda()



    """
     optimizer
    """
    if args.optimizer == 'AdamW':
        print("using AdamW")
        optimizer = torch.optim.AdamW(model.parameters(), args.lr,weight_decay=1e-4)
    else:
        print("using SGD")
        optimizer =  torch.optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)

    total = len(dataloader_train)


    train(args, model, optimizer,dataloader_train,total)
Exemplo n.º 3
0
def eval():


    args = DefaultConfig()
    print('#' * 20, 'Start Evaluation', '#' * 20)
    for dataset in tqdm.tqdm(args.testdataset, total=len(args.testdataset), position=0,
                             bar_format='{desc:<30}{percentage:3.0f}%|{bar:50}{r_bar}'):
        pred_path = 'E:\dataset\data\TestDataset/{}/output/'.format(dataset)
        gt_path = 'E:\dataset\data\TestDataset/{}/masks/'.format(dataset)
        preds = os.listdir(pred_path)
        gts = os.listdir(gt_path)
        total_batch =  len(preds)
        # metrics_logger initialization
        metrics = Metrics(['recall', 'specificity', 'precision', 'F1', 'F2',
                           'ACC_overall', 'IoU_poly', 'IoU_bg', 'IoU_mean', 'Dice'])

        for i, sample in tqdm.tqdm(enumerate(zip(preds, gts)), desc=dataset + ' - Evaluation', total=len(preds),
                                   position=1, leave=False, bar_format='{desc:<30}{percentage:3.0f}%|{bar:50}{r_bar}'):
            pred, gt = sample
            assert os.path.splitext(pred)[0] == os.path.splitext(gt)[0]

            pred_mask = np.array(Image.open(os.path.join(pred_path, pred)))
            gt_mask = np.array(Image.open(os.path.join(gt_path, gt)))
            if len(pred_mask.shape) != 2:
                pred_mask = pred_mask[:, :, 0]
            if len(gt_mask.shape) != 2:
                gt_mask = gt_mask[:, :, 0]

            assert pred_mask.shape == gt_mask.shape
            gt_mask = gt_mask.astype(np.float64) / 255
            pred_mask = pred_mask.astype(np.float64) / 255

            gt_mask = torch.from_numpy(gt_mask)
            pred_mask =  torch.from_numpy(pred_mask)
            _recall, _specificity, _precision, _F1, _F2, \
            _ACC_overall, _IoU_poly, _IoU_bg, _IoU_mean, _Dice = evaluate(pred_mask, gt_mask, 0.5)

            metrics.update(recall=_recall, specificity=_specificity, precision=_precision,
                           F1=_F1, F2=_F2, ACC_overall=_ACC_overall, IoU_poly=_IoU_poly,
                           IoU_bg=_IoU_bg, IoU_mean=_IoU_mean, Dice=_Dice
                           )
        metrics_result = metrics.mean(total_batch)
        print("Test Result:")
        print('recall: %.4f, specificity: %.4f, precision: %.4f, F1: %.4f, F2: %.4f, '
              'ACC_overall: %.4f, IoU_poly: %.4f, IoU_bg: %.4f, IoU_mean: %.4f, Dice:%.4f'
              % (metrics_result['recall'], metrics_result['specificity'], metrics_result['precision'],
                 metrics_result['F1'], metrics_result['F2'], metrics_result['ACC_overall'],
                 metrics_result['IoU_poly'], metrics_result['IoU_bg'], metrics_result['IoU_mean'],
                 metrics_result['Dice']))
Exemplo n.º 4
0
from torch.autograd import Variable
import models
from config.config import DefaultConfig
from utils.dataset import TGSSaltDataSet
from utils.utils import *
from utils.metrics import *
from utils.visualize import *
from utils.loss import *
from tqdm import tqdm
import gc
import matplotlib.pyplot as plt
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"


opt = DefaultConfig()
import logging
# 通过下面的方式进行简单配置输出方式与日志级别
logging.basicConfig(filename=os.path.join('./result',opt.logging_file), level=logging.INFO)



def test(bestModel, test_dataloader):
    if opt.use_tta:
        print('use tta...')
        test_df['tta_pred_fold%d' %n_fold] = np.nan
        test_df['tta_pred_fold%d' %n_fold] = test_df['tta_pred_fold%d' %n_fold].astype(object)
        test_unaugment = test_unaugment_flip
    else:
        print('not use tta...')
        test_df['pred_fold%d' %n_fold] = np.nan
import os
import numpy as np
from sklearn.metrics import jaccard_similarity_score
import pandas as pd
from config.config import DefaultConfig
from skimage.transform import resize
from sklearn.model_selection import BaseCrossValidator, train_test_split
from PIL import Image

opt = DefaultConfig()


def GenDataNameDict():
    np.random.seed(opt.seed)
    DataNameDict = {}
    AllTrainNameList = [
        trainname.split('.')[-2]
        for trainname in os.listdir(os.path.join(opt.train_data_images_root))
    ]
    np.random.shuffle(AllTrainNameList)
    AllTestNameList = [
        testname.split('.')[-2]
        for testname in os.listdir(os.path.join(opt.test_data_root))
    ]
    ValNameList = AllTrainNameList[:int(opt.valPercent *
                                        len(AllTrainNameList))]  # 取百分之二十作为验证集
    TrainNameList = AllTrainNameList[int(opt.valPercent *
                                         len(AllTrainNameList)):]
    DataNameDict['train'] = TrainNameList
    DataNameDict['val'] = ValNameList
    DataNameDict['test'] = AllTestNameList
Exemplo n.º 6
0
def charnge():
    args = DefaultConfig()

    save_path = os.path.join(args.data, args.dataset, "output")
    print(save_path)
Exemplo n.º 7
0
    arr = []
    arr2 = []
    for f in os.listdir(destimg):
        arr.append(f)
    for f in os.listdir(destmask):
        if f in arr:
            arr2.append(f)

    for f in arr:
        if f not in arr2:
            print(f)


def test8():
    for f in os.listdir(
            r"E:\dataset\dataset-video\dataset\TestDataset\CVC-ColonDB-300\masks"
    ):
        path = r"E:\dataset\dataset-video\dataset\TestDataset\CVC-ColonDB-300\masks\\" + f
        new_name = path.replace(".png", "-CVC-300.png")
        os.rename(path, new_name)


from torchvision import transforms
from PIL import Image
if __name__ == '__main__':
    from config.config import DefaultConfig
    arg = DefaultConfig()
    while True:
        time.sleep(1)
        print(arg.num_epochs)