# Prediction

Your model should predict the masks for **segmentation** bounding polygons in each image. 

A mask is a 2D matrix corresponding to each pixel of the input image. The contents of each pixel position can be `0` or `1`. If the pixel position is marked `0`, then that position corresponds to the background category, no paragraph, text box, image, or table exists at that pixel. This mask is called a **binary mask** because of its content. 

Each prediction category (paragraph, text box, image, table) has a binary mask for each image. For example, image `846df66a-610e-4356-b369-6788885a0dc5.png` has 4 binary masks for each existing category - that predict where that specific category occurs in the image. 

*Bounding boxes play no role in the evaluation for this competition.*

# Run Length Encoding (RLE) of Masks

The mask matrices become too big, considering the number of images you must predict. To compress the masks, we use RLE encoding. The function used for RLE conversion is the following:

```python
def rle_encode(mask: np.ndarray) -> str:
    # flatten the mask column wise
    pixels = mask.T.flatten()

    # pad the list of pixels to ensure they have leading and trailing 0
    use_padding = False
    if pixels[0] or pixels[-1]:
        use_padding = True
        pixel_padded = np.zeros([len(pixels) + 2], dtype=pixels.dtype)
        pixel_padded[1:-1] = pixels
        pixels = pixel_padded

    # get the pixel indices where consecutive pixels don't match => binary masks begin or end
    # +1 to convert to inclusive start indices, exclusive end indices of masks
    # +1 to convert to 1-indexed pixel ordering
    rle = np.where(pixels[1:] != pixels[:-1])[0] + 2
    if use_padding:
        rle = rle - 1  # remove the extra offset introduced by padding

    # end pixel indices of masks - start pixel indices of masks = length of RLE runs
    # store the RLE run lengths replacing the end pixel indices 
    rle[1::2] = rle[1::2] - rle[:-1:2]
    
    return ' '.join(str(x) for x in rle)
```

You can read about the RLE encoding format [here](https://www.kaggle.com/code/leahscherschel/run-length-encoding). *(NB: The explanation in the link uses RLE encoding in a 0-indexed grid, but the function above uses a 1-indexed grid. You are advised to use the 1-indexed implementation above to match the ground truth.)*

# Submission Format

You will submit a `CSV` file for each image, and each category. Your `submission.csv` will contain 2 columns: `Id` and `Predicted`. 
1. `Id`: This column will be composed of each `image_id`, and each `category_id` for that image. They will be concatenated in the form `<image_id>_<category_id>`. 
2. `Predicted`: This will contain the RLE encoded mask for each image category. 

You have a sample submission (`sample_subimssion.csv`) available in the dataset. 

**All teams will submit working Kaggle notebooks for Train and Test with appropriate instructions and documentation for reproducibility to verify the authenticity of the solution.**

# Evaluation Metric: Dice Score
 ![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F10747121%2F93d164345ff828b16e609c96f64d6791%2Fdice.png?generation=1687102960736961&alt=media)