pytorch数据增强分析

目录​​​​​​​

亮度、饱和度、色相增强

RandomCrop,必须小于原图,如果padding,可以等于原图

RandomResizedCrop


亮度、饱和度、色相增强


关于三者的介绍:https://blog.csdn.net/u011608180/article/details/86525766
先将opencv读取的图片转为PIL格式:
img = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
然后使用torchvision进行增广:
color_aug = torchvision.transforms.ColorJitter(brightness=0, contrast=0.0, saturation=[0.7,0.8], hue=0)
注意这里参数的含义,brightness为亮度,取值相当于原图的多少倍,如1.2,即更亮,反之0.5则变暗,可以通过[0.5,1.2]数组控制范围。
saturation饱和度,同理。
 

RandomCrop,必须小于原图,如果padding,可以等于原图

padding,相当于原图先扩黑边,然后再裁剪。

代码实例:

import cv2

import numpy as np
from torchvision import transforms

trans = transforms.Compose([transforms.ToTensor(),
                            transforms.RandomCrop(128)])

crop_trans = transforms.Compose([transforms.ToPILImage(),
                            transforms.RandomCrop(192,padding=8)])

  
img_o=cv2.imread(r'D:\qinlan3_192.jpg')


for i in range(10):

    img=crop_trans(img_o)

    img= np.array(img)

    cv2.imshow("asdfasdf'",img)
    cv2.waitKey()

RandomResizedCrop

可以和原图一样大,scale是缩放比例

import cv2

import numpy as np
from torchvision import transforms

trans = transforms.Compose([transforms.ToTensor(),
                            transforms.RandomCrop(128)])

crop_trans = transforms.Compose([transforms.ToPILImage(),
                            transforms.RandomCrop(192,padding=8)])

crop_trans = transforms.Compose([transforms.ToPILImage(),
                            transforms.RandomResizedCrop((192,192),scale=(0.6,1.0),ratio=(1.0,1.0),interpolation=3 )])

img_o=cv2.imread(r'D:\qinlan3_192.jpg')
 

for i in range(10):

    img=crop_trans(img_o)

    img= np.array(img)

    cv2.imshow("asdfasdf'",img)
    cv2.waitKey()

你可能感兴趣的:(python基础,pytorch,计算机视觉,深度学习)