Example #1
0
    def __init__(
        self,
        config=None,
        name=None,
        n_epochs=None,
        seed=None,
        base_dir=None,
        globs=None,
        parse_config_sys_argv=True,
        checkpoint_to_cpu=True,
        exp_ID='000',
    ):

        # super(PytorchExperiment, self).__init__()
        # Experiment.__init__(self)
        self.exp_ID = exp_ID

        self._epoch_idx = 0

        self._config_raw = None
        if isinstance(config, str):
            self._config_raw = Config(file_=config,
                                      update_from_argv=parse_config_sys_argv)
        elif isinstance(config, Config):
            self._config_raw = Config(config=config,
                                      update_from_argv=parse_config_sys_argv)
        elif isinstance(config, dict):
            self._config_raw = Config(config=config,
                                      update_from_argv=parse_config_sys_argv)
        else:
            self._config_raw = Config(update_from_argv=parse_config_sys_argv)

        self.n_epochs = n_epochs
        if 'n_epochs' in self._config_raw:
            self.n_epochs = self._config_raw["n_epochs"]
        if self.n_epochs is None:
            self.n_epochs = 0

        self._seed = seed
        if 'seed' in self._config_raw:
            self._seed = self._config_raw.seed
        if self._seed is None:
            random_data = os.urandom(4)
            seed = int.from_bytes(random_data, byteorder="big")
            self._config_raw.seed = seed
            self._seed = seed

        self.exp_name = name
        if 'name' in self._config_raw:
            self.exp_name = self._config_raw["name"]

        if 'base_dir' in self._config_raw:
            base_dir = self._config_raw["base_dir"]

        self.base_dir = os.path.join(
            base_dir,
            exp_ID + '_' + str(config.cross_vali_index) + "_" + name +
            time.strftime("_%y%m%d_%H%M%S", time.localtime(time.time())))
        if not os.path.exists(self.base_dir):
            os.makedirs(self.base_dir)
        self.checkpoint_dir = os.path.join(self.base_dir, 'model')
        if not os.path.exists(self.checkpoint_dir):
            os.makedirs(self.checkpoint_dir)
        self.code_dir = os.path.join(self.base_dir, 'code')
        if not os.path.exists(self.code_dir):
            os.makedirs(self.code_dir)

        self.elog = PytorchExperimentLogger(self.base_dir)

        self._checkpoint_to_cpu = checkpoint_to_cpu
        self.results = dict()

        set_seed(self._seed)

        # self.elog.save_config(self.config, "config_pre")
        #if globs is not None: # comment out by Yukun due to error in new CRC environment
        #    zip_name = os.path.join(self.code_dir, "sources.zip")
        #    SourcePacker.zip_sources(globs, zip_name)

        # Init objects in config
        self.config = Config.init_objects(self._config_raw)

        atexit.register(self.at_exit_func)