-
Notifications
You must be signed in to change notification settings - Fork 13
Wandb sweeps #254
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
Merged
Merged
Wandb sweeps #254
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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
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,78 @@ | ||
| import argparse | ||
|
|
||
|
|
||
| def get_parser(**parser_kwargs): | ||
| def str2bool(v): | ||
| if isinstance(v, bool): | ||
| return v | ||
| if v.lower() in ("yes", "true", "t", "y", "1"): | ||
| return True | ||
| elif v.lower() in ("no", "false", "f", "n", "0"): | ||
| return False | ||
| else: | ||
| raise argparse.ArgumentTypeError("Boolean value expected.") | ||
|
|
||
| parser = argparse.ArgumentParser(**parser_kwargs) | ||
| parser.add_argument( | ||
| "-n", "--name", metavar="description", help="postfix of log directory." | ||
| ) | ||
| parser.add_argument( | ||
| "-b", | ||
| "--base", | ||
| nargs="*", | ||
| metavar="base_config.yaml", | ||
| help="paths to base configs. Loaded from left-to-right. " | ||
| "Parameters can be overwritten or added with command-line options of the form `--key value`.", | ||
| default=None, | ||
| ) | ||
| parser.add_argument( | ||
| "-t", | ||
| "--train", | ||
| type=str2bool, | ||
| const=True, | ||
| default=False, | ||
| nargs="?", | ||
| help="run in training mode", | ||
| ) | ||
| parser.add_argument("-p", "--project", help="path to existing project") | ||
| parser.add_argument("-c", "--checkpoint", help="path to existing checkpoint") | ||
| parser.add_argument( | ||
| "-r", | ||
| "--retrain", | ||
| type=str2bool, | ||
| const=True, | ||
| default=False, | ||
| nargs="?", | ||
| help="reset global step", | ||
| ) | ||
| parser.add_argument( | ||
| "-l", | ||
| "--log_level", | ||
| metavar="LEVEL", | ||
| type=str, | ||
| choices=["warn", "info", "debug", "critical"], | ||
| default="info", | ||
| help="set the logging level.", | ||
| ) | ||
| parser.add_argument( | ||
| "-d", | ||
| "--debug", | ||
| type=str2bool, | ||
| nargs="?", | ||
| const=True, | ||
| default=False, | ||
| help="enable post-mortem debugging", | ||
| ) | ||
| parser.add_argument( | ||
| "-w", | ||
| "--wandb_sweep", | ||
| nargs="?", | ||
| const=True, | ||
| type=str2bool, | ||
| default=False, | ||
| help="Process additional arguments supplied by wandb's sweep mechanism," | ||
| "i.e. replace dots ('.') with slashes ('/') in the argument name: " | ||
| "--par.at.level=3 => --par/at/level 3", | ||
| ) | ||
|
|
||
| return parser | ||
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
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
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,112 @@ | ||
| #!/usr/bin/env python3 | ||
| ''' | ||
| This script is meant to be used together with wandb's great sweep | ||
| functionality. The workflow looks as follows: | ||
|
|
||
|
|
||
| 1. Create a sweep file from your wandb web client. You can put in anything | ||
| under ``Settings/Training Script`` or leave it as it is. This value will be | ||
| changed to match you local edflow later on. Choose all parameters you want | ||
| to sweep over and nothgin else. Don't worry about the dots in the preview, | ||
| these will be fixed automatically later on. | ||
|
|
||
| 2. Download the sweep file to your prefered location. | ||
|
|
||
| 3. Navigate to the script location and run ``edprep <sweep_file.yaml> [all | ||
| additional parameters]. Add all additional parameters as you would when | ||
| running ``edflow``. Usually those include ``-t -b | ||
| <path/to/some/config.yaml> -n <sweep name>``. You can also add any other | ||
| parameter as if working with edflow. All parameters are added to the sweep | ||
| as constants, which are not varied. | ||
|
|
||
| 4. Now follow the wandb manual: run ``wandb sweep <sweep_file.yaml>`` | ||
|
|
||
| 5. copy the command from the output of step 4 and run it. It should look | ||
| something like this: ``wandb agent <sweep_id>``. | ||
| ''' | ||
|
|
||
| import os | ||
| import argparse # noqa | ||
| import yaml # noqa | ||
| import time | ||
| import subprocess | ||
|
|
||
| from edflow.config import parse_unknown_args | ||
| from edflow.args import get_parser | ||
|
|
||
|
|
||
| def load_sweep(sweep_file_path): | ||
| '''Loads a yaml file in read only mode''' | ||
| with open(sweep_file_path, 'r') as sf: | ||
| content = yaml.safe_load(sf) | ||
|
|
||
| return content | ||
|
|
||
|
|
||
| def prepare_content(content, opt, additional_kwargs): | ||
| '''Exchanges the program parameter of the sweep file with the local edflow | ||
| executable and adds all commandline parameters as constant parameters for | ||
| the sweep arguments. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| content : dict | ||
| content of the sweep file. Needs at least the key ``parameters``. | ||
| opt : Namespace | ||
| Parsed arguments from the commandline. | ||
| additional_kwargs : dict | ||
| Preprocessed addtitional commandline kwargs. | ||
|
|
||
| Returns | ||
| ------- | ||
| content : dict | ||
| Updated version of the content. | ||
| ''' | ||
| edexec = subprocess.check_output("which edflow", shell=True).decode("utf-8") | ||
| edexec = edexec.replace('\n', '') | ||
| content['program'] = edexec | ||
|
|
||
| for key, value in additional_kwargs.items(): | ||
| _add_const_parameter(content, key, value) | ||
|
|
||
| for [key, value] in opt._get_kwargs(): | ||
| if value is not None: | ||
| _add_const_parameter(content, key, value) | ||
|
|
||
| return content | ||
|
|
||
| def _add_const_parameter(content, key, value): | ||
| if isinstance(value, list): | ||
| value = ' '.join(value) | ||
| par_dict = {'value': value, 'distribution': 'constant'} | ||
| content['parameters'][key] = par_dict | ||
|
|
||
|
|
||
| def store_sweep(content, sweep_file_path): | ||
| '''Stores the updated sweep file and makes a backup of the old one.''' | ||
| os.rename(sweep_file_path, f'.{sweep_file_path}.{time.time()}.backup') | ||
| with open(sweep_file_path, 'w') as sf: | ||
| sf.write(yaml.safe_dump(content, indent=2)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
| parents=[get_parser(add_help=False)], | ||
| description=__doc__ + '\nNote: the option ``--wand_sweep`` will ' | ||
| 'always be set to True!', | ||
| formatter_class=argparse.RawTextHelpFormatter) | ||
| parser.add_argument('sweep', metavar='SWEEP.YAML', | ||
| help='Sweep File as created by wandb. See ' | ||
| 'https://docs.wandb.com/sweeps for more.') | ||
|
|
||
| opt, unknown = parser.parse_known_args() | ||
|
|
||
| if not opt.wandb_sweep: | ||
| opt.wandb_sweep = True | ||
|
|
||
| additional_kwargs = parse_unknown_args(unknown, opt.wandb_sweep) | ||
|
|
||
| content = load_sweep(opt.sweep) | ||
| content = prepare_content(content, opt, additional_kwargs) | ||
| store_sweep(content, opt.sweep) |
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
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
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
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.