官网:https://imgaug.readthedocs.io/en/latest/
教程:https://github.com/aleju/imgaug-doc/tree/master/notebooks

文章目录

  • 1 复制随机状态和使用多个增强序列
    • 1.1 加载基础数据
    • 1.2 示例问题
    • 1.3 手动修改参数值
    • 1.4 使用copy_random_state()
    • 1.5 使用种子

1 复制随机状态和使用多个增强序列

到目前为止,所有其他教程都假设只有一个增强序列应用于所有输入。本教程将介绍如何使用多个增强序列,尤其是如何对齐这些序列之间的随机数,这对增强图像、热图、分割图非常有用。

imgaug提供了热图和分割图增强的方法,但这些方法适用于实况数据,并且仅适用于影响几何形状的增强(如,高斯噪声或dropout are deactivated)。

如果你想要非几何增强,你必须将热图或分割图作为图像处理并通过augment_images()提供,但是你可能想要使用与用于图像的增强器和参数不同的增强器和参数,仍然得到可比较的结果(例如作物数量应该匹配)。

本教程将展示如何控制随机数生成,从而使用具有对齐随机模式的多个增强序列。

1.1 加载基础数据

以下示例从标准方案开始,包含一张图像和一张热图:

import numpy as np
import imgaug as ia
%matplotlib inline
ia.seed(1)# load image + heatmap
image = ia.quokka(size=0.2)  # uint8 array
heatmap = ia.quokka_heatmap(size=0.2)  # HeatmapsOnImage object, contains a float array# show image + heatmap
ia.imshow(np.hstack([image, heatmap.draw_on_image(image)[0]]))# print min/max of value ranges
print("image min: %.2f, max: %.2f" % (np.min(image), np.max(image)))
print("heatmap min: %.2f, max: %.2f" % (np.min(heatmap.get_arr()), np.max(heatmap.get_arr())))


image min: 0.00, max: 255.00
heatmap min: 0.00, max: 1.00

1.2 示例问题

如你所见,图像(0到255)和热图(0.0到1.0)之间的值范围不同。 让我们野蛮地将相同的增强序列应用于图像和热图,同时使用augment_image()

import imgaug.augmenters as iaa# our augmentation sequence: affine rotation, dropout, gaussian noise
augs = iaa.Sequential([iaa.Affine(rotate=(-45, 45)),iaa.Dropout(0.2),iaa.AdditiveGaussianNoise(scale=20)
])# apply to image + heatmap
augs_det = augs.to_deterministic()
image_aug = augs_det.augment_image(image)
heatmap_aug = augs_det.augment_image(heatmap.get_arr())# print min/max of value ranges
print("image min: %.2f, max: %.2f" % (np.min(image_aug), np.max(image_aug)))
print("heatmap min: %.2f, max: %.2f" % (np.min(heatmap_aug), np.max(heatmap_aug)))# show results
ia.imshow(np.hstack([image_aug,ia.HeatmapsOnImage(np.clip(heatmap_aug, 0.0, 1.0),shape=image_aug.shape).draw_on_image(image_aug)[0]
]))

image min: 0.00, max: 255.00
heatmap min: -73.75, max: 77.37

1.3 手动修改参数值

热图的价值范围现在与以前相比非常不同。 它从[0.0,1.0]变为[-73.75,77.37]。 这是由AdditiveGaussianNoise引起的,它从N(0, 20)之后的高斯分布中采样。

如此大的变化对图像来说是不合适的,但对于热图则不然。

下面的示例显示了解决此问题的一种方法。

我们首先将特定于图像的增强序列复制一份用于热图。 然后,仅在热图特定序列中,我们将AdditiveGaussianNoise的scale参数包装在Multiply(..., 0.001)中,它将采样的比例值减少1000倍。(图像中的比例被定义为20,我们也可以通过iap.Deterministic(0.02)指定,并且具有完全相同的结果。)

import imgaug.parameters as iap# Ensure that all augmenters in 'augs' use their own random number generators,
# instead of using a global (shared) random number generator.
augs = augs.localize_random_state(recursive=True)# Now copy augs to create an augmentation sequence for heatmaps.
# This also copies all random number generators, which means that the
# augmenters will sample the same random numbers.
augs_heatmap = augs.deepcopy()# Reduce the scale for heatmaps to `0.001 * scale_for_images`.
# Here, this is the same as
#   augs_heatmap[2].value.scale.value = iap.Deterministic(0.2)
# because the scale was defined above as scale=20 and hence was a deterministic value.
# Note that it would be .value.scale.value and not just .scale, because AdditiveGaussianNoise
# returns an instance of AddElementwise, which adds values sampled from .value to images,
# where .value is a gaussian distribution with .value.scale.value.
augs_heatmap[2].value = iap.Multiply(augs_heatmap[2].value, 0.001)# Augment images and heatmaps.
# We can skip here calling to_deterministic(), as both sequences
# already use the exactly same random number generators.
image_aug = augs.augment_image(image)
heatmap_aug = augs_heatmap.augment_image(heatmap.get_arr())
print("image min: %.2f, max: %.2f" % (np.min(image_aug), np.max(image_aug)))
print("heatmap min: %.2f, max: %.2f" % (np.min(heatmap_aug), np.max(heatmap_aug)))ia.imshow(np.hstack([image_aug,ia.HeatmapsOnImage(np.clip(heatmap_aug, 0.0, 1.0),shape=image_aug.shape).draw_on_image(image_aug)[0]
]))

