moco-v3/moco/loader.py

40 lines
1.1 KiB
Python
Raw Normal View History

2021-06-17 02:09:43 -07:00
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
from PIL import ImageFilter, ImageOps
2021-06-16 19:59:59 -07:00
import random
class TwoCropsTransform:
2021-06-17 02:09:43 -07:00
"""Take two random crops of one image"""
2021-06-16 19:59:59 -07:00
2021-06-17 02:09:43 -07:00
def __init__(self, base_transform1, base_transform2):
self.base_transform1 = base_transform1
self.base_transform2 = base_transform2
2021-06-16 19:59:59 -07:00
def __call__(self, x):
2021-06-18 16:18:22 -07:00
im1 = self.base_transform1(x)
im2 = self.base_transform2(x)
return [im1, im2]
2021-06-16 19:59:59 -07:00
class GaussianBlur(object):
2021-06-17 02:09:43 -07:00
"""Gaussian blur augmentation from SimCLR: https://arxiv.org/abs/2002.05709"""
2021-06-16 19:59:59 -07:00
def __init__(self, sigma=[.1, 2.]):
self.sigma = sigma
def __call__(self, x):
sigma = random.uniform(self.sigma[0], self.sigma[1])
x = x.filter(ImageFilter.GaussianBlur(radius=sigma))
return x
2021-06-17 02:09:43 -07:00
class Solarize(object):
"""Solarize augmentation from BYOL: https://arxiv.org/abs/2006.07733"""
def __call__(self, x):
return ImageOps.solarize(x)