コード例 #1
0
ファイル: utils.py プロジェクト: MIPT-Oulu/Collagen
def init_mnist_cifar_transforms(n_channels=1, stage='train'):
    if n_channels == 1:
        norm_mean_std = Normalize((0.1307, ), (0.3081, ))
    elif n_channels == 3:
        norm_mean_std = Normalize((0.4914, 0.4822, 0.4465),
                                  (0.247, 0.243, 0.261))
    else:
        raise ValueError("Not support channels of {}".format(n_channels))

    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            slt.RandomScale(range_x=(0.9, 1.1), same=False, p=0.5),
            slt.RandomShear(range_x=(-0.05, 0.05), p=0.5),
            slt.RandomRotate(rotation_range=(-5, 5), p=0.5),
            slt.PadTransform(pad_to=34),
            slt.CropTransform(crop_size=32, crop_mode='r')
        ]), unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    if stage == 'train':
        return train_trf

    test_trf = Compose([
        wrap2solt,
        slt.PadTransform(pad_to=32), unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    return test_trf
コード例 #2
0
ファイル: utils.py プロジェクト: MIPT-Oulu/Collagen
def init_mnist_transforms():
    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            slt.ResizeTransform(resize_to=(64, 64), interpolation='bilinear'),
            slt.RandomScale(range_x=(0.9, 1.1), same=False, p=0.5),
            slt.RandomShear(range_x=(-0.05, 0.05), p=0.5),
            slt.RandomRotate(rotation_range=(-10, 10), p=0.5),
            # slt.RandomRotate(rotation_range=(-5, 5), p=0.5),
            slt.PadTransform(pad_to=70),
            slt.CropTransform(crop_size=64, crop_mode='r'),
            slt.ImageAdditiveGaussianNoise(p=1.0)
        ]),
        unpack_solt,
        ApplyTransform(Normalize((0.5, ), (0.5, )))
    ])

    test_trf = Compose([
        wrap2solt,
        slt.ResizeTransform(resize_to=(64, 64), interpolation='bilinear'),
        # slt.PadTransform(pad_to=64),
        unpack_solt,
        ApplyTransform(Normalize((0.5, ), (0.5, ))),
    ])

    return train_trf, test_trf
