Exemplo n.º 1
0
def aug_method():
    # 增强方法1: 图片变形
    # 1) 水平翻转: 最常用!! 对卷积神经网络而言, 左右互换(水平翻转)差异很大
    aug = image.HorizontalFlipAug(0.5)
    apply(img, aug)
    plt.gcf().suptitle('HorizontialFlipAug')
    plt.show()

    # 2) 随机裁剪: 卷积神经网络对位置敏感, 虽然Pool池化可以减小位置敏感, 但仍不不够
    # 2.1) 裁剪指定(width x height) 的图片
    aug = image.RandomCropAug((200, 200))
    apply(img, aug)
    plt.show()
    # 2.2) 裁剪指定'长宽比'范围大小且不小于最小值的随机图片, 并resize为指定(width x height)大小
    aug = image.RandomSizedCropAug(size=(100, 100),
                                   min_area=.1,
                                   ratio=(0.5, 2.0))
    apply(img, aug)
    plt.show()

    # 增强方法2: 颜色变化
    # 1) 亮度变化:
    aug = image.BrightnessJitterAug(
        brightness=0.5
    )  # brightness : The brightness jitter ratio range, [0, 1]
    apply(img, aug)
    plt.show()

    # 2) 色调变化:
    aug = image.HueJitterAug(hue=0.5)
    apply(img, aug)
    plt.show()
Exemplo n.º 2
0
def augment_data(imags, label):
    datas, labels = [], []
    n = imags.shape[0]
    for k in range(n):
        imag = imags[k].reshape(shape=(1, imags[k].shape[0], imags[k].shape[1],
                                       imags[k].shape[2]))
        for i in range(4):
            trans_band = transform(
                imag, image.HorizontalFlipAug(.5)).astype('float32')
            datas.append(img_norm(trans_band))
            labels.append(label[k].asscalar())

        for i in range(9):
            trans_band = transform(
                imag, image.RandomSizedCropAug((75, 75), .75,
                                               (.8, 1.2))).astype('float32')
            datas.append(img_norm(trans_band))
            labels.append(label[k].asscalar())
        # brightness augmenter
        for i in range(9):
            trans_band = transform(
                imag, image.BrightnessJitterAug(.1)).astype('float32')
            datas.append(img_norm(trans_band))
            labels.append(label[k].asscalar())
        # random crop augmenter
        for i in range(9):
            trans_band = resize(transform(imag, image.RandomCropAug((50, 50))),
                                75).astype('float32')
            datas.append(img_norm(trans_band))
            labels.append(label[k].asscalar())
        # center crop augmenter
        trans_band = resize(transform(imag, image.CenterCropAug((50, 50))),
                            75).astype('float32')
        datas.append(img_norm(trans_band))
        labels.append(label[k].asscalar())
    ds = nd.concat(*datas, dim=0)
    return ds, labels
Exemplo n.º 3
0
pretrained_net = mobilenet1_0(pretrained=True)
print(pretrained_net)

print("classes: " + str(len(training_map)))
net = mobilenet1_0(classes=len(training_map))

from mxnet import init
net.features = pretrained_net.features
net.output.initialize(init.Xavier())

from mxnet.image import color_normalize
from mxnet import image

train_augs = [
    image.HorizontalFlipAug(0.5),
    image.BrightnessJitterAug(.3),
    image.HueJitterAug(.1)
]
test_augs = [image.ResizeAug(224), image.CenterCropAug((224, 224))]


def transform(data, label, augs):
    data = data.astype('float32')
    for aug in augs:
        data = aug(data)
    data = nd.transpose(data, (2, 0, 1))
    return data, nd.array([label]).asscalar().astype('float32')


from mxnet.gluon.data.vision import ImageRecordDataset
Exemplo n.º 4
0
    X = [aug(img.astype('float32')) for _ in range(n * n)]
    Y = nd.stack(*X).clip(0, 255) / 255
    utils.show_images(Y, n, n, figsize=(256, 256))


# 水平翻转
aug = image.HorizontalFlipAug(.5)
apply(img, aug)

# 随机裁剪一个块, 256 * 256
aug = image.RandomCropAug([256, 256])
apply(img, aug)

# 颜色变化
# 随机将亮度增加或减少一个量
aug = image.BrightnessJitterAug(.5)
apply(img, aug)

# 随机色调变化
aug = image.HueJitterAug(.5)
apply(img, aug)


def apply_aug_list(img, augs):
    for aug in augs:
        img = aug(img)
    return img


train_augs = [image.HorizontalFlipAug(.5), image.RandomCropAug((28, 28))]
Exemplo n.º 5
0
from mxnet import image
from mxnet import init
from mxnet import gluon
import time
from mxnet import autograd
from mxnet import nd
from mxnet.gluon.data.vision import ImageRecordDataset


# Global constants ----
ctx = mx.cpu()

train_augs = [
    image.ResizeAug(224),
    image.HorizontalFlipAug(0.5),  # flip the image horizontally
    image.BrightnessJitterAug(.3), # randomly change the brightness
    image.HueJitterAug(.1)         # randomly change hue
]
test_augs = [
    image.ResizeAug(224)
]


# Functions ----
def transform(data, label, augs):
    # applies augmentations to img data
    data = data.astype('float32')
    for aug in augs:
        data = aug(data)
    data = nd.transpose(data, (2,0,1))
    return data, nd.array([label]).asscalar().astype('float32')
Exemplo n.º 6
0
            for i in range(nrows):
                for j in range(ncols):
                    figs[i][j].imshow(imgs[i * ncols + j].asnumpy())
                    figs[i][j].axes.get_xaxis().set_visible(False)
                    figs[i][j].axes.get_yaxis().set_visible(False)
            plt.show()

        self.__X = [
            aug(self.__img.astype("float32"))
            for _ in range(self.__n * self.__n)
        ]
        self.__Y = nd.stack(*self.__X).clip(0, 255) / 255
        show_images(self.__Y, self.__n, self.__n, figsize=(8, 8))


if __name__ == "__main__":
    ia = IamgeAugment(input_path="cat.jpg", n=3)
    # 变形
    # 50% 几率水平翻转图片
    ia.apply(aug=image.HorizontalFlipAug(.5))
    # 剪裁
    # 随机从原图片剪裁出 200×200 的一部分 , 可以图片先变大在剪小
    ia.apply(aug=image.RandomCropAug([200, 200]))
    # 随机从图片中剪裁一个区域要求是剪裁面积占原图片 >= 0.1 且剪裁的区域长/宽在 0.5~2 之间后将剪裁后的图片放缩到 200×200
    ia.apply(aug=image.RandomSizedCropAug((200, 200), .1, (.5, 2)))

    # 色调
    # 随机将亮度增加或者减小在 0-50% 间的一个量
    ia.apply(aug=image.BrightnessJitterAug(.5))
    # 随机色调变化
    ia.apply(aug=image.HueJitterAug(.5))