Exemple #1
0
def run_validation(data,
                   valid_path,
                   image_size,
                   batch_size,
                   splits,
                   fold_idx,
                   model,
                   exp_name,
                   labels,
                   ttatype=None):
    logdir = 'logs/{}_fold{}/'.format(exp_name, fold_idx)
    valid_data = data.loc[splits['test_idx'][fold_idx], :]
    model.load_state_dict(
        torch.load(os.path.join(logdir,
                                'checkpoints/best.pth'))['model_state_dict'])
    model.eval()
    if ttatype == 'd4':
        model = tta.TTAWrapper(model, tta.d4_image2label)
    elif ttatype == 'fliplr_image2label':
        model = tta.TTAWrapper(model, tta.d4_image2label)
    runner = SupervisedRunner(model=model)
    val_dataset = EyeDataset(dataset_path=valid_path,
                             labels=data.loc[splits['test_idx'][fold_idx],
                                             labels].values,
                             ids=data.loc[splits['test_idx'][fold_idx],
                                          'id'].values,
                             albumentations_tr=aug_val(image_size))
    val_loader = DataLoader(val_dataset,
                            num_workers=8,
                            pin_memory=False,
                            batch_size=batch_size,
                            shuffle=False)
    loaders = collections.OrderedDict()
    loaders["valid"] = val_loader
    #predictions = runner.predict_loader(loaders["valid"], resume=f"{logdir}/checkpoints/best.pth")
    runner.infer(model=model, loaders=loaders, callbacks=[InferCallback()])
    predictions = runner.callbacks[0].predictions['logits']
    probabilities = softmax(torch.from_numpy(predictions), dim=1).numpy()
    for idx in range(probabilities.shape[0]):
        if all(probabilities[idx, :] < 0.5):
            probabilities[idx, 0] = 1.0
    predicted_labels = pd.DataFrame(probabilities, columns=labels)
    predicted_labels['id'] = data.loc[splits['test_idx'][fold_idx],
                                      'id'].values
    predicted_labels.loc[:, 'group'] = predicted_labels.id.apply(
        lambda x: x.split('_')[0])
    valid_data.loc[:, 'group'] = valid_data.id.apply(lambda x: x.split('_')[0])
    valid_data_groupped = valid_data.groupby(['group']).aggregate(
        dict(zip(labels, ['max'] * (len(labels)))))
    predicted_labels_groupped = predicted_labels.groupby(['group']).aggregate(
        dict(zip(labels, ['max'] * (len(labels)))))
    return (valid_data_groupped, predicted_labels_groupped)
def valid_model(_print, cfg, model, valid_loader, valid_criterion, tta=False):

    losses = AverageMeter()
    top1 = AverageMeter()

    if tta:
        model = pytta.TTAWrapper(model, pytta.fliplr_image2label)

    model.eval()
    tbar = tqdm(valid_loader)

    with torch.no_grad():
        for i, (image, target) in enumerate(tbar):
            image = image.cuda()
            target = target.cuda()
            output = model(image)

            loss = valid_criterion(output, target)
            acc = accuracy(output, target)

            losses.update(loss.item() * cfg.OPT.GD_STEPS, image.size(0))
            top1.update(acc[0], image.size(0))

    _print("Valid top1: %.3f, loss: %.3f" % (top1.avg, losses.avg))
    return top1.avg.data.cpu().numpy()[0]
Exemple #3
0
def test_model(_print, cfg, model, test_loader, weight="", tta=False):

    if tta:
        model = pytta.TTAWrapper(model, pytta.fliplr_image2label)
    # print("@@@@@")
    # model = model()
    # print(model)
    # model = nn.DataParallel(model)
    model.load_state_dict(torch.load(weight)["state_dict"])

    model.eval()
    tbar = tqdm(test_loader)
    # if os.path.exists()
    with torch.no_grad():
        for batch in tbar:
            _id, finding, image = batch
            # print("#####",_id)
            # move data to cuda
            image = image.cuda()

            output = model(image)

            output = torch.softmax(output, dim=1)
            output = output.max(axis=1)[1]
            # print("@@@@out shape: ", output.size())
            output = output.type(torch.bool).cpu().numpy()
            for i in range(output.shape[0]):
                np.save(
                    os.path.join(
                        cfg.DIRS.TEST, 'LNDb-{:04d}_finding{}.npy'.format(
                            _id[i].item(), finding[i].item())), output[i])