image min: 0.00, max: 255.00
heatmap min: -0.07, max: 1.05

如你所见,增强热图的值范围现在更合适。 它略微超出[0.0,1.0]的期望值范围,但可以在通过剪裁增强后处理。

1.4 使用copy_random_state()

上述模式仍有一个值得注意的缺点:必须找出并保存要替换的参数以及替换它们的值。

然而,更容易的解决方案是基于以标准方式用略微不同的参数实例化两个序列,然后将随机性从一个序列复制到另一个序列。

成功复制的唯一要求是为每个增强器分配唯一的名称。 确保两个序列之间的名称匹配,以便相同的增强器具有相同的名称。 然后将随机状态从一个序列复制到另一个序列就可以了。 例:

# Create image-specific augmentation sequence.
# Give each augmenter its own name.
sequence_images = iaa.Sequential([iaa.Affine(rotate=(-45, 45), name="affine"),iaa.Dropout(0.2, name="dropout"),iaa.AdditiveGaussianNoise(scale=20, name="gauss-noise")
])# Create heatmap-specific augmentation sequence.
# Make sure that the names are the same for augmenters that are supposed to be aligned!
# Note how the scale of AdditiveGaussianNoise is much lower than in the image-specific sequence.
sequence_heatmaps = iaa.Sequential([iaa.Affine(rotate=(-45, 45), name="affine"),iaa.Dropout(0.2, name="dropout"),iaa.AdditiveGaussianNoise(scale=0.02, name="gauss-noise")  # different!
])# Copy once the random states between the sequences by name.
# As before, first make sure that the source augmentation sequence
# uses its own random states instead of global (shared) random states.
# Now both sequences will follow the same sampling behaviour.
sequence_images = sequence_images.localize_random_state(recursive=True)
sequence_heatmaps_det = sequence_heatmaps.copy_random_state(sequence_images, matching="name")# We can skip deterministic mode again, because both sequences have the same
# random states anyways.
image_aug = sequence_images.augment_image(image)
heatmap_aug = sequence_heatmaps.augment_image(heatmap.get_arr())
print("image min: %.2f, max: %.2f" % (np.min(image_aug), np.max(image_aug)))
print("heatmap min: %.2f, max: %.2f" % (np.min(heatmap_aug), np.max(heatmap_aug)))ia.imshow(np.hstack([image_aug,ia.HeatmapsOnImage(np.clip(heatmap_aug, 0.0, 1.0),shape=image_aug.shape).draw_on_image(image_aug)[0]
]))

image min: 0.00, max: 255.00
heatmap min: -0.08, max: 1.05

1.5 使用种子

你可能想知道:如果我们只是希望两个序列中的增强器都使用相似的随机状态,那么在实例化这些增强器时我们是否可以定义这些随机状态?然后我们可以确保相同的增强器获得相同的随机状态。是的,这是可能的。每个增强器都有一个random_state参数。您可以使用此参数来提供numpy.random.RandomState实例或仅提供种子值。

这使得对齐两个序列十分简单的,如下面的例子所示。

请注意,这次我们不仅要更改增强序列之间的值范围,还要为它们添加不同的增强器。这不会影响剩余增强器的指定随机状态。

请记住,虽然在这里我们只能在序列之间使用增强器的不同位置,因为我们没有在Sequential中激活random_order。否则会得不到匹配,因为图像序列序列中的第二个增强器与热图序列中的第二个不同。

ia.seed(1)  # to make Snowflakes reproducible# Image-specific sequence.
sequence_images = iaa.Sequential([iaa.Affine(rotate=(-45, 45), random_state=1),iaa.Snowflakes(),  # added!iaa.Dropout(0.2, random_state=2),iaa.AdditiveGaussianNoise(scale=20, random_state=3)
], random_state=4)# Heatmap-specific sequence.
# Make sure to use the same random state seeds as above.
sequence_heatmaps = iaa.Sequential([iaa.Affine(rotate=(-45, 45), random_state=1),iaa.Dropout(0.2, random_state=2),iaa.CoarseDropout(0.2, size_px=4, random_state=100),  # added!iaa.AdditiveGaussianNoise(scale=0.02, random_state=3)
], random_state=4)# We can skip deterministic mode again, because both sequences have the same
# random states anyways.
image_aug = sequence_images.augment_image(image)
heatmap_aug = sequence_heatmaps.augment_image(heatmap.get_arr())
print("image min: %.2f, max: %.2f" % (np.min(image_aug), np.max(image_aug)))
print("heatmap min: %.2f, max: %.2f" % (np.min(heatmap_aug), np.max(heatmap_aug)))ia.imshow(np.hstack([image_aug,ia.HeatmapsOnImage(np.clip(heatmap_aug, 0.0, 1.0),shape=image_aug.shape).draw_on_image(image_aug)[0]
]))

