<jupyter_start><jupyter_text>Test run-length encoding and decoding functions<jupyter_code>import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

# System paths
root_dir = "/media/davidtvs/Storage/Datasets/airbus-ship-detection"
trainset_dir = os.path.join(root_dir, "train_v2")
testset_dir = os.path.join(root_dir, "test_v2")
train_rle = os.path.join(root_dir, "train_ship_segmentations_v2.csv")<jupyter_output><empty_output><jupyter_text>Run-length encode and decode functions<jupyter_code>def rle_encode(mask):
    """Encodes a mask in run-length format.

    Source: https://www.kaggle.com/paulorzp/run-length-encode-and-decode

    Arguments:
        mask (numpy.ndarray): the mask to encode

    Returns:
        str: The run-length encoded pixels formatted as
            (start length) string

    """
    # Transpose is needed because RLE is numbered from top to bottom, then left to right
    pixels = mask.T.flatten()
    pixels = np.concatenate([[0], pixels, [0]])
    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
    runs[1::2] -= runs[::2]
    return ' '.join(str(x) for x in runs)
 
def rle_decode(mask_rle, shape):
    """Decodes a run-length encoded mask.

    Source: https://www.kaggle.com/paulorzp/run-length-encode-and-decode

    Arguments:
        mask_rle (str): the run-length encoded pixels formatted as
            (start length)
        shape (tuple): the dimensions of the array to return (height, width)

    Returns:
        numpy.ndarray: the mask with `shape` dimensions

    """
    s = mask_rle.split()
    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
    starts -= 1
    ends = starts + lengths
    mask = np.zeros(shape[0]*shape[1], dtype=np.uint8)
    for lo, hi in zip(starts, ends):
        mask[lo:hi] = 1
        
    # Transpose is needed because RLE is numbered from top to bottom, then left to right
    return mask.reshape(shape).T<jupyter_output><empty_output><jupyter_text>Test decode  Functionality testCode from https://www.kaggle.com/inversion/run-length-decoding-quick-start<jupyter_code># Run length encoding CSV
rle_df = pd.read_csv(train_rle)

image_id = '0005d01c8.jpg'
image_shape = (768, 768)

image_path = os.path.join(trainset_dir, image_id)
img = Image.open(image_path)
img_masks = rle_df.loc[rle_df['ImageId'] == image_id, 'EncodedPixels'].tolist()

# Take the individual ship masks and create a single mask array for all ships
all_masks = np.zeros(image_shape)
for mask in img_masks:
    all_masks += rle_decode(mask, image_shape)

fig, axarr = plt.subplots(1, 3, figsize=(15, 40))
axarr[0].axis('off')
axarr[1].axis('off')
axarr[2].axis('off')
axarr[0].imshow(img)
axarr[1].imshow(all_masks)
axarr[2].imshow(img)
axarr[2].imshow(all_masks, alpha=0.4)
plt.tight_layout(h_pad=0.1, w_pad=0.1)
plt.show()<jupyter_output><empty_output><jupyter_text>Performance<jupyter_code>mask = img_masks[0]
%timeit rle_decode(mask, image_shape)<jupyter_output>167 µs ± 3.22 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)<jupyter_text>Test encode  Functionality testTake a ground-truth RLE, decode it and then encode it again. The encoded RLE must match the ground-truth RLE.<jupyter_code>gt_rle = img_masks[0]
decoded_mask = rle_decode(gt_rle, image_shape)
rle = rle_encode(decoded_mask)
if gt_rle == rle:
    print("Encode is working correctly!")
else:
    print("Encode is not working correctly!")
    print()
    print("Ground-truth:")
    print(gt_rle)
    print()
    print("Output from rle_encode:")
    print(rle)<jupyter_output>Encode is working correctly!<jupyter_text>Performance<jupyter_code>%timeit rle_encode(all_masks)<jupyter_output>5.16 ms ± 138 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
{'fraction_non_alphanumeric': 0.12569758171671538, 'fraction_numerical': 0.02046239702365134, 'mean_word_length': 4.331444759206799, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '418487', 'license': 'MIT', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1372, 'n_words': 336, 'path': 'notebooks/rle_test.ipynb', 'repo_name': 'liyingben/kaggle-airbus-ship-detection', 'size': '3763'}