Exemple #4
0
 def forward(self, x):
     if self.tta: 
         clop_shape = (int(x.shape[-2]*0.8), int(x.shape[-1]*0.8))
         from pytorch_toolbelt.inference import tta
         model = lambda x: self.cls_head(self.encoder(x))
         model = tta.TTAWrapper(model, tta.fivecrop_image2label, crop_size=clop_shape)
         y = model(x)
     else:
         y = self.cls_head(self.encoder(x))
     return y
Exemple #5
0
def valid_model(_print, cfg, model, valid_criterion, valid_loader, tta=False):
    losses = AverageMeter()
    top_iou = AverageMeter()
    top_dice = AverageMeter()
    # top_iou = IoUStorer()
    top_iou = IoUStorer(sigmoid=cfg.METRIC.SIGMOID,
                        thresh=cfg.METRIC.THRESHOLD)
    top_dice = DiceScoreStorer(sigmoid=cfg.METRIC.SIGMOID,
                               thresh=cfg.METRIC.THRESHOLD)

    if tta:
        model = pytta.TTAWrapper(model, pytta.fliplr_image2label)

    model.eval()
    tbar = tqdm(valid_loader)

    with torch.no_grad():
        # for i, (image, target) in enumerate(tbar):
        for i, batch in enumerate(tbar):
            _id, finding, image, target = batch
            #move data to cuda
            image = image.cuda()
            target = target.cuda()
            output = model(image)

            #loss
            loss = valid_criterion(output, target)

            #metric
            # dice_score = DICE(output, target)
            # iou_score = IOU(output, target)

            #record metrics
            top_dice.update(output, target)
            top_iou.update(output, target)

            #record
            losses.update(loss.item(), image.size(0))
            # top_iou.update(iou_score, image.size(0))
            # top_dice.update(dice_score, image.size(0))

    _print("Valid iou: %.3f, dice: %.3f loss: %.3f" %
           (top_iou.avg, top_dice.avg, losses.avg))

    # return top_dice.avg.data.cpu().numpy(), top_iou.avg.data.cpu().numpy()
    return top_dice.avg, top_iou.avg
Exemple #6
0
def valid_model(_print, cfg, model, valid_criterion, valid_loader, tta=False):

    if tta:
        print("#############TTA###############")
        model = pytta.TTAWrapper(model, pytta.fliplr_image2label)

    model.eval()
    tbar = tqdm(valid_loader)
    preds = []
    targets = []

    losses = AverageMeter()
    # AUC = AverageMeter()
    # ACC = AverageMeter()
    with torch.no_grad():
        for i, (image, onehot, target) in enumerate(tbar):
            image = image.cuda()
            target = target.cuda()
            onehot = onehot.cuda()

            output = model(image).view(-1, cfg.TRAIN.NUM_CLASSES)
            loss = valid_criterion(output, target)
            # loss = valid_criterion(output, onehot)
            losses.update(loss.item(), image.size(0))

            #calculate sigmoid/softmax
            # pred = torch.sigmoid(output)
            pred = torch.softmax(output, dim=1)

            #get
            pred_2class = get_cumsum(pred)
            onehot_2class = get_cumsum(onehot)

            preds.append(pred_2class)
            # targets.append(target)
            targets.append(onehot_2class)

    #convert this shit to numpy
    preds, targets = torch.cat(preds, 0), torch.cat(targets, 0)
    preds = preds.cpu().detach().numpy()
    targets = targets.cpu().detach().numpy()

    auc = alaska_weighted_auc(targets[:, 1], preds[:, 1])
    _print("Weighted AUC: %.3f, loss: %.3f" % (auc, losses.avg))

    return auc
def test_model(_print, cfg, model, test_loader, tta=False):

    if tta:
        model = pytta.TTAWrapper(model, pytta.fliplr_image2label)
    model.eval()
    tbar = tqdm(test_loader)
    y_preds = []
    with torch.no_grad():
        for i, image in enumerate(tbar):
            image = image.cuda()
            output = model(image)

            _, top_1 = torch.topk(output, 1)
            y_preds.append(top_1.squeeze(1).cpu().numpy())

    y_preds = np.concatenate(y_preds, 0)
    np.save(os.path.join(cfg.DIRS.OUTPUTS, f"test_{cfg.EXP}.npy"), y_preds)
    return y_preds