コード例 #3
0
def init_transforms(nc=1):
    if nc == 1:
        norm_mean_std = Normalize((0.1307, ), (0.3081, ))
    elif nc == 3:
        norm_mean_std = Normalize((0.4914, 0.4822, 0.4465),
                                  (0.247, 0.243, 0.261))
    else:
        raise ValueError("Not support channels of {}".format(nc))

    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            # slt.ResizeTransform(resize_to=(32, 32), interpolation='bilinear'),
            slt.RandomScale(range_x=(0.95, 1.05), same=False, p=0.5),
            # slt.RandomShear(range_x=(-0.05, 0.05), p=0.5),
            slt.RandomRotate(rotation_range=(-10, 10), p=0.5),
            slt.RandomFlip(p=0.5, axis=1),
            # slt.RandomRotate(rotation_range=(-5, 5), p=0.5),
            slt.RandomTranslate(range_x=3, range_y=3),
            # slt.PadTransform(pad_to=34),
            # slt.CropTransform(crop_size=32, crop_mode='r'),
            # slt.ImageAdditiveGaussianNoise(p=1.0)
        ]),
        unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    test_trf = Compose([
        wrap2solt,
        slt.ResizeTransform(resize_to=(32, 32), interpolation='bilinear'),
        unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    def custom_augment(img):
        tr = Compose([
            wrap2solt,
            slc.Stream([
                slt.ResizeTransform(resize_to=(32, 32),
                                    interpolation='bilinear'),
                slt.RandomScale(range_x=(0.9, 1.1), same=False, p=0.5),
                slt.RandomShear(range_x=(-0.05, 0.05), p=0.5),
                slt.RandomRotate(rotation_range=(-10, 10), p=0.5),
                # slt.RandomRotate(rotation_range=(-5, 5), p=0.5),
                slt.PadTransform(pad_to=36),
                slt.CropTransform(crop_size=32, crop_mode='r'),
                slt.ImageAdditiveGaussianNoise(p=1.0)
            ]),
            unpack_solt,
            ApplyTransform(norm_mean_std)
        ])

        img_tr, _ = tr((img, 0))
        return img_tr

    return train_trf, test_trf, custom_augment
コード例 #4
0
def init_transform_wo_aug(nc=1):
    if nc == 1:
        norm_mean_std = Normalize((0.5, ), (0.5, ))
    elif nc == 3:
        norm_mean_std = Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    else:
        raise ValueError("Not support channels of {}".format(nc))

    trf = Compose([wrap2solt, unpack_solt, ApplyTransform(norm_mean_std)])
    return trf
コード例 #5
0
def custom_augment(img):
    if len(img.shape) == 3:
        imgs = img.expand_dims(img, axis=0)
    else:
        imgs = img

    out_imgs = []
    for b in range(img.shape[0]):
        img1 = imgs[b, :, :, 0:1].astype(np.uint8)
        img2 = imgs[b, :, :, 1:2].astype(np.uint8)
        tr = Compose([
            wrap2solt,
            slc.Stream([
                slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
                slt.RandomRotate(p=1, rotation_range=(-10, 10)),
                slt.PadTransform(pad_to=int(STD_SZ[0] * 1.05)),
                slt.CropTransform(crop_size=STD_SZ[0], crop_mode='r'),
                slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
            ]), unpack_solt,
            ApplyTransform(Normalize((0.5, ), (0.5, )))
        ])

        img1, _ = tr((img1, 0))
        img2, _ = tr((img2, 0))

        out_img = torch.cat((img1, img2), dim=0)
        out_imgs.append(out_img)
    out_imgs = torch.stack(out_imgs, dim=0)
    return out_imgs
コード例 #6
0
def init_mnist_transforms():
    return Compose([
        wrap2solt,
        slt.ResizeTransform(resize_to=(64, 64), interpolation='bilinear'),
        unpack_solt,
        ApplyTransform(Normalize((0.5, ), (0.5, )))
    ])
コード例 #7
0
def init_transforms():
    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
            slt.RandomRotate(p=1, rotation_range=(-10, 10)),
            slt.PadTransform(pad_to=int(STD_SZ[0] * 1.05)),
            slt.CropTransform(crop_size=STD_SZ[0], crop_mode='r'),
            slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        ]),
        unpack_solt,
        ApplyTransform(Normalize((0.5,), (0.5,)))
    ])

    test_trf = Compose([
        wrap2solt,
        unpack_solt,
        ApplyTransform(Normalize((0.5,), (0.5,)))
    ])

    return {"train": train_trf, "eval": test_trf}
コード例 #8
0
ファイル: utils.py プロジェクト: nlebang/semixup
def init_transforms(nc=1):
    if nc == 1:
        norm_mean_std = Normalize((0.5, ), (0.5, ))
    elif nc == 3:
        norm_mean_std = Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    else:
        raise ValueError("Not support channels of {}".format(nc))

    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
            slt.RandomRotate(p=1, rotation_range=(-10, 10)),
            slt.PadTransform(pad_to=int(STD_SZ[0] * 1.05)),
            slt.CropTransform(crop_size=STD_SZ[0], crop_mode='r'),
            slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        ]), unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    test_trf = Compose([wrap2solt, unpack_solt, ApplyTransform(norm_mean_std)])

    return train_trf, test_trf, custom_augment
コード例 #9
0
def init_mnist_transforms():
    norm_mean_std = Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    train_trf = Compose([
        wrap2solt,
        slt.PadTransform(pad_to=32), unpack_solt,
        ApplyTransform(norm_mean_std, [0, 1])
    ])

    test_trf = Compose([
        wrap2solt,
        slt.PadTransform(pad_to=32), unpack_solt,
        ApplyTransform(norm_mean_std, [0, 1])
    ])

    return train_trf, test_trf
