Ejemplo n.º 1
0
def load_data():
    train_data = dataset.Market_DataLoader(imgs_path=cfg.TRAIN.imgs_path,
                                           pose_path=cfg.TRAIN.pose_path,
                                           idx_path=cfg.TRAIN.idx_path,
                                           transform=dataset.train_transform(),
                                           loader=dataset.val_loader)
    train_loader = Data.DataLoader(train_data,
                                   batch_size=cfg.TRAIN.BATCH_SIZE,
                                   shuffle=True,
                                   num_workers=cfg.TRAIN.NUM_WORKERS,
                                   drop_last=True)

    val_data = dataset.Market_DataLoader(imgs_path=cfg.TRAIN.imgs_path,
                                         pose_path=cfg.TRAIN.pose_path,
                                         idx_path=cfg.TEST.idx_path,
                                         transform=dataset.val_transform(),
                                         loader=dataset.val_loader)
    val_loader = Data.DataLoader(val_data,
                                 batch_size=cfg.TEST.BATCH_SIZE,
                                 shuffle=False,
                                 num_workers=cfg.TRAIN.NUM_WORKERS)

    train = [train_data, train_loader]
    val = [val_data, val_loader]
    return train, val
Ejemplo n.º 2
0
def create_data_loaders(fold, batch_size, workers):
    train_ids, val_ids = dataset.get_split(fold)

    labeled_size = len(train_ids)
    unlabeled_size = 18000
    sampler = dataset.TwoStreamBatchSampler(
        range(labeled_size),  # labeled ids
        list(range(labeled_size,
                   labeled_size + unlabeled_size)),  # unlabeled ids
        batch_size,  # total batch size (labeled + unlabeled)
        LABELED_BATCH_SIZE  # labeled batch size # TODO: was .5
    )

    train_loader = dataset.DataLoader(
        dataset=dataset.MeanTeacherTGSDataset(
            train_ids, transform=dataset.train_transform(), mode='train'),
        # shuffle=True,
        num_workers=workers,
        batch_sampler=sampler,
        pin_memory=torch.cuda.is_available())

    valid_loader = dataset.make_loader(
        val_ids,
        transform=dataset.val_transform(),
        shuffle=False,
        # batch_size=len(device_ids),
        batch_size=batch_size,  # len(device_ids),
        workers=workers)

    return train_loader, valid_loader
Ejemplo n.º 3
0
def initialize_PN_GAN():
    model_path = './G_12.pkl'
    pn_gan = load_network(model_path)
    pose_data = dataset.Pose_Loader(pose_path='./poses/',
                                    transform=dataset.val_transform(),
                                    loader=dataset.val_loader)
    use_gpu = torch.cuda.is_available()
    pin_memory = True if use_gpu else False
    return pn_gan[0], DataLoader(pose_data,
                                 batch_size=8,
                                 shuffle=False,
                                 num_workers=1,
                                 pin_memory=pin_memory)
Ejemplo n.º 4
0
def load_data(samples):
    val_data = dataset.Market_test(imgs_name=samples,
                                   imgs_path=cfg.TEST.imgs_path,
                                   pose_path=cfg.TEST.pose_path,
                                   transform=dataset.val_transform(),
                                   loader=dataset.val_loader)
    val_loader = Data.DataLoader(val_data,
                                 batch_size=cfg.TEST.BATCH_SIZE,
                                 shuffle=False,
                                 num_workers=cfg.TEST.BATCH_SIZE)

    val = [val_data, val_loader]
    return val
Ejemplo n.º 5
0
def predict_tta(model, ids, output, kind='test', batch_size=32, n_tta=5):
    size = dataset.SIZE
    base_transform = dataset.val_transform()
    
    preds = np.zeros((1+n_tta, len(ids), dataset.NUM_CLASSES), dtype=np.float32)
    preds[0] = predict(model, ids, transform=base_transform, kind=kind, batch_size=batch_size)
            
    tta_transform = dataset.train_transform()
    
    for tta_idx in range(1, n_tta):
        preds[tta_idx] = predict(model, ids, transform=tta_transform, kind=kind, batch_size=batch_size)
        
    mean_preds = np.mean(preds, axis=0)
    np.save(output, mean_preds)
Ejemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser()
    arg = parser.add_argument
    arg('--name', type=str)
    arg('--jaccard-weight', default=0.25, type=float)
    arg('--device-ids',
        type=str,
        default='0',
        help='For example 0,1 to run on two GPUs')
    arg('--fold', type=int, help='fold', default=0)
    arg('--output-dir', default='../data/runs', help='checkpoint root')
    arg('--batch-size', type=int, default=32)
    arg('--iter-size', type=int, default=1)
    arg('--n-epochs', type=int, default=100)
    arg('--lr', type=float, default=0.0001)
    arg('--workers', type=int, default=4)
    arg('--seed', type=int, default=0)
    arg('--model', type=str, default=models.archs[0], choices=models.archs)
    arg('--loss',
        type=str,
        default='focal',
        choices=[
            'focal', 'lovasz', 'bjd', 'bce_jaccard', 'bce_dice', 'cos_dice',
            'hinge'
        ])
    arg('--focal-gamma', type=float, default=.5)
    arg('--num-channels', type=int, default=3)
    arg('--weighted-sampler', action="store_true")
    arg('--ignore-empty-masks', action='store_true')
    arg('--remove-suspicious', action='store_true')
    arg('--resume', action="store_true")
    args = parser.parse_args()

    random.seed(args.seed)
    torch.manual_seed(args.seed)

    if not args.name:
        experiment = uuid.uuid4().hex
    else:
        experiment = args.name

    output_dir = Path(args.output_dir) / experiment
    output_dir.mkdir(exist_ok=True, parents=True)
    output_dir.joinpath('params.json').write_text(
        json.dumps(vars(args), indent=True, sort_keys=True))

    # in case --resume is provided it will be loaded later
    model = models.get_model(None, args.model)
    # model = models.get_model(f"../data/runs/exp81/model_{args.fold}.pth", args.model)

    if torch.cuda.is_available():
        if args.device_ids:
            device_ids = list(map(int, args.device_ids.split(',')))
        else:
            device_ids = None
        model = nn.DataParallel(model, device_ids=device_ids).cuda()

    train_ids, val_ids = dataset.get_split(args.fold)

    cudnn.benchmark = True

    train_loader = dataset.make_loader(
        train_ids,
        num_channels=args.num_channels,
        transform=dataset.train_transform(),
        shuffle=True,
        weighted_sampling=args.weighted_sampler,
        ignore_empty_masks=args.ignore_empty_masks,
        remove_suspicious=args.remove_suspicious,
        batch_size=args.batch_size,
        workers=args.workers)

    valid_loader = dataset.make_loader(
        val_ids,
        num_channels=args.num_channels,
        transform=dataset.val_transform(),
        shuffle=False,
        #batch_size=len(device_ids),
        batch_size=args.batch_size,  # len(device_ids),
        workers=args.workers)

    # optimizer = Adam([p for p in model.parameters() if p.requires_grad], lr=args.lr)
    optimizer = Adam(model.parameters(), lr=args.lr)

    # loss = LossBinary(jaccard_weight=args.jaccard_weight)
    # loss = LossBinaryMixedDiceBCE(dice_weight=0.5, bce_weight=0.5)
    if args.loss == 'focal':
        loss = FocalLoss(args.focal_gamma)
    elif args.loss == 'lovasz':
        loss = LossLovasz()
    elif args.loss == 'bjd':
        loss = BCEDiceJaccardLoss({'bce': 0.25, 'jaccard': None, 'dice': 0.75})
    elif args.loss == 'bce_jaccard':
        loss = LossBinary(args.jaccard_weight)
    elif args.loss == 'bce_dice':
        import loss2
        bce_weight = 1
        dice_weight = 2
        loss = loss2.make_loss(bce_weight, dice_weight)
    elif args.loss == 'cos_dice':
        import loss2
        loss = loss2.make_cos_dice_loss()
    elif args.loss == 'hinge':
        loss = LossHinge()

    else:
        raise NotImplementedError

    validation = validation_binary
    scheduler = ReduceLROnPlateau(optimizer,
                                  verbose=True,
                                  min_lr=1e-7,
                                  factor=0.5)
    snapshot = utils.fold_snapshot(output_dir,
                                   args.fold) if args.resume else None

    utils.train(experiment=experiment,
                output_dir=output_dir,
                optimizer=optimizer,
                args=args,
                model=model,
                criterion=loss,
                scheduler=scheduler,
                train_loader=train_loader,
                valid_loader=valid_loader,
                validation=validation,
                fold=args.fold,
                batch_size=args.batch_size,
                n_epochs=args.n_epochs,
                snapshot=snapshot,
                iter_size=args.iter_size)
