Esempio n. 1
0
def app(test_img_path, submission_path, device):
    """Wraps inference code for Efficiency scoring.

        Parameters
        ----------
        test_img_path : str
            Path to test images.
        submission_path : str
            Path to save `submission.csv`.
        device : str
            Device to run model on.
    """
    ########################################
    #         place your code here         #
    ########################################
    model = DummyModel().to(device)
    weights = torch.load('model_weights/dummy_weights.pkl',
                         map_location=device)
    model.load_state_dict(weights)

    img_paths = glob(os.path.join(test_img_path, '*'))

    submission = ['ImageId_ClassId,EncodedPixels\n']
    model.eval()
    with torch.no_grad():
        for path in tqdm(img_paths):
            name = path.split('/')[-1]
            image = normalize(cv2.imread(path, cv2.IMREAD_COLOR))
            image = torch.from_numpy(image.transpose(2, 0, 1))
            score = model(image.unsqueeze(0).to(device))
            mask = (score.cpu().numpy()[0] > 0.5).astype(np.uint8)
            rle = mask_to_rle(mask, name)
            submission.append(rle)
    with open(os.path.join(submission_path, 'dummy_submission.csv'), 'w') as f:
        f.write(''.join(submission))
    def predict_on_test(self):
        sample_df = pd.read_csv(
            os.path.join(self.DATA_PATH_BASE, "sample_submission.csv"))

        # this part was taken from @raddar's kernel: https://www.kaggle.com/raddar/better-sample-submission
        masks_ = sample_df.groupby("ImageId")["ImageId"].count().reset_index(
            name="N")
        masks_ = masks_.loc[masks_.N > 1].ImageId.values
        ###
        sample_df = sample_df.drop_duplicates(
            "ImageId", keep="last").reset_index(drop=True)

        tt = transforms.ToTensor()
        sublist = []
        counter = 0
        # changed from 0.25 to 0.5... need to check the data and analyze...(test on dev set)
        threshold = 0.55
        for index, row in tqdm(sample_df.iterrows(), total=len(sample_df)):
            image_id = row["ImageId"]
            if image_id in masks_:
                img_path = os.path.join(self.DATA_PATH_BASE + "/test/" +
                                        image_id + ".png")

                img = Image.open(img_path).convert("RGB")
                width, height = img.size
                img = img.resize((1024, 1024), resample=Image.BILINEAR)
                img = tt(img)
                result = self.model_ft([img.to(self.device)])[0]
                if len(result["masks"]) > 0:
                    counter += 1
                    mask_added = 0
                    for ppx in range(len(result["masks"])):
                        if result["scores"][ppx] >= threshold:
                            mask_added += 1
                            res = transforms.ToPILImage()(
                                result["masks"][ppx].permute(1, 2,
                                                             0).cpu().numpy())
                            res = np.asarray(
                                res.resize((width, height),
                                           resample=Image.BILINEAR))
                            res = (res[:, :] * 255.0 > 127).astype(np.uint8).T
                            rle = utils.mask_to_rle(res, width, height)
                            sublist.append([image_id, rle])
                    if mask_added == 0:
                        rle = " -1"
                        sublist.append([image_id, rle])
                else:
                    rle = " -1"
                    sublist.append([image_id, rle])
            else:
                rle = " -1"
                sublist.append([image_id, rle])

        submission_df = pd.DataFrame(sublist, columns=sample_df.columns.values)
        submission_df.to_csv("submission.csv", index=False)
        print(counter)
    def predict_rle(model, device, data_loader, metric_logger, print_freq):
        header = "DEV prediction test"

        sublist = []
        counter = 0
        # changed from 0.25 to 0.5... need to check the data and analyze...(test on dev set)
        threshold = 0.55
        assert data_loader.batch_size == 1

        for images, targets in metric_logger.log_every(data_loader, print_freq,
                                                       header):
            width, height = images.size
            image_id = targets["image_id"]

            images = list(image.to(device) for image in images)
            targets = [{k: v.to(device)
                        for k, v in t.items()} for t in targets]

            result = model(images, targets)[0]

            if len(result["masks"]) > 0:
                counter += 1
                mask_added = 0
                for ppx in range(len(result["masks"])):
                    if result["scores"][ppx] >= threshold:
                        mask_added += 1
                        res = transforms.ToPILImage()(
                            result["masks"][ppx].permute(1, 2,
                                                         0).cpu().numpy())
                        res = np.asarray(
                            res.resize((width, height),
                                       resample=Image.BILINEAR))
                        res = (res[:, :] * 255.0 > 127).astype(np.uint8).T
                        rle = utils.mask_to_rle(res, width, height)
                        sublist.append([image_id, rle])
                if mask_added == 0:
                    rle = " -1"
                    sublist.append([image_id, rle])
            else:
                rle = " -1"
                sublist.append([image_id, rle])

        return sublist