コード例 #10
0
ファイル: utils.py プロジェクト: nlebang/semixup
def custom_augment(img):
    img1 = img[:, :, 0:1].astype(np.uint8)
    img2 = img[:, :, 1:2].astype(np.uint8)
    tr = Compose([
        wrap2solt,
        slc.Stream([
            slt.ImageAdditiveGaussianNoise(p=0.5, gain_range=0.3),
            slt.RandomRotate(p=1, rotation_range=(-10, 10)),
            slt.PadTransform(pad_to=int(STD_SZ[0] * 1.05)),
            slt.CropTransform(crop_size=STD_SZ[0], crop_mode='r'),
            slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)),
        ]), unpack_solt,
        ApplyTransform(Normalize((0.5, ), (0.5, )))
    ])

    img1, _ = tr((img1, 0))
    img2, _ = tr((img2, 0))

    out_img = torch.cat((img1, img2), dim=0)
    return out_img
コード例 #11
0
ファイル: utils.py プロジェクト: MIPT-Oulu/Collagen
def init_transforms(nc=1):
    if nc == 1:
        norm_mean_std = Normalize((0.1307, ), (0.3081, ))
    elif nc == 3:
        norm_mean_std = Normalize((0.4914, 0.4822, 0.4465),
                                  (0.247, 0.243, 0.261))
    else:
        raise ValueError("Not support channels of {}".format(nc))

    train_trf = Compose([
        wrap2solt,
        slc.Stream([
            slt.ResizeTransform(resize_to=(32, 32), interpolation='bilinear'),
            slt.RandomScale(range_x=(0.9, 1.1), same=False, p=0.5),
            slt.RandomFlip(axis=1, p=0.5),
            # slt.RandomShear(range_x=(-0.05, 0.05), p=0.5),
            # slt.RandomRotate(rotation_range=(-10, 10), p=0.5),
            slt.RandomRotate(rotation_range=(-5, 5), p=0.5),
            slt.PadTransform(pad_to=36),
            slt.CropTransform(crop_size=32, crop_mode='r'),
            slt.ImageAdditiveGaussianNoise(p=1.0)
        ]),
        unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    test_trf = Compose([
        wrap2solt,
        slt.ResizeTransform(resize_to=(32, 32), interpolation='bilinear'),
        unpack_solt,
        ApplyTransform(norm_mean_std)
    ])

    def custom_augment(img):

        tr = Compose([
            wrap2solt,
            slc.Stream([
                slt.ResizeTransform(resize_to=(32, 32),
                                    interpolation='bilinear'),
                slt.RandomScale(range_x=(0.9, 1.1), same=False, p=0.5),
                slt.RandomFlip(axis=1, p=0.5),
                # slt.RandomShear(range_x=(-0.05, 0.05), p=0.5),
                # slt.RandomRotate(rotation_range=(-10, 10), p=0.5),
                slt.RandomRotate(rotation_range=(-5, 5), p=0.5),
                slt.PadTransform(pad_to=36),
                slt.CropTransform(crop_size=32, crop_mode='r'),
                slt.ImageAdditiveGaussianNoise(p=1.0)
            ]),
            unpack_solt,
            ApplyTransform(norm_mean_std)
        ])

        if len(img.shape) == 3:
            imgs = np.expand_dims(img, axis=0)
        elif len(img.shape) == 4:
            imgs = img
        else:
            raise ValueError('Expect num of dims 3 or 4, but got {}'.format(
                len(img.shape)))

        out_imgs = []
        for b in range(imgs.shape[0]):
            _img = imgs[b, :].astype(np.uint8)
            _img, _ = tr((_img, 0))
            out_imgs.append(_img)

        return torch.stack(out_imgs, dim=0)

    return train_trf, test_trf, custom_augment