예제 #1
0
    def init_train_data(self, content_dir, style_dir, batch_size, num_workers,
                        image_size):
        self.batch_size = batch_size
        self.num_workers = num_workers
        self.image_size = image_size

        self.content_dir = content_dir
        self.style_dir = style_dir

        content_tf = dataset.train_transform(self.image_size)
        style_tf = dataset.train_transform(self.image_size)
        content_dataset = dataset.FlatFolderDataset(content_dir, content_tf)
        style_dataset = dataset.FlatFolderDataset(style_dir, style_tf)
        self.content_iter = iter(
            data.DataLoader(
                content_dataset,
                batch_size=self.batch_size,
                sampler=dataset.InfiniteSamplerWrapper(content_dataset),
                num_workers=self.num_workers))
        self.style_iter = iter(
            data.DataLoader(
                style_dataset,
                batch_size=self.batch_size,
                sampler=dataset.InfiniteSamplerWrapper(style_dataset),
                num_workers=self.num_workers))
예제 #2
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
예제 #3
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
예제 #4
0
    def init_train_data(self, content_dir, style_dir, batch_size, num_workers,
                        identity_radio, image_size):
        self.batch_size = batch_size
        self.num_workers = num_workers

        self.content_dir = content_dir
        self.style_dir = style_dir

        if self.train_identity:
            pair_dataset = dataset.FlatFolderDatasetPair(
                args.content_dir,
                args.style_dir,
                dataset.train_transform(image_size),
                identity_radio=5)
            self.pair_iter = iter(
                data.DataLoader(
                    pair_dataset,
                    batch_size=args.batch_size,
                    sampler=dataset.InfiniteSamplerWrapper(pair_dataset),
                    num_workers=num_workers))
        else:
            content_tf = dataset.train_transform(image_size)
            style_tf = dataset.train_transform(image_size)
            content_dataset = dataset.FlatFolderDataset(
                content_dir, content_tf)
            style_dataset = dataset.FlatFolderDataset(style_dir, style_tf)
            self.content_iter = iter(
                data.DataLoader(
                    content_dataset,
                    batch_size=self.batch_size,
                    sampler=dataset.InfiniteSamplerWrapper(content_dataset),
                    num_workers=self.num_workers))
            self.style_iter = iter(
                data.DataLoader(
                    style_dataset,
                    batch_size=self.batch_size,
                    sampler=dataset.InfiniteSamplerWrapper(style_dataset),
                    num_workers=self.num_workers))
예제 #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)
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)
    device = torch.device('cuda')
    save_dir = Path(args.save_dir + '_seed' + str(args.rand_seed))
    save_dir.mkdir(exist_ok=True, parents=True)
    print(f'=> save_dir: {str(save_dir)}')
    writer = SummaryWriter(log_dir=str(save_dir))

    vgg.load_state_dict(torch.load(args.vgg))
    vgg = nn.Sequential(*list(vgg.children())[:31])
    if args.saadain:
        network = SaAdaINNet(vgg, decoder)
    else:
        network = AdaINNet(vgg, decoder)
    network.train()
    network.to(device)

    content_tf = train_transform()
    style_tf = train_transform()

    content_dataset = FlatFolderDataset(args.content_dir, content_tf)
    style_dataset = FlatFolderDataset(args.style_dir, style_tf)

    val_content_dataset = FlatFolderDataset(args.val_content_dir, content_tf)
    val_style_dataset = FlatFolderDataset(args.val_style_dir, style_tf)

    content_iter = iter(
        data.DataLoader(content_dataset,
                        batch_size=args.batch_size,
                        sampler=InfiniteSamplerWrapper(content_dataset),
                        num_workers=args.n_threads))
    style_iter = iter(
        data.DataLoader(style_dataset,
예제 #8
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)
    criterionGAN = torch.nn.MSELoss().cuda()
    criterionIdt = torch.nn.L1Loss().cuda()
    criterionAtt = torch.nn.CrossEntropyLoss().cuda()
    criterion = [criterionGAN, criterionIdt, criterionAtt]

    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()