-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from sthalles/simclr-refactor
Simclr refactor
- Loading branch information
Showing
14 changed files
with
919 additions
and
1,286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from torchvision.transforms import transforms | ||
from data_aug.gaussian_blur import GaussianBlur | ||
from torchvision import transforms, datasets | ||
from data_aug.view_generator import ContrastiveLearningViewGenerator | ||
from exceptions.exceptions import InvalidDatasetSelection | ||
|
||
|
||
class ContrastiveLearningDataset: | ||
def __init__(self, root_folder): | ||
self.root_folder = root_folder | ||
|
||
@staticmethod | ||
def get_simclr_pipeline_transform(size, s=1): | ||
"""Return a set of data augmentation transformations as described in the SimCLR paper.""" | ||
color_jitter = transforms.ColorJitter(0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s) | ||
data_transforms = transforms.Compose([transforms.RandomResizedCrop(size=size), | ||
transforms.RandomHorizontalFlip(), | ||
transforms.RandomApply([color_jitter], p=0.8), | ||
transforms.RandomGrayscale(p=0.2), | ||
GaussianBlur(kernel_size=int(0.1 * size)), | ||
transforms.ToTensor()]) | ||
return data_transforms | ||
|
||
def get_dataset(self, name, n_views): | ||
valid_datasets = {'cifar10': lambda: datasets.CIFAR10(self.root_folder, train=True, | ||
transform=ContrastiveLearningViewGenerator( | ||
self.get_simclr_pipeline_transform(32), | ||
n_views), | ||
download=True), | ||
|
||
'stl10': lambda: datasets.STL10(self.root_folder, split='unlabeled', | ||
transform=ContrastiveLearningViewGenerator( | ||
self.get_simclr_pipeline_transform(96), | ||
n_views), | ||
download=True)} | ||
|
||
try: | ||
dataset_fn = valid_datasets[name] | ||
except KeyError: | ||
raise InvalidDatasetSelection() | ||
else: | ||
return dataset_fn() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,48 @@ | ||
import cv2 | ||
import numpy as np | ||
import torch | ||
from torch import nn | ||
from torchvision.transforms import transforms | ||
|
||
np.random.seed(0) | ||
|
||
|
||
class GaussianBlur(object): | ||
# Implements Gaussian blur as described in the SimCLR paper | ||
def __init__(self, kernel_size, min=0.1, max=2.0): | ||
self.min = min | ||
self.max = max | ||
# kernel size is set to be 10% of the image height/width | ||
self.kernel_size = kernel_size | ||
"""blur a single image on CPU""" | ||
def __init__(self, kernel_size): | ||
radias = kernel_size // 2 | ||
kernel_size = radias * 2 + 1 | ||
self.blur_h = nn.Conv2d(3, 3, kernel_size=(kernel_size, 1), | ||
stride=1, padding=0, bias=False, groups=3) | ||
self.blur_v = nn.Conv2d(3, 3, kernel_size=(1, kernel_size), | ||
stride=1, padding=0, bias=False, groups=3) | ||
self.k = kernel_size | ||
self.r = radias | ||
|
||
def __call__(self, sample): | ||
sample = np.array(sample) | ||
self.blur = nn.Sequential( | ||
nn.ReflectionPad2d(radias), | ||
self.blur_h, | ||
self.blur_v | ||
) | ||
|
||
# blur the image with a 50% chance | ||
prob = np.random.random_sample() | ||
self.pil_to_tensor = transforms.ToTensor() | ||
self.tensor_to_pil = transforms.ToPILImage() | ||
|
||
if prob < 0.5: | ||
sigma = (self.max - self.min) * np.random.random_sample() + self.min | ||
sample = cv2.GaussianBlur(sample, (self.kernel_size, self.kernel_size), sigma) | ||
def __call__(self, img): | ||
img = self.pil_to_tensor(img).unsqueeze(0) | ||
|
||
return sample | ||
sigma = np.random.uniform(0.1, 2.0) | ||
x = np.arange(-self.r, self.r + 1) | ||
x = np.exp(-np.power(x, 2) / (2 * sigma * sigma)) | ||
x = x / x.sum() | ||
x = torch.from_numpy(x).view(1, -1).repeat(3, 1) | ||
|
||
self.blur_h.weight.data.copy_(x.view(3, 1, self.k, 1)) | ||
self.blur_v.weight.data.copy_(x.view(3, 1, 1, self.k)) | ||
|
||
with torch.no_grad(): | ||
img = self.blur(img) | ||
img = img.squeeze() | ||
|
||
img = self.tensor_to_pil(img) | ||
|
||
return img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import numpy as np | ||
|
||
np.random.seed(0) | ||
|
||
|
||
class ContrastiveLearningViewGenerator(object): | ||
"""Take two random crops of one image as the query and key.""" | ||
|
||
def __init__(self, base_transform, n_views=2): | ||
self.base_transform = base_transform | ||
self.n_views = n_views | ||
|
||
def __call__(self, x): | ||
return [self.base_transform(x) for i in range(self.n_views)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class BaseSimCLRException(Exception): | ||
"""Base exception""" | ||
|
||
|
||
class InvalidBackboneError(BaseSimCLRException): | ||
"""Raised when the choice of backbone Convnet is invalid.""" | ||
|
||
|
||
class InvalidDatasetSelection(BaseSimCLRException): | ||
"""Raised when the choice of dataset is invalid.""" |
Oops, something went wrong.