| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """Launch script for pre-training representations.""" |
|
|
| import os.path as osp |
|
|
| from absl import app |
| from absl import flags |
| from absl import logging |
| from base_configs import validate_config |
| from ml_collections import config_flags |
| import torch |
| from torchkit import CheckpointManager |
| from torchkit import experiment |
| from torchkit import Logger |
| from torchkit.utils.py_utils import Stopwatch |
| from utils import setup_experiment |
| from xirl import common |
|
|
| import matplotlib |
| matplotlib.use('Agg') |
|
|
| |
|
|
| FLAGS = flags.FLAGS |
|
|
| flags.DEFINE_string("experiment_name", None, "Experiment name.") |
| flags.DEFINE_boolean("resume", False, "Whether to resume training.") |
| flags.DEFINE_string("device", "cuda:0", "The compute device.") |
| flags.DEFINE_boolean("raw_imagenet", False, "") |
|
|
| config_flags.DEFINE_config_file( |
| "config", |
| "base_configs/pretrain.py", |
| "File path to the training hyperparameter configuration.", |
| ) |
|
|
|
|
| @experiment.pdb_fallback |
| def main(_): |
| |
| |
| validate_config(FLAGS.config, mode="pretrain") |
|
|
| config = FLAGS.config |
| exp_dir = osp.join(config.root_dir, FLAGS.experiment_name) |
| setup_experiment(exp_dir, config, FLAGS.resume) |
|
|
| |
| |
| if FLAGS.raw_imagenet: |
| return |
|
|
| |
| if torch.cuda.is_available(): |
| device = torch.device(FLAGS.device) |
| else: |
| logging.info("No GPU device found. Falling back to CPU.") |
| device = torch.device("cpu") |
| logging.info("Using device: %s", device) |
|
|
| |
| if config.seed is not None: |
| logging.info("Pretraining experiment seed: %d", config.seed) |
| experiment.seed_rngs(config.seed) |
| experiment.set_cudnn(config.cudnn_deterministic, config.cudnn_benchmark) |
| else: |
| logging.info("No RNG seed has been set for this pretraining experiment.") |
|
|
| logger = Logger(osp.join(exp_dir, "tb"), FLAGS.resume) |
|
|
| |
| ( |
| model, |
| optimizer, |
| pretrain_loaders, |
| downstream_loaders, |
| trainer, |
| eval_manager, |
| ) = common.get_factories(config, device) |
|
|
| |
| checkpoint_dir = osp.join(exp_dir, "checkpoints") |
| checkpoint_manager = CheckpointManager( |
| checkpoint_dir, |
| model=model, |
| optimizer=optimizer, |
| ) |
|
|
| global_step = checkpoint_manager.restore_or_initialize() |
| total_batches = max(1, len(pretrain_loaders["train"])) |
| epoch = int(global_step / total_batches) |
| complete = False |
| stopwatch = Stopwatch() |
| try: |
| while not complete: |
| for batch in pretrain_loaders["train"]: |
| train_loss = trainer.train_one_iter(batch) |
|
|
| if not global_step % config.logging_frequency: |
| for k, v in train_loss.items(): |
| logger.log_scalar(v, global_step, k, "pretrain") |
| logger.flush() |
|
|
| if not global_step % config.eval.eval_frequency: |
| |
| valid_loss = trainer.eval_num_iters( |
| pretrain_loaders["valid"], |
| config.eval.val_iters, |
| ) |
| for k, v in valid_loss.items(): |
| logger.log_scalar(v, global_step, k, "pretrain") |
|
|
| |
| for split, downstream_loader in downstream_loaders.items(): |
| eval_to_metric = eval_manager.evaluate( |
| model, |
| downstream_loader, |
| device, |
| config.eval.val_iters, |
| ) |
| for eval_name, eval_out in eval_to_metric.items(): |
| eval_out.log( |
| logger, |
| global_step, |
| eval_name, |
| f"downstream/{split}", |
| ) |
|
|
| |
| if not global_step % config.checkpointing_frequency: |
| checkpoint_manager.save(global_step) |
|
|
| |
| global_step += 1 |
| if global_step > config.optim.train_max_iters: |
| complete = True |
| break |
|
|
| time_per_iter = stopwatch.elapsed() |
| logging.info( |
| "Iter[{}/{}] (Epoch {}), {:.6f}s/iter, Loss: {:.3f}".format( |
| global_step, |
| config.optim.train_max_iters, |
| epoch, |
| time_per_iter, |
| train_loss["train/total_loss"].item(), |
| )) |
| stopwatch.reset() |
| epoch += 1 |
|
|
| except KeyboardInterrupt: |
| logging.info("Caught keyboard interrupt. Saving model before quitting.") |
|
|
| finally: |
| checkpoint_manager.save(global_step) |
| logger.close() |
|
|
|
|
| if __name__ == "__main__": |
| flags.mark_flag_as_required("experiment_name") |
| app.run(main) |
|
|