Exemple #8
0
def run_inferece(
        config_filename,
        checkpoint_filename,
        output_folder,
        use_tta=False,
        out_shape=(640, 400),
):
    set_global_seed(42)
    output_folder = Path(output_folder)

    config_parser = ConfigParser(
        config_filename, False, **{
            "checkpoint.filename": checkpoint_filename,
            "checkpoint.model": True,
        })
    config = config_parser()
    dataloader = config.dataloaders.test
    device = config.device
    model = config.model
    model.eval()

    model = tta.TTAWrapper(model, tta.fliplr_image2mask) if use_tta else model

    print("Inference stage")
    filenames = []
    with torch.no_grad():
        for imgs, pos in tqdm(dataloader):
            imgs = imgs.to(device)
            outs = model(imgs)
            outs = outs.argmax(1).cpu()

            seqs = pos[0].long().tolist()
            orders = pos[1].long().tolist()

            for out, seq, order in zip(outs, seqs, orders):
                out = out.numpy().astype(np.uint8)
                out = cv2.resize(out, out_shape)
                filename = f"S_{seq}/{order}.npy"
                path = output_folder / filename
                path.parent.mkdir(parents=True, exist_ok=True)
                np.save(path, out)
                filenames.append(filename)
    with open(output_folder / "output.txt", "w") as output_file:
        output_file.writelines("\n".join(filenames))
def main(config_path='/SN7/configs/siamse_2.py',
         test_images='/data/SN7_buildings/test_public/',
         test_predict_result='/wdata/folds_predicts/',
         batch_size=1,
         workers=1,
         gpu='2'):

    with torch.no_grad():

        config = get_config(config_path)
        model_name = config['model_name']
        weights_path = config['load_from']
        device = config['device']
        preprocessing_fn = config['preprocessing_fn']
        valid_augs = config['valid_augs']
        limit_files = config['limit_files']

        os.environ["CUDA_VISIBLE_DEVICES"] = gpu
        if not os.path.exists(test_predict_result):
            os.mkdir(test_predict_result)
        fold_name = weights_path.split('/')[-3]
        folder_to_save = os.path.join(test_predict_result, fold_name)
        if not os.path.exists(folder_to_save):
            # shutil.rmtree(folder_to_save)
            os.mkdir(folder_to_save)

        test_dataset = TestSemSegDataset(images_dir=test_images,
                                         preprocessing=preprocessing_fn,
                                         augmentation=valid_augs,
                                         limit_files=limit_files)

        print('Loading {}'.format(weights_path))
        model = make_model(model_name=model_name).to(device)

        model.load_state_dict(torch.load(weights_path)['model_state_dict'])

        model.eval()
        model = tta.TTAWrapper(model, flip_image2mask)

        file_names = sorted(test_dataset.ids)
        aois = sorted(list(set([el.split('/')[-3] for el in file_names])))

        correct_augs = [albu.PadIfNeeded(1024, 1024, p=1.0)]
        target = {}
        target['image2'] = 'image'
        additional_targets = target

        for aoi in aois[10:15]:
            print('####')
            print(aoi)
            aoi_files = [el for el in file_names if el.split('/')[-3] == aoi]
            for _file in tqdm(aoi_files[:]):
                other_files = sorted((list(set(aoi_files) - {_file})))
                for other_index, other_file in enumerate(other_files):
                    data1 = skimage.io.imread(_file,
                                              plugin='tifffile')[..., :3]
                    data2 = skimage.io.imread(other_file,
                                              plugin='tifffile')[..., :3]
                    sample = albu.Compose(correct_augs,
                                          p=1,
                                          additional_targets=target)(
                                              image=data1, image2=data2)
                    data1, data2 = sample['image'], sample['image2']
                    data1 = preprocessing_fn(data1)
                    data2 = preprocessing_fn(data2)
                    image = np.concatenate((data1, data2), axis=-1)
                    image = np.moveaxis(image, -1, 0)
                    image = np.expand_dims(image, 0)
                    image = torch.as_tensor(image, dtype=torch.float)
                    if other_index == 0:
                        runner_out = model(image.cuda())
                    else:
                        runner_out += model(image.cuda())
                runner_out = runner_out / len(other_files)
                aoi_path = os.path.join(folder_to_save, aoi)
                if not os.path.exists(aoi_path):
                    os.mkdir(aoi_path)
                res_name = _file.split('/')[-1].split('.')[0] + '.png'
                file_name = os.path.join(aoi_path, res_name)
                image_pred = runner_out.cpu().detach().numpy()
                data = image_pred[0, :3, ...]
                data = np.moveaxis(data, 0, -1)
                data = (data * 255).astype(np.uint8)
                data[:, :, 1] = 0
                cv2.imwrite(file_name, data)
