基于Python實(shí)現(xiàn)RLE格式分割標(biāo)注文件的格式轉(zhuǎn)換
1.Airbus Ship Detection Challenge
url: https://www.kaggle.com/competitions/airbus-ship-detection
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.數(shù)據(jù)展示
2.1 標(biāo)注數(shù)據(jù)
該數(shù)據(jù)以csv格式存儲,具體如下:
2.2 圖象文件
3.格式轉(zhuǎn)換
由于圖太多,暫時(shí)轉(zhuǎn)換10個(gè)
#!/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 # 將目標(biāo)路徑下的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/')
其中為了方便查看,原計(jì)劃0為背景,1為mask,為了方便顯示,設(shè)置為255為mask。
4.轉(zhuǎn)換結(jié)果
到此這篇關(guān)于基于Python實(shí)現(xiàn)RLE格式分割標(biāo)注文件的格式轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)Python RLE文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)將兩個(gè)文件夾合并至另一個(gè)文件夾(制作數(shù)據(jù)集)
這篇文章主要介紹了python實(shí)現(xiàn)將兩個(gè)文件夾合并至另一個(gè)文件夾(制作數(shù)據(jù)集),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04對pytorch中x = x.view(x.size(0), -1) 的理解說明
這篇文章主要介紹了對pytorch中x = x.view(x.size(0), -1) 的理解說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03