def mkdir_and_rename(path): """mkdirs. If path exists, rename it with timestamp and create a new one. Args: path (str): Folder path. """ if osp.exists(path): new_name = path + '_archived_' + get_time_str() print(f'Path already exists. Rename it to {new_name}', flush=True) os.rename(path, new_name) mmcv.mkdir_or_exist(path)
def init_logger(log_dir=None, level=logging.INFO): """Init the logger. Args: log_dir(str, optional): Log file directory. If not specified, no log file will be used. level (int or str): See the built-in python logging module. Returns: :obj:`~logging.Logger`: Python logger. """ logging.basicConfig( format='%(asctime)s - %(levelname)s - %(message)s', level=level) logger = logging.getLogger(__name__) if log_dir: filename = '{}.log'.format(get_time_str()) log_file = osp.join(log_dir, filename) add_file_handler(logger, log_file, level=level) return logger
def __init__(self, model, optimizer=None, work_dir=None, logger=None, meta=None, max_iters=None, max_epochs=None): self.model = model self.optimizer = optimizer self.logger = logger self.meta = meta if work_dir: self.work_dir = osp.abspath(work_dir) mmcv.mkdir_or_exist(self.work_dir) elif work_dir is None: self.work_dir = None else: raise TypeError('"work_dir" must be a str or None') # get model name from the model class if hasattr(self.model, 'module'): self.model_name = self.model.module.__class__.__name__ else: self.model_name = self.model.__class__.__name__ self.rank, self.world_size = get_dist_info() self.timestamp = get_time_str() self.mode = None self.hooks = [] self.epoch = 0 self.iter = 0 self.inner_iter = 0 self.max_epochs = max_epochs self.max_iters = max_iters # TODO: Redesign LogBuffer, it is not flexible and elegant enough self.log_buffer = LogBuffer()
def __init__(self, model, batch_processor, optimizer=None, search_optimizer=None, work_dir=None, log_level=logging.INFO, logger=None, runner_attr_dict=None): assert callable(batch_processor) assert logger, "Please provide the logger." self.model = model if optimizer is not None: self.optimizer = self.init_optimizer(optimizer) else: self.optimizer = None if search_optimizer is not None: self.search_optimizer = self.init_optimizer( search_optimizer, 'arch_parameters') self.search_step = False else: self.search_optimizer = None self.batch_processor = batch_processor # create work_dir if mmcv.is_str(work_dir): self.work_dir = osp.abspath(work_dir) mmcv.mkdir_or_exist(self.work_dir) elif work_dir is None: self.work_dir = None else: raise TypeError('"work_dir" must be a str or None') # get model name from the model class if hasattr(self.model, 'module'): self._model_name = self.model.module.__class__.__name__ else: self._model_name = self.model.__class__.__name__ if runner_attr_dict: # set attributes to runner for k, v in runner_attr_dict.items(): setattr(self, k, v) self._rank, self._world_size = get_dist_info() self._checkpoint_batchsize = None self._checkpoint_global_step = None self._checkpoint_inner_iter = None self.batchsize = self._world_size * getattr(self, 'imgs_per_gpu', 1) self.timestamp = get_time_str() self.logger = logger self.log_buffer = LogBuffer() self.mode = None self._hooks = [] self._epoch = 0 self._iter = 0 # store the actual iter even batch size changed self._inner_iter = 0 self._max_inner_iter = 0 self._max_epochs = 0 self._max_iters = 0
def __init__(self, model, batch_processor=None, optimizer=None, work_dir=None, logger=None, meta=None, max_iters=None, max_epochs=None, amp=False): if batch_processor is not None: if not callable(batch_processor): raise TypeError('batch_processor must be callable, ' f'but got {type(batch_processor)}') warnings.warn('batch_processor is deprecated, please implement ' 'train_step() and val_step() in the model instead.') # raise an error is `batch_processor` is not None and # `model.train_step()` exists. if is_module_wrapper(model): _model = model.module else: _model = model if hasattr(_model, 'train_step') or hasattr(_model, 'val_step'): raise RuntimeError( 'batch_processor and model.train_step()/model.val_step() ' 'cannot be both available.') else: assert hasattr(model, 'train_step') # check the type of `optimizer` if isinstance(optimizer, dict): for name, optim in optimizer.items(): if not isinstance(optim, Optimizer): raise TypeError( f'optimizer must be a dict of torch.optim.Optimizers, ' f'but optimizer["{name}"] is a {type(optim)}') elif not isinstance(optimizer, Optimizer) and optimizer is not None: raise TypeError( f'optimizer must be a torch.optim.Optimizer object ' f'or dict or None, but got {type(optimizer)}') # check the type of `logger` if not isinstance(logger, logging.Logger): raise TypeError(f'logger must be a logging.Logger object, ' f'but got {type(logger)}') # check the type of `meta` if meta is not None and not isinstance(meta, dict): raise TypeError( f'meta must be a dict or None, but got {type(meta)}') self.model = model self.batch_processor = batch_processor self.optimizer = optimizer self.logger = logger self.meta = meta self.amp = amp # create work_dir if mmcv.is_str(work_dir): self.work_dir = osp.abspath(work_dir) mmcv.mkdir_or_exist(self.work_dir) elif work_dir is None: self.work_dir = None else: raise TypeError('"work_dir" must be a str or None') # get model name from the model class if hasattr(self.model, 'module'): self._model_name = self.model.module.__class__.__name__ else: self._model_name = self.model.__class__.__name__ self._rank, self._world_size = get_dist_info() self.timestamp = get_time_str() self.mode = None self._hooks = [] self._epoch = 0 self._iter = 0 self._inner_iter = 0 if max_epochs is not None and max_iters is not None: raise ValueError( 'Only one of `max_epochs` or `max_iters` can be set.') self._max_epochs = max_epochs self._max_iters = max_iters # TODO: Redesign LogBuffer, it is not flexible and elegant enough self.log_buffer = LogBuffer()