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

fix SeCo transforms #1324

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

isaaccorley
Copy link
Collaborator

The current SeCo transforms do the following:

  • resize
  • center crop
  • normalize using the quantiles
  • multiply by 255
  • normalize using imagenet stats

However in the script they actually do the following:

  • resize
  • center crop
  • normalize using the quantiles
  • multiply by 255
  • clip to [0. 255]
  • convert to uint8
  • divide by 255
  • normalize using imagenet stats

@github-actions github-actions bot added the models Models and pretrained weights label May 11, 2023
@github-actions github-actions bot added the transforms Data augmentation transforms label May 11, 2023
@isaaccorley isaaccorley assigned isaaccorley and unassigned calebrob6 May 11, 2023
Copy link
Collaborator

@adamjstewart adamjstewart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually disagree with all changes in this PR. You might be able to convince me to keep the clamp if you can show evidence that it actually improves downstream accuracy.

@@ -37,6 +38,9 @@
K.CenterCrop(224),
K.Normalize(mean=_min, std=_max - _min),
K.Normalize(mean=torch.tensor(0), std=1 / torch.tensor(255)),
Lambda(lambda x: torch.clamp(x, min=0.0, max=255.0)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a lot of value in this. All we're doing is losing information, right? We're already scaling to the same range, do we really care if there are values less/greater than what they trained on?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, I'm not agreeing with this, but these are the transforms used for SeCo. @calebrob6 and I have done some KNN experiments with SeCo and it's actually very sensitive to these transforms (significant changes in downstream performance if you don't use these). https://github.com/ServiceNow/seasonal-contrast/blob/8285173ec205b64bc3e53b880344dd6c3f79fa7a/datasets/seco_dataset.py#L51

@@ -37,6 +38,9 @@
K.CenterCrop(224),
K.Normalize(mean=_min, std=_max - _min),
K.Normalize(mean=torch.tensor(0), std=1 / torch.tensor(255)),
Lambda(lambda x: torch.clamp(x, min=0.0, max=255.0)),
Lambda(lambda x: x.to(torch.uint8).to(torch.float)), # type: ignore[no-any-return]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually do anything?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it converts from float to uint8 to reduce bit resolution. The conversion back to float is so that the other kornia augmentations don't complain about it being float

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like the same situation as the clip above. It reduces information from the image and makes the job harder for downstream tasks. This may help during SSL because image comparison is harder, but I have no a priori reason to believe that this would help with downstream tasks. If you perform an ablation study and find that it helps, I'm fine with adding it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you that it's bad to throw away information, but we should make another transform function if we want to remove certain pieces. Our current seco transform isn't correct which is what we are advertising by attaching it to the weights.

@@ -37,6 +38,9 @@
K.CenterCrop(224),
K.Normalize(mean=_min, std=_max - _min),
K.Normalize(mean=torch.tensor(0), std=1 / torch.tensor(255)),
Lambda(lambda x: torch.clamp(x, min=0.0, max=255.0)),
Lambda(lambda x: x.to(torch.uint8).to(torch.float)), # type: ignore[no-any-return]
K.Normalize(mean=torch.tensor(0), std=torch.tensor(255)),
Copy link
Collaborator

@adamjstewart adamjstewart May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

divide by 255

Where do you see this?

Copy link
Collaborator Author

@isaaccorley isaaccorley May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a multiply by 255, I don't see a divide by 255

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transforms.ToTensor(), right before imagenet_normalization()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. In that case, I would remove all 4 lines, so we neither multiply by 255 nor divide by 255. This will make the transform faster.

@isaaccorley
Copy link
Collaborator Author

Whether you agree or not with the changes, our current seco transforms are incorrect. I'm just taking what they have from their script.

@isaaccorley
Copy link
Collaborator Author

I don't actually know how to fix the mypy errors other than ignoring them.

  • I can't type hint a lambda but kornia doesn't have a clip augmentation.
  • K.AugmentationSequential complains about the K.contrib.Lambda input arg

@adamjstewart
Copy link
Collaborator

Whether you agree or not with the changes, our current seco transforms are incorrect. I'm just taking what they have from their script.

In my mind, the transforms supplied with the weights aren't designed to reproduce SeCo pre-training, they're designed to use the SeCo weights for downstream tasks. If these changes don't improve performance on downstream tasks and only make transforms slower, or even harm performance on downstream tasks, then we shouldn't add them. That's why an ablation study is needed to ensure that these changes are beneficial.

@isaaccorley
Copy link
Collaborator Author

In that case then the transforms are completely wrong, because SeCo doesn't use these for downstream tasks at all. They only divide eurosat by 255, they use different BigEarthNet mean and std. They also don't resize to 224x224. I would argue that you would need to show an ablation study of why you shouldn't use the same transforms as those used during training since that's less intuitive. It's trained to take 8-bit inputs so I would expect performance would degrade if you feed it 12-bit inputs.

@adamjstewart
Copy link
Collaborator

Let's hold off on this PR until after the deadline so we have time to do a proper ablation study on no transforms, current transforms, and proposed transforms.

@isaaccorley
Copy link
Collaborator Author

I'm good with this just wanted to use it for comparisons to SeCo

@adamjstewart adamjstewart marked this pull request as draft May 12, 2023 04:04
@adamjstewart adamjstewart added this to the 0.4.2 milestone May 25, 2023
@adamjstewart
Copy link
Collaborator

Added this to the 0.4.2 milestone so we don't forget about it. At the bare minimum, we should remove the multiply by 255 stuff. The rest is more controversial and will need ablation studies that we don't have time to work on until after the deadline.

@adamjstewart adamjstewart removed this from the 0.4.2 milestone Sep 28, 2023
@adamjstewart
Copy link
Collaborator

Is this still a work in progress?

@isaaccorley
Copy link
Collaborator Author

@adamjstewart I can finish this today. It fell off my radar.

@adamjstewart
Copy link
Collaborator

No rush. If we can squeeze it into 0.5.2, great. If not, we can continue to punt it. Just trying to finish up old PRs.

@adamjstewart
Copy link
Collaborator

I'm no longer opposed to the use of torch.clamp and added a Kornia-style _Clamp transform in #2038 you can use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
models Models and pretrained weights transforms Data augmentation transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants