def _output_records(self, step_name, records): """Dump records.""" columns = ["worker_id", "performance", "desc"] outputs = [] for record in records: record = record.serialize() _record = {} for key in columns: _record[key] = record[key] outputs.append(deepcopy(_record)) data = pd.DataFrame(outputs) step_path = FileOps.join_path(TaskOps().local_output_path, step_name) FileOps.make_dir(step_path) _file = FileOps.join_path(step_path, "output.csv") try: data.to_csv(_file, index=False) except Exception: logging.error("Failed to save output file, file={}".format(_file)) for record in outputs: worker_id = record["worker_id"] worker_path = TaskOps().get_local_worker_path(step_name, worker_id) outputs_globs = [] outputs_globs += glob.glob(FileOps.join_path(worker_path, "desc_*.json")) outputs_globs += glob.glob(FileOps.join_path(worker_path, "hps_*.json")) outputs_globs += glob.glob(FileOps.join_path(worker_path, "model_*")) outputs_globs += glob.glob(FileOps.join_path(worker_path, "performance_*.json")) for _file in outputs_globs: if os.path.isfile(_file): FileOps.copy_file(_file, step_path) elif os.path.isdir(_file): FileOps.copy_folder(_file, FileOps.join_path(step_path, os.path.basename(_file)))
def _get_model_desc(self): model_desc = self.trainer.model_desc if not model_desc: if ModelConfig.model_desc_file is not None: desc_file = ModelConfig.model_desc_file desc_file = desc_file.replace("{local_base_path}", self.trainer.local_base_path) if ":" not in desc_file: desc_file = os.path.abspath(desc_file) if ":" in desc_file: local_desc_file = FileOps.join_path( self.trainer.local_output_path, os.path.basename(desc_file)) FileOps.copy_file(desc_file, local_desc_file) desc_file = local_desc_file model_desc = Config(desc_file) logger.info("net_desc:{}".format(model_desc)) elif ModelConfig.model_desc is not None: model_desc = ModelConfig.model_desc elif ModelConfig.models_folder is not None: folder = ModelConfig.models_folder.replace( "{local_base_path}", self.trainer.local_base_path) pattern = FileOps.join_path(folder, "desc_*.json") desc_file = glob.glob(pattern)[0] model_desc = Config(desc_file) return model_desc
def _new_model_init(self): """Init new model. :return: initial model after loading pretrained model :rtype: torch.nn.Module """ init_model_file = self.config.init_model_file if ":" in init_model_file: local_path = FileOps.join_path( self.trainer.get_local_worker_path(), os.path.basename(init_model_file)) FileOps.copy_file(init_model_file, local_path) self.config.init_model_file = local_path network_desc = copy.deepcopy(self.base_net_desc) network_desc.backbone.cfgs = network_desc.backbone.base_cfgs model_init = NetworkDesc(network_desc).to_model() return model_init
def _save_descript(self): """Save result descript.""" template_file = self.config.darts_template_file genotypes = self.search_alg.codec.calc_genotype( self._get_arch_weights()) if template_file == "{default_darts_cifar10_template}": template = DartsNetworkTemplateConfig.cifar10 elif template_file == "{default_darts_cifar100_template}": template = DartsNetworkTemplateConfig.cifar100 elif template_file == "{default_darts_imagenet_template}": template = DartsNetworkTemplateConfig.imagenet else: dst = FileOps.join_path(self.trainer.get_local_worker_path(), os.path.basename(template_file)) FileOps.copy_file(template_file, dst) template = Config(dst) model_desc = self._gen_model_desc(genotypes, template) self.trainer.config.codec = model_desc
def _get_model_desc(self): model_desc = self.model_desc self.saved_folder = self.get_local_worker_path(self.step_name, self.worker_id) if not model_desc: if os.path.exists( FileOps.join_path(self.saved_folder, 'desc_{}.json'.format(self.worker_id))): model_config = Config( FileOps.join_path(self.saved_folder, 'desc_{}.json'.format(self.worker_id))) if "type" not in model_config and "modules" not in model_config: model_config = ModelConfig.model_desc model_desc = model_config elif ModelConfig.model_desc_file is not None: desc_file = ModelConfig.model_desc_file desc_file = desc_file.replace("{local_base_path}", self.local_base_path) if ":" not in desc_file: desc_file = os.path.abspath(desc_file) if ":" in desc_file: local_desc_file = FileOps.join_path( self.local_output_path, os.path.basename(desc_file)) FileOps.copy_file(desc_file, local_desc_file) desc_file = local_desc_file model_desc = Config(desc_file) logger.info("net_desc:{}".format(model_desc)) elif ModelConfig.model_desc is not None: model_desc = ModelConfig.model_desc elif ModelConfig.models_folder is not None: folder = ModelConfig.models_folder.replace( "{local_base_path}", self.local_base_path) pattern = FileOps.join_path(folder, "desc_*.json") desc_file = glob.glob(pattern)[0] model_desc = Config(desc_file) elif PipeStepConfig.pipe_step.get("models_folder") is not None: folder = PipeStepConfig.pipe_step.get("models_folder").replace( "{local_base_path}", self.local_base_path) desc_file = FileOps.join_path( folder, "desc_{}.json".format(self.worker_id)) model_desc = Config(desc_file) logger.info("Load model from model folder {}.".format(folder)) return model_desc
def _copy_needed_file(self): if self.config.pareto_front_file is None: raise FileNotFoundError( "Config item paretor_front_file not found in config file.") init_pareto_front_file = self.config.pareto_front_file.replace( "{local_base_path}", self.local_base_path) self.pareto_front_file = FileOps.join_path(self.local_output_path, self.step_name, "pareto_front.csv") FileOps.make_base_dir(self.pareto_front_file) FileOps.copy_file(init_pareto_front_file, self.pareto_front_file) if self.config.random_file is None: raise FileNotFoundError( "Config item random_file not found in config file.") init_random_file = self.config.random_file.replace( "{local_base_path}", self.local_base_path) self.random_file = FileOps.join_path(self.local_output_path, self.step_name, "random.csv") FileOps.copy_file(init_random_file, self.random_file)
def _get_pretrained_model_file(self): if not self.trainer.load_weights_file: return None model_file = self.trainer.config.kwargs.get("pretrained_model_file") if model_file: return model_file if ModelConfig.pretrained_model_file: model_file = ModelConfig.pretrained_model_file model_file = model_file.replace("{local_base_path}", self.trainer.local_base_path) model_file = model_file.replace("{worker_id}", str(self.trainer.worker_id)) if ":" not in model_file: model_file = os.path.abspath(model_file) if ":" in model_file: local_model_file = FileOps.join_path( self.trainer.local_output_path, os.path.basename(model_file)) FileOps.copy_file(model_file, local_model_file) model_file = local_model_file return model_file else: return None
def _save_best_model(self): """Save best model.""" if vega.is_torch_backend(): torch.save(self.trainer.model.state_dict(), self.trainer.weights_file) elif vega.is_tf_backend(): worker_path = self.trainer.get_local_worker_path() model_id = "model_{}".format(self.trainer.worker_id) weights_folder = FileOps.join_path(worker_path, model_id) FileOps.make_dir(weights_folder) checkpoint_file = tf.train.latest_checkpoint(worker_path) ckpt_globs = glob.glob("{}.*".format(checkpoint_file)) for _file in ckpt_globs: FileOps.copy_file( _file, FileOps.join_path(weights_folder, os.path.split(_file)[-1])) FileOps.copy_file(FileOps.join_path(worker_path, 'checkpoint'), weights_folder) if self.trainer.save_ext_model: self._save_pb_model(weights_folder, model_id) self.trainer.ext_model = FileOps.join_path( weights_folder, '{}.pb'.format(model_id)) elif vega.is_ms_backend(): worker_path = self.trainer.get_local_worker_path() save_path = os.path.join( worker_path, "model_{}.ckpt".format(self.trainer.worker_id)) for file in os.listdir(worker_path): if file.startswith("CKP") and file.endswith(".ckpt"): self.weights_file = FileOps.join_path(worker_path, file) os.rename(self.weights_file, save_path) if self.trainer.save_ext_model: model_id = "model_{}".format(self.trainer.worker_id) self._save_om_model(worker_path, model_id) self.trainer.ext_model = FileOps.join_path( worker_path, '{}.om'.format(model_id))