Пример #1
0
def eval_net(net, loader, device, n_classes):
    """Evaluation without the densecrf with the dice coefficient"""
    net.eval()
    mask_type = torch.float32 if n_classes == 1 else torch.long
    n_val = len(loader)  # the number of batch
    tot = 0

    with tqdm(total=n_val, desc='Validation round', unit='batch',
              leave=False) as pbar:
        for batch in loader:
            imgs, true_masks = batch['image'], batch['mask']
            true_biMasks = (true_masks > 0).int()
            imgs = imgs.to(device=device, dtype=torch.float32)
            true_biMasks = true_biMasks.to(device=device, dtype=mask_type)

            with torch.no_grad():
                #mask_pred = net(imgs)
                mask_pred = crop_tile(net, imgs)

            if n_classes > 1:
                tot += F.cross_entropy(mask_pred, true_biMasks).item()
            else:
                # pred = torch.sigmoid(mask_pred)
                # pred = (pred > 0.5).float()
                # tot += dice_coeff(pred, true_biMasks).item()
                tot += nn.BCEWithLogitsLoss(reduction='mean')(
                    mask_pred, true_biMasks).item()
            pbar.update()

    net.train()
    return tot / n_val
Пример #2
0
def predict_img(net, full_img, gt, device, out_threshold=0.5):
    net.eval()

    img, gt, _ = preprocess_img_gt(full_img, gt, np.zeros(gt.shape), False)
    gt = gt.squeeze(0)

    img = img.unsqueeze(0)
    img = img.to(device=device, dtype=torch.float32)

    with torch.no_grad():
        if CROP_FLAG:
            output = crop_tile(net, img)
        else:
            output = net(img)
        probs = torch.sigmoid(output)

        full_mask = probs.squeeze(0)
        bi_full_mask = (full_mask > out_threshold).int()
        bi_mask = bi_full_mask[0]
        bi_contour = bi_full_mask[1]

        pred_mask = instancing(bi_mask.cpu().numpy(),
                               bi_contour.cpu().numpy(), img[0,
                                                             0].cpu().numpy())
        result = jaccard(pred_mask, gt.numpy())

    return result, pred_mask, bi_mask.cpu().numpy(), bi_contour.cpu().numpy()
Пример #3
0
def segmentor(models, img, device, crop_flag, vis_flag=False):

    img[img > 255] = 255
    # img = np.array(img, dtype=np.float)
    img = np.array(img, dtype=np.uint8)

    img = np.repeat(img[:, :, None], 3, axis=2)
    img = all_transform(img)
    img = img.unsqueeze(0)
    img = img.to(device=device, dtype=torch.float32)
    # img=img.repeat([3,1,1])

    with torch.no_grad():
        # output = net(img)
        full_mask = torch.zeros([img.shape[2], img.shape[3]]).cuda()
        for i, net in enumerate(models):
            net.eval()
            if crop_flag[i]:
                output = crop_tile(net, img)
            else:
                output = net(img)

            probs = torch.sigmoid(output)

            full_mask += probs.squeeze(0).squeeze(0)
        full_mask /= len(models)
        bi_mask = (full_mask > 0.5).int()

        pred_mask = instancing(bi_mask.cpu().numpy(), vis_flag=vis_flag)
    return pred_mask, img, bi_mask.cpu().numpy()
Пример #4
0
def segmentor(net, img, device):
    img, _, _ = preprocess_img_gt(img, np.zeros(img.shape),
                                  np.zeros(img.shape), False)
    # img[img>255]=255
    # img = np.array(img, dtype=np.float)
    # img = np.array(img, dtype=np.uint8)

    # img = np.repeat(img[:,:,None],3,axis=2)
    # img = all_transform(img)

    img = img.unsqueeze(0)
    img = img.to(device=device, dtype=torch.float32)
    # img=img.repeat([3,1,1])

    net.eval()
    with torch.no_grad():
        if CROP_FLAG:
            output = crop_tile(net, img)
        else:
            output = net(img)
        probs = torch.sigmoid(output)

        full_mask = probs.squeeze(0)
        bi_full_mask = (full_mask > 0.5).int()
        bi_mask = bi_full_mask[0]
        bi_contour = bi_full_mask[1]

        pred_mask = instancing(bi_mask.cpu().numpy(),
                               bi_contour.cpu().numpy(), img[0,
                                                             0].cpu().numpy())
    return pred_mask, bi_mask.cpu().numpy(), bi_contour.cpu().numpy()
Пример #5
0
def eval_net(net, loader, device, n_classes):
    """Evaluation without the densecrf with the dice coefficient"""
    net.eval()
    mask_type = torch.float32 if n_classes == 1 else torch.long
    n_val = len(loader)  # the number of batch
    tot = 0
    tot_contour = 0

    score_num = 0
    with tqdm(total=n_val, desc='Validation round', unit='batch',
              leave=False) as pbar:
        for batch in loader:
            imgs, true_masks = batch['image'], batch['mask']
            true_biMasks = (true_masks > 0).int()
            contour_masks = batch['mask_contour']
            imgs = imgs.to(device=device, dtype=torch.float32)
            true_biMasks = true_biMasks.to(device=device, dtype=mask_type)
            contour_masks = contour_masks.to(device=device, dtype=mask_type)

            with torch.no_grad():
                if CROP_FLAG:
                    mask_pred = crop_tile(net, imgs)
                else:
                    mask_pred = net(imgs)

            if n_classes > 1:
                tot += F.cross_entropy(mask_pred, true_biMasks).item()
            else:
                pred = torch.sigmoid(mask_pred)
                pred = (pred > 0.5).float()
                pred_mask = pred[:, 0]
                pred_contour = pred[:, 1]
                tot += dice_coeff(pred_mask,
                                  true_biMasks).item() * pred_mask.shape[0]
                tot_contour += dice_coeff(
                    pred_contour, true_biMasks).item() * pred_mask.shape[0]
                score_num += pred_mask.shape[0]
                # tot += nn.BCEWithLogitsLoss(reduction='mean')(mask_pred, true_biMasks).item()
            pbar.update()

    net.train()
    return tot / score_num, tot_contour / score_num
Пример #6
0
def predict_img(net, full_img, gt, device, out_threshold=0.5):
    net.eval()

    img, gt = preprocess_img_gt(full_img, gt, False)
    gt = gt.squeeze(0)

    img = img.unsqueeze(0)
    img = img.to(device=device, dtype=torch.float32)

    with torch.no_grad():
        # output = net(img)
        output = crop_tile(net, img)
        probs = torch.sigmoid(output)

        full_mask = probs.squeeze(0).squeeze(0)
        bi_mask = (full_mask > out_threshold).int()

        pred_mask = instancing(bi_mask.cpu().numpy())
        result = jaccard(pred_mask, gt.numpy())

    return result, pred_mask, bi_mask.cpu().numpy()
Пример #7
0
def segmentor(net, img, device):
    img[img > 255] = 255
    # img = np.array(img, dtype=np.float)
    img = np.array(img, dtype=np.uint8)

    img = np.repeat(img[:, :, None], 3, axis=2)
    img = all_transform(img)
    img = img.unsqueeze(0)
    img = img.to(device=device, dtype=torch.float32)
    # img=img.repeat([3,1,1])

    with torch.no_grad():
        if CROP_FLAG:
            output = crop_tile(net, img)
        else:
            output = net(img)
        probs = torch.sigmoid(output)

        full_mask = probs.squeeze(0).squeeze(0)
        bi_mask = (full_mask > 0.5).int()

        pred_mask = instancing(bi_mask.cpu().numpy())
    return pred_mask, img, bi_mask.cpu().numpy()