conda activate conda环境 # 不使用conda可以忽略
pip install albumentations
# 上面未成功
pip install -U git+https://github.com/albu/albumentations
import albumentations as A
# 展示图片
def show(image1,image2):
plt.figure()
plt.subplot(121)
plt.imshow(image1)
plt.subplot(122)
plt.imshow(image2)
plt.show()
image1 = A.Resize(128, 128, p=1.0)(image=image)['image']
image2 = A.RandomCrop(128, 128, p=1.)(image=image)['image']
show(image1,image2)
image1 = A.VerticalFlip(p=1.0)(image=image)['image']
image2 = A.HorizontalFlip(p=1.)(image=image)['image']
show(image1, image2)
image1 = A.CLAHE(p=1.0)(image=image)['image']
image2 = A.Sharpen(p=1.)(image=image)['image']
show(image1, image2)
image1 = A.Blur(blur_limit=4,p=1.0)(image=image)['image']
image2 = A.MotionBlur(blur_limit=4,p=1.)(image=image)['image']
show(image1, image2)
image1 = A.ColorJitter(p=1.0)(image=image)['image']
image2 = A.HueSaturationValue(p=1.)(image=image)['image']
show(image1, image2)
normal_transforms = A.Compose([
A.Resize(width=256 // 2, height=256 // 2, p=1.0),
A.HorizontalFlip(p=0.5),
A.VerticalFlip(),
# 对比度受限直方图均衡
# (Contrast Limited Adaptive Histogram Equalization)
A.CLAHE(),
# 随机旋转 90°
A.RandomRotate90(),
# 转置
A.Transpose(),
# 随机仿射变换
# A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.50, rotate_limit=45, p=.75),
A.OneOf([
# 模糊
A.Blur(blur_limit=3),
# 光学畸变
A.OpticalDistortion(),
# 网格畸变
A.GridDistortion(),
# 随机改变图片的 HUE、饱和度和值
A.HueSaturationValue()
],p=1.0)
], p=1.0)
arg = normal_transforms(image=image,mask=mask)
image = arg['image']
mask = arg['mask']
show(image,mask)
import torchvision.transforms as T
arg = normal_transforms(image=image,mask=mask)
image = arg['image']
mask = arg['mask']
img_tensor = T.ToTensor()(image)
print(type(image),type(img_tensor))
<class 'numpy.ndarray'> <class 'torch.Tensor'>
官方文档:链接
Github: https://github.com/albumentations-team/albumentations