image min: 0.00, max: 255.00
heatmap min: -0.08, max: 1.05

imgaug数据增强神器:第十一章 复制随机状态和使用多个增强序列相关推荐

  1. imgaug数据增强神器:第一章 加载和增强图像

    官网:https://imgaug.readthedocs.io/en/latest/ 教程:https://nbviewer.jupyter.org/github/aleju/imgaug-doc/ ...

  2. imgaug数据增强神器:第二章 随机模式和确定模式

    官网:https://imgaug.readthedocs.io/en/latest/ 教程:https://nbviewer.jupyter.org/github/aleju/imgaug-doc/ ...

  3. imgaug数据增强神器:第零章 安装

    官网:https://imgaug.readthedocs.io/en/latest/ 教程:https://github.com/aleju/imgaug-doc/tree/master/noteb ...

  4. imgaug数据增强神器:第三章 调用多核CPU

    官网:https://imgaug.readthedocs.io/en/latest/ 教程:https://nbviewer.jupyter.org/github/aleju/imgaug-doc/ ...

  5. imgaug数据增强神器:第四章 增强关键点/界标

    官网:https://imgaug.readthedocs.io/en/latest/ 教程:https://nbviewer.jupyter.org/github/aleju/imgaug-doc/ ...

  6. yolo imgaug数据增强 标签同时也增强

    原文:https://blog.csdn.net/m0_37940759/article/details/115212083 完整代码: import xml.etree.ElementTree as ...

  7. 设计数据密集型应用 第五章:复制

    设计数据密集型应用 第五章:复制 与可能出错的东西比,'不可能'出错的东西最显著的特点就是:一旦真的出错,通常就彻底玩完了. --道格拉斯·亚当斯(1992) 文章目录 设计数据密集型应用 第五章:复 ...

  8. 大数据笔试真题集锦---第十一章:Sqoop面试题

    第十一章目录 第十一章 Sqoop sqoop本质是一款使用MR进行数据迁移的工具. 目前业界普遍用1.4.6版本,该版本与CDH集成. hive对外的一个统一存储格式的接口,使用hcatalog对接 ...

  9. 第十一章 数据可视化 - 地图可视化

    目录 疫情地图的使用 疫情地图-国内疫情地图 疫情地图-省级疫情地图 疫情地图的使用 第一阶段-第十一章-01-数据可视化案例-地图-基础地图使用_哔哩哔哩_bilibili "" ...

最新文章

  1. 博弈论 斯坦福game theory stanford week 5.0_
  2. Angular 开发中的 Source Map
  3. 求字符串里里面字符出现的次数和出现哪些不同的字符的字符串
  4. hadoop源码分析_Spark2.x精通:Job触发流程源码深度剖析(一)
  5. 【软件项目管理】软件项目的主要成本是人的劳动的消耗
  6. WCF BasicHttpBinding 安全解析(3)默认安全设置(IIS宿主)
  7. 【答辩问题】计算机专业本科毕业设计答辩的一般程序2
  8. 实测解决:Initialization failed for ‘httpsstart.spring.io‘ Please check URL, network and proxy settings
  9. bash不能运行c语言,解决:无法加载文件 C:\\Program Files\\.. 因为在此系统上禁止运行脚本。...
  10. MongoDb学习(四)--Repository
  11. 单服务器高性能:PPC、TPC、epoll、Reactor、Proactor
  12. IT项目画原型图工具介绍
  13. 单片机需要数电模电基础吗?单片机要多少模电数电知识 ?
  14. VHDL:基于 FPGA 实时处理的双目测距系统
  15. edp和lvds区别在哪里 [转载]
  16. Pubwin经典问题解答100例
  17. setup maven plugin connection
  18. 微信小程序checkbox的全选以及所有checkbox选中之后的全选
  19. Oracle学习——第二讲(函数)
  20. 计算机基础知识------操作系统

热门文章

  1. 点击tree节点,刷新表格
  2. EF实体中的数据修改更新
  3. stm32毕设分享 stm32的智能婴儿车系统(源码+硬件+论文)
  4. 【MATLAB】模糊控制篇 常用隶属函数介绍和实现
  5. 常见的距离计算公式——欧式距离(Euclidean Distance)
  6. 简述php无限极分类,PHP 无限极分类
  7. 抖音神剧本:美女喝酒后遭男子过分要求...
  8. 【LDA】吉布斯采样
  9. 视觉里程计Visual Odometry(VO)
  10. 关于shell脚本中双括号的问题