def main(): args = parser.parse_args() pipeline_config_path = find_config(args.config_path) key_main_model = args.key_main_model population_size = args.p_size gpus = [int(gpu) for gpu in args.gpus.split(",")] train_partition = int(args.train_partition) start_from_population = int(args.start_from_population) path_to_population = args.path_to_population elitism_with_weights = args.elitism_with_weights iterations = int(args.iterations) p_crossover = args.p_cross pow_crossover = args.pow_cross p_mutation = args.p_mut pow_mutation = args.pow_mut if os.environ.get("CUDA_VISIBLE_DEVICES") is None: pass else: cvd = [ int(gpu) for gpu in os.environ.get("CUDA_VISIBLE_DEVICES").split(",") ] if gpus == [-1]: gpus = cvd else: try: gpus = [cvd[gpu] for gpu in gpus] except IndexError: raise ConfigError( "Can not use gpus `{}` with CUDA_VISIBLE_DEVICES='{}'". format(",".join(map(str, gpus)), ",".join(map(str, cvd)))) basic_params = read_json(pipeline_config_path) log.info("Given basic params: {}\n".format( json.dumps(basic_params, indent=2))) # Initialize evolution evolution = ParamsEvolution(population_size=population_size, p_crossover=p_crossover, crossover_power=pow_crossover, p_mutation=p_mutation, mutation_power=pow_mutation, key_main_model=key_main_model, seed=42, train_partition=train_partition, elitism_with_weights=elitism_with_weights, **basic_params) considered_metrics = evolution.get_value_from_config( evolution.basic_config, list(evolution.find_model_path(evolution.basic_config, "metrics"))[0] + ["metrics"]) considered_metrics = [ metric['name'] if isinstance(metric, dict) else metric for metric in considered_metrics ] log.info(considered_metrics) evolve_metric = considered_metrics[0] # Create table variable for gathering results abs_path_to_main_models = expand_path( parse_value_with_config(evolution.models_path, evolution.basic_config)) abs_path_to_main_models.mkdir(parents=True, exist_ok=True) result_file = abs_path_to_main_models / "result_table.tsv" print(result_file) result_table_columns = [] result_table_dict = {} for el in considered_metrics: result_table_dict[el + "_valid"] = [] result_table_dict[el + "_test"] = [] result_table_columns.extend([el + "_valid", el + "_test"]) result_table_dict["params"] = [] result_table_columns.append("params") if start_from_population == 0: # if starting evolution from scratch iters = 0 result_table = pd.DataFrame(result_table_dict) # write down result table file result_table.loc[:, result_table_columns].to_csv(result_file, index=False, sep='\t') log.info("Iteration #{} starts".format(iters)) # randomly generate the first population population = evolution.first_generation() else: # if starting evolution from already existing population iters = start_from_population log.info("Iteration #{} starts".format(iters)) population = [] for i in range(population_size): config = read_json( expand_path(path_to_population) / f"model_{i}" / "config.json") evolution.insert_value_or_dict_into_config( config, evolution.path_to_models_save_path, str(evolution.main_model_path / f"population_{start_from_population}" / f"model_{i}")) population.append(config) run_population(population, evolution, gpus) population_scores = results_to_table(population, evolution, considered_metrics, result_file, result_table_columns)[evolve_metric] log.info("Population scores: {}".format(population_scores)) log.info("Iteration #{} was done".format(iters)) iters += 1 while True: if iterations != -1 and start_from_population + iterations == iters: log.info("End of evolution on iteration #{}".format(iters)) break log.info("Iteration #{} starts".format(iters)) population = evolution.next_generation(population, population_scores, iters) run_population(population, evolution, gpus) population_scores = results_to_table( population, evolution, considered_metrics, result_file, result_table_columns)[evolve_metric] log.info("Population scores: {}".format(population_scores)) log.info("Iteration #{} was done".format(iters)) iters += 1
def main(): args = parser.parse_args() pipeline_config_path = find_config(args.config_path) key_main_model = args.key_main_model population_size = args.p_size gpus = [int(gpu) for gpu in args.gpus.split(",")] train_partition = int(args.train_partition) start_from_population = int(args.start_from_population) path_to_population = args.path_to_population elitism_with_weights = args.elitism_with_weights iterations = int(args.iterations) p_crossover = args.p_cross pow_crossover = args.pow_cross p_mutation = args.p_mut pow_mutation = args.pow_mut if os.environ.get("CUDA_VISIBLE_DEVICES") is None: pass else: cvd = [int(gpu) for gpu in os.environ.get("CUDA_VISIBLE_DEVICES").split(",")] if gpus == [-1]: gpus = cvd else: try: gpus = [cvd[gpu] for gpu in gpus] except IndexError: raise ConfigError("Can not use gpus `{}` with CUDA_VISIBLE_DEVICES='{}'".format( ",".join(map(str, gpus)), ",".join(map(str, cvd)) )) basic_params = read_json(pipeline_config_path) log.info("Given basic params: {}\n".format(json.dumps(basic_params, indent=2))) # Initialize evolution evolution = ParamsEvolution(population_size=population_size, p_crossover=p_crossover, crossover_power=pow_crossover, p_mutation=p_mutation, mutation_power=pow_mutation, key_main_model=key_main_model, seed=42, train_partition=train_partition, elitism_with_weights=elitism_with_weights, **basic_params) considered_metrics = evolution.get_value_from_config(evolution.basic_config, list(evolution.find_model_path( evolution.basic_config, "metrics"))[0] + ["metrics"]) considered_metrics = [metric['name'] if isinstance(metric, dict) else metric for metric in considered_metrics] log.info(considered_metrics) evolve_metric = considered_metrics[0] # Create table variable for gathering results abs_path_to_main_models = expand_path(str(evolution.models_path).format( **evolution.basic_config['metadata']['variables'])) abs_path_to_main_models.mkdir(parents=True, exist_ok=True) result_file = abs_path_to_main_models / "result_table.tsv" print(result_file) result_table_columns = [] result_table_dict = {} for el in considered_metrics: result_table_dict[el + "_valid"] = [] result_table_dict[el + "_test"] = [] result_table_columns.extend([el + "_valid", el + "_test"]) result_table_dict["params"] = [] result_table_columns.append("params") if start_from_population == 0: # if starting evolution from scratch iters = 0 result_table = pd.DataFrame(result_table_dict) # write down result table file result_table.loc[:, result_table_columns].to_csv(result_file, index=False, sep='\t') log.info("Iteration #{} starts".format(iters)) # randomly generate the first population population = evolution.first_generation() else: # if starting evolution from already existing population iters = start_from_population log.info("Iteration #{} starts".format(iters)) population = [] for i in range(population_size): config = read_json(expand_path(path_to_population) / f"model_{i}" / "config.json") evolution.insert_value_or_dict_into_config( config, evolution.path_to_models_save_path, str(evolution.main_model_path / f"population_{start_from_population}" / f"model_{i}")) population.append(config) run_population(population, evolution, gpus) population_scores = results_to_table(population, evolution, considered_metrics, result_file, result_table_columns)[evolve_metric] log.info("Population scores: {}".format(population_scores)) log.info("Iteration #{} was done".format(iters)) iters += 1 while True: if iterations != -1 and start_from_population + iterations == iters: log.info("End of evolution on iteration #{}".format(iters)) break log.info("Iteration #{} starts".format(iters)) population = evolution.next_generation(population, population_scores, iters) run_population(population, evolution, gpus) population_scores = results_to_table(population, evolution, considered_metrics, result_file, result_table_columns)[evolve_metric] log.info("Population scores: {}".format(population_scores)) log.info("Iteration #{} was done".format(iters)) iters += 1