<jupyter_start><jupyter_text>Predictions and labels on train<jupyter_code>labels_train = joblib.load(os.path.join(DATA_DIR,'ground_truth','train','labels.pkl'))
masks_train = joblib.load(os.path.join(DATA_DIR,PREDICTION_TRAIN,'unet_multitask'))['mask_prediction']
contours_train = joblib.load(os.path.join(DATA_DIR,PREDICTION_TRAIN,'unet_multitask'))['contour_prediction']
postprocessed_train = joblib.load(os.path.join(DATA_DIR,PREDICTION_TRAIN,'binary_fill'))['filled_images']
ipy.interact(plot_with_labels, mask = ipy.fixed(masks_train),
              contour = ipy.fixed(contours_train),
              postprocessed = ipy.fixed(postprocessed_train),
              label = ipy.fixed(labels_train),
              idx = ipy.IntSlider(min=0, max=50, value=0, step=1))<jupyter_output><empty_output><jupyter_text>Predictions and labels on valid<jupyter_code>labels_valid = joblib.load(os.path.join(DATA_DIR,'ground_truth','valid','labels.pkl'))
masks_valid = joblib.load(os.path.join(DATA_DIR,PREDICTION_VALID,'unet_multitask'))['mask_prediction']
contours_valid = joblib.load(os.path.join(DATA_DIR,PREDICTION_VALID,'unet_multitask'))['contour_prediction']
postprocessed_valid = joblib.load(os.path.join(DATA_DIR,PREDICTION_VALID,'binary_fill'))['filled_images']
ipy.interact(plot_with_labels, mask = ipy.fixed(masks_valid),
              contour = ipy.fixed(contours_valid),
              postprocessed = ipy.fixed(postprocessed_valid),
              label = ipy.fixed(labels_valid),
              idx = ipy.IntSlider(min=0, max=50, value=0, step=1))<jupyter_output><empty_output><jupyter_text>Predictions and submission on test<jupyter_code>masks_test = joblib.load(os.path.join(DATA_DIR,PREDICTION_TEST,'unet_multitask'))['mask_prediction']
contours_test = joblib.load(os.path.join(DATA_DIR,PREDICTION_TEST,'unet_multitask'))['contour_prediction']
postprocessed_test = joblib.load(os.path.join(DATA_DIR,PREDICTION_TEST,'binary_fill'))['filled_images']
ipy.interact(plot_predictions, mask = ipy.fixed(masks_test),
              contour = ipy.fixed(contours_test),
              postprocessed = ipy.fixed(postprocessed_test),
              idx = ipy.IntSlider(min=0, max=50, value=0, step=1))
def decompose(labeled):

    nr_true = labeled.max()
    masks = []
    for i in range(1, nr_true + 1):
        msk = labeled.copy()
        msk[msk != i] = 0.
        msk[msk == i] = 255.
        masks.append(msk)

    if not masks:
        return [labeled]
    else:
        return masks

def run_length_encoding(x):
    dots = np.where(x.T.flatten() == 1)[0] 
    run_lengths = []
    prev = -2
    for b in dots:
        if (b > prev + 1): run_lengths.extend((b + 1, 0))
        run_lengths[-1] += 1
        prev = b
    return run_lengths
    
def generate_submission(predictions, meta):
    image_ids, encodings = [], []
    for image_id, prediction in zip(meta['ImageId'].values, predictions):
        for mask in decompose(prediction):
            image_ids.append(image_id)
            encodings.append(' '.join(str(rle) for rle in run_length_encoding(mask > 128.)))

    submission = pd.DataFrame({'ImageId': image_ids, 'EncodedPixels': encodings})
    return submission
submission = generate_submission(postprocessed_test, meta_test)
submission.head()
submission.to_csv(SUBMISSION_PATH, index=None)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13479435604923445, 'fraction_numerical': 0.009606724707295107, 'mean_word_length': 4.4983498349834985, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '501543', 'license': 'MIT', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1175, 'n_words': 161, 'path': 'devbook.ipynb', 'repo_name': 'dineshsonachalam/data-science-bowl-2018', 'size': '3331'}