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-24 17:05:59 浏览: 227
gpio-cfg.rar_If...
这是一个训练模型的函数,其参数包括一个配置文件和一些参数。在该函数中,首先清除了之前运行留下的缓存,然后设置了随机种子以保证可重复性,接着获取了训练、验证和测试数据集的加载器,构建了模型,设置了评估器和训练器,并调用了训练器的 train_classifier 方法进行训练。如果没有提供训练数据集,则输出 "No train loader presented. Exit"。最后,如果设置了总共的训练轮数为 0,则调用训练器的 eval_classifier 方法进行模型的测试评估。
阅读全文