Skip to content

Commit

Permalink
Release/post release update 2 (#963)
Browse files Browse the repository at this point in the history
* readme update

* v20.10.1

* docs fix

* docs fix
  • Loading branch information
Scitator authored Oct 15, 2020
2 parents c9587f3 + 88a1c53 commit fe285fc
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 33 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
-


## [20.10] - 2020-10-14
## [20.10.1] - 2020-10-15

### Added

Expand All @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- experiments, runners, callbacks grouped by primitives under `catalyst.experiments`/`catalyst.runners`/`catalyst.callbacks` respectively
- settings and typing moved from `catalyst.tools.*` to `catalyst.*`
- utils moved from `catalyst.*.utils` to `catalyst.utils`
- swa moved to `catalyst.utils` ([#963](https://github.com/catalyst-team/catalyst/pull/963))

### Removed

Expand All @@ -56,6 +57,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- `AMPOptimizerCallback` - fix grad clip fn support ([#948](https://github.com/catalyst-team/catalyst/pull/948))
- removed deprecated docs types ([#947](https://github.com/catalyst-team/catalyst/pull/947)) ([#952](https://github.com/catalyst-team/catalyst/pull/952))
- docs for a few files ([#952](https://github.com/catalyst-team/catalyst/pull/952))
- extra backward compatibility fixes ([#963](https://github.com/catalyst-team/catalyst/pull/963))


## [20.09.1] - 2020-09-25
Expand Down
3 changes: 3 additions & 0 deletions catalyst/contrib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
raise ex


COMMANDS = OrderedDict(sorted(COMMANDS.items()))


def build_parser() -> ArgumentParser:
"""Builds parser.
Expand Down
3 changes: 3 additions & 0 deletions catalyst/contrib/dl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# flake8: noqa
# backward compatibility
from catalyst.dl import *
3 changes: 3 additions & 0 deletions catalyst/data/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
raise ex


COMMANDS = OrderedDict(sorted(COMMANDS.items()))


def build_parser() -> ArgumentParser:
"""Builds parser.
Expand Down
7 changes: 5 additions & 2 deletions catalyst/dl/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from collections import OrderedDict

from catalyst.__version__ import __version__
from catalyst.dl.scripts import quantize, run, trace
from catalyst.dl.scripts import quantize, run, swa, trace
from catalyst.settings import IS_GIT_AVAILABLE, IS_OPTUNA_AVAILABLE

COMMANDS = OrderedDict(
[("run", run), ("trace", trace), ("quantize", quantize)]
[("quantize", quantize), ("run", run), ("swa", swa), ("trace", trace)]
)
if IS_GIT_AVAILABLE:
from catalyst.dl.scripts import init
Expand All @@ -18,6 +18,9 @@
COMMANDS["tune"] = tune


COMMANDS = OrderedDict(sorted(COMMANDS.items()))


def build_parser() -> ArgumentParser:
"""Builds parser.
Expand Down
14 changes: 9 additions & 5 deletions catalyst/dl/scripts/swa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

import torch

from catalyst.dl.utils.swa import generate_averaged_weights
from catalyst.utils.swa import get_averaged_weights_by_path_mask


def build_args(parser: ArgumentParser):
"""Builds the command line parameters."""
parser.add_argument(
"--logdir", type=Path, default=None, help="Path to models logdir"
)
parser.add_argument(
"--model-mask",
"--models-mask",
"-m",
type=str,
default="*.pth",
help="Pattern for models to average",
dest="models_mask",
)
parser.add_argument(
"--logdir", type=Path, default=None, help="Path to experiment logdir"
)
parser.add_argument(
"--output-path",
Expand All @@ -43,7 +45,9 @@ def main(args, _):
models_mask: str = args.models_mask
output_path: Path = args.output_path

averaged_weights = generate_averaged_weights(logdir, models_mask)
averaged_weights = get_averaged_weights_by_path_mask(
path_mask=models_mask, logdir=logdir
)

torch.save(averaged_weights, str(output_path))

Expand Down
3 changes: 3 additions & 0 deletions catalyst/dl/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# flake8: noqa
# backward compatibility
from catalyst.utils import *
4 changes: 4 additions & 0 deletions catalyst/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
distributed_cmd_run,
)
from catalyst.utils.seed import set_global_seed
from catalyst.utils.swa import (
average_weights,
get_averaged_weights_by_path_mask,
)
from catalyst.utils.sys import (
get_environment_vars,
list_conda_packages,
Expand Down
47 changes: 25 additions & 22 deletions catalyst/dl/utils/swa.py → catalyst/utils/swa.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
from catalyst.utils.checkpoint import load_checkpoint


def _load_weights(path: str) -> dict:
"""
Load weights of a model.
Args:
path: Path to model weights
Returns:
Weights
"""
weights = load_checkpoint(path)
if "model_state_dict" in weights:
weights = weights["model_state_dict"]
return weights


def average_weights(state_dicts: List[dict]) -> OrderedDict:
"""
Averaging of input weights.
Expand Down Expand Up @@ -43,43 +59,30 @@ def average_weights(state_dicts: List[dict]) -> OrderedDict:
return average_dict


def load_weight(path: str) -> dict:
"""
Load weights of a model.
Args:
path: Path to model weights
Returns:
Weights
"""
weights = load_checkpoint(path)
if "model_state_dict" in weights:
weights = weights["model_state_dict"]
return weights


def generate_averaged_weights(
logdir: Union[str, Path], models_mask: str
def get_averaged_weights_by_path_mask(
path_mask: str, logdir: Union[str, Path] = None,
) -> OrderedDict:
"""
Averaging of input weights and saving them.
Args:
path_mask: globe-like pattern for models to average
logdir: Path to logs directory
models_mask: globe-like pattern for models to average
Returns:
Averaged weights
"""
if logdir is None:
models_pathes = glob.glob(models_mask)
models_pathes = glob.glob(path_mask)
else:
models_pathes = glob.glob(
os.path.join(logdir, "checkpoints", models_mask)
os.path.join(logdir, "checkpoints", path_mask)
)

all_weights = [load_weight(path) for path in models_pathes]
all_weights = [_load_weights(path) for path in models_pathes]
averaged_dict = average_weights(all_weights)

return averaged_dict


__all__ = ["average_weights", "get_averaged_weights_by_path_mask"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import torch
import torch.nn as nn

from catalyst.dl.utils.swa import generate_averaged_weights
from catalyst.utils.checkpoint import load_checkpoint
from catalyst.utils.swa import get_averaged_weights_by_path_mask


class Net(nn.Module):
Expand Down Expand Up @@ -38,8 +38,8 @@ def tearDown(self):

def test_averaging(self):
"""Test SWA method."""
weights = generate_averaged_weights(
logdir=Path("./"), models_mask="net*"
weights = get_averaged_weights_by_path_mask(
logdir=Path("./"), path_mask="net*"
)
torch.save(weights, str("./checkpoints/swa_weights.pth"))
model = Net()
Expand Down
7 changes: 7 additions & 0 deletions docs/api/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ Seed
:undoc-members:
:show-inheritance:

Stochastic Weights Averaging (SWA)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: catalyst.utils.swa
:members:
:undoc-members:
:show-inheritance:

Sys
~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: catalyst.utils.sys
Expand Down

0 comments on commit fe285fc

Please sign in to comment.