imgaug 0.4.0 图像增强库实战指南:从安装配置、增强器组合到多核批量数据增强

发布时间:2026/10/3 8:20:17

imgaug 0.4.0 图像增强库实战指南:从安装配置、增强器组合到多核批量数据增强 数据增强计算机视觉图像处理机器学习【免费下载链接】imgaugImage augmentation for machine learning experiments.项目地址https://gitcode.com/gh_mirrors/im/imgaug点击查看免费下载imgaug 是一个面向机器学习尤其是卷积神经网络实验的 Python 图像增强库其核心思路是把一组输入图像转换成更大、更多样的一组微调图像从而扩充训练数据、缓解过拟合。本文以仓库根目录的 README.md 为骨架结合 imgaug/augmenters/meta.py 等源码实现系统讲解 imgaug 的特性与安装方式、13 大模块的增强器全览、以及从简单训练流程到复杂增强流水线、从图像到关键点/边界框/热力图等各类标注数据对齐增强的完整实战方案。核心特性一张图看懂 imgaug 能做什么按 README.md 的 Features 章节imgaug 的核心能力可以归纳为六个方面丰富的增强技术包括仿射变换affine、透视变换perspective、对比度变化contrast、高斯噪声gaussian noise、区域丢弃dropout、色相/饱和度调整hue/saturation、裁剪/填充crop/pad、模糊blur等覆盖几何、颜色、噪声、风格化等多个维度面向高性能优化增强流程针对批量数据处理做了性能优化0.4.0 起后端改为逐批次batchwise增强多类型标注数据支持图像uint8 完整支持其他 dtype 支持情况因增强器而异热力图 Heatmapsfloat32、分割图 Segmentation Mapsint、掩码 Masksbool——它们可以与图像尺寸不一致例如裁剪时无需额外代码即可自动按比例处理关键点/地标 Keypointsint/float 坐标边界框 Bounding Boxesint/float 坐标多边形 Polygonsint/float 坐标线串 Line Stringsint/float 坐标随机值的自动对齐例如从uniform(-10°, 45°)采样一个旋转角图像和叠加其上的分割图会自动使用同一个采样值零额外代码概率分布作为参数例如从uniform(-10°, 45°)采样旋转角度甚至支持ABS(N(0, 20.0))*(1B(1.0, 1.0))这类由绝对值函数ABS(.)、高斯分布N(.)、Beta 分布B(.)组合而成的复杂表达式丰富的辅助函数绘制热力图、分割图、关键点、边界框等缩放分割图、对图像/地图做平均池化或最大池化、将图像 pad 到指定宽高比如正方形把关键点转换为距离图、从图像中提取边界框内的像素、把多边形裁剪到图像平面等多 CPU 核心并行增强支持在后台进程中对多个 batch 进行并行增强。安装与依赖环境Anaconda 安装README 提供官方 Anaconda 安装方式通过 conda-forge 渠道安装conda config --add channels conda-forge conda install imgaug卸载则执行conda remove imgaug。pip 安装通过 PyPI 安装可能滞后于 GitHub 源码版本pip install imgaug或直接从 GitHub 安装最新版pip install githttps://github.com/aleju/imgaug.git卸载执行pip uninstall imgaug。版本与运行时要求当前仓库为0.4.0版本见 setup.pyREADME 声明支持 Python 2.7 与 3.4setup.py 的 classifiers 进一步确认支持 Python 2.7 及 3.43.8。其核心运行时依赖setup.py为依赖包版本约束用途six无Python 2/3 兼容层numpy1.15数值数组基础scipy无滤波/插值等科学计算Pillow无图像读写与 PIL 风格增强matplotlib无绘图与可视化scikit-image0.14.2仿射/透视等几何变换底层opencv-python-headless无OpenCV 运算可替换imageio无图像/视频读写Shapely无多边形几何运算注意一个细节opencv-python-headless存在三个可替换项opencv-python、opencv-contrib-python、opencv-contrib-python-headless。setup.py 的check_alternative_installation()会在安装时检测用户环境里是否已存在这些替代包若已安装则不再重复装 OpenCV避免同一库的多重安装冲突。文档资源README 推荐了两类学习资源官方 Jupyter Notebook涵盖图像加载与增强、多核增强以及关键点、边界框、多边形、线串、热力图、分割图的增强操作和 ReadTheDocs 文档快速上手示例、全部增强器总览、API 参考。仓库内另有 checks/ 目录包含数十个可直接运行的示例脚本如 check_affine.py、check_some_of.py、check_multicore_pool.py是本地验证增强效果、学习参数用法的快捷入口。版本演进脉络README 的 Recent Changes 章节与仓库 changelogs/ 目录记录了各版本的关键变化0.4.0新增多个增强器增强后端改为 batchwise 逐批次增强支持 numpy 1.18 与 Python 3.8。这一batchwise 后端在源码中有直接印证——imgaug/augmenters/meta.py 的_augment_batch_方法注释明确标注 Added in 0.4.0它统一处理同一 batch 内图像、关键点等多列数据的对齐采样0.3.0重构分割图增强适配 numpy 1.17 的随机数采样 API新增多个增强器0.2.9新增多边形增强、线串增强简化增强接口0.2.8改进性能、dtype 支持与多核增强。增强器全景总览按模块分类README 的 Example Images 章节按模块列出了绝大多数增强器。其中形如(a, b)的参数值表示从区间[a, b]中随机均匀采样。线串Line Strings几乎被所有增强器支持只是在该章节中未单独可视化。下表汇总各模块及其代表增强器对应源码模块位于 imgaug/augmenters/ 下模块源码文件代表增强器其他可用增强器metameta.pyIdentity、ChannelShuffleSequential、SomeOf、OneOf、Sometimes、WithChannels、Lambda、AssertLambda、AssertShape、RemoveCBAsByOutOfImageFraction、ClipCBAsToImagePlanesarithmeticarithmetic.pyAdd、AdditiveGaussianNoise、Multiply、Cutout、Dropout、CoarseDropout、Dropout2d、SaltAndPepper、CoarseSaltAndPepper、Invert、Solarize、JpegCompressionAddElementwise、AdditiveLaplaceNoise、AdditivePoissonNoise、MultiplyElementwise、TotalDropout、ReplaceElementwise、ImpulseNoise、Salt、Pepper、CoarseSalt、CoarsePepperartisticartistic.pyCartoon—blendblend.pyBlendAlpha、BlendAlphaSimplexNoise、BlendAlphaFrequencyNoise、BlendAlphaSomeColors、BlendAlphaRegularGridBlendAlphaMask、BlendAlphaElementwise、BlendAlphaVerticalLinearGradient、BlendAlphaHorizontalLinearGradient、BlendAlphaSegMapClassIds、BlendAlphaBoundingBoxes、BlendAlphaCheckerboard及 SomeColorsMaskGen、RegularGridMaskGen、CheckerboardMaskGen、InvertMaskGen 等 MaskGenblurblur.pyGaussianBlur、AverageBlur、MedianBlur、BilateralBlur、MotionBlur、MeanShiftBlur—collectionscollections.pyRandAugment—colorcolor.pyMultiplyAndAddToBrightness、MultiplyHueAndSaturation、MultiplyHue、MultiplySaturation、AddToHueAndSaturation、Grayscale、RemoveSaturation、ChangeColorTemperature、KMeansColorQuantization、UniformColorQuantizationWithColorspace、WithBrightnessChannels、MultiplyBrightness、AddToBrightness、WithHueAndSaturation、AddToHue、AddToSaturation、ChangeColorspace、Posterizecontrastcontrast.pyGammaContrast、SigmoidContrast、LogContrast、LinearContrast、HistogramEqualization、AllChannelsHistogramEqualization、AllChannelsCLAHE、CLAHEEqualizeconvolutionalconvolutional.pySharpen、Emboss、EdgeDetect、DirectedEdgeDetectConvolvedebugdebug.py—SaveDebugImageEveryNBatchesedgesedges.pyCanny—flipflip.pyFliplr、FlipudHorizontalFlip、VerticalFlipgeometricgeometric.pyAffine含 Modes/cval 变体、PiecewiseAffine、PerspectiveTransform、ElasticTransformation、Rot90、WithPolarWarping、JigsawScaleX、ScaleY、TranslateX、TranslateY、Rotateimgcorruptlikeimgcorruptlike.pyGlassBlur、DefocusBlur、ZoomBlur、Snow、SpatterGaussianNoise、ShotNoise、ImpulseNoise、SpeckleNoise、Fog、Frost、Contrast、Brightness、Saturate、JpegCompression、Pixelate、ElasticTransformpillikepillike.pyAutocontrast、EnhanceColor、EnhanceSharpness、FilterEdgeEnhanceMore、FilterContourSolarize、Posterize、Equalize、EnhanceContrast、EnhanceBrightness、FilterBlur、FilterSmooth、FilterSmoothMore、FilterEdgeEnhance、FilterFindEdges、FilterEmboss、FilterSharpen、FilterDetail、Affinepoolingpooling.pyAveragePooling、MaxPooling、MinPooling、MedianPooling—segmentationsegmentation.pySuperpixels、UniformVoronoi、RegularGridVoronoiVoronoi、RelativeRegularGridVoronoi及 RegularGridPointsSampler、UniformPointsSampler、DropoutPointsSampler、SubsamplingPointsSampler 等 PointsSamplersizesize.pyCropAndPad、Crop、Pad、PadToFixedSize、CropToFixedSizeResize、CropToMultiplesOf、PadToMultiplesOf、CropToPowersOf、PadToPowersOf、CropToAspectRatio、PadToAspectRatio、CropToSquare、PadToSquare以及对应的 Center 系列与 KeepSizeByResizeweatherweather.pyFastSnowyLandscape、Clouds、Fog、Snowflakes、RainCloudLayer、SnowflakesLayer、RainLayer实战一标准训练流程中的简单增强README 的第一个代码示例演示了最常见的机器学习训练场景——每个 batch 依次做随机裁剪、水平翻转、高斯模糊。其要点是图像输入约定为(N, height, width, channels)的 numpy 数组或不同尺寸(height, width, channels)数组组成的列表做颜色空间类增强时应使用 RGBcv2.imread()返回的是 BGR图像通常使用取值 0255 的uint8。import numpy as np import imgaug.augmenters as iaa def load_batch(batch_idx): # dummy function, implement this # Return a numpy array of shape (N, height, width, #channels) # or a list of (height, width, #channels) arrays (may have different image # sizes). # Images should be in RGB for colorspace augmentations. # (cv2.imread() returns BGR!) # Images should usually be in uint8 with values from 0-255. return np.zeros((128, 32, 32, 3), dtypenp.uint8) (batch_idx % 255) def train_on_images(images): # dummy function, implement this pass # Pipeline: # (1) Crop images from each side by 1-16px, do not resize the results # images back to the input size. Keep them at the cropped size. # (2) Horizontally flip 50% of the images. # (3) Blur images using a gaussian kernel with sigma between 0.0 and 3.0. seq iaa.Sequential([ iaa.Crop(px(1, 16), keep_sizeFalse), iaa.Fliplr(0.5), iaa.GaussianBlur(sigma(0, 3.0)) ]) for batch_idx in range(100): images load_batch(batch_idx) images_aug seq(imagesimages) # done by the library train_on_images(images_aug)其中Crop(px(1, 16), keep_sizeFalse)表示从每侧随机裁剪 116 像素且不把结果缩放回原尺寸Fliplr(0.5)表示 50% 概率水平翻转GaussianBlur(sigma(0, 3.0))使用 0.03.0 的高斯核 sigma 值模糊。从源码层面看seq(imagesimages)等价于调用 imgaug/augmenters/meta.py 的augment()方法它会将输入包装成UnnormalizedBatch再经augment_batch进入 meta.py 的_augment_batch_统一处理而Sequential本身就是list的子类meta.py它会依次把每个子增强器应用到数据上即第二个增强器接收的是已经过第一个增强器处理的输入。实战二非常复杂的增强流水线README 给出了用于生成首页效果图的重度增强流水线是组合各类增强器的最佳示范。先定义sometimes辅助函数让指定增强器只在 50% 的样本上生效import numpy as np import imgaug as ia import imgaug.augmenters as iaa # random example images images np.random.randint(0, 255, (16, 128, 128, 3), dtypenp.uint8) # Sometimes(0.5, ...) applies the given augmenter in 50% of all cases, # e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image. sometimes lambda aug: iaa.Sometimes(0.5, aug) seq iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width sometimes(iaa.CropAndPad( percent(-0.05, 0.1), pad_modeia.ALL, pad_cval(0, 255) )), sometimes(iaa.Affine( scale{x: (0.8, 1.2), y: (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis translate_percent{x: (-0.2, 0.2), y: (-0.2, 0.2)}, # translate by -20 to 20 percent (per axis) rotate(-45, 45), # rotate by -45 to 45 degrees shear(-16, 16), # shear by -16 to 16 degrees order[0, 1], # use nearest neighbour or bilinear interpolation (fast) cval(0, 255), # if mode is constant, use a cval between 0 and 255 modeia.ALL # use any of scikit-images warping modes )), # execute 0 to 5 of the following (less important) augmenters per image # dont execute all of them, as that would often be way too strong iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace(0, 1.0), n_segments(20, 200))), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k(3, 11)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha(0, 1.0), lightness(0.75, 1.5)), # sharpen images iaa.Emboss(alpha(0, 1.0), strength(0, 2.0)), # emboss images # search either for all edges or for directed edges, # blend the result with the original image using a blobby mask iaa.SimplexNoiseAlpha(iaa.OneOf([ iaa.EdgeDetect(alpha(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha(0.5, 1.0), direction(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise(loc0, scale(0.0, 0.05*255), per_channel0.5), # add gaussian noise to images iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel0.5), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.03, 0.15), size_percent(0.02, 0.05), per_channel0.2), ]), iaa.Invert(0.05, per_channelTrue), # invert color channels iaa.Add((-10, 10), per_channel0.5), # change brightness of images (by -10 to 10 of original value) iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel0.5), iaa.FrequencyNoiseAlpha( exponent(-4, 0), firstiaa.Multiply((0.5, 1.5), per_channelTrue), secondiaa.LinearContrast((0.5, 2.0)) ) ]), iaa.LinearContrast((0.5, 2.0), per_channel0.5), # improve or worsen the contrast iaa.Grayscale(alpha(0.0, 1.0)), sometimes(iaa.ElasticTransformation(alpha(0.5, 3.5), sigma0.25)), # move pixels locally around (with random strengths) sometimes(iaa.PiecewiseAffine(scale(0.01, 0.05))), # sometimes move parts of the image around sometimes(iaa.PerspectiveTransform(scale(0.01, 0.1))) ], random_orderTrue ) ], random_orderTrue ) images_aug seq(imagesimages)这段流水线集中体现了 README Features 章节所述的几大设计Sometimes(0.5, aug)让增强器以 50% 概率生效SomeOf((0, 5), [...], random_orderTrue)每张图随机执行列表中的 05 个子增强器且顺序随机——避免一次施加全部增强导致过度失真OneOf([...])每次只从列表中随机选一个执行例如三种模糊效果二选一/三选一per_channel0.5表示 50% 情况下按整张图采样一个值其余情况按每个通道各自采样一个值ia.ALL、iaa.Affine(modeia.ALL)等表示从全部合法取值中随机选择例如 scikit-image 的全部 warp 模式。这些容器增强器在 imgaug/augmenters/meta.py 中均有对应实现SequentialL3006顺序应用子增强器并支持random_orderSomeOfL3188随机挑选 n 个子增强器OneOfL3470是SomeOf的特例每次恰好激活一个子增强器SometimesL3539支持then_list/else_list两个分支。Sequential的 docstring 还特别说明random_orderTrue时子增强器的顺序会在每个 batch 随机采样一次能显著扩大增强空间。实战三图像与关键点/地标的对齐增强目标检测、姿态估计等任务通常需要同时增强图像与其上的关键点。README 示例中两张测试图在(64, 64)处标记白色像素关键点分别为第一张 1 个点、第二张 3 个点增强序列为高斯噪声 沿 x 轴平移 15 像素import numpy as np import imgaug.augmenters as iaa images np.zeros((2, 128, 128, 3), dtypenp.uint8) # two example images images[:, 64, 64, :] 255 points [ [(10.5, 20.5)], # points on first image [(50.5, 50.5), (60.5, 60.5), (70.5, 70.5)] # points on second image ] seq iaa.Sequential([ iaa.AdditiveGaussianNoise(scale0.05*255), iaa.Affine(translate_px{x: (1, 5)}) ]) # augment keypoints and images images_aug, points_aug seq(imagesimages, keypointspoints) print(Image 1 center, np.argmax(images_aug[0, 64, 64:646, 0])) print(Image 2 center, np.argmax(images_aug[1, 64, 64:646, 0])) print(Points 1, points_aug[0]) print(Points 2, points_aug[1])README 特别强调imgaug 中所有坐标都是亚像素精度subpixel-accurate因此x0.5, y0.5表示左上角像素的中心。这条约定同样适用于下文的所有坐标类数据。实战四边界框、多边形与线串三种坐标类标注数据的增强写法高度一致均由 图像 标注列表 构成输入seq(images..., xxx...)返回增强后的两者。边界框坐标形式为x1, y1, x2, y2import numpy as np import imgaug as ia import imgaug.augmenters as iaa images np.zeros((2, 128, 128, 3), dtypenp.uint8) # two example images images[:, 64, 64, :] 255 bbs [ [ia.BoundingBox(x110.5, y115.5, x230.5, y250.5)], [ia.BoundingBox(x110.5, y120.5, x250.5, y250.5), ia.BoundingBox(x140.5, y175.5, x270.5, y2100.5)] ] seq iaa.Sequential([ iaa.AdditiveGaussianNoise(scale0.05*255), iaa.Affine(translate_px{x: (1, 5)}) ]) images_aug, bbs_aug seq(imagesimages, bounding_boxesbbs)多边形每个多边形由 3 个以上顶点定义import numpy as np import imgaug as ia import imgaug.augmenters as iaa images np.zeros((2, 128, 128, 3), dtypenp.uint8) # two example images images[:, 64, 64, :] 255 polygons [ [ia.Polygon([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])], [ia.Polygon([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0), (64.5, 128.0)])] ] seq iaa.Sequential([ iaa.AdditiveGaussianNoise(scale0.05*255), iaa.Affine(translate_px{x: (1, 5)}) ]) images_aug, polygons_aug seq(imagesimages, polygonspolygons)线串与多边形类似但不闭合、可自交、无内部面积适合车道线、骨骼等场景import numpy as np import imgaug as ia import imgaug.augmenters as iaa images np.zeros((2, 128, 128, 3), dtypenp.uint8) # two example images images[:, 64, 64, :] 255 ls [ [ia.LineString([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])], [ia.LineString([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0), (64.5, 128.0), (128.0, 0.0)])] ] seq iaa.Sequential([ iaa.AdditiveGaussianNoise(scale0.05*255), iaa.Affine(translate_px{x: (1, 5)}) ]) images_aug, ls_aug seq(imagesimages, line_stringsls)实战五热力图与分割图不同尺寸自动对齐热力图是取值 0.01.0 的稠密 float 数组常用于训练人脸关键点定位等模型分割图则为int32稠密数组。两者都可以与图像尺寸不同——README 示例中热力图/分割图是64x64而图像是128x128imgaug 会自动处理这种差异例如图像每侧裁剪 10 像素时热力图只裁剪一半。热力图import numpy as np import imgaug.augmenters as iaa # Standard scenario: You have N RGB-images and additionally 21 heatmaps per # image. You want to augment each image and its heatmaps identically. images np.random.randint(0, 255, (16, 128, 128, 3), dtypenp.uint8) heatmaps np.random.random(size(16, 64, 64, 1)).astype(np.float32) seq iaa.Sequential([ iaa.GaussianBlur((0, 3.0)), iaa.Affine(translate_px{x: (-40, 40)}), iaa.Crop(px(0, 10)) ]) images_aug, heatmaps_aug seq(imagesimages, heatmapsheatmaps)分割图缩放等操作会自动使用最近邻插值避免产生非整数类别import numpy as np import imgaug.augmenters as iaa # Standard scenario: You have N16 RGB-images and additionally one segmentation # map per image. You want to augment each image and its heatmaps identically. images np.random.randint(0, 255, (16, 128, 128, 3), dtypenp.uint8) segmaps np.random.randint(0, 10, size(16, 64, 64, 1), dtypenp.int32) seq iaa.Sequential([ iaa.GaussianBlur((0, 3.0)), iaa.Affine(translate_px{x: (-40, 40)}), iaa.Crop(px(0, 10)) ]) images_aug, segmaps_aug seq(imagesimages, segmentation_mapssegmaps)多列数据如图像 边界框在同 batch 内使用相同采样值的对齐机制在 meta.py 的_augment_batch_中有明确实现当 batch 含多列数据时会自动进入该 batch 内的确定性采样模式避免各数据类型拿到不同的随机样本。实战六结果可视化可视化增强后的图像使用show_grid一次性铺开rows × cols个增强结果README 示例生成 8×8 网格对两张输入图施加相同增强import numpy as np import imgaug.augmenters as iaa images np.random.randint(0, 255, (16, 128, 128, 3), dtypenp.uint8) seq iaa.Sequential([iaa.Fliplr(0.5), iaa.GaussianBlur((0, 3.0))]) # Show an image with 8*8 augmented versions of image 0 and 8*8 augmented # versions of image 1. Identical augmentations will be applied to # image 0 and 1. seq.show_grid([images[0], images[1]], cols8, rows8)可视化非图像数据README 的辅助函数示例涵盖关键点、边界框、多边形、热力图的draw_on_imageimport numpy as np import imgaug as ia image np.zeros((64, 64, 3), dtypenp.uint8) # points kps [ia.Keypoint(x10.5, y20.5), ia.Keypoint(x60.5, y60.5)] kpsoi ia.KeypointsOnImage(kps, shapeimage.shape) image_with_kps kpsoi.draw_on_image(image, size7, color(0, 0, 255)) ia.imshow(image_with_kps) # bbs bbsoi ia.BoundingBoxesOnImage([ ia.BoundingBox(x110.5, y120.5, x250.5, y230.5) ], shapeimage.shape) image_with_bbs bbsoi.draw_on_image(image) image_with_bbs ia.BoundingBox( x150.5, y110.5, x2100.5, y216.5 ).draw_on_image(image_with_bbs, color(255, 0, 0), size3) ia.imshow(image_with_bbs) # polygons psoi ia.PolygonsOnImage([ ia.Polygon([(10.5, 20.5), (50.5, 30.5), (10.5, 50.5)]) ], shapeimage.shape) image_with_polys psoi.draw_on_image( image, alpha_points0, alpha_face0.5, color_lines(255, 0, 0)) ia.imshow(image_with_polys) # heatmaps hms ia.HeatmapsOnImage(np.random.random(size(32, 32, 1)).astype(np.float32), shapeimage.shape) image_with_hms hms.draw_on_image(image) ia.imshow(image_with_hms)LineStrings 与分割图支持与此类似的方法。这些辅助函数对调试数据增强前后的标注对齐非常有用。实战七一次性使用增强器虽然接口设计鼓励复用增强器实例但也可以即用即弃实例化开销通常可忽略from imgaug import augmenters as iaa import numpy as np images np.random.randint(0, 255, (16, 128, 128, 3), dtypenp.uint8) # always horizontally flip each input image images_aug iaa.Fliplr(1.0)(imagesimages) # vertically flip each input image with 90% probability images_aug iaa.Flipud(0.9)(imagesimages) # blur 50% of all images using a gaussian kernel with a sigma of 3.0 images_aug iaa.Sometimes(0.5, iaa.GaussianBlur(3.0))(imagesimages)实战八多核后台批量增强当数据量很大时可以把增强放到后台进程执行。核心 API 是augment_batches(batches, backgroundTrue)其中batches为 imgaug.augmentables.batches.UnnormalizedBatch 或Batch的列表/生成器。README 示例用同一张图构造 10 个 batch、每 batch 32 张图并用draw_grid展示结果import skimage.data import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmentables.batches import UnnormalizedBatch # Number of batches and batch size for this example nb_batches 10 batch_size 32 # Example augmentation sequence to run in the background augseq iaa.Sequential([ iaa.Fliplr(0.5), iaa.CoarseDropout(p0.1, size_percent0.1) ]) # For simplicity, we use the same image here many times astronaut skimage.data.astronaut() astronaut ia.imresize_single_image(astronaut, (64, 64)) # Make batches out of the example image (here: 10 batches, each 32 times # the example image) batches [] for _ in range(nb_batches): batches.append(UnnormalizedBatch(images[astronaut] * batch_size)) # Show the augmented images. # Note that augment_batches() returns a generator. for images_aug in augseq.augment_batches(batches, backgroundTrue): ia.imshow(ia.draw_grid(images_aug.images_aug, cols8))从 meta.py 的augment_batches实现可以看到几个关键行为该方法**产出yield**增强后的 batch而不是一次性返回完整列表更适合训练循环的流式消费backgroundTrue时会基于imgaug.multicore.Pool启动后台进程池默认使用所有可用逻辑 CPU 核输出缓冲区大小为C*10C为逻辑核数多核模式按batch 粒度分发数据不会把单个 batch 内的数据拆分到不同核因此对单 batch 使用backgroundTrue没有意义且后台模式下hooks不可用涉及函数序列化若需要更精细的控制设置种子、指定 CPU 核数、限制内存README 指向Augmenter.pool()与imgaug.multicore.Pool对应实现见 imgaug/multicore.py。实战九概率分布作为参数多数增强器的参数支持两种快捷写法元组(a, b)表示uniform(a, b)均匀分布列表[a, b, c]表示从给定集合中随机挑一个。需要更复杂分布高斯、截断高斯、泊松等时可用 imgaug/parameters.py 中的随机参数import numpy as np from imgaug import augmenters as iaa from imgaug import parameters as iap images np.random.randint(0, 255, (16, 128, 128, 3), dtypenp.uint8) # Blur by a value sigma which is sampled from a uniform distribution # of range 10.1 x 13.0. # The convenience shortcut for this is: GaussianBlur((10.1, 13.0)) blurer iaa.GaussianBlur(10 iap.Uniform(0.1, 3.0)) images_aug blurer(imagesimages) # Blur by a value sigma which is sampled from a gaussian distribution # N(1.0, 0.1), i.e. sample a value that is usually around 1.0. # Clip the resulting value so that it never gets below 0.1 or above 3.0. blurer iaa.GaussianBlur(iap.Clip(iap.Normal(1.0, 0.1), 0.1, 3.0)) images_aug blurer(imagesimages)库中还提供了截断高斯分布、泊松分布、Beta 分布等更多概率分布README Features 章节中的ABS(N(0, 20.0))*(1B(1.0, 1.0))即此类组合的典型示例。实战十按通道增强WithChannels某些场景下只想增强图像的指定通道如 R、G 通道。WithChannels正是为此设计import numpy as np import imgaug.augmenters as iaa # fake RGB images images np.random.randint(0, 255, (16, 128, 128, 3), dtypenp.uint8) # add a random value from the range (-30, 30) to the first two channels of # input images (e.g. to the R and G channels) aug iaa.WithChannels( channels[0, 1], childreniaa.Add((-30, 30)) ) images_aug aug(imagesimages)源码层面meta.py 的WithChannels实现会先把图像缩减到指定通道在子增强器上完成处理后再把未增强的其他通道替换回原值——这正是 README Features 中Easy to apply augmentations only to some images/channels的具体落地。如何在研究中使用与引用若该库对研究有帮助README 提供了 BibTeX 引用条目作者为 Alexander Jung 等贡献者年 2020。此外仓库根目录 CHANGELOG.md 与 changelogs/ 目录记录了自 0.2.8 以来的全部变更细节新增、修改、废弃、修复、重构升级版本前建议查阅对应版本的变更文档以确认接口变化例如 0.4.0 中random_state/deterministic参数已标记为废弃推荐改用seed与to_deterministic()。综上从简单的三行增强序列到覆盖十几个模块的重度流水线再到多核后台批量增强imgaug 0.4.0 提供了一整套开箱即用的图像与标注数据对齐增强方案。读者可以以本文的示例为起点结合 test/ 下的测试用例与 checks/ 下的可运行脚本进一步验证每个增强器在不同参数下的实际表现。赞分享数据增强计算机视觉图像处理机器学习【免费下载链接】imgaugImage augmentation for machine learning experiments.项目地址https://gitcode.com/gh_mirrors/im/imgaug点击查看免费下载相关推荐图像增强库ImgAug安装与使用指南图像增强库ImgAug安装与使用指南 一、项目介绍 ImgAug , 即 Image Augmentation , 是一个专用于图像增强的Python库在深度数据增强计算机视觉图像处理机器学习图像增强神器 imgaug机器学习数据增强的终极指南 图像增强神器 imgaug机器学习数据增强的终极指南 在机器学习特别是计算机视觉项目中数据增强是提升模型泛化能力的关键技术。imgaug 是一个功能强数据增强计算机视觉图像处理机器学习终极图像增强指南用imgaug实现虚实融合的增强现实技术 终极图像增强指南用imgaug实现虚实融合的增强现实技术 在机器学习的世界里数据是燃料而 图像增强技术 就是让燃料更高效、更丰富的秘密武器今天我要数据增强计算机视觉图像处理机器学习上一篇spin.js中的社区管理维护健康的开源生态下一篇YuE2常见问题清单8个ComfyUI音乐生成报错原因与快速解决办法创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
延伸阅读

