conv-neural-network - 使用 Python 进行数据增强
问题描述
我目前正在从事一个与 CNN 相关的项目,我是该特定领域的新手。我有一组图像,其中包含 500 张关于织物缺陷的图像。如何将图像数量增加到 2000 个?我可以在此使用的任何库?
解决方案
图像增强的首选库是imgaug。
该文档是自我解释的,但这里是一个示例:
import numpy as np
from imgaug import augmenters as iaa
from PIL import Image
# load image and convert to matrix
image = np.array(Image.open("<path to image>"))
# convert image to matrix
# image must passed into a list because you can also put a list of multiple images into the augmenter, but for this demonstration we will only take one.
image = [image]
# all these augmentation techniques will applied with a certain probability
augmenter = iaa.Sequential([
iaa.Fliplr(0.5), # horizontal flips
iaa.Crop(percent=(0, 0.1)), # random crops
iaa.Sometimes(
0.5,
iaa.GaussianBlur(sigma=(0, 0.5))
),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
], random_order=True) # apply augmenters in random order
augmented_image = augmenter(images=image)
augmented_image
现在是一个列表,其中包含一个原始图像的增强图像。既然您说要从 500 个图像中创建 2000 个,您可以执行以下操作:您将每个图像增强 4 次,即像这样:
total_images = []
for image_path in image_paths:
image = Image.load(image_path)
# create a list with for times the same image
images = [image for i in range(4)]
# pass it into the augmenter and get 4 different augmentations
augmented_images = augmenter(images=images)
# add all images to a list or save it otherwise
total_images += augmented_images
推荐阅读
- mongodb - MongoDB:副本集和循环查询分布?
- sql - 使用 PostgreSQL \COPY
- python - 将表拆分为多个数据框
- java - 当我使用 Camera2Api 拍照时,Android 图像被旋转
- mongodb - Docker 上的 MongoDB 错误:将集合 config.transactions 标记为集合版本:
- spring-boot - 使用 WebClient 设置超时
- javascript - 在 HTML 中动态添加 js 脚本标记元素 - 幕后发生了什么?
- c# - 使用实体框架的外键问题
- javascript - Antd 选择搜索框不渲染匹配项
- ms-access - Ms-Access 2016 在 DAO.Recordset OpenRecordset(