Esempio n. 4
0
mask = pd.read_csv(PATH)

k = mask.groupby('ImageId').count()[" EncodedPixels"]
list_ImageId = k.index[k > 1]

for i in tqdm(range(len(list_ImageId))):
    tmp = mask.loc[mask['ImageId'] == list_ImageId[i]]
    final_mask = np.zeros((1024, 1024))
    for index, row in tmp.iterrows():
        RLE_mask = row[" EncodedPixels"]
        if RLE_mask.strip() != str(-1):
            rle_mask = rle2mask(RLE_mask[1:], 1024, 1024).T
        else:
            rle_mask = np.zeros((1024, 1024))
        final_mask[np.where(rle_mask)] = 1
    final_rle = mask_to_rle(final_mask.T, 1024, 1024)

    new_csv['ImageId'].append(list_ImageId[i])
    new_csv[' EncodedPixels'].append(final_rle)

k = mask.groupby('ImageId').count()[" EncodedPixels"]
list_ImageId = k.index[k == 1]

for i in tqdm(range(len(list_ImageId))):
    new_csv['ImageId'].append(list_ImageId[i])
    new_csv[' EncodedPixels'].append(mask.loc[
        mask['ImageId'] == list_ImageId[i]][" EncodedPixels"].values[0])

new_df = pd.DataFrame(data=new_csv)
df = new_df
df = df.sample(frac=1).reset_index(drop=True)
Esempio n. 5
0
def inference(model_ft, device):
    model_ft.eval()
    for param in model_ft.parameters():
        param.requires_grad = False
    model_ft.to(device)
    assert model_ft.training == False
    test_start = time.time()
    sample_df = pd.read_csv(config.sub_sample_path)
    leak_df = pd.read_csv(config.leak_path)
    leak_id = []
    for id, anno in zip(leak_df['ImageId'], leak_df['EncodedPixels']):
        if anno == '-1':
            leak_id.append(id)
    print(f'leak_id: {len(leak_id)}')
    print(f'leak id: {leak_id[:5]}')
    tt = transforms.ToTensor()
    sublist = []
    counter = 0
    best_thr = 0.5
    Noise_th = 75.0 * (1024 / 128.0)**2
    for index, row in tqdm(sample_df.iterrows(),
                           total=len(sample_df),
                           desc='test'):
        image_id = row['ImageId']
        if image_id not in leak_id:
            img_path = os.path.join(config.test_png_path, image_id + '.png')

            img = Image.open(img_path).convert("RGB")
            width, height = img.size
            img_row = img.resize((1024, 1024), resample=Image.BILINEAR)
            img = tt(img_row)
            img = img.reshape((1, *img.numpy().shape))
            logits = model_ft(img.to(device))[:, 0, :, :]
            preds = torch.sigmoid(logits)[0].cpu().numpy()
            #         mask = (preds > best_thr).astype(np.uint8) # if no_tta

            # TTA HFlip
            hflip_img = img.flip(-1)
            hflip_logits = model_ft(hflip_img.to(device))[:, 0, :, :]
            hflip_logits = hflip_logits.flip(-1)
            hflip_preds = torch.sigmoid(hflip_logits)[0].cpu().numpy()

            # CLAHE TTA
            clahe = alb.CLAHE(p=1.0)
            clahe_im = clahe(image=np.asarray(img_row))['image']
            clahe_im = tt(clahe_im)
            clahe_im = clahe_im.reshape((1, *clahe_im.numpy().shape))
            clahe_logits = model_ft(clahe_im.to(device))[:, 0, :, :]
            clahe_preds = torch.sigmoid(clahe_logits)[0].cpu().numpy()

            preds = (preds + hflip_preds + clahe_preds) / 3
            mask = (preds > best_thr).astype(np.uint8)

            mask = Image.fromarray(np.uint8(mask))
            mask = mask.resize((1024, 1024), resample=Image.BILINEAR)
            mask = np.asarray(mask)
            mask = morphology.remove_small_objects(mask,
                                                   min_size=Noise_th,
                                                   connectivity=2)

            mask = mask.T
            if np.count_nonzero(mask) == 0:
                rle = " -1"
            else:
                rle = mask_to_rle(mask, width, height)
        else:
            rle = '-1'
        if image_id == '1.2.276.0.7230010.3.1.4.8323329.6830.1517875201.424165':
            rle = '-1'
        sublist.append([image_id, rle])

    if not os.path.exists('results'):
        os.makedirs('results')
    if not os.path.exists('checkpoints'):
        os.makedirs('checkpoints')
    submission_df = pd.DataFrame(sublist, columns=sample_df.columns.values)
    submission_df.to_csv("results/sub_ony_mask.csv", index=False)
    print('test time: {:.3f}min'.format((time.time() - test_start) / 60))
    torch.save({
        'model': model_ft,
        'leaks': leak_id
    }, 'checkpoints/deeplabv3_leak.pth')
    def predict_rle_from_acts(acts,
                              data_loader,
                              metric_logger,
                              print_freq,
                              threshold=0.5):
        header = "DEV prediction test"

        sublist = []
        counter = 0

        data_loader_b1 = torch.utils.data.DataLoader(
            data_loader.dataset,
            batch_size=1,
            shuffle=False,
            num_workers=0,  # 4: 08:19, 8: 08:40
            collate_fn=PS_torch._collate_fn_for_data_loader,
        )

        idx = 0
        mask_add_cnt = []
        for image, target in metric_logger.log_every(data_loader_b1,
                                                     print_freq, header):
            width, height = 1024, 1024

            result = acts[idx]
            image_id = target[0]["image_id"].cpu().numpy()
            idx += 1
            if len(result["masks"]) > 0:
                counter += 1
                mask_added = 0
                for ppx in range(len(result["masks"])):
                    if result["scores"][ppx] >= threshold:
                        mask_added += 1
                        # res = transforms.ToPILImage()(result["masks"][ppx].permute(1, 2, 0).cpu().numpy())
                        res = transforms.ToPILImage()(
                            result["masks"][ppx].transpose((1, 2, 0)))
                        res = np.asarray(
                            res.resize((width, height),
                                       resample=Image.BILINEAR))
                        res = (res[:, :] * 255.0 > 127).astype(np.uint8).T
                        rle = utils.mask_to_rle(res, width, height)
                        sublist.append([image_id, rle])
                if mask_added == 0:
                    rle = " -1"
                    sublist.append([image_id, rle])
                mask_add_cnt.append(mask_added)
            else:
                rle = " -1"
                sublist.append([image_id, rle])
                mask_add_cnt.append(0)  # no mask
            if idx % 100 == 0:
                mask_stat = np.array(mask_add_cnt)
                metric_logger.update(
                    **{
                        "mask0": ((mask_stat == 0).sum() / idx),
                        "mask1": ((mask_stat == 1).sum() / idx),
                        "mask2": ((mask_stat == 2).sum() / idx),
                        "mask>2": ((mask_stat > 2).sum() / idx),
                    })

        mask_stat = np.array(mask_add_cnt)
        assert len(mask_stat) == idx

        metric_logger.print_and_log_to_file(
            f"image cnt: {idx}, image predicted mask cnt: {counter}, "
            f"mask 0,1,2,3+ {(mask_stat==0).sum()/idx}, "
            f"{(mask_stat==1).sum()/idx}, {(mask_stat==2).sum()/idx}, {(mask_stat>2).sum()/idx}"
        )

        return sublist, mask_stat
