torch.backends.cudnn.deterministic = True np.random.seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) # fetch dataloaders # fetch dataloaders val_dl = dataloader.fetch_dataloader(args.data_dir, args.txt_val, "val", params) # Define the model model = get_network(params).to(params.device) #num_classes+1 for background. metrics = OrderedDict({}) for metric in params.metrics: metrics[metric] = get_metrics(metric, params) # Reload weights from the saved file model = utils.load_checkpoint(model, is_best=True, checkpoint_dir=args.checkpoint_dir)[0] # Evaluate eval_loss, val_metrics = evaluate(model, val_dl, loss_fn=None, metrics=metrics, params=params) best_json_path = os.path.join(args.model_dir, "logs/evaluation.json") for val_metric_name, val_metric_results in val_metrics.items():
ckpt_target_file_path = os.path.join(args.checkpoint_dir_target, best_ckpt_filename) if os.path.exists(ckpt_target_file_path): model_target = utils.load_checkpoint(model_target, ckpt_dir=args.checkpoint_dir_target, filename=best_ckpt_filename, is_best=True)[0] print("=> loaded target model checkpoint form {}".format(ckpt_target_file_path)) else: print("=> Initializing target model from scratch") for p in model_source.parameters(): p.requires_grad = False model_source.eval() for p in model_target.parameters(): p.requires_grad = False model_target.eval() opt = optim.AdamW(transfer.parameters(), lr=params_transfer.learning_rate) lr_scheduler = torch.optim.lr_scheduler.OneCycleLR( opt, max_lr=params_transfer.learning_rate, steps_per_epoch=len(train_dl), epochs=params_transfer.num_epochs, div_factor=20) # fetch loss function and metrics loss_fn = get_loss_fn(params_transfer) # num_classes+1 for background. metrics = OrderedDict({}) for metric in params_transfer.metrics: metrics[metric] = get_metrics(metric, params_transfer) # Train the model logging.info("Starting training for {} epoch(s)".format(params_transfer.num_epochs)) train_and_evaluate(model_source, model_target, transfer, train_dl, val_dl_source, val_dl_target, opt, loss_fn, metrics, params_transfer, lr_scheduler, args.checkpoint_dir_transfer, ckpt_filename, log_dir, writer)
None, None, ckpt_dir=args.checkpoint_dir_target, filename=ckpt_filename) print("=> loaded target model checkpoint form {}".format( ckpt_target_file_path)) else: print("=> Initializing target model from scratch") # fetch loss function and metrics loss_fn1 = get_loss_fn(params_source) loss_fn2 = get_loss_fn(params_target) metrics_depth = OrderedDict({}) for metric in params_source.metrics: metrics_depth[metric] = get_metrics(metric, params_source) metrics_segmentation = OrderedDict({}) for metric in params_target.metrics: metrics_segmentation[metric] = get_metrics(metric, params_target) # Train the model logging.info("Starting training for {} epoch(s)".format( params_transfer.num_epochs)) train_and_evaluate( model_source, model_target, transfer, train_dl_all, val_dl_all, val_dl_target, opt1, opt2, opt3, loss_fn1, loss_fn2, metrics_depth, metrics_segmentation, params_transfer, lr_scheduler1, lr_scheduler2, lr_scheduler3, args.checkpoint_dir_source, args.checkpoint_dir_target, args.checkpoint_dir_transfer, ckpt_filename, log_dir, writer)