Exemplo n.º 1
0
def build_transforms():
    def update_transforms(p):
        crop_size = tuple(
            [round(size // 2 + (size - size // 2) * p) for size in IMAGE_SIZE])

        random_crop.reset(crop_size)
        center_crop.reset(crop_size)

        print('update transforms p: {:.2f}, crop_size: {}'.format(
            p, crop_size))

    random_crop = Resettable(T.RandomCrop)
    center_crop = Resettable(T.CenterCrop)

    to_tensor_and_norm = T.Compose([
        T.ToTensor(),
        T.Normalize(np.mean((0.485, 0.456, 0.406), keepdims=True),
                    np.mean((0.229, 0.224, 0.225), keepdims=True)),
    ])
    train_transform = T.Compose([
        Invert(),
        T.RandomAffine(degrees=15,
                       scale=(0.8, 1 / 0.8),
                       resample=Image.BILINEAR),
        random_crop,
        to_tensor_and_norm,
        # T.RandomErasing(value=0),
    ])
    eval_transform = T.Compose([
        Invert(),
        center_crop,
        to_tensor_and_norm,
    ])

    return train_transform, eval_transform, update_transforms
Exemplo n.º 2
0
def build_transform():
    def update_transform(image_size):
        print("image_size {}".format(image_size))
        resize.reset(image_size)
        crop.reset(image_size)

    def to_rgb(input):
        return input.convert("RGB")

    resize = Resettable(T.Resize)
    crop = Resettable(T.CenterCrop)
    transform = T.Compose([
        T.Lambda(to_rgb),
        resize,
        crop,
        T.ToTensor(),
        T.Normalize(mean=[0.5], std=[0.5]),
    ])

    return transform, update_transform
Exemplo n.º 3
0
        size = round(np.random.uniform(self.min_size, self.max_size))
        input = Resize(size)(input)

        return input


def dist_sharpen(dist, temp):
    assert dist.dim() == 2

    dist = dist**(1 / temp)
    dist = dist / dist.sum(1, keepdim=True)

    return dist


random_crop = Resettable(RandomCrop)
center_crop = Resettable(CenterCrop)
infer_image_transform = Resettable(lambda tta: test_image_transform
                                   if tta else eval_image_transform)
to_tensor = ToTensor()

if config.normalize is None:
    normalize = T.Compose([])
elif config.normalize == 'experiment':
    normalize = NormalizeByExperimentStats(
        torch.load('./experiment_stats.pth'))
elif config.normalize == 'plate':
    normalize = NormalizeByPlateStats(torch.load('./plate_stats.pth'))
else:
    raise AssertionError('invalid normalization {}'.format(config.normalize))
Exemplo n.º 4
0
            assert c.mode == mode

        s1 = [np.array(c) for c in s1]
        s2 = [np.array(c) for c in s2]

        for c1, c2 in zip(s1, s2):
            c1[x1:x2, y1:y2] = c2[x1:x2, y1:y2]

        s1 = [Image.fromarray(c) for c in s1]

        assert s1[0].mode == mode

        return s1


random_resize = Resettable(RandomResize)
random_crop = Resettable(RandomCrop)
center_crop = Resettable(CenterCrop)
to_tensor = ToTensor()

if config.normalize is None:
    normalize = T.Compose([])
elif config.normalize == 'experiment':
    normalize = NormalizeByExperimentStats(
        torch.load('./experiment_stats.pth'))  # TODO: needs realtime computation on private
elif config.normalize == 'plate':
    normalize = NormalizeByPlateStats(
        torch.load('./plate_stats.pth'))  # TODO: needs realtime computation on private
else:
    raise AssertionError('invalide normalization {}'.format(config.normalize))