Image augmentation for classification¶

参考文章:

We can divide the process of image augmentation into four steps:

1.导入albumentations和 a library库以从磁盘读取图像(例如,OpenCV)。

2.Define an augmentation pipeline 管道.

3.Read images from the disk.

4.Pass images to the augmentation pipeline and receive augmented images.

Step 1. Import the required libraries.

  • Import Albumentations
import albumentations as A
  • Import a library to read images from the disk. In this example, we will use OpenCV. It is an open-source computer vision library that supports many image formats. Albumentations has OpenCV as a dependency, so you already have OpenCV installed.
import cv2

Step 2. Define an augmentation pipeline.

To define an augmentation pipeline, you need to create an instance实例 of the Compose class. As an argument to the Compose class, you need to pass a list of augmentations you want to apply. A call to Compose will return返回 a transform function转换函数 that will perform image augmentation执行图像增强.

Let's look at an example:

transform = A.Compose([A.RandomCrop(width=256, height=256),A.HorizontalFlip(p=0.5),A.RandomBrightnessContrast(p=0.2),
])

To create an augmentation, you create an instance of the required augmentation class and pass augmentation parameters to it. A.RandomCrop receives two parameters, height and widthA.RandomCrop(width=256, height=256) means that A.RandomCrop will take an input image, extract a random patch with size 256 by 256 pixels from it and then pass the result to the next augmentation in the pipeline (in this case to A.HorizontalFlip).

A.HorizontalFlip in this example has one parameter named pp is a special parameter that is supported by almost all augmentations扩充. It controls the probability of applying the augmentation. p=0.5 means that with a probability of 50%, the transform will flip the image horizontally, and with a probability of 50%, the transform won't modify the input image.

A.RandomBrighntessContrast in the example also has one parameter, p. With a probability of 20%, this augmentation will change the brightness 亮度和对比度 and contrast of the image received from A.HorizontalFlip. And with a probability of 80%, it will keep the received image unchanged.

 A visualized version of the augmentation pipeline. You pass an image to it, the image goes through all transformations, and then you receive an augmented image from the pipeline.

Step 3. Read images from the disk.¶

 To pass an image to the augmentation pipeline, you need to read it from the disk. The pipeline expects to receive an image in the form of a NumPy array. If it is a color image, it should have three channels in the following order: Red, Green, Blue (so a regular RGB image).

To read images from the disk, you can use OpenCV - a popular library for image processing. It supports a lot of input formats and is installed along with Albumentations 并与相册一起安装 since Albumentations utilizes that library under the hood for a lot of augmentations.因为相册在后台使用该库进行大量扩充

To import OpenCV

import cv2

To read an image with OpenCV