更多相关文章

2026/10/3 8:20:17

SQL Server 2008备份还原到2012:兼容级别与路径规划实操指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/3 9:15:22

企业级大模型落地:数据质量与治理才是规模化的胜负手

这两年聊企业级AI落地,几乎绕不开大模型。但真正在企业里趟过一遍的朋友,心里都有一本账:大模型选型、微调、部署、Agent编排,表面看的全是算法和算力,最后拼的却是数据。训练数据、微调数据、知识库数据、用户反馈数据…

2026/10/3 9:15:22

基于动态贝叶斯网络的配电网在线故障概率诊断系统

简介:本资源是一套基于贝叶斯概率推理的电力系统故障诊断仿真与实现方案,面向电气工程、智能电网方向的本科生、研究生及一线继保/自动化工程师,聚焦解决复杂电力系统中不确定性故障的识别、定位与决策支持问题。压缩包共32个文件&#xff08…

2026/10/3 9:15:22

LangGraph+FastAPI+Streamlit:从Demo到生产级AI Agent的完整改造指南

最近大半年,我花了大量时间把一个基于大模型的项目,从 Jupyter Notebook 里的演示 Demo,一步步改造成能够真正持续对外提供服务、接受真实用户请求的生产级 AI 助手。这个过程中最核心的技术组合,就是LangGraph FastAPI Streaml…

