-
Notifications
You must be signed in to change notification settings - Fork 36
[WIP] Test time deep method #305
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
Open
mbarneche
wants to merge
27
commits into
scikit-adaptation:main
Choose a base branch
from
mbarneche:mbarneche-tent
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
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6b8ee39
Create _test_time.py
cd13eed
removed unnecessary clauses in TestTimeCriterion
d0ee0a3
added tests for TestTimeNet
32aec72
Corrected wrong attribute call
c23e8f2
fixed wrong dataloader in test
a3c0ced
remade tests, improved TestTimeNet
28ce3f3
Updated tests
e81a62e
added softmax_entropy, added foundation of Tent
4c76ba4
Updated Tent
aab0c09
added Tent, param freeze
44b2df9
Merge branch 'main' into mbarneche-tent
mbarneche 616cc29
changed None to all to adapt all params
26840fd
Merge branch 'mbarneche-tent' of https://github.com/mbarneche/skada i…
b4c1841
Add shot loss
marionpavaux 8bacda4
Merge branch 'mbarneche-tent' of https://github.com/mbarneche/skada i…
marionpavaux 1f52a1b
Add reference for shot loss
marionpavaux bc37c6e
Implement SHOT model
FDerrida 5a71c1d
Merge branch 'mbarneche-tent' of github.com:mbarneche/skada into mbar…
FDerrida 6d34ed8
Add estimated labels
marionpavaux 0c0dfae
Merge branch 'mbarneche-tent' of https://github.com/mbarneche/skada i…
marionpavaux 1e501b8
Merge branch 'main' into mbarneche-tent
tgnassou 7b17529
Add example for test time
marionpavaux 62d86db
Add example for test time
marionpavaux d29d299
Add shot implementation and PseudoLabels class
FDerrida 65c60d3
Merge branch 'mbarneche-tent' of github.com:mbarneche/skada into mbar…
FDerrida d103ef8
Adapt estimated labels to callbacks
marionpavaux 96717f7
Merge branch 'main' into mbarneche-tent
tgnassou 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,99 @@ | ||
| # Author : Maxence Barneche | ||
| # | ||
| # License: BSD-3-Clause | ||
|
|
||
| import torch | ||
|
|
||
| from skada.deep.base import DomainAwareNet | ||
|
|
||
|
|
||
| class TestTimeCriterion(torch.nn.Module): | ||
| def __init__( | ||
| self, | ||
| base_criterion, | ||
| adapt_criterion, | ||
| reg=1, | ||
| reduction="mean", | ||
| train_on_target=False, | ||
| ): | ||
| super().__init__() | ||
| self.base_criterion = base_criterion | ||
| self.adapt_criterion = adapt_criterion | ||
| self.reg = reg | ||
| self.train_on_target = train_on_target | ||
|
|
||
| # Update the reduce parameter for both criteria if specified | ||
| if hasattr(self.base_criterion, "reduction"): | ||
| self.base_criterion.reduction = reduction | ||
|
|
||
| # TODO: implement losses between source and target | ||
| # that are sum of the losses of each sample | ||
| # if hasattr(self.adapt_criterion, 'reduction'): | ||
| # self.adapt_criterion.reduction = reduction | ||
|
|
||
| def forward( | ||
| self, | ||
| y_pred, | ||
| y_true, | ||
| ): | ||
| """ | ||
| Parameters | ||
| ---------- | ||
| y_pred : tuple | ||
| This tuple comprises all the different data | ||
| needed to compute DA loss: | ||
| - y_pred : prediction of the source and target domains | ||
| - domain_pred : prediction of domain classifier if given | ||
| - features : features of the chosen layer | ||
| of source and target domains | ||
| - sample_domain : giving the domain of each samples | ||
| y_true : | ||
| The true labels. Available for source, masked for target. | ||
| """ | ||
| y_pred, domain_pred, features, _, sample_idx = y_pred | ||
|
|
||
| if self.train_on_target: | ||
| # In finetune mode, we only compute the base loss | ||
| return self.base_criterion(y_pred, y_true) | ||
| else: | ||
| # In adapt mode, we compute the adaptation loss | ||
| return self.adapt_criterion( | ||
| y_s=y_true, | ||
| y_pred=y_pred, | ||
| domain_pred=domain_pred, | ||
| features=features, | ||
| sample_idx=sample_idx, | ||
| ) | ||
|
|
||
|
|
||
| class TestTimeNet(DomainAwareNet): | ||
| def __init__(self, module, criterion: "TestTimeCriterion", **kwargs): | ||
| super().__init__(module, criterion=criterion, **kwargs) | ||
|
|
||
| def fit_prepare( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not fan of fit_prepare but I don't have other chouchou for now |
||
| self, X, y=None, sample_domain=None, sample_weight=None, **fit_params | ||
| ): | ||
| X = self._prepare_input(X, y, sample_domain, sample_weight) | ||
| X = X.select_source() | ||
| temp = self.criterion.train_on_target | ||
| self.criterion.train_on_target = False | ||
| self.partial_fit(X, None, **fit_params) | ||
| self.criterion.train_on_target = temp | ||
| return self | ||
|
|
||
| def fit_adapt(self, X, sample_domain=None, sample_weight=None, **fit_params): | ||
| if sample_domain is None: | ||
| sample_domain = -1 | ||
| X = self._prepare_input(X, None, sample_domain, sample_weight) | ||
| X = X.select_target() | ||
| temp = self.criterion.train_on_target | ||
| self.criterion.train_on_target = True | ||
| self.partial_fit(X, None, **fit_params) | ||
| self.criterion.train_on_target = temp | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get this temp parameters |
||
| return self | ||
|
|
||
| def fit(self, X, y=None, sample_domain=None, sample_weight=None, **fit_params): | ||
| X = self._prepare_input(X, y, sample_domain, sample_weight) | ||
| self.fit_prepare(X, **fit_params) | ||
| self.fit_adapt(X, **fit_params) | ||
| return self | ||
This file contains hidden or 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,41 @@ | ||
| # Author : Maxence Barneche | ||
| # | ||
| # License: BSD-3-Clause | ||
|
|
||
| import pytest | ||
|
|
||
| torch = pytest.importorskip("torch") | ||
|
|
||
| from torch.utils.data import DataLoader | ||
|
|
||
| from skada.datasets import make_shifted_datasets | ||
| from skada.deep._test_time import TestTimeCriterion, TestTimeNet | ||
| from skada.deep.base import DomainAwareModule | ||
| from skada.deep.losses import TestLoss | ||
| from skada.deep.modules import ToyModule2D | ||
|
|
||
|
|
||
| def test_test_time_criterion(): | ||
| num_features = 10 | ||
| module = ToyModule2D(num_features=num_features) | ||
| criterion = TestTimeCriterion(torch.nn.CrossEntropyLoss(), TestLoss()) | ||
|
|
||
| n_samples = 20 | ||
| dataset = make_shifted_datasets( | ||
| n_samples_source=n_samples, | ||
| n_samples_target=n_samples, | ||
| shift="conditional_shift", | ||
| noise=0.1, | ||
| random_state=42, | ||
| ) | ||
| X, y, sample_domain = dataset | ||
| method = TestTimeNet( | ||
| DomainAwareModule(module, "dropout"), | ||
| iterator_train=DataLoader, | ||
| criterion=criterion, | ||
| batch_size=10, | ||
| max_epochs=2, | ||
| train_split=None, | ||
| ) | ||
|
|
||
| method.fit(X, y, sample_domain) |
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.
Uh oh!
There was an error while loading. Please reload this page.