def train(cfg, args): # clear up residual cache from previous runs if torch.cuda.is_available(): torch.cuda.empty_cache() # main training / eval actions here # fix the seed for reproducibility if cfg.SEED is not None: torch.manual_seed(cfg.SEED) np.random.seed(cfg.SEED) random.seed(0) # setup training env including loggers logging_train_setup(args, cfg) logger = logging.get_logger("visual_prompt") train_loader, val_loader, test_loader = get_loaders(cfg, logger) logger.info("Constructing models...") model, cur_device = build_model(cfg) logger.info("Setting up Evalutator...") evaluator = Evaluator() logger.info("Setting up Trainer...") trainer = Trainer(cfg, model, evaluator, cur_device) if train_loader: trainer.train_classifier(train_loader, val_loader, test_loader) else: print("No train loader presented. Exit") if cfg.SOLVER.TOTAL_EPOCH == 0: trainer.eval_classifier(test_loader, "test", 0)
时间: 2023-06-18 08:05:17 浏览: 249
这是一个训练函数的代码,它接受两个参数:cfg 和 args。在函数中,首先清除之前运行的缓存,然后设置随机种子以便实现可重复性。接下来,设置日志记录器,获取数据加载器并构建模型。然后设置评估器和训练器,并调用训练器的 train_classifier 方法来训练分类器。如果没有提供训练数据加载器,则输出“没有训练加载器呈现。退出”。最后,如果 SOLVER.TOTAL_EPOCH 为 0,则调用训练器的 eval_classifier 方法在测试数据集上评估分类器。
阅读全文