def get_model_and_data_loaders( config: ConfigParser, logger: logging.Logger, ckpt_path: Path, ) -> Tuple[torch.nn.Module, module_data.ExpertDataLoader]: expert_dims, raw_input_dims, text_dim = compute_dims(config) data_loaders = config.init( name='data_loader', module=module_data, logger=logger, raw_input_dims=raw_input_dims, challenge_mode=config.get("challenge_mode", False), text_dim=text_dim, text_feat=config["experts"]["text_feat"], text_agg=config["experts"]["text_agg"], use_zeros_for_missing=config["experts"].get("use_zeros_for_missing", False), task=config.get("task", "retrieval"), eval_only=True, distil_params=config.get("distil_params", None), training_file=config.get("training_file", None), caption_masks=config.get("caption_masks", None), ce_shared_dim=config["experts"].get("ce_shared_dim", None), ) trn_config = compute_trn_config(config) model = config.init( name='arch', module=module_arch, trn_config=trn_config, expert_dims=expert_dims, text_dim=text_dim, disable_nan_checks=config["disable_nan_checks"], task=config.get("task", "retrieval"), ce_shared_dim=config["experts"].get("ce_shared_dim", None), feat_aggregation=config["data_loader"]["args"]["feat_aggregation"], trn_cat=config["data_loader"]["args"].get("trn_cat", 0), ) ckpt_path = config._args.resume logger.info(f"Loading checkpoint: {ckpt_path} ...") checkpoint = torch.load(ckpt_path) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) # support backwards compatibility deprecated = ["ce.moe_fc_bottleneck1", "ce.moe_cg", "ce.moe_fc_proj"] for mod in deprecated: for suffix in ("weight", "bias"): key = f"{mod}.{suffix}" if key in state_dict: print(f"WARNING: Removing deprecated key {key} from model") state_dict.pop(key) model.load_state_dict(state_dict) return model, data_loaders
def get_model_and_data_loaders( config: ConfigParser, logger: logging.Logger, ckpt_path: Path, ) -> Tuple[torch.nn.Module, module_data.ExpertDataLoader]: expert_dims, raw_input_dims = compute_dims(config) trn_config = compute_trn_config(config) data_loaders = config.init( name='data_loader', module=module_data, logger=logger, raw_input_dims=raw_input_dims, challenge_mode=config.get("challenge_mode", False), text_feat=config["experts"]["text_feat"], text_dim=config["experts"]["text_dim"], text_agg=config["experts"]["text_agg"], use_zeros_for_missing=config["experts"].get("use_zeros_for_missing", False), task=config.get("task", "retrieval"), eval_only=True, ) model = config.init( name='arch', module=module_arch, trn_config=trn_config, expert_dims=expert_dims, text_dim=config["experts"]["text_dim"], disable_nan_checks=config["disable_nan_checks"], task=config.get("task", "retrieval"), ce_shared_dim=config["experts"].get("ce_shared_dim", None), feat_aggregation=config["data_loader"]["args"]["feat_aggregation"], trn_cat=config["data_loader"]["args"].get("trn_cat", 0), ) ckpt_path = config._args.resume logger.info(f"Loading checkpoint: {ckpt_path} ...") checkpoint = torch.load(ckpt_path) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) return model, data_loaders
def evaluation(config, logger=None, trainer=None): if logger is None: logger = config.get_logger('test') if getattr(config._args, "eval_from_training_config", False): eval_conf = copy.deepcopy(config) merge(eval_conf._config, config["eval_settings"], strategy=Strategy.REPLACE) config = eval_conf logger.info("Running evaluation with configuration:") logger.info(config) expert_dims, raw_input_dims = compute_dims(config) trn_config = compute_trn_config(config) # Set the random initial seeds seed = config["seed"] logger.info(f"Setting experiment random seed to {seed}") random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) update_src_web_video_dir(config) visualizer = config.init( name='visualizer', module=module_vis, exp_name=config._exper_name, web_dir=config._web_log_dir, ) data_loaders = config.init( name='data_loader', module=module_data, logger=logger, raw_input_dims=raw_input_dims, challenge_mode=config.get("challenge_mode", False), text_feat=config["experts"]["text_feat"], text_dim=config["experts"]["text_dim"], text_agg=config["experts"]["text_agg"], use_zeros_for_missing=config["experts"].get("use_zeros_for_missing", False), task=config.get("task", "retrieval"), eval_only=True, ) model = config.init( name='arch', module=module_arch, trn_config=trn_config, expert_dims=expert_dims, text_dim=config["experts"]["text_dim"], disable_nan_checks=config["disable_nan_checks"], task=config.get("task", "retrieval"), ce_shared_dim=config["experts"].get("ce_shared_dim", None), feat_aggregation=config["data_loader"]["args"]["feat_aggregation"], trn_cat=config["data_loader"]["args"].get("trn_cat", 0), ) logger.info(model) metrics = [getattr(module_metric, met) for met in config['metrics']] ckpt_path = config._args.resume logger.info(f"Loading checkpoint: {ckpt_path} ...") checkpoint = torch.load(ckpt_path) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) challenge_mode = config.get("challenge_mode", False) challenge_msg = ( "\n" "Evaluation ran on challenge features. To obtain a score, upload the similarity" "matrix for each dataset to the test server after running the " "`misc/cvpr2020-challenge/prepare_submission.py` script and following the " "instructions at: " "https://www.robots.ox.ac.uk/~vgg/challenges/video-pentathlon/" "\n") # prepare model for testing. Note that some datasets fail to fit the retrieval # set on the GPU, so we run them on the CPU if torch.cuda.is_available() and not config.get("disable_gpu", True): device = "cuda" else: device = "cpu" logger.info(f"Running evaluation on {device}") model = model.to(device) model.eval() with torch.no_grad(): samples, meta = data_loaders["retrieval"] # To use the nan-checks safely, we need make temporary copies of the data disable_nan_checks = config._config["disable_nan_checks"] with ctxt_mgr(samples, device, disable_nan_checks) as valid: output = model(**valid) sims = output["cross_view_conf_matrix"].data.cpu().float().numpy() dataset = data_loaders.dataset_name if challenge_mode: split = data_loaders.dataloaders["dataset"].split_name prediction_path = config._log_dir / f"{dataset}-{split}-predictions.csv" compressed_preds = compress_predictions( query_masks=meta["query_masks"], sims=sims, ) np.savetxt(prediction_path, compressed_preds, delimiter=',', fmt="%d") print(f"Saved similarity matrix predictions to {prediction_path}") print(challenge_msg) return nested_metrics = {} for metric in metrics: metric_name = metric.__name__ res = metric(sims, query_masks=meta["query_masks"]) verbose(epoch=0, metrics=res, name=dataset, mode=metric_name) if trainer is not None: if not trainer.mini_train: trainer.writer.set_step(step=0, mode="val") # avoid tensboard folding by prefixing metric_name_ = f"test_{metric_name}" trainer.log_metrics(res, metric_name=metric_name_, mode="val") nested_metrics[metric_name] = res if data_loaders.num_test_captions == 1: visualizer.visualize_ranking( sims=sims, meta=meta, epoch=0, nested_metrics=nested_metrics, ) log = {} for subkey, subval in nested_metrics.items(): for subsubkey, subsubval in subval.items(): log[f"test_{subkey}_{subsubkey}"] = subsubval for key, value in log.items(): logger.info(" {:15s}: {}".format(str(key), value))
def evaluation(config, logger=None, trainer=None): if logger is None: logger = config.get_logger('test') if getattr(config._args, "eval_from_training_config", False): eval_conf = copy.deepcopy(config) merge(eval_conf._config, config["eval_settings"], strategy=Strategy.REPLACE) config = eval_conf logger.info("Running evaluation with configuration:") logger.info(config) expert_dims, raw_input_dims = compute_dims(config) trn_config = compute_trn_config(config) # Set the random initial seeds seed = config["seed"] logger.info(f"Setting experiment random seed to {seed}") random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) # We use cls defaults for backwards compatibility with the MMIT configs. In the # long run this should be handled by the json configs themselves cls_defaults = ["train", "val", "tiny", "challenge"] data_loaders = config.init( name='data_loader', module=module_data, logger=logger, raw_input_dims=raw_input_dims, text_feat=config["experts"]["text_feat"], text_dim=config["experts"]["text_dim"], text_agg=config["experts"]["text_agg"], use_zeros_for_missing=config["experts"].get("use_zeros_for_missing", False), task=config.get("task", "retrieval"), cls_partitions=config.get("cls_partitions", cls_defaults), ) model = config.init( name='arch', module=module_arch, trn_config=trn_config, expert_dims=expert_dims, text_dim=config["experts"]["text_dim"], disable_nan_checks=config["disable_nan_checks"], task=config.get("task", "retrieval"), ce_shared_dim=config["experts"].get("ce_shared_dim", None), feat_aggregation=config["data_loader"]["args"]["feat_aggregation"], trn_cat=config["data_loader"]["args"].get("trn_cat", 0), ) logger.info(model) metrics = [getattr(module_metric, met) for met in config['metrics']] visualizer = config.init( name='visualizer', module=module_vis, exp_name=config._exper_name, web_dir=config._web_log_dir, ) ckpt_path = config._args.resume logger.info(f"Loading checkpoint: {ckpt_path} ...") checkpoint = torch.load(ckpt_path) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing. Note that some datasets fail to fit the retrieval # set on the GPU, so we run them on the CPU if torch.cuda.is_available() and not config.get("disable_gpu", True): device = "cuda" else: device = "cpu" logger.info(f"Running evaluation on {device}") model = model.to(device) model.eval() with torch.no_grad(): samples, meta = data_loaders["retrieval"] # To use the nan-checks safely, we need make temporary copies of the data disable_nan_checks = config._config["disable_nan_checks"] with ctxt_mgr(samples, device, disable_nan_checks) as valid: output = model(**valid) sims = output["cross_view_conf_matrix"].data.cpu().float().numpy() dataset = data_loaders.dataset_name nested_metrics = {} for metric in metrics: metric_name = metric.__name__ res = metric(sims, query_masks=meta["query_masks"]) verbose(epoch=0, metrics=res, name=dataset, mode=metric_name) if trainer is not None: if not trainer.mini_train: trainer.writer.set_step(step=0, mode="val") # avoid tensboard folding by prefixing metric_name_ = f"test_{metric_name}" trainer.log_metrics(res, metric_name=metric_name_, mode="val") nested_metrics[metric_name] = res if data_loaders.num_test_captions == 1: visualizer.visualize_ranking( sims=sims, meta=meta, epoch=0, nested_metrics=nested_metrics, ) log = {} for subkey, subval in nested_metrics.items(): for subsubkey, subsubval in subval.items(): log[f"test_{subkey}_{subsubkey}"] = subsubval for key, value in log.items(): logger.info(" {:15s}: {}".format(str(key), value))
def run_exp(config): warnings.filterwarnings('ignore') logger = config.get_logger('train') leaderboard_path = config._args.leaderboard Path(leaderboard_path).parent.mkdir(exist_ok=True, parents=True) with open(leaderboard_path, 'a') as f: txt_path = f"{config._log_dir}/preds.txt" print(txt_path, file=f, flush=True) expert_dims, raw_input_dims = compute_dims(config, logger) trn_config = compute_trn_config(config) if config._args.group_seed: seeds = [int(config._args.group_seed)] else: seeds = [int(x) for x in config._args.seeds.split(",")] # set up local filesystem on the cluster if socket.gethostname().endswith("cluster"): os.system(str(Path.home() / "configure_tmp_data.sh")) for ii, seed in enumerate(seeds): tic = time.time() logger.info(f"{ii + 1}/{len(seeds)} Setting experiment random seed to {seed}") set_seeds(seed) config["seed"] = seed # We use cls defaults for backwards compatibility with the MMIT configs. In the # long run this should be handled by the json configs themselves cls_defaults = ["train", "val", "tiny", "challenge"] model = config.init( name='arch', module=module_arch, expert_dims=expert_dims, text_dim=config["experts"]["text_dim"], disable_nan_checks=config["disable_nan_checks"], spatial_feats=config["data_loader"]["args"].get("spatial_feats", False), task=config.get("task", "retrieval"), ce_shared_dim=config["experts"].get("ce_shared_dim", None), feat_aggregation=config["data_loader"]["args"]["feat_aggregation"], trn_config=trn_config, trn_cat=config["data_loader"]["args"].get("trn_cat", 0), ) logger.info(model) data_loaders = config.init( name='data_loader', module=module_data, logger=logger, raw_input_dims=raw_input_dims, text_feat=config["experts"]["text_feat"], text_dim=config["experts"]["text_dim"], text_agg=config["experts"]["text_agg"], use_zeros_for_missing=config["experts"].get("use_zeros_for_missing", False), task=config.get("task", "retrieval"), cls_partitions=config.get("cls_partitions", cls_defaults) ) if config.get("manual_linear_init", False): logger.info("manually setting init for linear layers") def init_weights(m): if isinstance(m, nn.Linear): torch.nn.init.xavier_uniform(m.weight) m.bias.data.fill_(0.01) model.apply(init_weights) loss = config.init(name="loss", module=module_loss) metrics = [getattr(module_metric, met) for met in config['metrics']] trainable_params = filter(lambda p: p.requires_grad, model.parameters()) if config["optimizer"]["type"] == "RAdam": optimizer = config.init('optimizer', radam, trainable_params) elif config["optimizer"]["type"] == "Ranger": optimizer = config.init('optimizer', ranger, trainable_params) elif config["optimizer"]["type"] == "SWATS": optimizer = config.init('optimizer', swats, trainable_params) else: optimizer = config.init('optimizer', torch.optim, trainable_params) if config["lr_scheduler"]["type"] == "StepLR": lr_scheduler = config.init('lr_scheduler', torch.optim.lr_scheduler, optimizer) else: lr_scheduler = config.init('lr_scheduler', cos_restart, optimizer) visualizer = config.init( name='visualizer', module=module_vis, exp_name=config._exper_name, web_dir=config._web_log_dir, ) trainer = Trainer( model, loss, metrics, optimizer, config=config, data_loaders=data_loaders, lr_scheduler=lr_scheduler, mini_train=config._args.mini_train, disable_nan_checks=config["disable_nan_checks"], visualizer=visualizer, val_freq=config["trainer"].get("val_freq", 1), force_cpu_val=config.get("force_cpu_val", False), skip_first_n_saves=config["trainer"].get("skip_first_n_saves", 0), include_optim_in_ckpts=config["trainer"].get("include_optim_in_ckpts", 1), cache_targets=set(config.get("cache_targets", [])), ) trainer.train() best_ckpt_path = config.save_dir / "trained_model.pth" duration = time.strftime('%Hh%Mm%Ss', time.gmtime(time.time() - tic)) logger.info(f"Training took {duration}") if config._config.get("eval_settings", False): eval_config = copy.deepcopy(config) merge(eval_config._config, config["eval_settings"], strategy=Strategy.REPLACE) eval_config._args.resume = best_ckpt_path evaluation(eval_config, logger=logger, trainer=trainer) # If multiple runs were conducted, report relevant statistics if len(seeds) > 1: log_summary( logger=logger, log_path=config.log_path, eval_mode=config["eval_mode"], fixed_num_epochs=config["trainer"]["epochs"], ) print(f"Log file stored at {config.log_path}") # Report the location of the "best" checkpoint of the final seeded run (here # "best" corresponds to the model with the highest geometric mean over the # R@1, R@5 and R@10 metrics when a validation set is used, or simply the final # epoch of training for fixed-length schedules). print(f"The best performing ckpt can be found at {str(best_ckpt_path)}")