print('test_pred_ids.shape = {}'.format(np.array(test_pred_ids).shape))
print('test_pred_rle.shape = {}'.format(np.array(test_pred_rle).shape))

# Inspect a test prediction and check run length encoding.
# n = np.random.randint(len(x_test))
n = 171
pred = u_net.get_prediction_from_path(
    sess, [x_test[n]],
    method=METHOD,
    tgt_size=(IMG_HEIGHT, IMG_WIDTH),
    compatibility_multiplier=32,
    full_prediction=True)[0]
y_test_pred = utils.trsf_proba_to_binary(pred)
y_test_pred_original_size = utils.resize_as_original(
    [y_test_pred], [test_sizes[n]])[:, :, 0]
rle = list(utils.mask_to_rle(y_test_pred_original_size))
mask_rec = utils.rle_to_mask(rle, test_sizes[n])
print('Run length encoding: {} matches, {} misses'.format(
    np.sum((mask_rec == y_test_pred_original_size)),
    np.sum((mask_rec != y_test_pred_original_size))))

fig, axs = plt.subplots(2, 3, figsize=(20, 13))
axs[0, 0].imshow(utils.read_image(test_df['image_path'].loc[n]))
axs[0, 0].set_title('{}.) original test image'.format(n))
axs[0, 1].imshow(np.squeeze((utils.read_image(x_test[n]))))
axs[0, 1].set_title('{}.) transformed test image'.format(n))
axs[0, 2].imshow(np.squeeze(pred[:, :, 0]), cm.gray)
axs[0, 2].set_title('{}.) predicted test mask probabilities'.format(n))
axs[1, 0].imshow(np.squeeze(y_test_pred[:, :, 0]), cm.gray)
axs[1, 0].set_title('{}.) predicted test mask'.format(n))
axs[1, 1].imshow(np.squeeze(y_test_pred_original_size[:, :, 0]), cm.gray)