论文:

https://arxiv.org/pdf/2012.07177.pdf

非官方源码【官方源码未公布】:

https://github.com/qq995431104/Copy-Paste-for-Semantic-Segmentation

https://github.com/conradry/copy-paste-aug

一、语义分割任务

1、准备数据

data
    ----VOC2012
                  ----JPEGImages
                               ----00001.jpg
                               ----00002.jpg
                               …
                               ----00100.jpg
                  ----SegmentationClass
                               ----00001.png
                               ----00002.png
                               …
                               ----00100.png

JPEGImages/00001.jpg 图片如下:

SegmentationClass/00001.png 图片如下:

代码实现:

copy_paste.py

"""
Unofficial implementation of Copy-Paste for semantic segmentation
"""from PIL import Image
import imgviz
import cv2
import argparse
import os
import numpy as np
import tqdmdef 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, imgdef img_add(img_src, img_main, mask_src):if len(img_main.shape) == 3:h, w, c = img_main.shapeelif len(img_main.shape) == 2:h, w = img_main.shapemask = 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=mask_02)img_main = img_main - sub_img02 + cv2.resize(sub_img01, (img_main.shape[1], img_main.shape[0]),interpolation=cv2.INTER_NEAREST)return img_maindef rescale_src(mask_src, img_src, h, w):if len(mask_src.shape) == 3:h_src, w_src, c = mask_src.shapeelif len(mask_src.shape) == 2:h_src, w_src = mask_src.shapemax_reshape_ratio = min(h / h_src, w / w_src)rescale_ratio = np.random.uniform(0.2, max_reshape_ratio)# reshape src img and maskrescale_h, rescale_w = int(h_src * rescale_ratio), int(w_src * rescale_ratio)mask_src = cv2.resize(mask_src, (rescale_w, rescale_h),interpolation=cv2.INTER_NEAREST)# mask_src = mask_src.resize((rescale_w, rescale_h), Image.NEAREST)img_src = cv2.resize(img_src, (rescale_w, rescale_h),interpolation=cv2.INTER_LINEAR)# set paste coordpy = int(np.random.random() * (h - rescale_h))px = int(np.random.random() * (w - rescale_w))# paste src img and mask to a zeros backgroundimg_pad = np.zeros((h, w, 3), dtype=np.uint8)mask_pad = np.zeros((h, w), dtype=np.uint8)img_pad[py:int(py + h_src * rescale_ratio), px:int(px + w_src * rescale_ratio), :] = img_srcmask_pad[py:int(py + h_src * rescale_ratio), px:int(px + w_src * rescale_ratio)] = mask_srcreturn mask_pad, img_paddef Large_Scale_Jittering(mask, img, min_scale=0.1, max_scale=2.0):rescale_ratio = np.random.uniform(min_scale, max_scale)h, w, _ = img.shape# rescaleh_new, w_new = int(h * rescale_ratio), int(w * rescale_ratio)img = cv2.resize(img, (w_new, h_new), interpolation=cv2.INTER_LINEAR)mask = cv2.resize(mask, (w_new, h_new), interpolation=cv2.INTER_NEAREST)# mask = mask.resize((w_new, h_new), Image.NEAREST)# crop or paddingx, y = int(np.random.uniform(0, abs(w_new - w))), int(np.random.uniform(0, abs(h_new - h)))if rescale_ratio <= 1.0:  # paddingimg_pad = np.ones((h, w, 3), dtype=np.uint8) * 168mask_pad = np.zeros((h, w), dtype=np.uint8)img_pad[y:y+h_new, x:x+w_new, :] = imgmask_pad[y:y+h_new, x:x+w_new] = maskreturn mask_pad, img_padelse:  # cropimg_crop = img[y:y+h, x:x+w, :]mask_crop = mask[y:y+h, x:x+w]return mask_crop, img_cropdef copy_paste(mask_src, img_src, mask_main, img_main):mask_src, img_src = random_flip_horizontal(mask_src, img_src)mask_main, img_main = random_flip_horizontal(mask_main, img_main)# LSJ, Large_Scale_Jitteringif args.lsj:mask_src, img_src = Large_Scale_Jittering(mask_src, img_src)mask_main, img_main = Large_Scale_Jittering(mask_main, img_main)else:# rescale mask_src/img_src to less than mask_main/img_main's sizeh, w, _ = img_main.shapemask_src, img_src = rescale_src(mask_src, img_src, h, w)img = img_add(img_src, img_main, mask_src)mask = img_add(mask_src, mask_main, mask_src)return mask, imgdef main(args):# input pathsegclass = os.path.join(args.input_dir, 'SegmentationClass')JPEGs = os.path.join(args.input_dir, 'JPEGImages')# create output pathos.makedirs(args.output_dir, exist_ok=True)os.makedirs(os.path.join(args.output_dir, 'SegmentationClass'), exist_ok=True)os.makedirs(os.path.join(args.output_dir, 'JPEGImages'), exist_ok=True)masks_path = os.listdir(segclass)tbar = tqdm.tqdm(masks_path, ncols=100)for mask_path in tbar:# get source mask and imgmask_src = np.asarray(Image.open(os.path.join(segclass, mask_path)), dtype=np.uint8)img_src = cv2.imread(os.path.join(JPEGs, mask_path.replace('.png', '.jpg')))# random choice main mask/imgmask_main_path = np.random.choice(masks_path)mask_main = np.asarray(Image.open(os.path.join(segclass, mask_main_path)), dtype=np.uint8)img_main = cv2.imread(os.path.join(JPEGs, mask_main_path.replace('.png', '.jpg')))# Copy-Paste data augmentationmask, img = copy_paste(mask_src, img_src, mask_main, img_main)mask_filename = "copy_paste_" + mask_pathimg_filename = mask_filename.replace('.png', '.jpg')save_colored_mask(mask, os.path.join(args.output_dir, 'SegmentationClass', mask_filename))cv2.imwrite(os.path.join(args.output_dir, 'JPEGImages', img_filename), img)def get_args():parser = argparse.ArgumentParser()parser.add_argument("--input_dir", default="data/VOC2012", type=str,help="input annotated directory")parser.add_argument("--output_dir", default="data/VOC2012_copy_paste", type=str,help="output dataset directory")parser.add_argument("--lsj", default=True, type=bool, help="if use Large Scale Jittering")return parser.parse_args()if __name__ == '__main__':args = get_args()main(args)

