def __init__(self, batch_size, num_workers, root_dir, log, **kwargs):

        self.batch_size = batch_size
        self.num_workers = num_workers
        self.root_dir = root_dir
        self.log = log
        self.kwargs = kwargs

        self.transform_train = transforms.Compose([
            transforms.Resize(98),
            transforms.RandomResizedCrop(92),
            transforms.RandomHorizontalFlip(),
            transforms.RandomAutocontrast(p=.25),
            transforms.RandomRotation(degrees=25),
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
        ])
        self.transform_val = transforms.Compose([
            transforms.Resize(98),
            transforms.ToTensor(),
            transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
        ])
        self.target_transform = transforms.Compose([
            ColumnSelect(['arousal']),  #, 'valence']), #, 'expression']),
            torch.FloatTensor,
            # ReplaceValues(-2, None),
            # Digitize(range=(-1, 1.01), step=0.1),
            # torch.LongTensor
        ])
        self.filter_expression = list(range(8))
        # self.filter_expression.append(9)  # train on uncertain

        self.filter_expression_test = list(range(8))
Example #2
0
def get_standard_data_augmentation():
    """
    Standard data augmentation used on my experiments
    """
    transform = transforms.Compose([
        lambda x: bob.io.image.to_matplotlib(x),
        lambda x: x.astype("uint8"),
        transforms.ToPILImage(),
        transforms.RandomHorizontalFlip(p=0.5),
        transforms.RandomRotation(degrees=(-3, 3)),
        transforms.RandomAutocontrast(p=0.1),
        transforms.PILToTensor(),
        lambda x: (x - 127.5) / 128.0,
    ])
    return transform
Example #3
0
 def return_train_transforms(self):
     tfms = transforms.Compose([
         transforms.Lambda(self.pad_image),
         transforms.RandomHorizontalFlip(),
         transforms.RandomVerticalFlip(),
         transforms.RandomAdjustSharpness(2),
         transforms.RandomAutocontrast(),
         transforms.RandomRotation([-90, 90]),
         transforms.RandomAffine((-90, 90)),
         transforms.Resize([self.img_size[0], self.img_size[1]],
                           transforms.transforms.InterpolationMode.BICUBIC),
         transforms.ToTensor(),
         transforms.RandomErasing(0.25),
         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
     ])
     return tfms
Example #4
0
def make_transform(sz_resize=256,
                   sz_crop=227,
                   mean=None,
                   std=None,
                   rgb_to_bgr=True,
                   is_train=True,
                   intensity_scale=None,
                   rotate=0,
                   jitter=0,
                   hue=0):
    if std is None:
        std = [1, 1, 1]
    if mean is None:
        mean = [104, 117, 128]
    return transforms.Compose([
        RGBToBGR() if rgb_to_bgr else Identity(),
        transforms.RandomRotation(rotate) if is_train else Identity(),
        transforms.RandomResizedCrop(sz_crop) if is_train else Identity(),
        transforms.Resize(sz_resize) if not is_train else Identity(),
        transforms.CenterCrop(sz_crop) if not is_train else Identity(),
        transforms.RandomHorizontalFlip() if is_train else Identity(),
        #transforms.ColorJitter(brightness=jitter, contrast=jitter, saturation=jitter, hue=hue) if is_train else Identity(),

        #### New Added ####
        transforms.RandomPerspective() if is_train else Identity(),
        transforms.RandomEqualize() if is_train else Identity(),
        transforms.RandomVerticalFlip() if is_train else Identity(),
        transforms.RandomAdjustSharpness(
            sharpness_factor=2) if is_train else Identity(),
        transforms.RandomAutocontrast() if is_train else Identity(),
        transforms.RandomErasing() if is_train else Identity(),
        transforms.RandomAffine(degrees=(-60, 60),
                                translate=(0.1, 0.3),
                                scale=(0.5, 0.75)) if is_train else Identity(),
        #### New Added ####
        transforms.ToTensor(),
        ScaleIntensities(
            *intensity_scale) if intensity_scale is not None else Identity(),
        transforms.Normalize(
            mean=mean,
            std=std,
        )
    ])
