记录一种另类的语义分割增强算法,copy and paste,简而言之,就是把A图的mask扣下来放到B图中,需要A图的原图,A图的8位mask,B图的原图,B图的8位mask。不过很可惜,在我这里尝试了以后,感觉造出来的数据极度不真实,后来也就没用了,仅仅记录一下,代码如下。
"""
Unofficial implementation of Copy-Paste for semantic segmentation
"""
#coding:utf-8
from PIL import Image
import imgviz
import cv2
import argparse
import os
import numpy as np
import tqdm
def save_colored_mask(mask, save_path):
lbl_pil = Image.fromarray(mask.astype(np.uint8), mode="P")
colormap = imgviz.label_colormap()
lbl_pil.putpalette(colormap.flatten())
lbl_pil.save(save_path)
def random_flip_horizontal(mask, img, p=0.5):
if np.random.random() < p:
img = img[:, ::-1, :]
mask = mask[:, ::-1]
return mask, img
def img_add(img_src, img_main, mask_src):
if len(img_main.shape) == 3:
h, w, c = img_main.shape
elif len(img_main.shape) == 2:
h, w = img_main.shape
mask = np.asarray(mask_src, dtype=np.uint8)
sub_img01 = cv2.add(img_src, np.zeros(np.shape(img_src), dtype=np.uint8), mask=mask)
mask_02 = cv2.resize(mask, (w, h), interpolation=cv2.INTER_NEAREST)
mask_02 = np.asarray(mask_02, dtype=np.uint8)
sub_img02 = cv2.add(img_main, np.zeros(np.shape(img_main), dtype=np.uint8),
mask=ma

该代码实现了一个非官方的CopyAndPaste语义分割增强算法,它涉及到从A图中复制mask并粘贴到B图中以创建新的训练数据。然而,作者发现这种方法生成的数据不够真实,因此未在实际应用中使用。主要步骤包括随机水平翻转、图像缩放以及大规模抖动等操作。

4235

被折叠的 条评论
为什么被折叠?