执行命令:

ubuntu@ubuntu-desktop: cd xxx/xxx #### 路径
ubuntu@ubuntu-desktop: python copy_paste.py

运行后,在data文件夹下生成一个
VOC2012_copy_paste文件夹

data
    ----VOC2012_copy_paste
                  ----JPEGImages
                               ----copy_paste_00001.jpg
                               ----copy_paste_00002.jpg
                               …
                               ----copy_paste_00100.jpg
                  ----SegmentationClass
                               ----copy_paste_00001.png
                               ----copy_paste_00002.png
                               …
                               ----copy_paste_00100.png

JPEGImages/copy_paste_00001.jpg 图片如下:

SegmentationClass/copy_paste_00001.png 图片如下:

参考:

https://blog.csdn.net/oYeZhou/article/details/111696577

https://blog.csdn.net/amusi1994/article/details/111240196

【论文笔记】Copy-Paste研读相关推荐

  1. 【论文笔记】如何研读一篇论文

    如何研读一篇论文 **用自己的语言,将资源的核心发现和技术有条理地记录下来.** 至少三遍!至少三遍!至少三遍! 第一遍:读标题.摘要和图片 第二遍:读引言和结论部分,再浏览一遍图片,并快速浏览论文的 ...

  2. 论文笔记2:Deep Attention Recurrent Q-Network

    参考文献:[1512.01693] Deep Attention Recurrent Q-Network (本篇DARQN) [1507.06527v3] Deep Recurrent Q-Learn ...

  3. 论文笔记-Vanilla Transformer:Character-Level Language Modeling with Deeper Self-Attention

    论文笔记-Vanilla Transformer:Character-Level Language Modeling with Deeper Self-Attention 1. 介绍 2. Chara ...

  4. 论文笔记:Editing-Based SQL Query Generation for Cross-Domain Context-Dependent Questions

    论文笔记:Editing-Based SQL Query Generation for Cross-Domain Context-Dependent Questions 目录 论文笔记:Editing ...

  5. 论文笔记 | 谷歌 Soft Prompt Learning ,Prefix-Tuning的 -> soft promt -> p tuning v2

    论文笔记 | 谷歌 Soft Prompt Learning ptuning -> Prefix-Tuning -> soft promt -> p tuning v2 " ...

  6. 论文笔记《Incorporating Copying Mechanism in Sequence-to-Sequence Learning》

    论文笔记<Incorporating Copying Mechanism in Sequence-to-Sequence Learning> 论文来源:2016 ACL 论文主要贡献:提出 ...

  7. ORB-SLAM3 论文笔记

    ORB-SLAM3 论文笔记 这篇博客 ORB-SLAM3系统 相机模型的抽象(Camera Model) 重定位的问题 图片矫正的问题 视觉惯性SLAM的工作原理 相关公式 IMU初始化 跟踪和建图 ...

  8. 【论文笔记】 LSTM-BASED DEEP LEARNING MODELS FOR NONFACTOID ANSWER SELECTION

    一.简介 这篇论文由IBM Watson发表在2016 ICLR,目前引用量92.这篇论文的研究主题是answer selection,作者在这篇论文基础上[Applying Deep Learnin ...

  9. 最新图神经网络论文笔记汇总(附pdf下载)

    点击上方,选择星标或置顶,不定期资源大放送! 阅读大概需要15分钟 Follow小博主,每天更新前沿干货 [导读]近年来,图神经网络变得非常火热,每年顶会在该领域内都会出现大量的研究论文,本文为大家提 ...

最新文章

  1. Transformer 架构逐层功能介绍和详细解释
  2. Mining of Massive Dataset----PageRank的两种问题spider traps和dead ends
  3. 10元权限gm游戏_游戏P图超能打!揭秘10年老本儿500元升级计划
  4. docker运行python程序_如何使用Docker运行多个Python脚本和一个可执行文件?
  5. hadoop2.4.1源码编译步骤 hive0.13.1编译
  6. 云计算入门科普系列:小型云计算平台怎么搭建?
  7. EA(Enterprise Architecture,企业架构)
  8. php自己遇到的一些问题
  9. 清理autodesk产品注册表_如何清理卸载Inventor产品
  10. linux下lex词法分析器,Lex词法分析器
  11. 3亿流量还能让Keep错几次?
  12. 微信小程序之组件的四种传值方式
  13. 大数据可视化应用_在数据可视化中应用种族平等意识
  14. 水果 hdu 1263 模拟
  15. 五、伊森商城 前端基础-Vue v-on 事件修饰符 按键修饰符 v-for v-if 和v-show v-else和v-else-if p24
  16. 学习 Python 之 Pygame 开发魂斗罗(二)
  17. 海底光缆是如何铺设出来的?
  18. Matlab答疑五:使用微分定义求解微分方程的数值解
  19. android 友盟统计动态设置渠道,Android 友盟多渠道打包
  20. CentOS 消亡?不怕!替代发行版 AlmaLinux 获得商业支持

热门文章

  1. matlab解无解析解微分方程组,数学应用软件作业6 用Matlab求解微分方程(组)的解析解和数值解...
  2. 汉得企业级PaaS平台 HZERO 发布 1.5.0.RELEASE 版本
  3. K-S检验法判断数据分布类型
  4. H3C交换机配置详解
  5. 苹果x充电慢是什么原因_为什么苹果x比xr贵
  6. 《android AP/BP理解》
  7. 手持终端RFID技术,养老院人员定位系统——新导智能
  8. 一个嵌入式程序员对X280的测评
  9. 计算机思维导论二讲答案,大学计算机计算思维导论第2讲习题与解析.pdf
  10. ims 注册鉴权认证过程