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

functionality for learning on the prior with QR loss and ChesapeakeCVPR data #202

Merged
merged 30 commits into from
Dec 28, 2021

Conversation

estherrolf
Copy link
Contributor

No description provided.

@ghost
Copy link

ghost commented Oct 18, 2021

CLA assistant check
All CLA requirements met.

@calebrob6 calebrob6 added the trainers PyTorch Lightning trainers label Oct 18, 2021
@calebrob6 calebrob6 self-requested a review October 18, 2021 23:00
@calebrob6
Copy link
Member

calebrob6 commented Oct 18, 2021

Context: PR to add a trainer, new loss function, and an update to the ChesapeakeCVPR dataset that includes prior estimates of land cover derived from weak supervision. This is for reproducibility of a paper under review.

@estherrolf, looks great, thank you so much! I can take it from here more/less, will check with you offline for major changes and to assert that the results look similar.

Things on my radar for any other reviewers that want to jump in:

  • Merge the ChesapeakeCVPR datasets
  • Remove FCN_modified (and use FCN in the trainer)
  • Satisfy mypy ridiculousness
  • Add to docs where appropriate
  • Copy the testing template from the other PR

@isaaccorley
Copy link
Collaborator

I started reviewing but seems like you're going to take over @calebrob6

@adamjstewart adamjstewart marked this pull request as draft November 1, 2021 21:31
@adamjstewart
Copy link
Collaborator

@calebrob6 converted to a draft, just mark as "Ready for review" once this is ready

@adamjstewart adamjstewart added this to the 0.2.0 milestone Nov 20, 2021
@calebrob6 calebrob6 marked this pull request as ready for review December 17, 2021 10:12
@calebrob6
Copy link
Member

calebrob6 commented Dec 17, 2021

@adamjstewart @isaaccorley this is ready for a review pass if you have the time (ignoring the code in the trainer file for now).

Summary:

@estherrolf, questions:

  • can we move the trainer to the independent code repo? (we're trying to minimize the amount of custom trainer code that lives directly in torchgeo)
  • can we talk through the args in ChesapeakeCVPRPriorDataModule? (I want to make sure I understand the purpose of everything before I change more)

@estherrolf
Copy link
Contributor Author

@calebrob6 re: trainers yes, I've moved the chesapeake_train_on_prior.py as well as all the envioratlas trainers to my experimentation/paper repo so feel free to remove or edit those, whichever makes sense. I also will instantiate the "modified" fcns in my experimentation repo so don't feel any pressure to integrate those unless you want to for some other reason.

re: ChesapeakeCVPRPriorDataModule i think we sorted that out in a call just now, but lmk if questions arise!

@calebrob6
Copy link
Member

calebrob6 commented Dec 18, 2021

@estherrolf this one is finished (after code review)! I ended up merging the ChesapeakeCVPRPriorDataModule with the existing ChesapeakeCVPRDataModule. If you pass use_prior_labels, then it will return batches where the "mask" is the prior labels, else it will return batches where the "mask" is the high-res labels as usual (also carried over the spatial smoothing parameter).

Before removing the old DataModule, I asserted that this new merged one gives the same outputs, e.g. both return this image as the "tree" prior for the same bounding box in de-train (the blank area is because of padding):

image

Of note, in the old ChesapeakeCVPRPriorDataModule you returned the prior as batch["mask"] and the high-res labels as batch["high_res_labels"], however I dropped the "high_res_labels" for consistency with other DataModules (also training should go faster if you don't load and extra layer with each patch). If you really want this for visualization during training I can show you how to make a new DataModule that extends this class and does that (it would only be a few lines).

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'm hesitant to add these loss functions to TorchGeo. From what I can tell from reading the paper, these loss functions are not specific to geospatial data. I want to keep TorchGeo as focused on geospatial data as possible for two reasons:

  1. Maintainer burden: we don't want to maintain things that we don't need to
  2. Sharing: we want as many people to benefit from these functions as possible, not just geoscientists

Can these functions instead be contributed to PyTorch/torchvision/Kornia?

torchgeo/losses/qr_losses.py Outdated Show resolved Hide resolved
torchgeo/losses/qr_losses.py Outdated Show resolved Hide resolved
torchgeo/losses/qr_losses.py Outdated Show resolved Hide resolved
@calebrob6
Copy link
Member

these loss functions are not specific to geospatial data

Do you know of any loss functions that are specific to geospatial data that are candidates here? Off the top of my head the geospatial ML specific papers we've talked about would not fit:

  • Tile2Vec is a standard triplet loss with custom sampling strategy.
  • Geography Aware SSL uses InfoNCE but uses time to select samples. They also have predicting lat, lon as a pretext task, however they bin the location and use normal classification class.
  • SeCo also uses InfoNCE but uses time to select samples.

Maintainer burden: we don't want to maintain things that we don't need to

Can you explain this further?

Sharing: we want as many people to benefit from these functions as possible, not just geoscientists

Implementing QR / RQ loss here doesn't preclude anyone else from implementing it (and I'd argue gives it a higher likelihood of being noticed by someone that might want to use it). Maybe I don't understand this point either?

Can these functions instead be contributed to PyTorch/torchvision/Kornia?

Maybe, but I definitely don't have the bandwidth to learn how to contribute to pytorch. E.g. I tried to fix a basic typing issue with F.normalize (that affects this PR) the other day and a large number of tests failed cryptically pytorch/pytorch#70149 😄. Something with JIT.

@isaaccorley
Copy link
Collaborator

isaaccorley commented Dec 21, 2021

Should we consider making separate datasets and datamodules for the original dataset and the one with the priors? If I'm a user and I just want the vanilla ChesapeakeCVPR dataset, I'd imagine this would be harder to parse what's going on with all the additions in this PR.

Edit: actually since this just adds another layer to the dataset maybe it's fine as is.

@adamjstewart
Copy link
Collaborator

Do you know of any loss functions that are specific to geospatial data that are candidates here?

Nope, that's why we've never proposed a torchgeo.losses module before.

Maintainer burden: we don't want to maintain things that we don't need to

Can you explain this further?

Maintainer burden is the idea that the more code you add to a project, the more you have to maintain. When you add a feature to a library like TorchGeo, you're making a promise to the user that this feature works. If a user discovers a bug in that feature, the burden is on the maintainer to fix it. In the future, once TorchGeo becomes more stable, it's also a promise that this feature will exist and won't disappear all of a sudden. If we need to refactor (for example, function -> nn.Module) at a later date, this becomes more complicated the more features we have to refactor.

We're starting to see this with our datasets. We have a ton of datasets now, which is awesome, but we also decided after the fact that all datasets should have a plot function and should allow already downloaded tarballs. This is quite a bit of work to do for all datasets, and would be much worse if we also decided to include MNIST/CIFAR/ImageNet/etc.

If you recall the torchvision PRs I did a while back, most of them were rejected not because they weren't useful, but because they increased maintainer burden on the torchvision devs for something that wasn't directly useful for their project.

Sharing: we want as many people to benefit from these functions as possible, not just geoscientists

Implementing QR / RQ loss here doesn't preclude anyone else from implementing it (and I'd argue gives it a higher likelihood of being noticed by someone that might want to use it). Maybe I don't understand this point either?

