深さ学習訓練データpythonコード——データ拡張(三)


imgaug
http://imgaug.readthedocs.io/en/latest/index.html
インストール
に頼る
  • numpy
  • scipy
  • scikit-image (pip install -U + scikit-image)
  • six (pip install -U six)
  • OpenCV (i.e. cv2 must be available in python). The library is mainly tested in OpenCV 2, but seems to also work in OpenCV 3.
  • pip install git+https://github.com/aleju/imgaug

    または
    pip install imgaug

    前者はgithubの最新バージョン、後者はpypiバージョンをインストールします. 
     
    import imgaug as ia
    from imgaug import augmenters as iaa
    import numpy as np
    import cv2
    
    images = cv2.imread("./125573.png")
    
    images = np.expand_dims(images, axis=0)#N C H W
    
    
    plt.imshow(images[0])
    plt.show()
    seq = iaa.Sequential([
    #     iaa.Fliplr(0.5), # horizontal flips
    
    #     iaa.Crop(percent=(0, 0.1)), # random crops
    
        # Small gaussian blur with random sigma between 0 and 0.5.
        # But we only blur about 50% of all images.
        iaa.Sometimes(0.5,
            iaa.GaussianBlur(sigma=(0.25, 0.75))
        ),
        
        # Strengthen or weaken the contrast in each image.
    #     iaa.ContrastNormalization((0.75, 1.5)),
        
        # Add gaussian noise.
        # For 50% of all images, we sample the noise once per pixel.
        # For the other 50% of all images, we sample the noise per pixel AND
        # channel. This can change the color (not only brightness) of the
        # pixels.
        iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
        
        # Make some images brighter and some darker.
        # In 20% of all cases, we sample the multiplier once per channel,
        # which can end up changing the color of the images.
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        
        # Apply affine transformations to each image.
        # Scale/zoom them, translate/move them, rotate them and shear them.
        iaa.Affine(
            scale={"x": (0.9, 1.1), "y": (0.9, 1.1)},
            translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)},
            rotate=(-6, 6),
            shear=(-4, 4)
        )
    ], random_order=True) # apply augmenters in random order
    images_aug = seq.augment_images(image)