热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用pytorch的目标检测数据增强

我的示例代码的dataloader中打包传入的是一个target字典,里面包括boxes和label,如果你们传入的是boxes和label,直接修改参数就行了,然后因为我传入的i

  我的示例代码的dataloader中打包传入的是一个target字典,里面包括boxes和label,如果你们传入的是boxes和label,直接修改参数就行了,然后因为我传入的image和target都是经过torch的转换,数据格式是tensor,所以有一些转换格式的代码,然后图片shape是(c,h,w),随机概率设的是0.3,都按需要修改就行。

  随机缩放

  class randomScale(object):

  def __call__(self,image,target):

  #固定住高度,以0.8-1.2伸缩宽度,做图像形变

  if random.random() <0.3:

  image = np.array(image)

  image = np.transpose(image, (1, 2, 0))

  boxes = target["boxes"]

  scale = random.uniform(0.8,1.2)

  height,width,c = image.shape

  image = cv2.resize(image,(int(width*scale),height))

  scale_tensor = torch.FloatTensor([[scale,1,scale,1]]).expand_as(boxes)

  boxes = boxes * scale_tensor

  image = np.transpose(image, (2, 0, 1))

  image = torch.from_numpy(image)

  target["boxes"] = boxes

  return image,target

  随机模糊

  class randomBlur(object):

  def __call__(self, image, target):

  if random.random() <0.3:

  image = np.array(image)

  image = np.transpose(image, (1, 2, 0))

  image = cv2.blur(image, (5, 5))

  image = np.transpose(image, (2, 0, 1))

  image = torch.from_numpy(image)

  return image, target

  随机擦除(遮挡)

  可以增加鲁棒性,提供两个经典算法,cutout和randomerase

  class Cutout(object):

  """Randomly mask out one or more patches from an image.

  Args:

  n_holes (int): Number of patches to cut out of each image.

  length (int): The length (in pixels) of each square patch.

  """

  def __init__(self, n_holes=6, length=50):

  self.n_holes = n_holes

  self.length = length

  def __call__(self, image, target):

  """

  Args:

  img (Tensor): Tensor image of size (C, H, W).

  Returns:

  Tensor: Image with n_holes of dimension length x length cut out of it.

  """

  if random.random() <0.3:

  img = image

  h = img.shape[1]

  w = img.shape[2]

  mask = np.ones((h, w), np.float32)

  for n in range(self.n_holes):

  y = np.random.randint(h)

  x = np.random.randint(w)

  y1 = np.clip(y - self.length // 2, 0, h)

  y2 = np.clip(y + self.length // 2, 0, h)

  x1 = np.clip(x - self.length // 2, 0, w)

  x2 = np.clip(x + self.length // 2, 0, w)

  mask[y1: y2, x1: x2] = 0.

  mask = torch.from_numpy(mask)

  mask = mask.expand_as(img)

  img = img * mask

  image = img

  return image, target

  class RandomErasing(object):

  '''

  Class that performs Random Erasing in Random Erasing Data Augmentation by
Zhong et al.

  -------------------------------------------------------------------------------------

  probability: The probability that the operation will be performed.

  sl: min erasing area

  sh: max erasing area

  r1: min aspect ratio

  mean: erasing value

  -------------------------------------------------------------------------------------

  '''

  def __init__(self, sl=0.01, sh=0.25, r1=0.3, mean=[0.4914, 0.4822,
0.4465]):

  self.mean = mean

  self.sl = sl

  self.sh = sh

  self.r1 = r1

  def __call__(self, image, target):

  if random.random() <0.3:

  image = np.array(image)

  boxes = target["boxes"].numpy()

  area_box = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])

  for attempt in range(100):

  area = image.shape[1] * image.shape[2]

  target_area = random.uniform(self.sl, self.sh) * area

  aspect_ratio = random.uniform(self.r1, 1 / self.r1)

  if target_area > area_box.all() * 3:

  break

  h = int(round(math.sqrt(target_area * aspect_ratio)))

  w = int(round(math.sqrt(target_area / aspect_ratio)))

  if w

  x1 = random.randint(0, image.shape[1] - h)

  y1 = random.randint(0, image.shape[2] - w)

  if image.shape[0] == 3:

  image[0, x1:x1 + h, y1:y1 + w] = self.mean[0]

  image[1, x1:x1 + h, y1:y1 + w] = self.mean[1]

  image[2, x1:x1 + h, y1:y1 + w] = self.mean[2]

  else:

  image[0, x1:x1 + h, y1:y1 + w] = self.mean[0]

  image = torch.from_numpy(image)

  return image, target

  随机裁剪

  class Random_crop(object):

  def __call__(self, image, target):

  if random.random() <0.3:

  boxes = target["boxes"]

  labels = target["labels"]

  image = np.array(image)

  image = np.transpose(image, (1, 2, 0))

  center = (boxes[:, 2:] + boxes[:, :2]) / 2

  height, width, c = image.shape

  h = random.uniform(0.6 * height, height)

  w = random.uniform(0.6 * width, width)

  x = random.uniform(0, width - w)

  y = random.uniform(0, height - h)

  x, y, h, w = int(x), int(y), int(h), int(w)

  center = center - torch.FloatTensor([[x, y]]).expand_as(center)

  mask1 = (center[:, 0] > 0) & (center[:, 0]

  mask2 = (center[:, 1] > 0) & (center[:, 1]

  mask = (mask1 & mask2).view(-1, 1)

  boxes_in = boxes[mask.expand_as(boxes)].view(-1, 4)

  # if (len(boxes_in) == 0):

  # return image, boxes, labels

  box_shift = torch.FloatTensor([[x, y, x, y]]).expand_as(boxes_in)

  boxes_in = boxes_in - box_shift

  boxes_in[:, 0] = boxes_in[:, 0].clamp_(min=0, max=w)

  boxes_in[:, 2] = boxes_in[:, 2].clamp_(min=0, max=w)

  boxes_in[:, 1] = boxes_in[:, 1].clamp_(min=0, max=h)

  boxes_in[:, 3] = boxes_in[:, 3].clamp_(min=0, max=h)

  labels_in = labels[mask.view(-1)]

  img_croped = image[y:y + h, x:x + w, :]

  image = np.transpose(img_croped, (2, 0, 1))

  image = torch.from_numpy(image)

  target["labels"] = labels_in

  target["boxes"] = boxes_in

  return image, target

  随机平移

  class randomShift(object):

  def __call__(self, image, target):

  #平移变换

  if random.random() <0.3:

  boxes = target["boxes"]

  labels = target["labels"]

  image = np.array(image)

  image = np.transpose(image, (1, 2, 0))

  center = (boxes[:, 2:] + boxes[:, :2]) / 2

  height,width,c = image.shape

  after_shfit_image = np.zeros((height,width,c),dtype=image.dtype)

  after_shfit_image[:,:,:] = (104,117,123) #bgr

  shift_x = random.uniform(-width*0.01,width*0.01)

  shift_y = random.uniform(-height*0.01,height*0.01)

  #print(bgr.shape,shift_x,shift_y)

  #原图像的平移

  if shift_x>=0 and shift_y>=0:

  after_shfit_image[int(shift_y):,int(shift_x):,:] =
image[:height-int(shift_y),:width-int(shift_x),:]

  elif shift_x>=0 and shift_y<0:

  after_shfit_image[:height+int(shift_y),int(shift_x):,:] =
image[-int(shift_y):,:width-int(shift_x),:]

  elif shift_x <0 and shift_y >=0:

  after_shfit_image[int(shift_y):,:width+int(shift_x),:] =
image[:height-int(shift_y),-int(shift_x):,:]

  elif shift_x<0 and shift_y<0:

  after_shfit_image[:height+int(shift_y),:width+int(shift_x),:] =
image[-int(shift_y):,-int(shift_x):,:]

  shift_xy =
torch.FloatTensor([[int(shift_x),int(shift_y)]]).expand_as(center)

  center = center + shift_xy

  mask1 = (center[:,0] >0) & (center[:,0]

  mask2 = (center[:,1] >0) & (center[:,1]

  mask = (mask1 & mask2).view(-1,1)

  boxes_in = boxes[mask.expand_as(boxes)].view(-1,4)

  # if len(boxes_in) == 0:

  # return bgr,boxes,labels大连妇科医院哪个好 http://xmobile.bhbyby.com/

  box_shift =
torch.FloatTensor([[int(shift_x),int(shift_y),int(shift_x),int(shift_y)]]).expand_as(boxes_in)

  boxes_in = boxes_in+box_shift

  labels_in = labels[mask.view(-1)]

  image = np.transpose(after_shfit_image, (2, 0, 1))

  image = torch.from_numpy(image)

  target["labels"] = labels_in

  target["boxes"] = boxes_in

  return image,target

  随机变换通道

  class Random_swap(object):

  def __call__(self, image, target):

  image = np.array(image)

  image = np.transpose(image, (1, 2, 0))

  perms = ((0, 1, 2), (0, 2, 1),

  (1, 0, 2), (1, 2, 0),

  (2, 0, 1), (2, 1, 0))

  if random.random() <0.3:

  swap = perms[random.randrange(1, len(perms))]

  image = image[:, :, swap]

  image = np.transpose(image, (2, 0, 1))

  image = torch.from_numpy(image)

  return image, target

  随机变换对比度

  class Random_contrast(object):

  def __init__(self, lower=0.7, upper=1.3):

  self.lower = lower

  self.upper = upper

  def __call__(self, image, target):

  if random.random() <0.3:

  alpha = random.uniform(self.lower, self.upper)

  image *= alpha

  image = image.clip(min=0, max=255)

  return image, target

  随机变换饱和度

  class Random_saturation(object):

  def __init__(self, lower=0.7, upper=1.3):

  self.lower = lower

  self.upper = upper

  def __call__(self, image, target):

  if random.random() <0.3:

  image = np.array(image)

  image = np.transpose(image, (1, 2, 0))

  image[:, :, 1] *= random.uniform(self.lower, self.upper)

  image = np.transpose(image, (2, 0, 1))

  image = torch.from_numpy(image)

  return image, target

  随机变换色度(HSV空间下(-180,180))

  class Random_hue(object):

  def __init__(self, delta=18.0):

  self.delta = delta

  def __call__(self, image, target):

  if random.random() <0.3:

  image = np.array(image)

  image = np.transpose(image, (1, 2, 0))

  image[:, :, 0] += random.uniform(-self.delta, self.delta)

  image[:, :, 0][image[:, :, 0] > 360.0] -= 360.0

  image[:, :, 0][image[:, :, 0] <0.0] += 360.0

  image = np.transpose(image, (2, 0, 1))

  image = torch.from_numpy(image)

  return image, target

  转换图像的色彩空间

  class ConvertColor(object):

  def __init__(self, current='BGR', transform='HSV'):

  self.transform = transform

  self.current = current

  def __call__(self, image, target):

  image = np.array(image)

  image = np.transpose(image,(1,2,0))

  if self.current == 'BGR' and self.transform == 'HSV':

  image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

  elif self.current == 'HSV' and self.transform == 'BGR':

  image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)

  else:

  raise NotImplementedError

  image = np.transpose(image,(2,0,1))

  image = torch.from_numpy(image)

  return image, target


推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
  • 本文介绍了腾讯最近开源的BERT推理模型TurboTransformers,该模型在推理速度上比PyTorch快1~4倍。TurboTransformers采用了分层设计的思想,通过简化问题和加速开发,实现了快速推理能力。同时,文章还探讨了PyTorch在中间层延迟和深度神经网络中存在的问题,并提出了合并计算的解决方案。 ... [详细]
  • 突破MIUI14限制,自定义胶囊图标、大图标样式,支持任意APP
    本文介绍了如何突破MIUI14的限制,实现自定义胶囊图标和大图标样式,并支持任意APP。需要一定的动手能力和主题设计师账号权限或者会主题pojie。详细步骤包括应用包名获取、素材制作和封包获取等。 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
author-avatar
修月夕杨_433
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有