2026/10/3 9:15:22

时间复杂度深度解析:从大O复杂度到经典算法题优化实战

做了这么多年技术面试,我特别喜欢问同一个看似基础的问题:这段代码的时间复杂度是多少?大多数候选人能写出功能完整的程序,却说不清楚自己的代码为什么慢,更别提在大数据量下怎么选方案了。时间复杂度的概念谁都懂个大…

2026/10/3 9:15:22

RV1106 ISP与MIPI/LVDS配置实战:设备树调优与画质问题定位

做 RV1106 方案的第一个晚上,我盯着排线陷入沉思:sensor 供电正常、复位也拉完了,dmesg里死活不报 sensor 挂载,MIPI 时钟测出来却又是波形。后来翻了一整晚的资料,猜了无数种可能,最后发现根因既不在硬件上…

2026/10/3 9:10:21

语法分析器.cpp全解析:从Token流到AST与虚拟机指令生成

简介:编译原理课程中语法分析器环节的完整C实现代码,面向计算机专业学生与需要动手构建词法/语法分析模块的开发者。资源包仅含1个cpp文件,压缩后体积2KB,结构精简,便于直接阅读算法主流程,也适合作为课程实…

2026/10/2 8:16:46

东莞市品牌网站建设报价常见报错与解决

东莞品牌网站建设报价单背后:一份保姆级建站教程避坑实录 网站做好了没人访问,这大概是很多老板最头疼的事。花了大几万做的品牌站,上线后流量惨淡,比路边摊还冷清。别急着骂外包公司,很多“东莞品牌网站建设报价”里藏着不少猫腻,比如用模板站冒充定制…

