-
Notifications
You must be signed in to change notification settings - Fork 188
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
Implements SSL-EY #378
Open
jameschapman19
wants to merge
9
commits into
vturrisi:main
Choose a base branch
from
jameschapman19:ssley
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implements SSL-EY #378
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
760033c
Adding SSL-EY
jameschapman19 f2f18de
Adding SSL-EY to README.md
jameschapman19 2dda063
Adding SSL-EY to README.md
jameschapman19 39290ed
Adding SSL-EY to README.md
jameschapman19 ffb5928
Adding SSL-EY to README.md
jameschapman19 9e8ad7e
Adding SSL-EY to README.md
jameschapman19 b2ad189
Adding SSL-EY to README.md
jameschapman19 4a2e837
Adding SSL-EY to README.md
jameschapman19 11eac92
Adding SSL-EY to README.md
jameschapman19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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,5 @@ | ||
SSL-EY | ||
------- | ||
|
||
.. autofunction:: solo.losses.ssley.ssley_loss_func | ||
:noindex: |
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,26 @@ | ||
SSL-EY | ||
======= | ||
|
||
|
||
.. automethod:: solo.methods.ssley.SSLEY.__init__ | ||
:noindex: | ||
|
||
add_model_specific_args | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
.. automethod:: solo.methods.ssley.SSLEY.add_model_specific_args | ||
:noindex: | ||
|
||
learnable_params | ||
~~~~~~~~~~~~~~~~ | ||
.. autoattribute:: solo.methods.ssley.SSLEY.learnable_params | ||
:noindex: | ||
|
||
forward | ||
~~~~~~~ | ||
.. automethod:: solo.methods.ssley.SSLEY.forward | ||
:noindex: | ||
|
||
training_step | ||
~~~~~~~~~~~~~ | ||
.. automethod:: solo.methods.ssley.SSLEY.training_step | ||
:noindex: |
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 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,45 @@ | ||
defaults: | ||
- _self_ | ||
- wandb: private.yaml | ||
- override hydra/hydra_logging: disabled | ||
- override hydra/job_logging: disabled | ||
|
||
# disable hydra outputs | ||
hydra: | ||
output_subdir: null | ||
run: | ||
dir: . | ||
|
||
name: "ssley-imagenet100-linear" | ||
pretrained_feature_extractor: None | ||
backbone: | ||
name: "resnet18" | ||
pretrain_method: "ssley" | ||
data: | ||
dataset: imagenet100 | ||
train_path: "./datasets/imagenet-100/train" | ||
val_path: "./datasets/imagenet-100/val" | ||
format: "dali" | ||
num_workers: 4 | ||
optimizer: | ||
name: "sgd" | ||
batch_size: 256 | ||
lr: 0.3 | ||
weight_decay: 0 | ||
scheduler: | ||
name: "step" | ||
lr_decay_steps: [60, 80] | ||
checkpoint: | ||
enabled: True | ||
dir: "trained_models" | ||
frequency: 1 | ||
auto_resume: | ||
enabled: True | ||
|
||
# overwrite PL stuff | ||
max_epochs: 100 | ||
devices: [0] | ||
sync_batchnorm: True | ||
accelerator: "gpu" | ||
strategy: "ddp" | ||
precision: 16 |
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,80 @@ | ||
defaults: | ||
- _self_ | ||
- wandb: private.yaml | ||
- override hydra/hydra_logging: disabled | ||
- override hydra/job_logging: disabled | ||
|
||
# disable hydra outputs | ||
hydra: | ||
output_subdir: null | ||
run: | ||
dir: . | ||
|
||
name: "ssley-cifar10" # change here for cifar100 | ||
method: "ssley" | ||
backbone: | ||
name: "resnet18" | ||
method_kwargs: | ||
proj_hidden_dim: 2048 | ||
proj_output_dim: 2048 | ||
data: | ||
dataset: cifar10 # change here for cifar100 | ||
train_path: "./datasets" | ||
val_path: "datasets/imagenet100/val" | ||
format: "image_folder" | ||
num_workers: 4 | ||
augmentations: | ||
- rrc: | ||
enabled: True | ||
crop_min_scale: 0.2 | ||
crop_max_scale: 1.0 | ||
color_jitter: | ||
enabled: True | ||
brightness: 0.4 | ||
contrast: 0.4 | ||
saturation: 0.2 | ||
hue: 0.1 | ||
prob: 0.8 | ||
grayscale: | ||
enabled: True | ||
prob: 0.2 | ||
gaussian_blur: | ||
enabled: False | ||
prob: 0.0 | ||
solarization: | ||
enabled: True | ||
prob: 0.1 | ||
equalization: | ||
enabled: False | ||
prob: 0.0 | ||
horizontal_flip: | ||
enabled: True | ||
prob: 0.5 | ||
crop_size: 32 | ||
num_crops: 2 | ||
optimizer: | ||
name: "lars" | ||
batch_size: 256 | ||
lr: 0.3 | ||
classifier_lr: 0.1 | ||
weight_decay: 1e-4 | ||
kwargs: | ||
clip_lr: True | ||
eta: 0.02 | ||
exclude_bias_n_norm: True | ||
scheduler: | ||
name: "warmup_cosine" | ||
checkpoint: | ||
enabled: True | ||
dir: "trained_models" | ||
frequency: 1 | ||
auto_resume: | ||
enabled: True | ||
|
||
# overwrite PL stuff | ||
max_epochs: 1000 | ||
devices: [0] | ||
sync_batchnorm: True | ||
accelerator: "gpu" | ||
strategy: "ddp" | ||
precision: 16-mixed |
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,81 @@ | ||
defaults: | ||
- _self_ | ||
- augmentations: ssley.yaml | ||
- wandb: private.yaml | ||
- override hydra/hydra_logging: disabled | ||
- override hydra/job_logging: disabled | ||
|
||
# disable hydra outputs | ||
hydra: | ||
output_subdir: null | ||
run: | ||
dir: . | ||
|
||
name: "ssley-imagenet100" | ||
method: "ssley" | ||
backbone: | ||
name: "resnet18" | ||
method_kwargs: | ||
proj_hidden_dim: 2048 | ||
proj_output_dim: 2048 | ||
data: | ||
dataset: imagenet100 | ||
train_path: "datasets/imagenet100/train" | ||
val_path: "datasets/imagenet100/val" | ||
format: "dali" | ||
num_workers: 4 | ||
augmentations: | ||
- rrc: | ||
enabled: True | ||
crop_min_scale: 0.2 | ||
crop_max_scale: 1.0 | ||
color_jitter: | ||
enabled: True | ||
brightness: 0.4 | ||
contrast: 0.4 | ||
saturation: 0.2 | ||
hue: 0.1 | ||
prob: 0.8 | ||
grayscale: | ||
enabled: True | ||
prob: 0.2 | ||
gaussian_blur: | ||
enabled: True | ||
prob: 0.5 | ||
solarization: | ||
enabled: True | ||
prob: 0.1 | ||
equalization: | ||
enabled: False | ||
prob: 0.0 | ||
horizontal_flip: | ||
enabled: True | ||
prob: 0.5 | ||
crop_size: 224 | ||
num_crops: 2 | ||
optimizer: | ||
name: "lars" | ||
batch_size: 128 | ||
lr: 0.3 | ||
classifier_lr: 0.1 | ||
weight_decay: 1e-4 | ||
kwargs: | ||
clip_lr: True | ||
eta: 0.02 | ||
exclude_bias_n_norm: True | ||
scheduler: | ||
name: "warmup_cosine" | ||
checkpoint: | ||
enabled: True | ||
dir: "trained_models" | ||
frequency: 1 | ||
auto_resume: | ||
enabled: True | ||
|
||
# overwrite PL stuff | ||
max_epochs: 400 | ||
devices: [0, 1] | ||
sync_batchnorm: True | ||
accelerator: "gpu" | ||
strategy: "ddp" | ||
precision: 16-mixed |
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 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,53 @@ | ||
# Copyright 2023 solo-learn development team. | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to use, | ||
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the | ||
# Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all copies | ||
# or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | ||
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
# DEALINGS IN THE SOFTWARE. | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
from solo.utils.misc import gather | ||
|
||
|
||
def ssley_loss_func( | ||
z1: torch.Tensor, | ||
z2: torch.Tensor, | ||
) -> torch.Tensor: | ||
"""Computes SSL-EY's loss given batch of projected features z1 from view 1 and | ||
projected features z2 from view 2. | ||
|
||
Args: | ||
z1 (torch.Tensor): NxD Tensor containing projected features from view 1. | ||
z2 (torch.Tensor): NxD Tensor containing projected features from view 2. | ||
|
||
Returns: | ||
torch.Tensor: VICReg loss. | ||
""" | ||
|
||
sim_loss = invariance_loss(z1, z2) | ||
|
||
N, D = z1.size() | ||
B = torch.cov(torch.hstack((z1, z2)).T) | ||
|
||
if dist.is_available() and dist.is_initialized(): | ||
dist.all_reduce(B) | ||
world_size = dist.get_world_size() | ||
B /= world_size | ||
|
||
A = B[:D, D:] + B[D:, :D] | ||
B = B[:D, :D] + B[D:, D:] | ||
|
||
return -torch.trace(2 * A - B @ B) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You already have the augmentations below, either remove this or move the augmentations to a separate file (both are fine)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot - a heads up I think this is also the case for scripts/pretrain/imagenet-100/vicreg.yaml in that case.
This method should plug in wherever VICReg is used. In many ways the benefit of the loss function is it's got a lot of the nice properties of VICReg but without needing to tune the three loss function parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea why VICReg is like that haha. I'm fine with having a separate file for the augmentations or having it merged with the main file. Should also prob fix VICReg after that.