Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor pl_bolts.models.self_supervised.amdim #479

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions pl_bolts/models/self_supervised/amdim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from pl_bolts.models.self_supervised.amdim.amdim_module import AMDIM # noqa: F401
from pl_bolts.models.self_supervised.amdim.networks import AMDIMEncoder # noqa: F401
from pl_bolts.models.self_supervised.amdim.transforms import ( # noqa: F401
AMDIMEvalTransformsCIFAR10,
AMDIMEvalTransformsImageNet128,
AMDIMEvalTransformsSTL10,
AMDIMTrainTransformsCIFAR10,
AMDIMTrainTransformsImageNet128,
AMDIMTrainTransformsSTL10,
)

try:
from pl_bolts.models.self_supervised.amdim.transforms import ( # noqa: F401
AMDIMEvalTransformsCIFAR10,
AMDIMEvalTransformsImageNet128,
AMDIMEvalTransformsSTL10,
AMDIMTrainTransformsCIFAR10,
AMDIMTrainTransformsImageNet128,
AMDIMTrainTransformsSTL10,
)
except ModuleNotFoundError:
pass
__all__ = [
"AMDIM",
"AMDIMEncoder",
"AMDIMEvalTransformsCIFAR10",
"AMDIMEvalTransformsImageNet128",
"AMDIMEvalTransformsSTL10",
"AMDIMTrainTransformsCIFAR10",
"AMDIMTrainTransformsImageNet128",
"AMDIMTrainTransformsSTL10",
]
5 changes: 1 addition & 4 deletions pl_bolts/models/self_supervised/amdim/amdim_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import torch.optim as optim
from torch.utils.data import DataLoader

try:
from pl_bolts.models.self_supervised.amdim.datasets import AMDIMPretraining
except ModuleNotFoundError:
pass
from pl_bolts.losses.self_supervised_learning import FeatureMapContrastiveTask
from pl_bolts.models.self_supervised.amdim.datasets import AMDIMPretraining
from pl_bolts.models.self_supervised.amdim.networks import AMDIMEncoder
from pl_bolts.utils.self_supervised import torchvision_ssl_encoder

Expand Down
22 changes: 15 additions & 7 deletions pl_bolts/models/self_supervised/amdim/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

from torch.utils.data import random_split

from pl_bolts.datasets.imagenet_dataset import UnlabeledImagenet
from pl_bolts.datasets.ssl_amdim_datasets import CIFAR10Mixed
from pl_bolts.models.self_supervised.amdim import transforms as amdim_transforms
from pl_bolts.utils import _TORCHVISION_AVAILABLE
from pl_bolts.utils.warnings import warn_missing_pkg

try:
if _TORCHVISION_AVAILABLE:
from torchvision.datasets import STL10

from pl_bolts.datasets.imagenet_dataset import UnlabeledImagenet
from pl_bolts.datasets.ssl_amdim_datasets import CIFAR10Mixed
from pl_bolts.models.self_supervised.amdim import transforms as amdim_transforms
except ModuleNotFoundError:
warn_missing_pkg('torchvision') # pragma: no-cover
else: # pragma: no cover
warn_missing_pkg('torchvision')


class AMDIMPretraining():
Expand Down Expand Up @@ -55,6 +55,10 @@ def imagenet(dataset_root, nb_classes, split: str = 'train'):

@staticmethod
def stl(dataset_root, split: Optional[str] = None):
if not _TORCHVISION_AVAILABLE: # pragma: no cover
raise ModuleNotFoundError(
'You want to use STL10 dataset loaded from `torchvision` which is not installed yet.'
)
dataset = STL10(
root=dataset_root,
split='unlabeled',
Expand Down Expand Up @@ -96,6 +100,10 @@ def cifar10(dataset_root, patch_size, patch_overlap, split: str = 'train'):

@staticmethod
def stl(dataset_root, patch_size, patch_overlap, split: Optional[str] = None):
if not _TORCHVISION_AVAILABLE: # pragma: no cover
raise ModuleNotFoundError(
'You want to use STL10 dataset loaded from `torchvision` which is not installed yet.'
)
train_transform = amdim_transforms.TransformsSTL10Patches(
patch_size=patch_size,
overlap=patch_overlap
Expand Down
24 changes: 12 additions & 12 deletions pl_bolts/models/self_supervised/amdim/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 AMDIMTrainTransformsCIFAR10:
Expand Down Expand Up @@ -78,8 +78,8 @@ class AMDIMEvalTransformsCIFAR10:
"""

def __init__(self):
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.'
)

Expand Down Expand Up @@ -121,8 +121,8 @@ class AMDIMTrainTransformsSTL10:
"""

def __init__(self, height=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.'
)

Expand Down Expand Up @@ -170,8 +170,8 @@ class AMDIMEvalTransformsSTL10(object):
"""

def __init__(self, height=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.'
)

Expand Down Expand Up @@ -215,8 +215,8 @@ class AMDIMTrainTransformsImageNet128(object):
"""

def __init__(self, height=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.'
)

Expand Down Expand Up @@ -264,8 +264,8 @@ class AMDIMEvalTransformsImageNet128(object):
"""

def __init__(self, height=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.'
)

Expand Down