2026/10/2 18:20:53

如何划分训练/验证集:Spirula Studio五种eval_mode策略详解

如何划分训练/验证集:Spirula Studio五种eval_mode策略详解 【免费下载链接】spirula-studio Cross-vendor 3D Gaussian Splatting trainer - video to splat to mesh, Vulkan or CUDA. 项目地址: https://gitcode.com/GitHub_Trending/sp/spirula-studio Sp…

2026/10/1 10:48:55

SEO怎么推广速查手册新手避坑实战指南

SEO怎么推广速查手册新手避坑实战指南 模板网站太丑不够用?别急着加滤镜,那是治标不治本。很多老板盯着后台流量掉得眼红,却还在纠结首页Banner的圆角是不是3像素。这就像穿着西装去挖土,姿势不对,努力白费。我整理这份 速查手册…

2026/10/3 0:04:31

国内大学生必备的AI写作辅助软件是哪款?

国内高校学生在论文写作过程中,越来越依赖AI辅助工具提升效率,主流方案以本土化全流程工具为核心,结合通用大模型与专业插件,覆盖选题构思、框架搭建、初稿撰写、查重降重、格式调整等关键环节,本文将深入解析当前主流…

2026/10/3 0:04:31

Codex接入Jev模型完整指南:配置方法、本地部署与踩坑排查

最近不少人在讨论 Codex 搭配 Jev 这套玩法,我一开始没太当回事,直到自己把 Jev 接进 Codex跑了几轮编码任务之后,才明白那些说“直接起飞”的人是怎么想的。Codex 作为工具本身已经够能打了,但模型固定、上下文策略固定&#xff…

2026/10/3 0:04:31

GitHub 热门: NVIDIA/Model-Optimizer

👋 Hi,我擅长 AI 大模型应用落地、意识解码与 AI 开发工具链 。 💡 创业路上,用技术换时间,一起把 AI 变成生产力 🚀 >GitHub 热门: NVIDIA/Model-Optimizer 凌晨两点,你刚把跑通了的 Qwen3.…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