Ejemplo n.º 1
0
def train(cfg, local_rank, distributed, use_tensorboard=False):
    model = build_detection_model(cfg)
    device = torch.device(cfg.MODEL.DEVICE)
    model.to(device)

    optimizer = make_optimizer(cfg, model)
    scheduler = make_lr_scheduler(cfg, optimizer)

    # Initialize mixed-precision training
    use_mixed_precision = cfg.DTYPE == "float16"
    amp_opt_level = 'O1' if use_mixed_precision else 'O0'
    model, optimizer = amp.initialize(model, optimizer, opt_level=amp_opt_level)

    if distributed:
        model = torch.nn.parallel.DistributedDataParallel(
            model, device_ids=[local_rank], output_device=local_rank,
            # this should be removed if we update BatchNorm stats
            broadcast_buffers=False, find_unused_parameters=True,
        )

    arguments = {"iteration": 0, "iter_size": cfg.SOLVER.ITER_SIZE}
    output_dir = cfg.OUTPUT_DIR
    save_to_disk = get_rank() == 0
    checkpoint_period = cfg.SOLVER.CHECKPOINT_PERIOD
    checkpointer = DetectronCheckpointer(
        cfg, model, optimizer, scheduler, output_dir, save_to_disk
    )
    extra_checkpoint_data = checkpointer.load(cfg.MODEL.WEIGHT)
    arguments.update(extra_checkpoint_data)

    data_loader = make_data_loader(
        cfg,
        is_train=True,
        is_distributed=distributed,
        start_iter=arguments["iteration"],
    )

    if use_tensorboard:
        meters = TensorboardLogger(
            log_dir=os.path.join(cfg['OUTPUT_DIR'], 'log/'),
            start_iter=arguments['iteration'],
            delimiter="  ")
    else:
        meters = MetricLogger(delimiter="  ")
        
    do_train(
        model,
        data_loader,
        optimizer,
        scheduler,
        checkpointer,
        device,
        checkpoint_period,
        arguments,
        meters
    )

    return model
Ejemplo n.º 2
0
def train_cdb(cfg, local_rank, distributed, use_tensorboard=False):
    model = build_detection_model(cfg)
    device = torch.device(cfg.MODEL.DEVICE)
    model.to(device)
    model_cdb = ConvConcreteDB(cfg, model.backbone.out_channels)
    model_cdb.to(device)

    optimizer = make_optimizer(cfg, model)
    scheduler = make_lr_scheduler(cfg, optimizer)
    optimizer_cdb = make_cdb_optimizer(cfg, model_cdb)
    scheduler_cdb = make_lr_cdb_scheduler(cfg, optimizer_cdb)

    # Initialize mixed-precision training
    use_mixed_precision = cfg.DTYPE == "float16"
    amp_opt_level = 'O1' if use_mixed_precision else 'O0'
    model, optimizer = amp.initialize(model,
                                      optimizer,
                                      opt_level=amp_opt_level)
    model_cdb, optimizer_cdb, = amp.initialize(model_cdb,
                                               optimizer_cdb,
                                               opt_level=amp_opt_level)

    if distributed:
        model = torch.nn.parallel.DistributedDataParallel(
            model,
            device_ids=[local_rank],
            output_device=local_rank,
            # this should be removed if we update BatchNorm stats
            broadcast_buffers=False,
            find_unused_parameters=True,
        )
        model_cdb = torch.nn.parallel.DistributedDataParallel(
            model_cdb,
            device_ids=[local_rank],
            output_device=local_rank,
            # this should be removed if we update BatchNorm stats
            broadcast_buffers=False,
            find_unused_parameters=True,
        )

    arguments = {"iteration": 0}
    output_dir = cfg.OUTPUT_DIR
    save_to_disk = get_rank() == 0
    checkpoint_period = cfg.SOLVER.CHECKPOINT_PERIOD
    # TODO: check whether the *_cdb is properly loaded for inference when using 1 GPU
    checkpointer = DetectronCheckpointer(cfg,
                                         model,
                                         optimizer,
                                         scheduler,
                                         output_dir,
                                         save_to_disk,
                                         model_cdb=model_cdb)
    extra_checkpoint_data = checkpointer.load(cfg.MODEL.WEIGHT)
    arguments.update(extra_checkpoint_data)

    print('Do CFG_DIRE', cfg.PATH_DATA_TRAIN)
    data_loader = make_data_loader(
        cfg,
        is_train=True,
        is_distributed=distributed,
        start_iter=arguments["iteration"],
    )

    if use_tensorboard:
        meters = TensorboardLogger(log_dir=os.path.join(
            cfg['OUTPUT_DIR'], 'log/'),
                                   start_iter=arguments['iteration'],
                                   delimiter="  ")
    else:
        meters = MetricLogger(delimiter="  ")

    do_train_cdb(model, model_cdb, data_loader, optimizer, optimizer_cdb,
                 scheduler, scheduler_cdb, checkpointer, device,
                 checkpoint_period, arguments, meters, cfg)

    return model