RLE格式分割标注文件格式转换【以Airbus Ship Detection Challenge为例】

简介: RLE格式分割标注文件格式转换【以Airbus Ship Detection Challenge为例】

RLE格式分割标注文件格式转换【以Airbus Ship Detection Challenge为例】


1.Airbus Ship Detection Challenge


url:www.kaggle.com/competition…

Find ships on satellite images as quickly as possible

Data Description

In this competition, you are required to locate ships in images, and put an aligned bounding box segment around the ships you locate. Many images do not contain ships, and those that do may contain multiple ships. Ships within and across images may differ in size (sometimes significantly) and be located in open sea, at docks, marinas, etc.

For this metric, object segments cannot overlap. There were a small percentage of images in both the Train and Test set that had slight overlap of object segments when ships were directly next to each other. Any segments overlaps were removed by setting them to background (i.e., non-ship) encoding. Therefore, some images have a ground truth may be an aligned bounding box with some pixels removed from an edge of the segment. These small adjustments will have a minimal impact on scoring, since the scoring evaluates over increasing overlap thresholds.

The train_ship_segmentations.csv file provides the ground truth (in run-length encoding format) for the training images. The sample_submission files contains the images in the test images.

Please click on each file / folder in the Data Sources section to get more information about the files.

kaggle competitions download -c airbus-ship-detection


2.数据展示


2.1 标注数据


该数据以csv格式存储,具体如下:

image.png


2.2 图象文件


image.pngimage.pngimage.png


3.格式转换


由于图太多,暂时转换10个

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)
from PIL import Image
# ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode
# 将图片编码成rle格式
def rle_encode(img, min_max_threshold=1e-3, max_mean_threshold=None):
    '''
    img: numpy array, 1 - mask, 0 - background
    Returns run length as string formated
    '''
    if np.max(img) < min_max_threshold:
        return ''  ## no need to encode if it's all zeros
    if max_mean_threshold and np.mean(img) > max_mean_threshold:
        return ''  ## ignore overfilled mask
    pixels = img.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)
# 将图片从rle解码
def rle_decode(mask_rle, shape=(768, 768)):
    '''
    mask_rle: run-length as string formated (start length)
    shape: (height,width) of array to return
    Returns numpy array, 1 - mask, 0 - background
    '''
    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
    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
    for lo, hi in zip(starts, ends):
        # img[lo:hi] = 1
        img[lo:hi] = 255 #方便可视化
    return img.reshape(shape).T  # Needed to align to RLE direction
def masks_as_image(in_mask_list):
    # Take the individual ship masks and create a single mask array for all ships
    all_masks = np.zeros((768, 768), dtype=np.uint8)
    for mask in in_mask_list:
        if isinstance(mask, str):
            all_masks |= rle_decode(mask)
    return all_masks
# 将目标路径下的rle文件中所包含的所有rle编码,保存到save_img_dir中去
def rle_2_img(train_rle_dir, save_img_dir):
    masks = pd.read_csv(train_rle_dir)
    not_empty = pd.notna(masks.EncodedPixels)
    print(not_empty.sum(), 'masks in', masks[not_empty].ImageId.nunique(), 'images')
    print((~not_empty).sum(), 'empty images in', masks.ImageId.nunique(), 'total images')
    all_batchs = list(masks.groupby('ImageId'))
    train_images = []
    train_masks = []
    i = 0
    for img_id, mask in all_batchs[:10]:
        c_mask = masks_as_image(mask['EncodedPixels'].values)
        im = Image.fromarray(c_mask)
        im.save(save_img_dir + img_id.split('.')[0] + '.png')
        print(i, img_id.split('.')[0] + '.png')
        i += 1
    return train_images, train_masks
if __name__ == '__main__':
    rle_2_img('train_ship_segmentations_v2.csv',
              'mask/')

其中为了方便查看,原计划0为背景,1为mask,为了方便显示,设置为255为mask。


3.转换结果


image.pngimage.pngimage.pngimage.pngimage.pngimage.pngimage.png


目录
相关文章
|
2月前
|
数据处理 开发工具 git
coco2017数据集转换为yolo格式(记录过程)
最近做一个yolov5的落地应用项目,用的anylabeling打标,需要将coco2017的数据集转为yolo格式,故写下记录过程!
|
2月前
|
人工智能 搜索推荐
ERNIE-Bot 4.0提示词格式
ERNIE-Bot 4.0提示词格式
22 0
|
存储 算法 索引
RLE格式分割标注文件表示
RLE格式分割标注文件表示
866 0
|
3月前
|
机器学习/深度学习 人工智能 文字识别
通用场景OCR文本识别任务-baseline学习(PaddleOCR)
通用场景OCR文本识别任务-baseline学习(PaddleOCR)
71 0
|
3月前
要将ModelScope的应用检测模型转换为ONNX格式或RKNN格式
要将ModelScope的应用检测模型转换为ONNX格式或RKNN格式
103 1
|
9月前
|
索引
ENVI_IDL:批量拼接Modis Swath的逐日数据并输出为Geotiff格式
ENVI_IDL:批量拼接Modis Swath的逐日数据并输出为Geotiff格式
95 0
|
9月前
ENVI_IDL:批量重投影Modis Swath产品并指定范围输出为Geotiff格式+解析
ENVI_IDL:批量重投影Modis Swath产品并指定范围输出为Geotiff格式+解析
127 0
|
算法 PyTorch 算法框架/工具
【DSW Gallery】基于EasyCV的STDC图像语义分割示例
EasyCV是基于Pytorch,以自监督学习和Transformer技术为核心的 all-in-one 视觉算法建模工具,并包含图像分类,度量学习,目标检测,姿态识别等视觉任务的SOTA算法。本文将为您介绍如何在PAI-DSW中使用EasyCV训练轻量化语义分割模型STDC
【DSW Gallery】基于EasyCV的STDC图像语义分割示例
|
XML 存储 JSON
2.基于Label studio的训练数据标注指南:(智能文档)文档抽取任务、PDF、表格、图片抽取标注等
2.基于Label studio的训练数据标注指南:(智能文档)文档抽取任务、PDF、表格、图片抽取标注等
|
数据格式
将icdar2015数据集转换成paddleOCR标注数据格式
将icdar2015数据集转换成paddleOCR标注数据格式
将icdar2015数据集转换成paddleOCR标注数据格式