Beispiel #1
0
    def test_sequential(self):

        f = nn.Sequential(
            RandomHorizontalFlip(1.0, return_transform=True),
            RandomHorizontalFlip(1.0, return_transform=True),
        )
        f1 = nn.Sequential(
            RandomHorizontalFlip(1.0, return_transform=True),
            RandomHorizontalFlip(1.0),
        )

        input = torch.tensor([[[[0., 0., 0.],
                                [0., 0., 0.],
                                [0., 1., 1.]]]])  # 1 x 1 x 3 x 3

        expected_transform = torch.tensor([[[-1., 0., 3.],
                                            [0., 1., 0.],
                                            [0., 0., 1.]]])  # 1 x 3 x 3

        expected_transform_1 = expected_transform @ expected_transform

        assert(f(input)[0] == input).all()
        assert(f(input)[1] == expected_transform_1).all()
        assert(f1(input)[0] == input).all()
        assert(f1(input)[1] == expected_transform).all()
Beispiel #2
0
    def test_random_hflip(self):

        f = RandomHorizontalFlip(p=1.0, return_transform=True)
        f1 = RandomHorizontalFlip(p=0., return_transform=True)
        f2 = RandomHorizontalFlip(p=1.)
        f3 = RandomHorizontalFlip(p=0.)

        input = torch.tensor([[0., 0., 0.],
                              [0., 0., 0.],
                              [0., 1., 1.]])  # 3 x 3

        expected = torch.tensor([[0., 0., 0.],
                                 [0., 0., 0.],
                                 [1., 1., 0.]])  # 3 x 3

        expected_transform = torch.tensor([[-1., 0., 3.],
                                           [0., 1., 0.],
                                           [0., 0., 1.]])  # 3 x 3

        identity = torch.tensor([[1., 0., 0.],
                                 [0., 1., 0.],
                                 [0., 0., 1.]])  # 3 x 3

        assert (f(input)[0] == expected).all()
        assert (f(input)[1] == expected_transform).all()
        assert (f1(input)[0] == input).all()
        assert (f1(input)[1] == identity).all()
        assert (f2(input) == expected).all()
        assert (f3(input) == input).all()
Beispiel #3
0
    def test_batch_random_hflip(self):

        f = RandomHorizontalFlip(p=1.0, return_transform=True)
        f1 = RandomHorizontalFlip(p=0.0, return_transform=True)

        input = torch.tensor([[[[0., 0., 0.],
                                [0., 0., 0.],
                                [0., 1., 1.]]]])  # 1 x 1 x 3 x 3

        expected = torch.tensor([[[[0., 0., 0.],
                                   [0., 0., 0.],
                                   [1., 1., 0.]]]])  # 1 x 1 x 3 x 3

        expected_transform = torch.tensor([[[-1., 0., 3.],
                                            [0., 1., 0.],
                                            [0., 0., 1.]]])  # 1 x 3 x 3

        identity = torch.tensor([[[1., 0., 0.],
                                  [0., 1., 0.],
                                  [0., 0., 1.]]])  # 1 x 3 x 3

        input = input.repeat(5, 3, 1, 1)  # 5 x 3 x 3 x 3
        expected = expected.repeat(5, 3, 1, 1)  # 5 x 3 x 3 x 3
        expected_transform = expected_transform.repeat(5, 1, 1)  # 5 x 3 x 3
        identity = identity.repeat(5, 1, 1)  # 5 x 3 x 3

        assert (f(input)[0] == expected).all()
        assert (f(input)[1] == expected_transform).all()
        assert (f1(input)[0] == input).all()
        assert (f1(input)[1] == identity).all()
Beispiel #4
0
    def test_gradcheck(self):

        input = torch.rand((3, 3)).double()  # 3 x 3

        input = utils.tensor_to_gradcheck_var(input)  # to var

        assert gradcheck(RandomHorizontalFlip(p=1.), (input, ), raise_exception=True)
