From ecb78062fdd1c75aa43a84e8f67df71f8d6110f8 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Fri, 25 Dec 2020 15:50:15 +0900 Subject: [PATCH 1/5] Refactor cpc --- .../models/self_supervised/cpc/__init__.py | 13 +++++++++ .../models/self_supervised/cpc/transforms.py | 28 +++++++++---------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/pl_bolts/models/self_supervised/cpc/__init__.py b/pl_bolts/models/self_supervised/cpc/__init__.py index e4e0736d74..85f6338613 100644 --- a/pl_bolts/models/self_supervised/cpc/__init__.py +++ b/pl_bolts/models/self_supervised/cpc/__init__.py @@ -8,3 +8,16 @@ CPCTrainTransformsImageNet128, CPCTrainTransformsSTL10, ) + + +__all__ = [ + "CPCV2", + "cpc_resnet50", + "cpc_resnet101", + "CPCEvalTransformsCIFAR10", + "CPCEvalTransformsImageNet128", + "CPCEvalTransformsSTL10", + "CPCTrainTransformsCIFAR10", + "CPCTrainTransformsImageNet128", + "CPCTrainTransformsSTL10", +] diff --git a/pl_bolts/models/self_supervised/cpc/transforms.py b/pl_bolts/models/self_supervised/cpc/transforms.py index 511cc88618..efbc7ad115 100644 --- a/pl_bolts/models/self_supervised/cpc/transforms.py +++ b/pl_bolts/models/self_supervised/cpc/transforms.py @@ -4,8 +4,8 @@ if _TORCHVISION_AVAILABLE: from torchvision import transforms -else: - warn_missing_pkg('torchvision') # pragma: no-cover +else: # pragma: no cover + warn_missing_pkg('torchvision') class CPCTrainTransformsCIFAR10: @@ -39,8 +39,8 @@ def __init__(self, patch_size=8, overlap=4): patch_size: size of patches when cutting up the image into overlapping patches overlap: how much to overlap patches """ - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -97,8 +97,8 @@ def __init__(self, patch_size: int = 8, overlap: int = 4): patch_size: size of patches when cutting up the image into overlapping patches overlap: how much to overlap patches """ - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -151,8 +151,8 @@ def __init__(self, patch_size: int = 16, overlap: int = 8): patch_size: size of patches when cutting up the image into overlapping patches overlap: how much to overlap patches """ - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -210,8 +210,8 @@ def __init__(self, patch_size: int = 16, overlap: int = 8): patch_size: size of patches when cutting up the image into overlapping patches overlap: how much to overlap patches """ - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -260,8 +260,8 @@ def __init__(self, patch_size: int = 32, overlap: int = 16): patch_size: size of patches when cutting up the image into overlapping patches overlap: how much to overlap patches """ - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -320,8 +320,8 @@ def __init__(self, patch_size: int = 32, overlap: int = 16): patch_size: size of patches when cutting up the image into overlapping patches overlap: how much to overlap patches """ - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) From b62dcfa592b156a23fe62e8566cc81cf46f5d564 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Fri, 25 Dec 2020 15:50:50 +0900 Subject: [PATCH 2/5] Refactor moco --- .../models/self_supervised/moco/transforms.py | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/pl_bolts/models/self_supervised/moco/transforms.py b/pl_bolts/models/self_supervised/moco/transforms.py index 352e576d11..37291b6ad4 100644 --- a/pl_bolts/models/self_supervised/moco/transforms.py +++ b/pl_bolts/models/self_supervised/moco/transforms.py @@ -10,13 +10,13 @@ if _TORCHVISION_AVAILABLE: from torchvision import transforms -else: - warn_missing_pkg('torchvision') # pragma: no-cover +else: # pragma: no cover + warn_missing_pkg('torchvision') if _PIL_AVAILABLE: from PIL import ImageFilter -else: - warn_missing_pkg('PIL', pypi_name='Pillow') # pragma: no-cover +else: # pragma: no cover + warn_missing_pkg('PIL', pypi_name='Pillow') class Moco2TrainCIFAR10Transforms: @@ -25,8 +25,8 @@ class Moco2TrainCIFAR10Transforms: https://arxiv.org/pdf/2003.04297.pdf """ def __init__(self, height: int = 32): - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -55,8 +55,8 @@ class Moco2EvalCIFAR10Transforms: https://arxiv.org/pdf/2003.04297.pdf """ def __init__(self, height: int = 32): - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -79,8 +79,8 @@ class Moco2TrainSTL10Transforms: https://arxiv.org/pdf/2003.04297.pdf """ def __init__(self, height: int = 64): - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -109,8 +109,8 @@ class Moco2EvalSTL10Transforms: https://arxiv.org/pdf/2003.04297.pdf """ def __init__(self, height: int = 64): - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -135,8 +135,8 @@ class Moco2TrainImagenetTransforms: """ def __init__(self, height: int = 128): - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -165,8 +165,8 @@ class Moco2EvalImagenetTransforms: https://arxiv.org/pdf/2003.04297.pdf """ def __init__(self, height: int = 128): - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -187,8 +187,8 @@ class GaussianBlur(object): """Gaussian blur augmentation in SimCLR https://arxiv.org/abs/2002.05709""" def __init__(self, sigma=(0.1, 2.0)): - if not _PIL_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _PIL_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `Pillow` which is not installed yet, install it with `pip install Pillow`.' ) self.sigma = sigma From 952a4afa479e73f7b2a5fb0bd7d07539046e7e70 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Fri, 25 Dec 2020 15:51:10 +0900 Subject: [PATCH 3/5] Refactor simclr --- .../self_supervised/simclr/simclr_finetuner.py | 2 +- .../models/self_supervised/simclr/transforms.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pl_bolts/models/self_supervised/simclr/simclr_finetuner.py b/pl_bolts/models/self_supervised/simclr/simclr_finetuner.py index 5e720e8f82..ffd066e131 100644 --- a/pl_bolts/models/self_supervised/simclr/simclr_finetuner.py +++ b/pl_bolts/models/self_supervised/simclr/simclr_finetuner.py @@ -13,7 +13,7 @@ ) -def cli_main(): # pragma: no-cover +def cli_main(): # pragma: no cover from pl_bolts.datamodules import CIFAR10DataModule, ImagenetDataModule, STL10DataModule pl.seed_everything(1234) diff --git a/pl_bolts/models/self_supervised/simclr/transforms.py b/pl_bolts/models/self_supervised/simclr/transforms.py index faf9202120..39813c9d61 100644 --- a/pl_bolts/models/self_supervised/simclr/transforms.py +++ b/pl_bolts/models/self_supervised/simclr/transforms.py @@ -1,6 +1,6 @@ import numpy as np -from pl_bolts.utils import _TORCHVISION_AVAILABLE +from pl_bolts.utils import _TORCHVISION_AVAILABLE, _OPENCV_AVAILABLE from pl_bolts.utils.warnings import warn_missing_pkg if _TORCHVISION_AVAILABLE: @@ -8,10 +8,10 @@ else: warn_missing_pkg('torchvision') # pragma: no-cover -try: +if _OPENCV_AVAILABLE: import cv2 -except ModuleNotFoundError: - warn_missing_pkg('cv2', pypi_name='opencv-python') # pragma: no-cover +else: # pragma: no cover + warn_missing_pkg('cv2', pypi_name='opencv-python') class SimCLRTrainDataTransform(object): @@ -43,8 +43,8 @@ def __init__( normalize=None ) -> None: - if not _TORCHVISION_AVAILABLE: - raise ModuleNotFoundError( # pragma: no-cover + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( 'You want to use `transforms` from `torchvision` which is not installed yet.' ) @@ -187,6 +187,11 @@ def __call__(self, sample): class GaussianBlur(object): # Implements Gaussian blur as described in the SimCLR paper def __init__(self, kernel_size, p=0.5, min=0.1, max=2.0): + if not _TORCHVISION_AVAILABLE: # pragma: no cover + raise ModuleNotFoundError( + 'You want to use `GaussianBlur` from `cv2` which is not installed yet.' + ) + self.min = min self.max = max From 0d9af905dce2e4e3831a67a6ff4302415b1249a9 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Fri, 25 Dec 2020 15:51:22 +0900 Subject: [PATCH 4/5] Refactor swav --- pl_bolts/models/self_supervised/swav/__init__.py | 10 ++++++++++ pl_bolts/models/self_supervised/swav/swav_finetuner.py | 2 +- pl_bolts/models/self_supervised/swav/transforms.py | 8 ++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pl_bolts/models/self_supervised/swav/__init__.py b/pl_bolts/models/self_supervised/swav/__init__.py index 486495557f..f9eafc8f10 100644 --- a/pl_bolts/models/self_supervised/swav/__init__.py +++ b/pl_bolts/models/self_supervised/swav/__init__.py @@ -5,3 +5,13 @@ SwAVFinetuneTransform, SwAVTrainDataTransform, ) + + +__all__ = [ + "SwAV", + "resnet18", + "resnet50", + "SwAVEvalDataTransform", + "SwAVFinetuneTransform", + "SwAVTrainDataTransform", +] diff --git a/pl_bolts/models/self_supervised/swav/swav_finetuner.py b/pl_bolts/models/self_supervised/swav/swav_finetuner.py index 7555121d9b..da07afa8d8 100644 --- a/pl_bolts/models/self_supervised/swav/swav_finetuner.py +++ b/pl_bolts/models/self_supervised/swav/swav_finetuner.py @@ -9,7 +9,7 @@ from pl_bolts.transforms.dataset_normalizations import imagenet_normalization, stl10_normalization -def cli_main(): # pragma: no-cover +def cli_main(): # pragma: no cover from pl_bolts.datamodules import ImagenetDataModule, STL10DataModule pl.seed_everything(1234) diff --git a/pl_bolts/models/self_supervised/swav/transforms.py b/pl_bolts/models/self_supervised/swav/transforms.py index 9b853f7bac..047ee6f8b4 100644 --- a/pl_bolts/models/self_supervised/swav/transforms.py +++ b/pl_bolts/models/self_supervised/swav/transforms.py @@ -7,13 +7,13 @@ if _TORCHVISION_AVAILABLE: import torchvision.transforms as transforms -else: - warn_missing_pkg('torchvision') # pragma: no-cover +else: # pragma: no cover + warn_missing_pkg('torchvision') if _OPENCV_AVAILABLE: import cv2 -else: - warn_missing_pkg('cv2', pypi_name='opencv-python') # pragma: no-cover +else: # pragma: no cover + warn_missing_pkg('cv2', pypi_name='opencv-python') class SwAVTrainDataTransform(object): From 78ab7d9ab97dc258ce7c7f7d6e1d0c45688d3bf2 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Fri, 25 Dec 2020 15:51:45 +0900 Subject: [PATCH 5/5] Refactor self_supervised/__init__.py --- pl_bolts/models/self_supervised/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pl_bolts/models/self_supervised/__init__.py b/pl_bolts/models/self_supervised/__init__.py index 99646d6f75..2ffd0fb33e 100644 --- a/pl_bolts/models/self_supervised/__init__.py +++ b/pl_bolts/models/self_supervised/__init__.py @@ -26,3 +26,15 @@ from pl_bolts.models.self_supervised.simclr.simclr_module import SimCLR # noqa: F401 from pl_bolts.models.self_supervised.ssl_finetuner import SSLFineTuner # noqa: F401 from pl_bolts.models.self_supervised.swav.swav_module import SwAV # noqa: F401 + + +__all__ = [ + "AMDIM", + "BYOL", + "CPCV2", + "SSLEvaluator" + "MocoV2", + "SimCLR", + "SSLFineTuner", + "SwAV", +]