image = cv2.imread("/path/to/image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

Note the usage of cv2.cvtColor. For historical reasons, OpenCV reads an image in BGR format (so color channels of the image have the following order 以下顺序: Blue, Green, Red). Albumentations uses the most common and popular RGB image format. So when using OpenCV, we need to convert the image format to RGB explicitly.

Besides OpenCV, you can use other image processing libraries.

Pillow¶

Pillow is a popular Python image processing library.

  • Install Pillow
    pip install pillow
  • Import Pillow and NumPy (we need NumPy to convert a Pillow image to a NumPy array. NumPy is already installed along with Albumentations).
from PIL import Image
import numpy as np
  • Read an image with Pillow and convert it to a NumPy array.
    pillow_image = Image.open("image.jpg")
    image = np.array(pillow_image)

Step 4. Pass images to the augmentation pipeline and receive augmented images. 

To pass an image to the augmentation pipeline you need to call the transform function created by a call to A.Compose at Step 2. In the image argument to that function, you need to pass an image that you want to augment.

transformed = transform(image=image)

transform will return a dictionary with a single key image. Value at that key will contain an augmented image一个带有单个关键字图像的字典.

transformed_image = transformed["image"]

To augment the next image, you need to call transform again and pass a new image as the image argument:

another_transformed_image = transform(image=another_image)["image"]

如果您多次调用同一个输入图像进行转换,每次都会得到不同的输出图像。

transform = A.Compose([A.RandomBrightnessContrast(brightness_limit=1, contrast_limit=1, p=1.0),
])
transformed_image_1 = transform(image=image)['image']
transformed_image_2 = transform(image=image)['image']
transformed_image_3 = transform(image=image)['image']

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/255468.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

EUREKA: HUMAN-LEVEL REWARD DESIGN VIACODING LARGE LANGUAGE MODELS

目录 一、论文速读 1.1 摘要 1.2 论文概要总结 相关工作 主要贡献 论文主要方法 实验数据 未来研究方向 二、论文精度 2.1 论文试图解决什么问题? 2.2 论文中提到的解决方案之关键是什么? 2.3 用于定量评估的数据集是什么?代码有…

各种滤波算法的比较(GF、KF、EKF、UKF、PF)

目录 一、前言 二、滤波算法介绍 1、GF(高斯滤波) 2、KF(卡尔曼滤波) 3、EKF(可扩展卡尔曼滤波) 4、UKF(无迹卡尔曼滤波) 5、PF(粒子滤波) 三、不同滤…

windows MYSQL下载和自定路径安装,以及解决中文乱码问题。

文章讲的很详细,请耐心往下看。 一、mysql下载 下载网址:https://www.mysql.com/downloads/ 表示不登录,直接下载。 以上就把安装包下载完了。下载是8.0.35版本。 二、接下来看怎么安装 1.双击安装包,进行安装。 注意&#x…

零容忍泄露:掌握上海迅软DLP企业加密系统的数据安全秘诀

由于企事业单位内部数据呈暴发式增长,且数据类型日益繁杂,企业对数据的安全管控难度也在不断增大。这其中包括研发的设计文档、代码、图纸,以及业务运营的详细收支情况、招商投标的详细信息等,这些涉及到企业核心数据资产的信息需…

情怀阿拉德【弑神大陆】linux服务端+双端+GM后台+详细搭建教程

搭建资源下载:情怀阿拉德【弑神大陆】linux服务端双端GM后台详细搭建教程-海盗空间

idea开发环境配置

idea重新安装后,配置的东西还挺多的,这里简单记录一下。 1、基础配置 1.1、主题、背景、主题字体大小 1.2、默认字体设置 控制台默认编码设置: 全局文件默认编码设置: 2、构建、编译、部署配置 说明:本地装了JD…

MPU6050姿态解算2-欧拉角旋转矩阵 -->相关推导

IMU姿态解算 IMU,即惯性测量单元,一般包含三轴陀螺仪与三轴加速度计。之前的文章 MPU6050姿态解算1-DMP方式已将对MPU6050这款IMU作了简单的介绍,并通过其内部的DMP处理单元直接得到姿态解算的四元数结果。本篇将通过软件解算的方式&#xff…

Vector Quantized Diffusion Model for Text-to-Image Synthesis

Vector Quantized Diffusion Model for Text-to-Image Synthesis Shuyang Gu, University of Science and Technology of China, Microsoft, CVPR2022, Cited: 340, Code, Paper 1. 前言 我们提出了用于文本到图像生成的矢量量化扩散(Vector Quantized Diffusion Model&…

由于找不到xinput1_3.dll,无法继续执行代码的多种解决方法指南,xinput1_3.dll文件修复

当玩家或用户在启动某些游戏和应用程序时,可能会遭遇到一个系统错误提示:“由于找不到xinput1_3.dll,无法继续执行代码l”。这种情况通常指出系统中DirectX组件存在问题。以下我们将介绍几种常用的解决方法,并提供详细的操作步骤。 一.找不到…

ROS 动态坐标变换

在ROS 中,坐标变换是一个重要的概念,因为它允许系统中的不同节点和模块以统一的方式描述物体的位置和方向。 动态坐标变换指的是当机器人或其环境中物体的位姿(位置和姿态)发生变化时,能够实时更新这些信息的过程。 …

【React Hooks】useReducer()

useReducer 的三个参数是可选的,默认就是initialState,如果在调用的时候传递第三个参数那么他就会改变为你传递的参数,实际开发不建议这样写。会增加代码的不可读性。 使用方法: 必须将 useReducer 的第一个参数(函数…

分享一个基础面试题---手写call

分享一个基础面试题---手写call 手写call笔记第一步第二步第三步 手写call笔记 call():在使用一个指定的this值和若干个指定的参数值的前提下调用某个函数或方法。 let foo {value:1 }; function bar(){console.log(this.value); } bar.call(foo);//1注意两点&…