Exemple #10
0
                      pin_memory=False,
                      batch_size=batch_size,
                      shuffle=False)  
 loaders = collections.OrderedDict()
 loaders["valid"] = test_loader    
 probabilities_list = []
 ttatype='d4'
 for fold_idx in range(len(splits['test_idx'])):
     print('Getting predictions from fold {}'.format(fold_idx))
     logdir = 'logs/{}_fold{}/'.format(exp_name, fold_idx)    
     model = prepare_model(model_name, n_classes)
     model.cuda()
     model.load_state_dict(torch.load(os.path.join(logdir,'checkpoints/best.pth'))['model_state_dict'])
     model.eval()   
     if ttatype=='d4':
         model = tta.TTAWrapper(model, tta.d4_image2label)
     elif ttatype=='fliplr_image2label':
         model = tta.TTAWrapper(model, tta.d4_image2label)
     runner = SupervisedRunner(model=model)   
     #predictions = runner.predict_loader(loaders["valid"], resume=f"{logdir}/checkpoints/best.pth")
     runner.infer(model=model,loaders=loaders,callbacks=[InferCallback()])
     predictions = runner.callbacks[0].predictions['logits']
     probabilities = softmax(torch.from_numpy(predictions),dim=1).numpy()    
     for idx in range(probabilities.shape[0]):
         if all(probabilities[idx,:]<0.5):
             probabilities[idx,0] = 1.0
     probabilities_list.append(probabilities)
 probabilities_combined = np.stack(probabilities_list,axis=0).mean(axis=0) 
 predicted_labels = pd.DataFrame(probabilities_combined, columns=labels)
 predicted_labels['id'] = test_data.loc[:,'id'].values
 predicted_labels.loc[:,'ID'] = predicted_labels.id.apply(lambda x: x.split('_')[0])
Exemple #11
0
def main(config_path='/project/configs/senet154_gcc_fold1.py',
         test_images='/data/SN6_buildings/test_public/AOI_11_Rotterdam/',
         test_predict_result='/wdata/folds_predicts/',
         batch_size=1,
         workers=1,
         gpu='1'):

    with torch.no_grad():

        config = get_config(config_path)
        model_name = config['model_name']
        weights_path = config['load_from']
        device = config['device']
        val_batch_size = batch_size
        input_channels = config['input_channels']

        original_size = config['original_size']
        cropper = albu.Compose(
            [albu.CenterCrop(original_size[0], original_size[1], p=1.0)])
        n_classes = config['n_classes']
        preprocessing_fn = config['preprocessing_fn']
        valid_augs = config['valid_augs']
        limit_files = config['limit_files']
        num_workers = workers
        os.environ["CUDA_VISIBLE_DEVICES"] = gpu
        if not os.path.exists(test_predict_result):
            os.mkdir(test_predict_result)
        fold_name = weights_path.split('/')[-3]
        folder_to_save = os.path.join(test_predict_result, fold_name)
        if os.path.exists(folder_to_save):
            shutil.rmtree(folder_to_save)

        os.mkdir(folder_to_save)

        test_dataset = TestSemSegDataset(images_dir=os.path.join(
            test_images, 'SAR-Intensity'),
                                         preprocessing=preprocessing_fn,
                                         augmentation=valid_augs,
                                         limit_files=limit_files)

        test_loader = DataLoader(dataset=test_dataset,
                                 batch_size=val_batch_size,
                                 shuffle=False,
                                 num_workers=num_workers)
        print('Loading {}'.format(weights_path))
        model = make_model(model_name=model_name,
                           weights=None,
                           n_classes=n_classes,
                           input_channels=input_channels).to(device)

        model.load_state_dict(torch.load(weights_path)['model_state_dict'])

        model.eval()
        model = tta.TTAWrapper(model, flip_image2mask)
        model = torch.nn.DataParallel(model).cuda()

        file_names = sorted(test_dataset.ids)

        for batch_i, test_batch in enumerate(tqdm(test_loader)):
            runner_out = model(test_batch.cuda())
            image_pred = runner_out

            image_pred = image_pred.cpu().detach().numpy()
            names = file_names[batch_i * val_batch_size:(batch_i + 1) *
                               val_batch_size]
            for i in range(len(names)):
                file_name = os.path.join(folder_to_save,
                                         names[i].split('.')[0] + '.png')

                data = image_pred[i, ...]
                data = np.moveaxis(data, 0, -1)
                sample = cropper(image=data)
                data = sample['image']
                data = (data * 255).astype(np.uint8)
                cv2.imwrite(file_name, data)
 def on_stage_start(self, state: "State"):
     if self.tta is not None:
         # state.model = tta.SegmentationTTAWrapper(state.model, tta.aliases.d4_transform())
         state.model = tta.TTAWrapper(state.model, tta.d4_image2mask)
         print(f"tta model created! type={type(state.model)}")