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


目录
相关文章
|
存储 算法 索引
RLE格式分割标注文件表示
RLE格式分割标注文件表示
1287 0
|
机器学习/深度学习 监控 算法
yolov8+多算法多目标追踪+实例分割+目标检测+姿态估计(代码+教程)
yolov8+多算法多目标追踪+实例分割+目标检测+姿态估计(代码+教程)
|
监控 数据可视化 API
yolo-nas无人机高空红外热数据小目标检测(教程+代码)
yolo-nas无人机高空红外热数据小目标检测(教程+代码)
|
机器学习/深度学习 PyTorch 算法框架/工具
PyTorch 中的动态计算图:实现灵活的神经网络架构
【8月更文第27天】PyTorch 是一款流行的深度学习框架,它以其灵活性和易用性而闻名。与 TensorFlow 等其他框架相比,PyTorch 最大的特点之一是支持动态计算图。这意味着开发者可以在运行时定义网络结构,这为构建复杂的模型提供了极大的便利。本文将深入探讨 PyTorch 中动态计算图的工作原理,并通过一些示例代码展示如何利用这一特性来构建灵活的神经网络架构。
984 1
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
如何从0到1开始并一步步巩固自己的AI职业生涯
2024年,AI领域的技术革新正在重塑各行业的运营模式。本文结合吴恩达发布的《如何构建AI职业生涯》及最新的AI技术进展,为信息技术和计算机科学领域的毕业生及从业者提供详细的职业发展指南。内容涵盖基础技能的掌握、项目实践、团队合作、生成式大模型与AI基础设施的发展,以及职业发展中的挑战与应对策略。通过扎实的学习和实践,帮助读者逐步建立并巩固AI职业生涯。
769 1
|
12月前
|
数据可视化 IDE 开发工具
【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)
【Python篇】PyQt5 超详细教程——由入门到精通(中篇二)
813 13
|
机器学习/深度学习 人工智能 文字识别
一种基于YOLOv8改进的高精度红外小目标检测算法 (原创自研)
【7月更文挑战第2天】 💡💡💡创新点: 1)SPD-Conv特别是在处理低分辨率图像和小物体等更困难的任务时优势明显; 2)引入Wasserstein Distance Loss提升小目标检测能力; 3)YOLOv8中的Conv用cvpr2024中的DynamicConv代替;
1088 4
|
机器学习/深度学习
【保姆级教程】【YOLOv8替换主干网络】【1】使用efficientViT替换YOLOV8主干网络结构(4)
【保姆级教程】【YOLOv8替换主干网络】【1】使用efficientViT替换YOLOV8主干网络结构
|
缓存 并行计算 Ubuntu
科研GPU环境配置-快让师兄弟们优雅地享受共享环境吧!
以下列举我对实验室4090的操作,目的是为了让实验室所有人都有隔离的沙盒环境,节省硬盘以及更方便 一听说老师买了24G 4090,真的超级兴奋!!!
420 0
科研GPU环境配置-快让师兄弟们优雅地享受共享环境吧!
|
算法 数据可视化 C#
C# | Chaikin算法 —— 计算折线对应的平滑曲线坐标点
本文将介绍一种计算折线对应的平滑曲线坐标点的算法。该算法使用Chaikin曲线平滑处理的方法,通过控制张力因子和迭代次数来调整曲线的平滑程度和精度。通过对原始点集合进行切割和插值操作,得到平滑的曲线坐标点集合。实验结果表明,该算法能够有效地平滑折线,并且具有较高的精度和可控性。
578 0
C# | Chaikin算法 —— 计算折线对应的平滑曲线坐标点