def run(self): common_s2_prepare_run(self.logger, self.trainer, self.s1_path, self.tmp_load_path, self.reset_bn, self.methods) checkpoint_dir = self.checkpoint_dir(self.save_dir) candidate_dir = '%s/candidates/' % checkpoint_dir # get problem, run xu = np.array([ n - 1 for n in self.get_method().strategy_manager.get_num_choices( unique=True) ]) xl = np.zeros_like(xu) problem = PymooProblem(estimators=self.estimators, xl=xl, xu=xu, n_var=len(xu)) wrapper = PymooHPOUtils.run(problem, self.algorithm, self.termination, self.seed, self.logger, checkpoint_dir) # save results for sr in wrapper.sorted_best(): self.get_method().get_network().forward_strategy( fixed_arc=tuple(sr.x)) Builder.save_config( self.get_method().get_network().config(finalize=True), candidate_dir, 'candidate-%s' % '-'.join([str(xs) for xs in sr.x]))
def _run(self, save=True): common_s2_prepare_run(self.logger, self.trainer, self.s1_path, self.tmp_load_path, self.reset_bn, self.methods) checkpoint_dir = self.checkpoint_dir(self.save_dir) candidate_dir = '%s/candidates/' % checkpoint_dir file_viz = '%sx.pdf' % checkpoint_dir self.get_method().eval() # run algorithm = self.hpo.run_opt(hparams=self.args, logger=self.logger, checkpoint_dir=checkpoint_dir, value_space=self._architecture_space, constraints=self.constraints, objectives=self.objectives) population = algorithm.get_total_population(sort=True) # save results if save: population.plot(self.objectives[0].key, self.objectives[1].key, show=False, save_path=file_viz) for candidate in population.fronts[0]: self.get_method().get_network().forward_strategy( fixed_arc=candidate.values) Builder.save_config( self.get_method().get_network().config(finalize=True), candidate_dir, 'candidate-%s' % '-'.join([str(g) for g in candidate.values])) return algorithm, population
def assert_stats_match(name, task_cfg, cfg: dict, num_params=None, num_macs=None): cfg_dir = replace_standard_paths('{path_tmp}/tests/cfgs/') cfg_path = Builder.save_config(cfg, cfg_dir, name) exp = Main.new_task( task_cfg, args_changes={ '{cls_data}.fake': True, '{cls_data}.batch_size_train': 2, '{cls_data}.batch_size_test': -1, '{cls_task}.is_test_run': True, '{cls_task}.save_dir': '{path_tmp}/tests/workdir/', "{cls_network}.config_path": cfg_path, "{cls_trainer}.ema_decay": -1, 'cls_network_heads': 'ClassificationHead', # necessary for the DARTS search space to disable the aux heads }, raise_unparsed=False) net = exp.get_method().get_network() macs = exp.get_method().profile_macs() net.eval() # print(net) cp = count_parameters(net) if num_params is not None: assert cp == num_params, 'Got unexpected num params for %s: %d, expected %d, diff: %d'\ % (name, cp, num_params, abs(cp - num_params)) if num_macs is not None: assert macs == num_macs, 'Got unexpected num macs for %s: %d, expected %d, diff: %d'\ % (name, macs, num_macs, abs(macs - num_macs))
def save(net: nn.Module, save_dir: str, name: str, verbose=True) -> dict: # saving config now will only use the currently active connections, since we have a search network cfg = net.config(finalize=True) if (save_dir is not None) and (name is not None): path = Builder.save_config(cfg, save_dir, name) if verbose: print('Saved config: %s' % path) return cfg
def generate_from_name(name: str, save=True, verbose=True): genotype, compact = compact_from_name(name, verbose=verbose) run_configs = '{path_conf_tasks}/d1_dartsv1.run_config, {path_conf_net_search}darts.run_config' # create weight sharing cell model changes = { 'cls_data': 'Cifar10Data', '{cls_data}.fake': True, '{cls_task}.save_del_old': False, '{cls_network_body}.cell_order': 'n, r', '{cls_network_body}.features_first_cell': 36 * 4, '{cls_network_stem}.features': 36 * 3, 'cls_network_cells_primitives': "%s, %s" % (compact.get('primitives'), compact.get('primitives')), } task = Main.new_task(run_configs, args_changes=changes) net = task.get_method().get_network() args = task.args wss = StrategyManager().get_strategies() assert len(wss) == 1 ws = wss[list(wss.keys())[0]] # fix arc, all block inputs use different weights # go through all weights in the search cell for n, w in ws.named_parameters_single(): # figure out cell type ("normal", "reduce"), block index, and if it's the first, second, ... op of that block c_type, block_idx, num_inputs, num_idx = n.split('/')[-4:] block_idx = int(block_idx.split('-')[-1]) num_idx = int(num_idx.split('-')[-1]) # set all paths weights to zero w.data.zero_() # go through the cell description of the genotype, if input and op number match, set the weight to be higher for op_idx, from_idx in compact.get(c_type)[block_idx]: if num_idx == from_idx: w[op_idx] = 1 ws.forward() # saving config now will only use the highest weighted connections, since we have a search network cfg = net.config(finalize=True, num_block_ops=2) if save: path = Builder.save_config(cfg, get_net_config_dir(genotype.source), name) print('Saved config: %s' % path) return net, cfg, args
def visualize_config(config: dict, save_path: str): save_path = replace_standard_paths(save_path) cfg_path = Builder.save_config(config, replace_standard_paths('{path_tmp}/viz/'), 'viz') exp = Main.new_task(run_config, args_changes={ '{cls_data}.fake': True, '{cls_data}.batch_size_train': 4, '{cls_task}.is_test_run': True, '{cls_task}.save_dir': '{path_tmp}/viz/task/', '{cls_task}.save_del_old': True, "{cls_network}.config_path": cfg_path, }) net = exp.get_method().get_network() vt = VizTree(net) vt.print() vt.plot(save_path + 'net', add_subgraphs=True) print('Saved cell viz to %s' % save_path)
def visualize_config(config: dict, save_path: str): save_path = replace_standard_paths(save_path) cfg_path = Builder.save_config(config, replace_standard_paths('{path_tmp}/viz/'), 'viz') exp = Main.new_task(run_config, args_changes={ '{cls_data}.fake': True, '{cls_data}.batch_size_train': 2, '{cls_task}.is_test_run': True, '{cls_task}.save_dir': '{path_tmp}/viz/task/', '{cls_task}.save_del_old': True, "{cls_task}.note": "viz", "{cls_network}.config_path": cfg_path, }) net = exp.get_method().get_network() for s in ['n', 'r']: for cell in net.get_cells(): if cell.name.startswith(s): visualize_cell(cell, save_path, s) break print('Saved cell viz to %s' % save_path)