# RandomAdjustSharpness
# ~~~~~~~~~~~~~~~~~~~~~
# The :class:`~torchvision.transforms.RandomAdjustSharpness` transform
# (see also :func:`~torchvision.transforms.functional.adjust_sharpness`)
# randomly adjusts the sharpness of the given image.
sharpness_adjuster = T.RandomAdjustSharpness(sharpness_factor=2)
sharpened_imgs = [sharpness_adjuster(orig_img) for _ in range(4)]
plot(sharpened_imgs)

####################################
# RandomAutocontrast
# ~~~~~~~~~~~~~~~~~~
# The :class:`~torchvision.transforms.RandomAutocontrast` transform
# (see also :func:`~torchvision.transforms.functional.autocontrast`)
# randomly applies autocontrast to the given image.
autocontraster = T.RandomAutocontrast()
autocontrasted_imgs = [autocontraster(orig_img) for _ in range(4)]
plot(autocontrasted_imgs)

####################################
# RandomEqualize
# ~~~~~~~~~~~~~~
# The :class:`~torchvision.transforms.RandomEqualize` transform
# (see also :func:`~torchvision.transforms.functional.equalize`)
# randomly equalizes the histogram of the given image.
equalizer = T.RandomEqualize()
equalized_imgs = [equalizer(orig_img) for _ in range(4)]
plot(equalized_imgs)

####################################
# AutoAugment
Example #6
0
EXAMPLE_DIR = Path(__file__).parent.parent / 'docs/examples'
INDEX_DIR = Path(__file__).parent.parent / 'data/test'
IMAGE_DIR = Path(__file__).parent.parent / 'data/images'

from cnocr import gen_model
from cnocr.data_utils.aug import NormalizeAug
from cnocr.dataset import OcrDataModule
from cnocr.trainer import PlTrainer

train_transform = transforms.Compose(
    [
        transforms.RandomInvert(p=0.5),
        transforms.RandomErasing(p=0.05),
        transforms.RandomRotation(degrees=2),
        transforms.RandomAutocontrast(p=0.05),
        NormalizeAug(),
    ]
)
val_transform = NormalizeAug()


