Пример #1
0
def convert_mask(mask):
    y_bin = binarize_sample((mask, mask_thresh, min_size_thresh, dilation))
    if np.sum(y_bin) > 0:
        rle = mask_to_rle(y_bin.T, 1024, 1024)
    else:
        rle = " -1"
    return rle
Пример #2
0
def convert_one(sample_id):
    if "+" in model_name:
        model_lst = model_name.split("+")
    else:
        model_lst = [model_name]

    y_pred = []
    for model in model_lst:
        for fold in range(n_fold):
            filename = osp.join(dumps_dir, f"{fold}_{model}_test",
                                f"{sample_id}.png")
            y_pred.append(cv2.imread(filename, 0) / 255.0)

    y_pred = np.mean(np.array(y_pred), axis=0)

    cv2.imwrite(
        f"/mnt/ssd2/dataset/pneumo/predictions/uint8/mean/{sample_id}.png",
        np.uint8(255 * y_pred))

    y_bin = binarize_sample((y_pred, mask_thresh, min_size_thresh, dilation))
    if np.sum(y_bin) > 0:
        rle = mask_to_rle(y_bin.T, 1024, 1024)
    else:
        rle = " -1"
    return rle
def convert_one(sample_id):
    path_pattern = osp.join(predict_dir, "tmp", f"{model_name}*{sample_id}*")
    fns = glob(path_pattern)
    assert len(fns) == 1, path_pattern

    y_pred = np.load(fns[0])
    agreement = float(osp.basename(fns[0]).split("|")[1])
    if agreement < 0.65 and correction:
        y_bin = binarize_sample((y_pred, mask_thresh, 1500, 2))
    else:
        y_bin = binarize_sample(
            (y_pred, mask_thresh, min_size_thresh, dilation))

    if np.sum(y_bin) > 0:
        rle = mask_to_rle(y_bin.T, 1024, 1024)
    else:
        rle = " -1"
    return rle
def main():
    os.makedirs("outs/", exist_ok=True)
    model_name = "sx50_sx101_se154"

    sample_df = pd.read_csv("tables/stage_2_sample_submission.csv")
    sample_df = sample_df.drop_duplicates("ImageId",
                                          keep="last").reset_index(drop=True)

    pred_dict = {}

    for subm_path in sorted(glob("subm/st2*csv")):
        print(subm_path)
        df = pd.read_csv(subm_path)

        for index, row in tqdm.tqdm(df.iterrows(), total=len(df)):
            image_id = row["ImageId"]
            annot = row["EncodedPixels"].strip()

            if annot != "-1":
                mask = rle2mask(annot, 1024, 1024)
            else:
                mask = np.zeros((1024, 1024))

            if image_id in pred_dict:
                pred_dict[image_id] += mask
            else:
                pred_dict[image_id] = mask
        del df

    print(len(pred_dict))

    #################################################################

    threshold_list = [
        0
    ]  # 0 - if union, 1 - if with certainty of 2 for many models

    for threshold in threshold_list:
        sublist = []
        for index, row in tqdm.tqdm(sample_df.iterrows(),
                                    total=len(sample_df)):
            image_id = row["ImageId"]
            pred = pred_dict[image_id]

            if pred.sum() > 0:

                out_cut = np.copy(pred)
                out_cut[np.nonzero(out_cut <= threshold)] = 0.0
                out_cut[np.nonzero(out_cut > threshold)] = 1.0

                out_cut = ndimage.binary_fill_holes(out_cut).astype(
                    out_cut.dtype)

                # imsave(model_name+'/'+image_id + '.png', out_cut)

                rle = mask_to_rle(out_cut, 1024, 1024)
                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(
                f"subm/st2_union_submission_corr1_{model_name}_{threshold}.csv",
                index=False)