def save(self, fname=None): """ Save the model parameters into <<fname>>_opt.json (or <<ser_file>>_opt.json) and model weights into <<fname>>.h5 (or <<ser_file>>.h5) Args: fname: file_path to save model. If not explicitly given seld.opt["ser_file"] will be used Returns: None """ if not fname: fname = self.save_path else: fname = Path(fname).resolve() if not fname.parent.is_dir(): raise ConfigError("Provided save path is incorrect!") else: opt_path = f"{fname}_opt.json" weights_path = f"{fname}.h5" log.info(f"[saving model to {opt_path}]") self.model.save_weights(weights_path) save_json(self.opt, opt_path) return True
def save(self, fname: str = None) -> None: """ Save the model parameters into <<fname>>_opt.json (or <<ser_file>>_opt.json) and model weights into <<fname>>.h5 (or <<ser_file>>.h5) Args: fname: file_path to save model. If not explicitly given seld.opt["ser_file"] will be used Returns: None """ if not fname: fname = self.save_path else: fname = Path(fname).resolve() if not fname.parent.is_dir(): raise ConfigError("Provided save path is incorrect!") else: opt_path = f"{fname}_opt.json" weights_path = f"{fname}.h5" log.info(f"[saving model to {opt_path}]") self.model.save_weights(weights_path) # if model was loaded from one path and saved to another one # then change load_path to save_path for config self.opt["epochs_done"] = self.epochs_done self.opt["final_learning_rate"] = K.eval(self.optimizer.lr) / (1. + K.eval(self.optimizer.decay) * self.batches_seen) if self.opt.get("load_path") and self.opt.get("save_path"): if self.opt.get("save_path") != self.opt.get("load_path"): self.opt["load_path"] = str(self.opt["save_path"]) save_json(self.opt, opt_path)
def save(self, fname: str = None) -> None: """ Save the model parameters into <<fname>>_opt.json (or <<ser_file>>_opt.json) and model weights into <<fname>>.h5 (or <<ser_file>>.h5) Args: fname: file_path to save model. If not explicitly given seld.opt["ser_file"] will be used Returns: None """ if not fname: fname = self.save_path else: fname = Path(fname).resolve() if not fname.parent.is_dir(): raise ConfigError("Provided save path is incorrect!") else: opt_path = f"{fname}_opt.json" weights_path = f"{fname}.h5" log.info(f"[saving model to {opt_path}]") self.model.save_weights(weights_path) # if model was loaded from one path and saved to another one # then change load_path to save_path for config self.opt["epochs_done"] = self.epochs_done self.opt["final_lear_rate"] = K.eval(self.optimizer.lr) / (1. + K.eval(self.optimizer.decay) * self.batches_seen) if self.opt.get("load_path") and self.opt.get("save_path"): if self.opt.get("save_path") != self.opt.get("load_path"): self.opt["load_path"] = str(self.opt["save_path"]) save_json(self.opt, opt_path)
def run_population(population, evolution, gpus): """ Change save and load paths for obtained population, save config.json with model config, run population via current python executor (with which evolve.py already run) and on given devices (-1 means CPU, other integeres - visible for evolve.py GPUs) Args: population: list of dictionaries - configs of current population evolution: ParamsEvolution gpus: list of given devices (list of integers) Returns: None """ population_size = len(population) for k in range(population_size // len(gpus) + 1): procs = [] for j in range(len(gpus)): i = k * len(gpus) + j if i < population_size: save_path = expand_path( Path( evolution.get_value_from_config( population[i], evolution.main_model_path + ["save_path"])).parent) save_path.mkdir(parents=True, exist_ok=True) f_name = save_path.joinpath("config.json") save_json(population[i], f_name) with save_path.joinpath('out.txt').open('w', encoding='utf8') as outlog,\ save_path.joinpath('err.txt').open('w', encoding='utf8') as errlog: env = dict(os.environ) if len(gpus) > 1 or gpus[0] != -1: env['CUDA_VISIBLE_DEVICES'] = str(gpus[j]) procs.append( Popen("{} -m deeppavlov train {}".format( sys.executable, str(f_name)), shell=True, stdout=outlog, stderr=errlog, env=env)) for j, proc in enumerate(procs): i = k * len(gpus) + j log.info(f'Waiting on {i}th proc') if proc.wait() != 0: save_path = expand_path( Path( evolution.get_value_from_config( population[i], evolution.main_model_path + ["save_path"])).parent) with save_path.joinpath('err.txt').open( encoding='utf8') as errlog: log.warning( f'Population {i} returned an error code {proc.returncode} and an error log:\n' + errlog.read()) return None
def run_population(population, evolution, gpus): """ Change save and load paths for obtained population, save config.json with model config, run population via current python executor (with which evolve.py already run) and on given devices (-1 means CPU, other integeres - visible for evolve.py GPUs) Args: population: list of dictionaries - configs of current population evolution: ParamsEvolution gpus: list of given devices (list of integers) Returns: None """ population_size = len(population) for k in range(population_size // len(gpus) + 1): procs = [] for j in range(len(gpus)): i = k * len(gpus) + j if i < population_size: save_path = expand_path( Path( evolution.get_value_from_config( population[i], evolution.main_model_path + ["save_path"])).parent) save_path.mkdir(parents=True, exist_ok=True) f_name = save_path.joinpath("config.json") save_json(population[i], f_name) if len(gpus) == 1 and gpus[0] == -1: procs.append( Popen("{} -m deeppavlov train {}" " 1>{}/out.txt 2>{}/err.txt".format( sys.executable, str(f_name), str(save_path), str(save_path)), shell=True, stdout=PIPE, stderr=PIPE)) else: procs.append( Popen( "CUDA_VISIBLE_DEVICES={} {} -m deeppavlov train {}" " 1>{}/out.txt 2>{}/err.txt".format( gpus[j], sys.executable, str(f_name), str(save_path), str(save_path)), shell=True, stdout=PIPE, stderr=PIPE)) for j, proc in enumerate(procs): i = k * len(gpus) + j log.info(f'Waiting on {i}th proc') proc.wait() return None
def run_population(population, evolution, gpus): """ Change save and load paths for obtained population, save config.json with model config, run population via current python executor (with which evolve.py already run) and on given devices (-1 means CPU, other integeres - visible for evolve.py GPUs) Args: population: list of dictionaries - configs of current population evolution: ParamsEvolution gpus: list of given devices (list of integers) Returns: None """ population_size = len(population) for k in range(population_size // len(gpus) + 1): procs = [] for j in range(len(gpus)): i = k * len(gpus) + j if i < population_size: save_path = expand_path(Path(evolution.get_value_from_config( population[i], evolution.main_model_path + ["save_path"])).parent) save_path.mkdir(parents=True, exist_ok=True) f_name = save_path.joinpath("config.json") save_json(population[i], f_name) if len(gpus) == 1 and gpus[0] == -1: procs.append(Popen("{} -m deeppavlov train {}" " 1>{}/out.txt 2>{}/err.txt".format(sys.executable, str(f_name), str(save_path), str(save_path) ), shell=True, stdout=PIPE, stderr=PIPE)) else: procs.append(Popen("CUDA_VISIBLE_DEVICES={} {} -m deeppavlov train {}" " 1>{}/out.txt 2>{}/err.txt".format(gpus[j], sys.executable, str(f_name), str(save_path), str(save_path) ), shell=True, stdout=PIPE, stderr=PIPE)) for j, proc in enumerate(procs): i = k * len(gpus) + j log.info(f'Waiting on {i}th proc') proc.wait() return None
def run_population(population, evolution, gpus): """ Change save and load paths for obtained population, save config.json with model config, run population via current python executor (with which evolve.py already run) and on given devices (-1 means CPU, other integeres - visible for evolve.py GPUs) Args: population: list of dictionaries - configs of current population evolution: ParamsEvolution gpus: list of given devices (list of integers) Returns: None """ population_size = len(population) for k in range(population_size // len(gpus) + 1): procs = [] for j in range(len(gpus)): i = k * len(gpus) + j if i < population_size: save_path = expand_path( evolution.get_value_from_config(parse_config(population[i]), evolution.path_to_models_save_path)) save_path.mkdir(parents=True, exist_ok=True) f_name = save_path / "config.json" save_json(population[i], f_name) with save_path.joinpath('out.txt').open('w', encoding='utf8') as outlog,\ save_path.joinpath('err.txt').open('w', encoding='utf8') as errlog: env = dict(os.environ) if len(gpus) > 1 or gpus[0] != -1: env['CUDA_VISIBLE_DEVICES'] = str(gpus[j]) procs.append(Popen("{} -m deeppavlov train {}".format(sys.executable, str(f_name)), shell=True, stdout=outlog, stderr=errlog, env=env)) for j, proc in enumerate(procs): i = k * len(gpus) + j log.info(f'Waiting on {i}th proc') if proc.wait() != 0: save_path = expand_path( evolution.get_value_from_config(parse_config(population[i]), evolution.path_to_models_save_path)) with save_path.joinpath('err.txt').open(encoding='utf8') as errlog: log.warning(f'Population {i} returned an error code {proc.returncode} and an error log:\n' + errlog.read()) return None
def save(self, fname=None): """ Save the model parameters into <<fname>>_opt.json (or <<ser_file>>_opt.json) and model weights into <<fname>>.h5 (or <<ser_file>>.h5) Args: fname: file_path to save model. If not explicitly given seld.opt["ser_file"] will be used Returns: None """ if not self.save_path: raise ConfigError("No `save_path` is provided for Keras model!") elif isinstance(self.save_path, Path) and not self.save_path.parent.is_dir(): raise ConfigError("Provided save path is incorrect!") else: opt_path = "{}_opt.json".format(str(self.save_path.resolve())) weights_path = "{}.h5".format(str(self.save_path.resolve())) print("[ saving model: {} ]".format(opt_path)) self.model.save_weights(weights_path) save_json(self.opt, opt_path) return True
def __init__(self, errors, model_path, mes): save_json(errors, model_path + 'config_chceck_report.json')
def main(): params_helper = ParamsSearch() args = parser.parse_args() is_loo = False n_folds = None if args.folds == 'loo': is_loo = True elif args.folds is None: n_folds = None elif args.folds.isdigit(): n_folds = int(args.folds) else: raise NotImplementedError('Not implemented this type of CV') # read config pipeline_config_path = find_config(args.config_path) config_init = read_json(pipeline_config_path) config = parse_config(config_init) data = read_data_by_config(config) target_metric = parse_config(config_init)['train']['metrics'][0] if isinstance(target_metric, dict): target_metric = target_metric['name'] # get all params for search param_paths = list(params_helper.find_model_path(config, 'search_choice')) param_values = [] param_names = [] for path in param_paths: value = params_helper.get_value_from_config(config, path) param_name = path[-1] param_value_search = value['search_choice'] param_names.append(param_name) param_values.append(param_value_search) # find optimal params if args.search_type == 'grid': # generate params combnations for grid search combinations = list(product(*param_values)) # calculate cv scores scores = [] for comb in combinations: config = deepcopy(config_init) for param_path, param_value in zip(param_paths, comb): params_helper.insert_value_or_dict_into_config( config, param_path, param_value) config = parse_config(config) if (n_folds is not None) | is_loo: # CV for model evaluation score_dict = calc_cv_score(config, data=data, n_folds=n_folds, is_loo=is_loo) score = score_dict[next(iter(score_dict))] else: # train/valid for model evaluation data_to_evaluate = data.copy() if len(data_to_evaluate['valid']) == 0: data_to_evaluate['train'], data_to_evaluate[ 'valid'] = train_test_split(data_to_evaluate['train'], test_size=0.2) iterator = get_iterator_from_config(config, data_to_evaluate) score = train_evaluate_model_from_config( config, iterator=iterator)['valid'][target_metric] scores.append(score) # get model with best score best_params_dict = get_best_params(combinations, scores, param_names, target_metric) log.info('Best model params: {}'.format(best_params_dict)) else: raise NotImplementedError('Not implemented this type of search') # save config best_config = config_init for i, param_name in enumerate(best_params_dict.keys()): if param_name != target_metric: params_helper.insert_value_or_dict_into_config( best_config, param_paths[i], best_params_dict[param_name]) best_model_filename = pipeline_config_path.with_suffix('.cvbest.json') save_json(best_config, best_model_filename) log.info('Best model saved in json-file: {}'.format(best_model_filename))
from pathlib import Path from deeppavlov.utils import settings from deeppavlov.core.common.file import read_json, save_json settings_path = Path(settings.__path__[0]) / 'server_config.json' settings = read_json(settings_path) settings['model_defaults']['Chitchat'] = { "host": "", "port": "", "model_endpoint": "/model", "model_args_names": ["utterances", "annotations", "u_histories", "dialogs"] } save_json(settings, settings_path)