def test_trainer():
    data_mod = OcrDataModule(
        index_dir=INDEX_DIR,
        vocab_fp=EXAMPLE_DIR / 'label_cn.txt',
        img_folder=IMAGE_DIR,
        train_transforms=train_transform,
        val_transforms=val_transform,
        batch_size=64,
        num_workers=0,
Example #7
0
import torchvision.transforms as T  # noqa: N812

from matplotlib.image import AxesImage

from hearth.vision.datasets import RGBImageDataset
from hearth.vision.transforms import RandomFlipOrient

_ids = [str(uuid.uuid4()) for i in range(100)]

_augmentations = T.RandomApply(
    [
        T.RandomHorizontalFlip(p=0.5),
        RandomFlipOrient(0.5),
        T.ColorJitter(brightness=0.1, hue=0.1),
        T.RandomAdjustSharpness(sharpness_factor=2),
        T.RandomAutocontrast(),
        T.RandomEqualize(),
    ],
    p=0.8,
)


@pytest.fixture(scope='module')
def image_dir(tmpdir_factory):
    d = tmpdir_factory.mktemp('images')
    dirname = str(d)

    # compute and save 100 random images of random sizes
    for _id in _ids:
        w = random.randint(32, 512)
        h = random.randint(32, 512)
#                          IMG_DIR, 
#                          target_size = 224, 
#                          transform = T.Compose([
#                                                 T.GaussianBlur(kernel_size = 3),
#                                                 T.RandomAutocontrast(p = 0.5),
#                                                 T.RandomAdjustSharpness(1.2, p = 0.5)
#                          ]))
# next(iter(val_data))
# type(img)
# np.shape(img)
# print(img)
# plt.imshow(sample['image'], cmap = 'gray')

transform_img = T.Compose([
                          T.GaussianBlur(kernel_size = 3),
                          T.RandomAutocontrast(p = 0.5),
                          T.RandomAdjustSharpness(1.2, p = 0.5)])
train_data = BrixiaDataset(annot_train, IMG_DIR, target_size = 224, transform = transform_img)
test_data = BrixiaDataset(annot_test, IMG_DIR, target_size = 224)

train_dl = DataLoader(train_data, BATCH_SIZE, shuffle = True, num_workers = 4)
test_dl = DataLoader(test_data, BATCH_SIZE, shuffle = False, num_workers = 4)
dataloaders = {'train': train_dl,  'test': test_dl}
data_sizes = {'train': len(train_data), 'test': len(test_data)}

data_sizes

## Creating an Instance of DataLoader!
# train_dl = DataLoader(data, batch_size = 8, shuffle = True)
# X, y = next(iter(val_dl))   ## 43s! After multiple runs. 
# ## With Four Workers: 1m 15s. First run!
Example #9
0
def data_augmentation(ToTensor=False,
                      Resize=None,
                      Contrast=None,
                      Equalize=None,
                      HFlip=None,
                      Invert=None,
                      VFlip=None,
                      Rotation=None,
                      Grayscale=None,
                      Perspective=None,
                      Erasing=None,
                      Crop=None):
    '''
    DataAgumentation 2021/03/23 by Mr.w
    -------------------------------------------------------------
    ToTensor : False/True , 注意转为Tensor,通道会放在第一维
    Resize : tuple-->(500,500)
    Contrast : 0-1 -->图像被自动对比度的可能
    Equalize : 0-1 -->图像均衡可能性
    HFlip : 0-1 --> 图像水平翻转
    Invert : 0-1--> 随机翻转
    VFlip : 0-1 --> 图像垂直翻转
    Rotation : 0-360 --> 随机旋转度数范围, as : 90 , [-90,90]
    Grayscale : 0-1 --> 随机转换为灰度图像
    Perspective : 0-1 --> 随机扭曲图像
    Erasing : 0-1 --> 随机擦除
    Crop : tuple --> (500,500)
    -------------------------------------------------------------
    return : transforms.Compose(train_transform) --> 方法汇总
    '''
    #列表导入Compose
    train_transform = []
    if ToTensor == True:
        trans_totensor = transforms.ToTensor()
        train_transform.append(trans_totensor)

    if Resize != None:
        trans_Rsize = transforms.Resize(Resize)  # Resize=(500,500)
        train_transform.append(trans_Rsize)
    if Contrast != None:
        trans_Rcontrast = transforms.RandomAutocontrast(p=Contrast)
        train_transform.append(trans_Rcontrast)
    if Equalize != None:
        trans_REqualize = transforms.RandomEqualize(p=Equalize)
        train_transform.append(trans_REqualize)
    if HFlip != None:
        train_transform.append(transforms.RandomHorizontalFlip(p=HFlip))
    if Invert != None:
        train_transform.append(transforms.RandomInvert(p=Invert))
    if VFlip != None:
        train_transform.append(transforms.RandomVerticalFlip(p=VFlip))
    if Rotation != None:
        train_transform.append(
            transforms.RandomRotation(Rotation,
                                      expand=False,
                                      center=None,
                                      fill=0,
                                      resample=None))
    if Grayscale != None:
        train_transform.append(transforms.RandomGrayscale(p=Grayscale))
    if Perspective != None:
        train_transform.append(
            transforms.RandomPerspective(distortion_scale=0.5,
                                         p=Perspective,
                                         fill=0))
    if Erasing != None:
        train_transform.append(
            transforms.RandomErasing(p=Erasing,
                                     scale=(0.02, 0.33),
                                     ratio=(0.3, 3.3),
                                     value=0,
                                     inplace=False))
    if Crop != None:
        train_transform.append(
            transforms.RandomCrop(Crop,
                                  padding=None,
                                  pad_if_needed=False,
                                  fill=0,
                                  padding_mode='constant'))
    return transforms.Compose(train_transform)