albumentationsのまとめ

データサイエンス




albumentationsのまとめ

albumentationsは、画像拡張(image augmentationのライブラリです。
albumentationsは、OpenCV

インストール

#pip
pip install albumentation
#anacondaの場合
conda install albumentation

使い方

import cv2
import matplotlib.pyplot as plt
import albumentations as albu

def get_augmentation():
    transform = [
        albu.HorizontalFlip(p=1),
        albu.VerticalFlip(p=1),
    ]
    return albu.Compose(transform)

#画像の読み込み・変換・表示
img = cv2.imread("image_path")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
trans = get_augmentation()

img = trans(image=img)['image']

plt.imshow(img);

機能

GitHub-βshor|albumentations.ipynb

コンパイル

Compose

一連の画像操作をまとめる

OneOf

1つの画像操作だけをまとめる

Float変換

ToFloat

Normalize

Normalize

リサイズ

Resize

反転・回転

* HorizontalFlip
* Verticalflip
* ShiftScaleRotate

切り取り(クロップ)

* Crop
* RandomCrop
* CenterCrop
* RandomSizedCrop
* RandomCrop

* HueSaturationValue
* Padding
* ElasticTransform

ノイズ付与

* GaussNoise

ぼかし

* Blur
* MotionBlur
* MedianBlur

風景加工

* RandomRain
* RandomSnow
* RandomSunFlare
* RandomShadow
* RandomFog

Mask

引数maskに渡す

Bounding Box

(How to use albumentations for detection tasks)
https://github.com/albumentations-team/albumentations/blob/master/notebooks/example_bboxes.ipynb
* BboxParams

Pytorch ToTensor

from albumentations.pytorch import ToTensor

Pytorch

class Dataset(Dataset):
    
    def __init__(self, file_paths, labels, transform=None):
        self.file_paths = file_paths
        self.labels = labels
        self.transform = transform
        
    def __len__(self):
        return len(self.file_paths)

    def __getitem__(self, idx):
        label = self.labels[idx]
        file_path = self.file_paths[idx]

        image = cv2.imread(file_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        
        if self.transform:
            augmented = self.transform(image=image)
            image = augmented['image']
        return image, label


albumentations_transform = Compose([
    Resize(256, 256), 
    RandomCrop(224, 224),
    HorizontalFlip(),
    Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225],
    ),
    ToTensor()
])

参考

  1. albumentations
  2. albumentations/tree/master/notebooks
  3. migrating_from_torchvision_to_albumentations.ipynb
  4. example_weather_transforms.ipynb
  5. https://qiita.com/Takayoshi_Makabe/items/79c8a5ba692aa94043f7
  6. http://ni4muraano.hatenablog.com/entry/2018/11/21/000727
タイトルとURLをコピーしました