Ejemplo n.º 7
0
def main():
    args = build_train_args()

    random.seed(args.seed)
    torch.manual_seed(args.seed)

    experiment = args.name if args.name else uuid.uuid4().hex

    output_dir = Path(args.output_dir) / experiment
    output_dir.mkdir(exist_ok=True, parents=True)
    output_dir.joinpath('params.json').write_text(
        json.dumps(vars(args), indent=True, sort_keys=True))

    # this is different than --resume, which loads optimizer state as well
    initial_model_path = os.path.join(
        output_dir, f"model_{args.fold}.pth") if args.load_weights else None
    model = models.get_model(initial_model_path, args.model)

    train_ids, val_ids = dataset.get_split(args.fold)

    cudnn.benchmark = True

    train_loader = dataset.make_loader(train_ids,
                                       num_channels=args.num_channels,
                                       transform=dataset.train_transform(),
                                       shuffle=True,
                                       weighted_sampling=args.weighted_sampler,
                                       batch_size=args.batch_size,
                                       workers=args.workers)

    valid_loader = dataset.make_loader(
        val_ids,
        num_channels=args.num_channels,
        transform=dataset.val_transform(),
        shuffle=False,
        weighted_sampling=False,
        batch_size=args.batch_size,  # len(device_ids),
        workers=args.workers)

    #     train last layer only
    #     for m in model.children():
    #         if m in [model.fc]:
    #             continue
    #         for param in m.parameters():
    #             param.requires_grad = False

    # set all layers except fc to lr=0
    #fc_params = list(map(id, model.fc.parameters()))
    #base_params = filter(lambda p: id(p) not in fc_params, model.parameters())
    #optimizer = torch.optim.Adam([
    #     {'params': base_params, 'lr': args.lr * 0.001},
    #     {'params': model.fc.parameters(), 'lr': args.lr}
    #], lr=args.lr * 0.001)
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    # optimizer = torch.optim.SGD(model.parameters(), lr=0.03, momentum=0.9, weight_decay=1e-4)

    # criterion = FocalLoss(gamma=args.focal_gamma)
    criterion = nn.BCEWithLogitsLoss().cuda()
    # criterion = losses.f1_loss

    validation_fn = validation_multi
    #scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
    #    optimizer, mode='max', verbose=True, min_lr=1e-7, factor=0.5, patience=5)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=10,
                                                gamma=0.1)

    snapshot = utils.fold_snapshot(output_dir,
                                   args.fold) if args.resume else None

    device_ids = list(map(
        int, args.device_ids.split(','))) if args.device_ids else None
    wrapped_model = nn.DataParallel(model, device_ids=device_ids).cuda()

    # unfreeze
    # for m in model.children():
    #     for param in m.parameters():
    #         param.requires_grad = True

    # fc1_params = list(map(id, model.fc1.parameters()))
    # fc2_params = list(map(id, model.fc2.parameters()))
    # base_params = filter(lambda p: id(p) not in fc1_params and id(p) not in fc2_params, model.parameters())
    #
    # # fc at args.lr and rest with args.lr * 0.1
    # optimizer = torch.optim.Adam([
    #     {'params': base_params},
    #     {'params': model.fc1.parameters(), 'lr': args.lr},
    #     {'params': model.fc2.parameters(), 'lr': args.lr},
    # ], lr=args.lr * 0.1)

    # aternatively, add the unfrozen fc2 weight to the current optimizer
    # optimizer.add_param_group({'params': net.fc2.parameters()})

    utils.train(experiment=experiment,
                output_dir=output_dir,
                optimizer=optimizer,
                args=args,
                model=wrapped_model,
                criterion=criterion,
                scheduler=scheduler,
                train_loader=train_loader,
                valid_loader=valid_loader,
                validation_fn=validation_fn,
                fold=args.fold,
                batch_size=args.batch_size,
                n_epochs=args.n_epochs,
                snapshot=snapshot,
                iter_size=args.iter_size)
    return criterion



#%%
if __name__ == '__main__':
    
    sys.stdout = logger.Logger('./log_GAN_ep2.txt')
    
    train_data = dataset.Market_DataLoader(imgs_path=cfg.TRAIN.imgs_path, pose_path=cfg.TRAIN.pose_path, idx_path=cfg.TRAIN.idx_path,
                                           transform=dataset.train_transform(), img_loader=dataset.val_loader, pose_loader=dataset.pose_loader)
    
    train_loader = Data.DataLoader(train_data, batch_size=cfg.TRAIN.BATCH_SIZE, shuffle=True, num_workers=cfg.TRAIN.NUM_WORKERS, drop_last=True)

    val_data = dataset.Market_DataLoader(imgs_path=cfg.TRAIN.imgs_path, pose_path=cfg.TRAIN.pose_path, idx_path=cfg.TEST.idx_path,
                                         transform=dataset.val_transform(), img_loader=dataset.val_loader, pose_loader=dataset.pose_loader)
    
    val_loader = Data.DataLoader(val_data, batch_size=cfg.TEST.BATCH_SIZE, shuffle=False,
                                 num_workers=cfg.TRAIN.NUM_WORKERS)

    train_file = [train_data, train_loader]
    val_file = [val_data, val_loader]
    
    
#    train_file, val_file = load_data()
    nets = load_network()
    optimizers, schedulers, summary = Optimizer(nets)
    criterion = loss_func()
    
#    train(train_file, val_file, nets, optimizers, schedulers, summary, criterion)
    
    loss = loss = LossLovasz()
    validation = validation_binary
    scheduler = ReduceLROnPlateau(optimizer,
                                  verbose=True,
                                  min_lr=1e-7,
                                  factor=0.5,
                                  patience=5)

    output_dir = Path('tmp')
    output_dir.mkdir(exist_ok=True, parents=True)

    train_ids, val_ids = dataset.get_split(fold)
    train_loader = DataLoader(dataset=TGSDataset(
        train_ids,
        num_channels=3,
        transform=dataset.val_transform(),
        mode='train'),
                              shuffle=True,
                              num_workers=4,
                              batch_size=24,
                              sampler=None,
                              pin_memory=torch.cuda.is_available())
    val_loader = DataLoader(dataset=TGSDataset(
        val_ids,
        num_channels=3,
        transform=dataset.val_transform(),
        mode='train'),
                            shuffle=False,
                            num_workers=4,
                            batch_size=1,
                            sampler=None,