Data augmentation: 利用python进行图像扩建
最近在做开集问题时正好有扩建数据集的需求,把一些方法分享给大家~
关于数据扩充
本文代码将从现有文件夹中选择一些随机图像并进行转换,例如添加噪点、旋转、翻转等等。
Step 1: 图像转换(transformations)
现有的python库例如 OpenCV 或 Pillow 都具备有图像转换的功能。这里我们将在scikit-image 上进行实现。
现在定义一些数据扩建所需要的 transformation functions。
import random
from scipy import ndarray
import skimage as sk
from skimage import transform
from skimage import util
def random_rotation(image_array: ndarray):
# pick a random degree of rotation between 25% on the left and 25% on the right
random_degree = random.uniform(-25, 25)
return sk.transform.rotate(image_array, random_degree)
def random_noise(image_array: ndarray):
# 给图像添加随机噪声
return sk.util.random_noise(image_array)
def horizontal_flip(image_array: ndarray):
# horizontal flip 不需要skimage
return image_array[:, ::-1]
到现在为止我们已经有了三种图像转变方式:随机旋转、随机噪声以及水平翻转。
*Note:*使用scipy.ndarray 来呈现图像。
Step 2: 列出文件夹中所有图片并且read
import random
import os
# my folder path containing some images
folder_path = '/Users/mac/Desktop/DATA/plankton41/train'
# the number of file to generate
num_files_desired = 1000
# loop on all files of the folder and build a list of files paths
images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
num_generated_files = 0
while num_generated_files <= num_files_desired:
# random image from the folder
image_path = random.choice(images)
# read image as an two dimensional array of pixels
image_to_transform = sk.io.imread(image_path)
Step 3: Image transformations
# dictionary of the transformations functions we defined earlier
available_transformations = {
'rotate': random_rotation,
'noise': random_noise,
'horizontal_flip': horizontal_flip
}
# random num of transformations to apply
num_transformations_to_apply = random.randint(1, len(available_transformations))
num_transformations = 0
transformed_image = None
while num_transformations <= num_transformations_to_apply:
# choose a random transformation to apply for a single image
key = random.choice(list(available_transformations))
transformed_image = available_transformations[key](image_to_transform)
num_transformations += 1
这里我们将单个图像的变换数量和要应用的变换类型进行随机选取。然后,我们只调用定义在转换字典中的函数(倒数第二行)。
Step 4: 存储新图像
# define a name for our new file
new_file_path = '%s/augmented_image_%s.jpg' % (folder_path, num_generated_files)
# write image to the disk
sk.io.imsave(new_file_path, transformed_image)
Conclusion
现在,你可以通过data augmentation 生成1000个新图像。当然,还可以添加一些额外的变换,对于不同变换发生的可能性也可以自己调整。
希望本文能对你有所帮助~