For our paper, one of our reviewers pointed out that many of the problems associated with remote sensing imagery are not unique to remote sensing, they are also found in the biomedical domain. Our response to that question was basically what I'm suggesting here, that transforms specific to geospatial data belong in TorchGeo, while transforms that are useful for any kind of multispectral image data belong in libraries like Kornia/torchvision. There are far more people who use Kornia/torchvision than there are TorchGeo. If every library is forced to reimplement this same feature, you end up with a lot of code duplication, which is bad.

I'm thinking about these loss functions the same way I would if someone wanted to contribute a new dataset or model to TorchGeo that had absolutely nothing to do with geospatial data. Another example would be if you wanted a metric that wasn't already in torchmetrics. I wouldn't want to accept that into TorchGeo because it really belongs in torchmetrics. This is in line with the Unix philosophy of doing one thing well.

Basically, I'm not 100% opposed to adding non-geospatial-specific datasets/models/losses/transforms to TorchGeo, but I'm going to need quite a bit of convincing as to why they have to be in TorchGeo before I commit to maintaining them. Also, as soon as we accept one non-geospatial-specific feature, that sets a precedent for adding more in the future.

@adamjstewart
Copy link
Collaborator

Can these functions instead be contributed to PyTorch/torchvision/Kornia?

Maybe, but I definitely don't have the bandwidth to learn how to contribute to pytorch.

Another option is to move these loss functions to your independent code repo until someone has the bandwidth to contribute them to one of these libraries.

@adamjstewart
Copy link
Collaborator

Before I forget, we need to add losses to pyproject.toml under [tool.pydocstyle].

@calebrob6
Copy link
Member

For clarity to anyone reading this, we discussed this offline and decided to include the losses. There are several losses that, while not created exclusively for geospatial data, are particularly useful with geospatial data and that aren't implemented elsewhere. We feel that these are in scope for torchgeo until/unless they are picked up by a general-purpose library (e.g. a torchmetrics for loss functions would be an awesome place for these 😄).

@calebrob6
Copy link
Member

@adamjstewart, rebased to accommodate the new datamodule organization, fixed previous changes

@github-actions github-actions bot added datamodules PyTorch Lightning datamodules datasets Geospatial or benchmark datasets documentation Improvements or additions to documentation models Models and pretrained weights testing Continuous integration testing losses Geospatial loss functions and removed trainers PyTorch Lightning trainers labels Dec 25, 2021
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 think this is pretty close, just had some comments on tests and naming

tests/datamodules/test_chesapeake.py Outdated Show resolved Hide resolved
torchgeo/losses/qr_losses.py Outdated Show resolved Hide resolved
@adamjstewart adamjstewart merged commit 0d4811b into microsoft:main Dec 28, 2021
@adamjstewart adamjstewart added utilities Utilities for working with geospatial data and removed utilities Utilities for working with geospatial data labels Jan 2, 2022
yichiac pushed a commit to yichiac/torchgeo that referenced this pull request Apr 29, 2023
…PR data (microsoft#202)

* adding QR loss functions for learning on the prior

* chesapake learn on prior trainer with self-contained code for visualization

* adding prior dataset to the chesapeake datasets; doesn't handle downloading or anything like that yet

* updating init files to include chesapeake CVPR prior

* adding FCNModified for learning on the prior

* changing input to samplers to pass dataset instead of dataset.index

* fixing style issues

* Removing FCN_modified

* Fixing super call and mypy in FCN model

* Added learning on the prior extension

* Update tests

* Formatting

* Adding QR loss

* Added losses to docs

* Removing trainer, moving datamodule

* Combining chesapeake and chesapeake prior datamodules

* Formatting

* Test coverage

* Formatting

* Adding losses

* Re-moving the datamodules around

* Make loss function a torch Module

* Version added

* Fixed some stuff that got messed up in the rebase

* Formatting

* How'd this get there?

* Change qr losses to expect probabilities instead of log-probabilities

* Clean up test

* Rename qr loss file

* Renamed test file

Co-authored-by: Caleb Robinson <calebrob6@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datamodules PyTorch Lightning datamodules datasets Geospatial or benchmark datasets documentation Improvements or additions to documentation losses Geospatial loss functions models Models and pretrained weights testing Continuous integration testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants