def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. :param argv: split command-line like ``sys.argv``. :type argv: list[str] :returns: the Run object corresponding to the finished run :rtype: sacred.run.Run """ if argv is None: argv = sys.argv all_commands = self.gather_commands() args = parse_args(argv, description=self.doc, commands=OrderedDict(all_commands)) config_updates, named_configs = get_config_updates(args['UPDATE']) cmd_name = args.get('COMMAND') or self.default_command try: return self.run_command(cmd_name, config_updates, named_configs, args) except Exception: if not self.current_run or self.current_run.debug: raise elif self.current_run.pdb: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace()
def generate_config(zealot, env): # check for user-scope config file user_config_path = os.path.join(os.path.expanduser('~'), '.zealot.yaml') if (os.path.exists(user_config_path)): zealot.add_config(user_config_path) # check for command-line config args = arg_parser.parse_args(sys.argv) conf_updates = arg_parser.get_config_updates(args['UPDATE']) # capture env variables sys_vars = {} for k, v in os.environ.items(): try: assert_is_valid_key(k) sys_vars[k] = v except KeyError: pass # need to be a valid identifier env.add_config({'sys_variables': sys_vars}) # create dict config = {} for c in zealot.configurations: if isinstance(c, ConfigScope): config.update(c.__call__()) else: config.update(c._conf) config.update(conf_updates[0]) return config
def main(): parser = argparse.ArgumentParser() parser.add_argument( "experiment_name", help="Name of the experiment's package in the ./experiments directory.", type=str, ) parser.add_argument( "--skip_observe", help="Whether to add an sacred observer.", type=bool, default=False, nargs="?", ) args, unknown_args = parser.parse_known_args() config_updates, _ = get_config_updates( [arg for arg in unknown_args if arg != "with"] ) experiment_module = importlib.import_module( f"experiments.{args.experiment_name}.main" ) if args.skip_observe is False: experiment_module.ex.observers.append( FileStorageObserver.create(f"./tmp/{args.experiment_name}") ) experiment_module.ex.run(config_updates=config_updates)
def get_environment_yaml(ex): """ Get the name of the environment_yaml file that should be specified in the command line as: 'python main.py -p with environment.config_file=<env_config_file>.yaml [...]' """ _, _, usage = ex.get_usage() args = docopt(usage, [str(a) for a in sys.argv[1:]], help=False) config_updates, _ = get_config_updates(args['UPDATE']) # updates = arg_parser.get_config_updates(args['UPDATE'])[0] environment_yaml = config_updates.get('environment', {}).get('config_file', None) return environment_yaml
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. Parameters ---------- argv : list[str] or str, optional Command-line as string or list of strings like ``sys.argv``. Returns ------- sacred.run.Run The Run object corresponding to the finished run. """ if argv is None: argv = sys.argv elif isinstance(argv, basestring): argv = shlex.split(argv) else: if not isinstance(argv, (list, tuple)): raise ValueError("argv must be str or list, but was {}".format( type(argv))) if not all([isinstance(a, basestring) for a in argv]): problems = [a for a in argv if not isinstance(a, basestring)] raise ValueError("argv must be list of str but contained the " "following elements: {}".format(problems)) all_commands = self.gather_commands() args = parse_args(argv, description=self.doc, commands=OrderedDict(all_commands)) config_updates, named_configs = get_config_updates(args['UPDATE']) cmd_name = args.get('COMMAND') or self.default_command try: return self.run(cmd_name, config_updates, named_configs, {}, args) except Exception: if not self.current_run or self.current_run.debug: raise elif self.current_run.pdb: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace() exit(1)
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. Parameters ---------- argv : list[str] or str, optional Command-line as string or list of strings like ``sys.argv``. Returns ------- sacred.run.Run The Run object corresponding to the finished run. """ argv = ensure_wellformed_argv(argv) short_usage, usage, internal_usage = self.get_usage() args = docopt(internal_usage, [str(a) for a in argv[1:]], help=False) cmd_name = args.get('COMMAND') or self.default_command config_updates, named_configs = get_config_updates(args['UPDATE']) err = self._check_command(cmd_name) if not args['help'] and err: print(short_usage) print(err) exit(1) if self._handle_help(args, usage): exit() try: return self.run(cmd_name, config_updates, named_configs, {}, args) except Exception: if not self.current_run or self.current_run.debug: raise elif self.current_run.pdb: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace() exit(1)
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. Parameters ---------- argv : list[str] or str, optional Command-line as string or list of strings like ``sys.argv``. Returns ------- sacred.run.Run The Run object corresponding to the finished run. """ argv = ensure_wellformed_argv(argv) short_usage, usage = self.get_usage() args = docopt(usage, [str(a) for a in argv[1:]], help=False) cmd_name = args.get('COMMAND') or self.default_command config_updates, named_configs = get_config_updates(args['UPDATE']) err = self._check_command(cmd_name) if not args['help'] and err: print(short_usage) print(err) exit(1) if self._handle_help(args, usage): exit() try: return self.run(cmd_name, config_updates, named_configs, {}, args) except Exception: if not self.current_run or self.current_run.debug: raise elif self.current_run.pdb: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace() exit(1)
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. :param argv: split command-line like ``sys.argv``. :type argv: list[str] :returns: the Run object corresponding to the finished run :rtype: sacred.run.Run """ if argv is None: argv = sys.argv all_commands = self._gather_commands() args = parse_args(argv, description=self.doc, commands=OrderedDict(all_commands)) config_updates, named_configs = get_config_updates(args['UPDATE']) loglevel = args.get('--logging') for obs in get_observers(args): if obs not in self.observers: self.observers.append(obs) if args['COMMAND']: cmd_name = args['COMMAND'] else: cmd_name = self.default_command try: return self.run_command(cmd_name, config_updates=config_updates, named_configs_to_use=named_configs, log_level=loglevel) except Exception: if args['--debug']: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace()
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. :param argv: split command-line like ``sys.argv``. :type argv: list[str] :return: The result of the command that was run. """ if argv is None: argv = sys.argv all_commands = self._gather_commands() args = parse_args(argv, description=self.doc, commands=OrderedDict(all_commands)) config_updates, named_configs = get_config_updates(args['UPDATE']) loglevel = args.get('--logging') for obs in get_observers(args): if obs not in self.observers: self.observers.append(obs) if args['COMMAND']: cmd_name = args['COMMAND'] else: cmd_name = self.default_command try: return self.run_command(cmd_name, config_updates=config_updates, named_configs_to_use=named_configs, loglevel=loglevel) except: if args['--debug']: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace()
import torch import torch.nn.functional as F import torchvision from sacred.arg_parser import get_config_updates from sacred.observers import FileStorageObserver from sacred.observers import MongoObserver from torch.utils.data import DataLoader from torchvision import transforms import mnist_loss from mnist_loss import MultitaskMnistLoss from mnist_model import MultitaskMnistModel ex = sacred.Experiment() config_updates, _ = get_config_updates(sys.argv) # Disable saving to mongo using "with save_to_db=False" if ("save_to_db" not in config_updates) or config_updates["save_to_db"]: # Server disabled, credentials useless. mongo_observer = MongoObserver.create( url='mongodb://*****:*****@134.209.21.201/admin?retryWrites=true', db_name='multitask-learning') ex.observers.append(mongo_observer) else: ex.observers.append(FileStorageObserver.create('multitask_results')) @ex.config def config(): """Default config values."""
def test_get_config_updates(update, expected): assert get_config_updates(update) == (expected, [])
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. Parameters ---------- argv : list[str] or str, optional Command-line as string or list of strings like ``sys.argv``. Returns ------- sacred.run.Run The Run object corresponding to the finished run. """ argv = ensure_wellformed_argv(argv) short_usage, usage, internal_usage = self.get_usage() args = docopt(internal_usage, [str(a) for a in argv[1:]], help=False) cmd_name = args.get('COMMAND') or self.default_command config_updates, named_configs = get_config_updates(args['UPDATE']) err = self._check_command(cmd_name) if not args['help'] and err: print(short_usage) print(err) exit(1) if self._handle_help(args, usage): exit() try: return self.run(cmd_name, config_updates, named_configs, {}, args) except Exception as e: if self.current_run: debug = self.current_run.debug else: # The usual command line options are applied after the run # object is built completely. Some exceptions (e.g. # ConfigAddedError) are raised before this. In these cases, # the debug flag must be checked manually. debug = args.get('--debug', False) if debug: # Debug: Don't change behaviour, just re-raise exception raise elif self.current_run and self.current_run.pdb: # Print exception and attach pdb debugger import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: # Handle pretty printing of exceptions. This includes # filtering the stacktrace and printing the usage, as # specified by the exceptions attributes if isinstance(e, SacredError): print(format_sacred_error(e, short_usage), file=sys.stderr) else: print_filtered_stacktrace() exit(1)
def run_commandline(self, argv=None): """ Run the command-line interface of this experiment. If ``argv`` is omitted it defaults to ``sys.argv``. Parameters ---------- argv : list[str] or str, optional Command-line as string or list of strings like ``sys.argv``. Returns ------- sacred.run.Run The Run object corresponding to the finished run. """ if argv is None: argv = sys.argv elif isinstance(argv, basestring): argv = shlex.split(argv) else: if not isinstance(argv, (list, tuple)): raise ValueError("argv must be str or list, but was {}" .format(type(argv))) if not all([isinstance(a, basestring) for a in argv]): problems = [a for a in argv if not isinstance(a, basestring)] raise ValueError("argv must be list of str but contained the " "following elements: {}".format(problems)) short_usage, usage = self.get_usage() args = docopt(usage, [str(a) for a in argv[1:]], help=False) commands = OrderedDict(self.gather_commands()) cmd_name = args.get('COMMAND') or self.default_command config_updates, named_configs = get_config_updates(args['UPDATE']) if cmd_name is not None and cmd_name not in commands: print(short_usage) print('Error: Command "{}" not found. Available commands are: ' '{}'.format(cmd_name, ", ".join(commands.keys()))) exit(1) if args['help'] or args['--help']: if args['COMMAND'] is None: print(usage) else: print(help_for_command(commands[args['COMMAND']])) exit() if cmd_name is None: print(short_usage) print('Error: No command found to be run. Specify a command' ' or define main function. Available commands' ' are: {}'.format(", ".join(commands.keys()))) exit(1) try: return self.run(cmd_name, config_updates, named_configs, {}, args) except Exception: if not self.current_run or self.current_run.debug: raise elif self.current_run.pdb: import traceback import pdb traceback.print_exception(*sys.exc_info()) pdb.post_mortem() else: print_filtered_stacktrace() exit(1)
os.makedirs('./output/stacking/', exist_ok=True) import socket host_name = socket.gethostname() conf_name = 'retinanet' best_score = trainer.best_score oof_file = f'./output/stacking/{version}_{host_name[:5]}_s{best_score:6.5f}_{conf_name}_f{val_fold}_val{len(oof_val)}.h5' print(f'Stacking file save to:{oof_file}') save_stack_feature(oof_val, oof_test, oof_file) if __name__ == '__main__': from sacred.arg_parser import get_config_updates import sys config_updates, named_configs = get_config_updates(sys.argv[1:]) conf_name = config_updates.get('conf_name') fold = config_updates.get('fold') locker = task_locker( 'mongodb://*****:*****@10.10.20.103:27017/db?authSource=admin', remove_failed=9, version=version) task_id = f'lung_{conf_name}_{fold}' #pydevd_pycharm.settrace('192.168.1.101', port=1234, stdoutToServer=True, stderrToServer=True) with locker.lock_block(task_id=task_id) as lock_id: if lock_id is not None: ex.add_config({ 'lock_id': lock_id, 'lock_name': task_id, 'version': version,
def save_checkpoint(model, optimizer, epoch, dataloaders, checkpoint_dir, use_thread=False): dict_to_save = { 'state_dict': model.state_dict(), 'epoch': epoch, 'model': model, 'optimizer': optimizer, 'curr_iter_idx': dataloaders['train'].curr_iter_idx, } checkpoints.save_checkpoint(dict_to_save, checkpoint_dir, epoch) return [] if __name__ == '__main__': assert LOG_DIR, 'log dir cannot be empty' # Manually parse command line opts short_usage, usage, internal_usage = ex.get_usage() args = docopt(internal_usage, [str(a) for a in sys.argv[1:]], help=False) config_updates, named_configs = get_config_updates(args['UPDATE']) ex.run('prologue', config_updates, named_configs, options=args) ex.observers.append(FileStorageObserverWithExUuid.create(LOG_DIR)) ex.run_commandline() else: print(__name__)
python -u customer/classify.py main with conf_name=5cls_efficientnet-b0 fold=0 version=r1 python -u customer/classify.py main with conf_name=ens_res_den_vgg fold=0 version=r1 python -u customer/classify.py main with conf_name=5cls_efficientnet-b6 image_size=400 fold=0 version=r1 python -u customer/classify.py main with backbone=efficientnet-b3 image_size=300 fold=0 version=r1 python -u customer/classify.py main with backbone=resnet34 fold=0 version=c0 model_type='fastai' lock_layer=0 python -u customer/classify.py main with conf_name=ens_res_den_vgg fold=0 version=c0 model_type='fastai' lock_layer=0 """ from sacred.arg_parser import get_config_updates import sys argv_conf, _ = get_config_updates(sys.argv[1:]) conf_name = argv_conf.get('conf_name') real_conf = get_file_conf(conf_name) real_conf.update(argv_conf) real_conf.conf_name = conf_name or real_conf.backbone print(real_conf) if 'version' in real_conf : version = real_conf.get('version') locker = task_locker('mongodb://*****:*****@10.10.20.103:27017/db?authSource=admin', remove_failed =9 , version=version) task_id = f'lung_{real_conf}' #pydevd_pycharm.settrace('192.168.1.101', port=1234, stdoutToServer=True, stderrToServer=True) with locker.lock_block(task_id=task_id) as lock_id: if lock_id is not None: ex.add_config({
from sys import argv from sacred.arg_parser import get_config_updates from imitation_learning.imitate import imitate from imitation_learning.utils.experiment import combine_configs from imitation_learning.utils.commands import normalize_command command, *updates = argv[1:] config_updates, config_paths = get_config_updates(updates) configs = combine_configs(config_paths, config_updates) configs['command'] = command commands = {'main': imitate, 'normalize': normalize_command} assert command in commands, f"Command {command} not found" commands[command](configs)