-
Notifications
You must be signed in to change notification settings - Fork 322
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
add types in bolts/callbacks #444
Merged
Merged
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
95cb8b1
add types in bolts/callbacks
660856d
flake8/pep8
d9601f3
Merge remote-tracking branch 'upstream/master' into types/callbacks
3fd7e18
merge master
9a985ff
suggestions
0a0fb72
type: ignore more specific
1a8288f
type: ignore more specific
e1f7dda
type: ignore more specific
6770bc3
install matplotlib
5eb8e20
install matplotlib
f69a505
Apply suggestions from code review
Borda 00f8c2c
flake8
Borda 35cfcaf
Update pl_bolts/callbacks/vision/confused_logit.py
468340a
fix pep8
bf8c76b
Merge remote-tracking branch 'upstream/master' into types/callbacks
c363d9b
merge master
a9b6f72
flake8 and isort
c1916fb
install req
3b4a7f3
Apply suggestions from code review
fe24fcc
install req
2099feb
Merge branch 'types/callbacks' of https://github.com/ydcjeff/pytorch-…
0ef7f82
Update pl_bolts/callbacks/vision/confused_logit.py
928419b
Merge remote-tracking branch 'upstream/master' into types/callbacks
e2e8a36
isort
798f68b
Merge branch 'types/callbacks' of https://github.com/ydcjeff/pytorch-…
db6637d
flake8
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
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
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 |
---|---|---|
|
@@ -4,10 +4,11 @@ | |
import torch | ||
import torch.nn as nn | ||
from pytorch_lightning import Callback, LightningModule, Trainer | ||
from pytorch_lightning.loggers import TensorBoardLogger, WandbLogger | ||
from pytorch_lightning.loggers import LightningLoggerBase, TensorBoardLogger, WandbLogger | ||
from pytorch_lightning.utilities import rank_zero_warn | ||
from pytorch_lightning.utilities.apply_func import apply_to_collection | ||
from torch import Tensor | ||
from torch.nn import Module | ||
from torch.utils.hooks import RemovableHandle | ||
|
||
from pl_bolts.utils import _WANDB_AVAILABLE | ||
|
@@ -38,22 +39,22 @@ def __init__(self, log_every_n_steps: int = None): | |
interval defined in the Trainer. Use this to override the Trainer default. | ||
""" | ||
super().__init__() | ||
self._log_every_n_steps = log_every_n_steps | ||
self._log_every_n_steps: Optional[int] = log_every_n_steps | ||
self._log = False | ||
self._trainer = None | ||
self._train_batch_idx = None | ||
self._trainer: Trainer | ||
self._train_batch_idx: int | ||
|
||
def on_train_start(self, trainer, pl_module): | ||
def on_train_start(self, trainer: Trainer, pl_module: LightningModule) -> None: | ||
self._log = self._is_logger_available(trainer.logger) | ||
self._log_every_n_steps = self._log_every_n_steps or trainer.log_every_n_steps | ||
self._log_every_n_steps = self._log_every_n_steps or trainer.log_every_n_steps # type: ignore[attr-defined] | ||
self._trainer = trainer | ||
|
||
def on_train_batch_start( | ||
self, trainer, pl_module, batch, batch_idx, dataloader_idx | ||
): | ||
self, trainer: Trainer, pl_module: LightningModule, batch: Sequence, batch_idx: int, dataloader_idx: int | ||
) -> None: | ||
self._train_batch_idx = batch_idx | ||
|
||
def log_histograms(self, batch, group="") -> None: | ||
def log_histograms(self, batch: Sequence, group: str = "") -> None: | ||
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. same here |
||
""" | ||
Logs the histograms at the interval defined by `row_log_interval`, given a logger is available. | ||
|
||
|
@@ -64,11 +65,11 @@ def log_histograms(self, batch, group="") -> None: | |
Each label also has the tensors's shape as suffix. | ||
group: Name under which the histograms will be grouped. | ||
""" | ||
if not self._log or (self._train_batch_idx + 1) % self._log_every_n_steps != 0: | ||
if not self._log or (self._train_batch_idx + 1) % self._log_every_n_steps != 0: # type: ignore[operator] | ||
return | ||
|
||
batch = apply_to_collection(batch, dtype=np.ndarray, function=torch.from_numpy) | ||
named_tensors = {} | ||
named_tensors: Dict[str, Tensor] = {} | ||
collect_and_name_tensors(batch, output=named_tensors, parent_name=group) | ||
|
||
for name, tensor in named_tensors.items(): | ||
|
@@ -100,7 +101,7 @@ def log_histogram(self, tensor: Tensor, name: str) -> None: | |
data={name: wandb.Histogram(tensor)}, commit=False, | ||
) | ||
|
||
def _is_logger_available(self, logger) -> bool: | ||
def _is_logger_available(self, logger: LightningLoggerBase) -> bool: | ||
available = True | ||
if not logger: | ||
rank_zero_warn("Cannot log histograms because Trainer has no logger.") | ||
|
@@ -154,9 +155,9 @@ def __init__( | |
""" | ||
super().__init__(log_every_n_steps=log_every_n_steps) | ||
self._submodule_names = submodules | ||
self._hook_handles = [] | ||
self._hook_handles: List = [] | ||
|
||
def on_train_start(self, trainer: Trainer, pl_module: LightningModule): | ||
def on_train_start(self, trainer: Trainer, pl_module: LightningModule) -> None: | ||
super().on_train_start(trainer, pl_module) | ||
submodule_dict = dict(pl_module.named_modules()) | ||
self._hook_handles = [] | ||
|
@@ -170,7 +171,7 @@ def on_train_start(self, trainer: Trainer, pl_module: LightningModule): | |
handle = self._register_hook(name, submodule_dict[name]) | ||
self._hook_handles.append(handle) | ||
|
||
def on_train_end(self, trainer, pl_module): | ||
def on_train_end(self, trainer: Trainer, pl_module: LightningModule) -> None: | ||
for handle in self._hook_handles: | ||
handle.remove() | ||
|
||
|
@@ -198,7 +199,7 @@ def _register_hook(self, module_name: str, module: nn.Module) -> RemovableHandle | |
else self.GROUP_NAME_OUTPUT | ||
) | ||
|
||
def hook(_, inp, out): | ||
def hook(_: Module, inp: Sequence, out: Sequence) -> None: | ||
inp = inp[0] if len(inp) == 1 else inp | ||
self.log_histograms(inp, group=input_group_name) | ||
self.log_histograms(out, group=output_group_name) | ||
|
@@ -228,7 +229,14 @@ def __init__(self, log_every_n_steps: int = None): | |
""" | ||
super().__init__(log_every_n_steps=log_every_n_steps) | ||
|
||
def on_train_batch_start(self, trainer, pl_module, batch, *args, **kwargs): | ||
def on_train_batch_start( | ||
self, | ||
trainer: Trainer, | ||
pl_module: LightningModule, | ||
batch: Sequence, | ||
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. same here |
||
*args: int, | ||
**kwargs: int, | ||
ydcjeff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> None: | ||
super().on_train_batch_start(trainer, pl_module, batch, *args, **kwargs) | ||
self.log_histograms(batch, group=self.GROUP_NAME) | ||
|
||
|
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
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.
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.
for batch it is too strict, this could be almost anything (Any)