Beispiel #5
0
 def monochrome_model(gaussian_p, solarize_p):
     return nn.Sequential(
         RandomResizedCrop((image_size, image_size),
                           interpolation="BICUBIC"), RandomHorizontalFlip(),
         RandomApply(GaussianBlur2d(get_kernel_size(image_size),
                                    (0.1, 2.0)),
                     p=gaussian_p), RandomSolarize(0, 0, p=solarize_p))
Beispiel #6
0
 def color_model(gaussian_p, solarize_p):
     return nn.Sequential(
         RandomResizedCrop((image_size, image_size),
                           interpolation="BICUBIC"), RandomHorizontalFlip(),
         ColorJitter(0.4, 0.4, 0.2, 0.1, p=0.8), RandomGrayscale(p=0.2),
         RandomApply(GaussianBlur2d(get_kernel_size(image_size),
                                    (0.1, 2.0)),
                     p=gaussian_p), RandomSolarize(0, 0, p=solarize_p))
Beispiel #7
0
def train_transforms(image_size,
                     train_img_scale=(0.35, 1),
                     normalize: bool = True,
                     mean=torch.tensor([0.485, 0.456, 0.406]),
                     std=torch.tensor([0.229, 0.224, 0.225])):
    """Transforms for train augmentation with Kornia."""

    transforms = [
        AccimageImageToTensorNN(),
        RandomResizedCrop((image_size, image_size),
                          train_img_scale,
                          keepdim=True),
        RandomHorizontalFlip(keepdim=True)
    ]
    if normalize:
        transforms.append(Normalize(mean=std, std=std, keepdim=True))
    return torch.nn.Sequential(*transforms)
Beispiel #8
0
    def __init__(self, input_shape, s=1.0, apply_transforms=None):

        assert len(input_shape) == 3, "input_shape should be (H, W, C)"

        self.input_shape = input_shape
        self.H, self.W, self.C = input_shape[0], input_shape[1], input_shape[2]
        self.s = s
        self.apply_transforms = apply_transforms

        if self.apply_transforms is None:
            kernel_size = int(0.1 * self.H)
            sigma = self._get_sigma()

            self.apply_transforms = KorniaCompose([
                RandomResizedCrop(size=(self.H, self.W), scale=(0.08, 1.0)),
                RandomHorizontalFlip(p=0.5),
                ColorJitter(0.8 * self.s, 0.8 * self.s, 0.8 * self.s, 0.2 * self.s),
                RandomGrayscale(p=0.2),
                GaussianBlur2d(kernel_size=(kernel_size, kernel_size),
                               sigma=(sigma, sigma))
            ])
 def random_flip(self, p: float = 0.5) -> TransformType:
     return RandomHorizontalFlip(p=p)
Beispiel #10
0
 def smoke_test(self):
     f = RandomHorizontalFlip(0.5)
     repr = "RandomHorizontalFlip(p=0.5, return_transform=False)"
     assert str(f) == repr
Beispiel #11
0
from itertools import chain
import torch
import torch.nn as nn
import torch.optim as optim
from kornia.augmentation import RandomHorizontalFlip, RandomCrop
from kornia.geometry.transform import resize
from fastcore.all import *

from .data_anime_heads import Datasets as AnimeHeadsDatasets, DataLoaders as AnimeHeadsDataLoaders
from .data_birds import Datasets as BirdsDatasets, DataLoaders as BirdsDataLoaders
from .model import RnnEncoder, CnnEncoder
from .torch_utils import *

# Internal Cell
ce_loss = nn.CrossEntropyLoss()
random_hflip = RandomHorizontalFlip().requires_grad_(False).eval()
random_crop = RandomCrop((229, 229)).requires_grad_(False).eval()


# Internal Cell
def cosine_similarity(x1, x2, dim=1, eps=1e-8):
    """Returns cosine similarity between x1 and x2, computed along dim.
    """
    w12 = torch.sum(x1 * x2, dim)
    w1 = torch.norm(x1, 2, dim)
    w2 = torch.norm(x2, 2, dim)
    return (w12 / (w1 * w2).clamp(min=eps)).squeeze()


def func_attention(query, context, gamma1):
    """