Ejemplo n.º 1
0
def get_train_loader():
    train_loader = TripletImageLoader(
        'data/FID-300/',
        'train.txt',
        [0, 250],
        transform=transforms.Compose([
            transforms.Resize((224, 112)),
            transforms.CenterCrop((224, 112)),
            transforms.ToTensor(),
            #normalize,
            transforms.Normalize(mean=[0.5], std=[1])
        ]))
    train_loader.real_mode_prob = 0.3
    return train_loader
Ejemplo n.º 2
0
    def __make_default_dataloader(self, split: str, augment: bool,
                                  num_triplets: int):
        transforms = self.__get_transforms(augment)

        dataset = TripletImageLoader(root='data',
                                     base_path='ut-zap50k-images',
                                     filenames_filename='filenames.json',
                                     conditions=[0, 1, 2, 3],
                                     split=split,
                                     n_triplets=num_triplets,
                                     transform=Compose(transforms))

        return self.__make_dataloader_from_dataset(dataset)
Ejemplo n.º 3
0
def get_ref_loader():
    # Ref Loader
    ref_loader = TripletImageLoader('data/FID-300/',
                                    'ref.txt', [0, 2000],
                                    transform=transforms.Compose([
                                        transforms.Resize((224, 112)),
                                        transforms.CenterCrop((224, 112)),
                                        transforms.ToTensor(),
                                        transforms.Normalize(mean=[0.5],
                                                             std=[1])
                                    ]))
    ref_loader.real_mode_prob = 1
    ref_loader.angle_mod = 0
    ref_loader.scale_mod = 0
    ref_loader.trans_mod = 0
    ref_loader.flip_mod = 0
    ref_loader.load_ref = True

    return ref_loader
Ejemplo n.º 4
0
def get_val_loader():
    # Val Loader
    #val_loader =  TripletImageLoader('data/FID-300/', 'train.txt', [0,50],
    val_loader = TripletImageLoader('data/FID-300/',
                                    'val.txt', [0, 250],
                                    transform=transforms.Compose([
                                        transforms.Resize((224, 112)),
                                        transforms.CenterCrop((224, 112)),
                                        transforms.ToTensor(),
                                        transforms.Normalize(mean=[0.5],
                                                             std=[1])
                                    ]))
    val_loader.real_mode_prob = 1
    val_loader.angle_mod = 0
    val_loader.scale_mod = 0
    val_loader.trans_mod = 0
    val_loader.flip_mod = 0

    return val_loader
Ejemplo n.º 5
0
def main():
    global args, best_acc
    args = parser.parse_args()
    args.cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)
    if args.visdom:
        global plotter
        plotter = VisdomLinePlotter(env_name=args.name)

    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    global conditions
    if args.conditions is not None:
        conditions = args.conditions
    else:
        conditions = [0, 1, 2, 3]

    kwargs = {'num_workers': 8, 'pin_memory': True} if args.cuda else {}
    print('Loading Train Dataset')
    train_loader = torch.utils.data.DataLoader(TripletImageLoader(
        'data',
        'ut-zap50k-images',
        'filenames.json',
        conditions,
        'train',
        n_triplets=args.num_traintriplets,
        transform=transforms.Compose([
            transforms.Resize(112),
            transforms.CenterCrop(112),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ])),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               **kwargs)
    print('Loading Test Dataset')
    test_loader = torch.utils.data.DataLoader(TripletImageLoader(
        'data',
        'ut-zap50k-images',
        'filenames.json',
        conditions,
        'test',
        n_triplets=160000,
        transform=transforms.Compose([
            transforms.Resize(112),
            transforms.CenterCrop(112),
            transforms.ToTensor(),
            normalize,
        ])),
                                              batch_size=16,
                                              shuffle=True,
                                              **kwargs)
    print('Loading Val Dataset')
    val_loader = torch.utils.data.DataLoader(TripletImageLoader(
        'data',
        'ut-zap50k-images',
        'filenames.json',
        conditions,
        'val',
        n_triplets=80000,
        transform=transforms.Compose([
            transforms.Resize(112),
            transforms.CenterCrop(112),
            transforms.ToTensor(),
            normalize,
        ])),
                                             batch_size=16,
                                             shuffle=True,
                                             **kwargs)

    model = Resnet_models.resnext50_32x4d()
    csn_model = ConditionalSimNet(model,
                                  n_conditions=args.num_concepts,
                                  embedding_size=args.dim_embed,
                                  learnedmask=args.learned,
                                  prein=args.prein)
    global mask_var
    mask_var = csn_model.masks.weight
    tnet = CS_Tripletnet(csn_model, args.num_concepts)
    if args.cuda:
        tnet.cuda()

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            tnet.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    criterion = torch.nn.MarginRankingLoss(margin=args.margin)
    parameters = filter(lambda p: p.requires_grad, tnet.parameters())
    optimizer = optim.Adam(parameters, lr=args.lr)

    n_parameters = sum([p.data.nelement() for p in tnet.parameters()])
    print('  + Number of params: {}'.format(n_parameters))

    if args.test:
        checkpoint = torch.load('runs/%s/' % ('new_context_4/') +
                                'model_best.pth.tar')
        tnet.load_state_dict(checkpoint['state_dict'])
        test_acc = test(test_loader, tnet, criterion, 1)
        sys.exit()

    for epoch in range(args.start_epoch, args.epochs + 1):
        # update learning rate
        adjust_learning_rate(optimizer, epoch)
        # train for one epoch
        train(train_loader, tnet, criterion, optimizer, epoch)
        # evaluate on validation set
        acc = test(val_loader, tnet, criterion, epoch)

        # remember best acc and save checkpoint
        is_best = acc > best_acc
        best_acc = max(acc, best_acc)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'state_dict': tnet.state_dict(),
                'best_prec1': best_acc,
            }, is_best)

    checkpoint = torch.load('runs/%s/' % (args.name) + 'model_best.pth.tar')
    tnet.load_state_dict(checkpoint['state_dict'])
    test_acc = test(test_loader, tnet, criterion, 1)
def main():
    parser = argparse.ArgumentParser(description='This is a WIP program')
    parser.add_argument('--batch-size', type=int, default=64, metavar='N',
                        help='input batch size for training (default: 64)')
    parser.add_argument('--epochs', type=int, default=200, metavar='N',
                        help='number of epochs to train (default: 200)')
    parser.add_argument('--start_epoch', type=int, default=1, metavar='N',
                        help='number of start epoch (default: 1)')
    parser.add_argument('--lr', type=float, default=5e-5, metavar='LR',
                        help='learning rate (default: 5e-5)')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='enables CUDA training')
    parser.add_argument('--log-interval', type=int, default=20, metavar='N',
                        help='how many batches to wait before logging training status')
    parser.add_argument('--margin', type=float, default=0.2, metavar='M',
                        help='margin for triplet loss (default: 0.2)')
    parser.add_argument('--resume', default='', type=str,
                        help='path to latest checkpoint (default: none)')
    parser.add_argument('--name', default='Conditional_Similarity_Network', type=str,
                        help='name of experiment')
    parser.add_argument('--embed_loss', type=float, default=5e-3, metavar='M',
                        help='parameter for loss for embedding norm')
    parser.add_argument('--mask_loss', type=float, default=5e-4, metavar='M',
                        help='parameter for loss for mask norm')
    parser.add_argument('--num_traintriplets', type=int, default=100000, metavar='N',
                        help='how many unique training triplets (default: 100000)')
    parser.add_argument('--dim_embed', type=int, default=64, metavar='N',
                        help='how many dimensions in embedding (default: 64)')
    parser.add_argument('--test', dest='test', action='store_true',
                        help='To only run inference on test set')
    parser.add_argument('--learned', dest='learned', action='store_true',
                        help='To learn masks from random initialization')
    parser.add_argument('--prein', dest='prein', action='store_true',
                        help='To initialize masks to be disjoint')
    parser.add_argument('--conditions', nargs='*', type=int,
                        help='Set of similarity notions')
    parser.add_argument('--out', type=str, default='result',
                        help='dir to save models and log')
    parser.set_defaults(test=False)
    parser.set_defaults(learned=False)
    parser.set_defaults(prein=False)
    args = parser.parse_args()
    args.cuda = not args.no_cuda and torch.cuda.is_available()

    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)

    normalize = T.Normalize(mean=[0.485, 0.456, 0.406],
                            std=[0.229, 0.224, 0.225])
    if args.conditions is not None:
        conditions = args.conditions
    else:
        conditions = list(range(4))

    kwargs = {'num_workers': 4, 'pin_memory': True} if args.cuda else dict()
    train_loader = torch.utils.data.DataLoader(
        TripletImageLoader('data', 'ut-zap50k-images', 'filenames.json',
                           conditions, 'train', n_triplets=args.num_traintriplets,
                           transform=T.Compose([
                               T.Scale(112),
                               T.CenterCrop(112),
                               T.RandomHorizontalFlip(),
                               T.ToTensor(),
                               normalize,
                           ])),
        batch_size=args.batch_size, shuffle=True, **kwargs)
    test_loader = torch.utils.data.DataLoader(
        TripletImageLoader('data', 'ut-zap50k-images', 'filenames.json',
                           conditions, 'test', n_triplets=160000,
                           transform=T.Compose([
                               T.Scale(112),
                               T.CenterCrop(112),
                               T.ToTensor(),
                               normalize,
                           ])),
        batch_size=args.batch_size, shuffle=True, **kwargs)
    val_loader = torch.utils.data.DataLoader(
        TripletImageLoader('data', 'ut-zap50k-images', 'filenames.json',
                           conditions, 'val', n_triplets=80000,
                           transform=T.Compose([
                               T.Scale(112),
                               T.CenterCrop(112),
                               T.ToTensor(),
                               normalize,
                           ])),
        batch_size=args.batch_size, shuffle=True, **kwargs)

    model = resnet_18.resnet18(pretrained=True, embedding_size=args.dim_embed)
    csn_model = ConditionalSimNet(model, n_conditions=len(conditions),
                                  embedding_size=args.dim_embed,
                                  learnedmask=args.learned, prein=args.prein)
    mask_var = csn_model.masks.weight
    triplet_net = CS_Tripletnet(csn_model)
    if args.cuda:
        triplet_net.cuda()

    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            triplet_net.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    criterion = torch.nn.MarginRankingLoss(margin=args.margin)
    parameters = filter(lambda p: p.requires_grad, triplet_net.parameters())
    optimizer = optim.Adam(parameters, lr=args.lr)
    n_param = sum([p.data.nelement() for p in triplet_net.parameters()])
    print('# of parameters: {}'.format(n_param))
    print('\n\n')

    if args.test:
        import sys
        test_loss, test_acc = test(test_loader, triplet_net, args.epochs + 1)
        print('accuracy: {}, loss: {}'.format(test_acc.avg, test_loss.avg))
        sys.exit()

    root = os.path.join(args.out, dt.now().strftime('%m%d_%H%M'))
    if not os.path.exists(root):
        os.makedirs(root)
    best_acc = .0
    log = dict()
    start_time = dt.now()
    for epoch in tqdm(range(args.start_epoch, args.epochs + 1), desc='total'):
        adjust_lr(args, optimizer, epoch)
        loss_acc_log = train(args, train_loader, triplet_net,
                             criterion, optimizer, epoch)
        log['epoch_{}_train'.format(epoch)] = loss_acc_log
        losses, accs = test(args, val_loader, triplet_net, criterion, epoch)
        log['epoch_{}_val'.format(epoch)] = {
            'loss': losses.avg, 'acc': 100. * accs.avg}
        tqdm.write('[validation]\nloss: {:.4f}\tacc: {:.2f}%\n'.format(
            losses.avg, 100. * accs.avg))

        is_best = accs.avg > best_acc
        best_acc = max(accs.avg, best_acc)
        save_ckpt(root, triplet_net.state_dict(), is_best)

    end_time = dt.now()
    print('\ntraining finished.')
    print('started at {}, ended at {}, duration is {} hours'.format(
        start_time.strftime('%m%d, %H:%M'), end_time.strftime('%m%d, %H:%M'),
        (end_time - start_time).total_seconds() / 3600.))
    save_ckpt(root, triplet_net.state_dict(), filename='model.pth')
    log_filepath = os.path.join(root, 'log.pkl')
    with open(log_filepath, 'wb') as f:
        pickle.dump(log, f)
    print('log files saved at {}'.format(log_filepath))
Ejemplo n.º 7
0
def main():
    global args, best_acc
    global  log_interval
    log_interval = 30
    args = parser.parse_args()
    print(args)
    nz = int(args.dim_embed)
    nef = int(args.nef)
    ndf = int(args.ndf)
    ngpu = int(args.ngpu)
    nc = int(args.nc)
    out_size = args.image_size // 16

    args.cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)
    

    normalize = transforms.Normalize(mean=[0.0, 0.0, 0.0],
                                     std=[1, 1, 1])

    
    
    out_size = args.image_size // 16  
    kwargs = {'num_workers': 4, 'pin_memory': True} if args.cuda else {}
    train_loader = torch.utils.data.DataLoader(
        TripletImageLoader('../data', '', 'train/train_data.json', 
                        'train', n_triplets=args.num_traintriplets,
                        transform=transforms.Compose([
                            transforms.Scale(args.image_size),
                            transforms.CenterCrop(args.image_size),
                            transforms.RandomHorizontalFlip(),
                            transforms.ToTensor(),
                            normalize,
                    ])),
        batch_size=args.train_batch_size, shuffle=True, **kwargs)

    test_loader = torch.utils.data.DataLoader(
        TripletImageLoader('../data', '', 'test/test_data.json', 
                'test', n_triplets=args.num_testtriplets,
                        transform=transforms.Compose([
                            transforms.Scale(args.image_size),
                            transforms.CenterCrop(args.image_size),
                            transforms.ToTensor(),
                            normalize,
                    ])),
        batch_size=args.batch_size, shuffle=True, **kwargs)
    val_loader = torch.utils.data.DataLoader(
        TripletImageLoader('../data', '', 'val/val_data.json', 
                        'val', n_triplets=args.num_valtriplets,
                        transform=transforms.Compose([
                            transforms.Scale(args.image_size),
                            transforms.CenterCrop(args.image_size),
                            transforms.ToTensor(),
                            normalize,
                    ])),
        batch_size=args.batch_size, shuffle=True, **kwargs)
    
    encoder = _Encoder(ngpu,nc,nef,out_size,nz)
    encoder.apply(weights_init)
    
    print(encoder)
    if args.cuda:
        encoder = encoder.cuda()
    tnet = Tripletnet(encoder)
    if args.cuda:
        tnet.cuda()

    decoder = _Decoder(ngpu,nc,ndf,out_size,nz)
    decoder.apply(weights_init)

    print(decoder)
     
    if args.cuda:
        decoder = decoder.cuda()

    descriptor = _VGG(ngpu)
    
    if args.cuda:
        descriptor = descriptor.cuda()
    print(descriptor)

    global train_loss_metric,train_loss_VAE,train_acc_metric,test_loss_metric,test_loss_VAE,test_acc_metric
    train_loss_metric = []
    train_loss_VAE = []
    train_acc_metric = []
    test_loss_metric = []
    test_loss_VAE = []
    test_acc_metric = []


    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            train_loss_metric = checkpoint['train_loss_metric']
            train_loss_VAE = checkpoint['train_loss_VAE']
            train_acc_metric = checkpoint['train_acc_metric']
            test_loss_metric = checkpoint['test_loss_metric']
            test_loss_VAE = checkpoint['test_loss_VAE']
            test_acc_metric = checkpoint['test_acc_metric']

            tnet.load_state_dict(checkpoint['state_dict'])
            encoder.load_state_dict(checkpoint['encoder_state_dict'])
            decoder.load_state_dict(checkpoint['decoder_state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})"
                    .format(args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = False

    criterion = torch.nn.MarginRankingLoss(margin = args.margin)
    parameters = list(tnet.parameters()) + list(decoder.parameters())
    optimizer = optim.Adam(parameters, lr=args.lr, betas=(args.beta1, args.beta2))

    n_parameters = sum([p.data.nelement() for p in tnet.parameters()])
    print('  + Number of params in tnet: {}'.format(n_parameters))


    if args.test:
        test_acc = test(test_loader, tnet,decoder,descriptor, criterion, 1)
        sys.exit()

    for epoch in range(args.start_epoch, args.epochs + 1):
        # update learning rate
        adjust_learning_rate(optimizer, epoch)
        # train for one epoch
        train(train_loader, tnet,decoder,descriptor, criterion, optimizer, epoch)
        # evaluate on validation set
        acc = test(val_loader, tnet,decoder,descriptor, criterion, epoch)

        # remember best acc and save checkpoint
        is_best = acc > best_acc
        best_acc = max(acc, best_acc)
        save_checkpoint({
            'epoch': epoch + 1,
            'state_dict': tnet.state_dict(),
            'encoder_state_dict': encoder.state_dict(),
            'decoder_state_dict': decoder.state_dict(),
            'best_prec1': best_acc,
            'train_loss_metric':train_loss_metric,
            'train_loss_VAE':train_loss_VAE,
            'train_acc_metric':train_acc_metric,
            'test_loss_metric':test_loss_metric,
            'test_loss_VAE':test_loss_VAE,
            'test_acc_metric':test_acc_metric,
        }, is_best)
def main():
    global args, best_acc
    args = parser.parse_args()
    args.cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)
    #global plotter
    #plotter = VisdomLinePlotter(env_name=args.name)

    kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}

    train_loader = torch.utils.data.DataLoader(TripletImageLoader(
        '.',
        './filenames_filename.txt',
        './triplets_train_name.txt',
        transform=transforms.Compose([
            transforms.Resize((128, 128)),
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               **kwargs)

    valid_loader = torch.utils.data.DataLoader(TripletImageLoader(
        '.',
        './filenames_filename.txt',
        './triplets_valid_name.txt',
        transform=transforms.Compose([
            transforms.Resize((128, 128)),
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                               batch_size=args.test_batch_size,
                                               shuffle=True,
                                               **kwargs)

    class Net(nn.Module):
        def __init__(self):
            super(Net, self).__init__()
            self.conv1 = nn.Conv2d(3, 10, kernel_size=5)
            self.conv1_drop = nn.Dropout2d()
            self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
            self.conv2_drop = nn.Dropout2d()
            self.fc1 = nn.Linear(16820, 128)
            self.fc2 = nn.Linear(128, 20)

        def forward(self, x):
            x = F.relu(F.max_pool2d(self.conv1(x), 2))
            x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
            x = x.view(-1, 16820)
            x = F.relu(self.fc1(x))
            x = F.dropout(x, training=self.training)
            return self.fc2(x)

    model = Net()
    tnet = Tripletnet(model)
    if args.cuda:
        tnet.cuda()

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            tnet.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    criterion = torch.nn.MarginRankingLoss(margin=args.margin)

    #optimizer = optim.SGD(tnet.parameters(), lr=args.lr, momentum=args.momentum)
    def make_optimizer(model, opt, lr, weight_decay, momentum, nesterov=True):
        if opt == 'SGD':
            optimizer = getattr(torch.optim, opt)(model.parameters(),
                                                  lr=lr,
                                                  weight_decay=weight_decay,
                                                  momentum=momentum,
                                                  nesterov=nesterov)
        elif opt == 'AMSGRAD':
            optimizer = getattr(torch.optim, 'Adam')(model.parameters(),
                                                     lr=lr,
                                                     weight_decay=weight_decay,
                                                     amsgrad=True)
        elif opt == 'Ranger':
            optimizer = Ranger(params=filter(lambda p: p.requires_grad,
                                             model.parameters()),
                               lr=lr)
        elif opt == 'RMS':
            optimizer = torch.optim.RMSprop(model.parameters(),
                                            lr=lr,
                                            alpha=0.99,
                                            eps=1e-08,
                                            weight_decay=weight_decay,
                                            momentum=momentum,
                                            centered=False)
        return optimizer

    optimizer = make_optimizer(tnet,
                               opt=args.opt,
                               lr=args.lr,
                               weight_decay=args.weight_decay,
                               momentum=args.momentum,
                               nesterov=True)
    n_parameters = sum([p.data.nelement() for p in tnet.parameters()])
    print('  + Number of params: {}'.format(n_parameters))

    for epoch in range(1, args.epochs + 1):
        # train for one epoch
        train(train_loader, tnet, criterion, optimizer, epoch)
        # evaluate on validation set
        acc = test(valid_loader, tnet, criterion, epoch)

        # remember best acc and save checkpoint
        is_best = acc > best_acc
        best_acc = max(acc, best_acc)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'state_dict': tnet.state_dict(),
                'best_prec1': best_acc,
            }, is_best)
def main():
    global args, best_acc
    global log_interval
    log_interval = 30
    args = parser.parse_args()
    print(args)
    nz = int(args.dim_embed)
    nef = int(args.nef)
    ndf = int(args.ndf)
    ngpu = int(args.ngpu)
    nc = 3
    out_size = args.image_size // 16
    # Normalize = nn.BatchNorm2d

    args.cuda = not args.no_cuda and torch.cuda.is_available()
    #cuda = args.cuda
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)
    # if args.visdom:
    #     global plotter
    #     plotter = VisdomLinePlotter(env_name=args.name)

    #VGG!!!!
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    # transform = transforms.Compose([
    #     transforms.Scale(args.image_size),
    #     #transforms.CenterCrop(args.image_size),
    #     transforms.ToTensor(),
    #     transforms.Normalize(mean=(0.485, 0.456, 0.406),
    #                         std=(0.229, 0.224, 0.225))])

    global conditions
    #'Set of similarity notions'
    if args.conditions is not None:
        conditions = args.conditions
    else:
        conditions = [0, 1, 2, 3]
    print("#" * 20)
    print('conditions are ', conditions)
    print("#" * 20)
    # out_size = args.image_size // 2**5 #5 is because in vgg19 there are 5 pool layers
    out_size = args.image_size // 16  # from garbage.py: Encoder makes x/2*4 w/ 4 conv/max layer
    kwargs = {'num_workers': 4, 'pin_memory': True} if args.cuda else {}
    train_loader = torch.utils.data.DataLoader(TripletImageLoader(
        'data',
        'ut-zap50k-images',
        'filenames.json',
        conditions,
        'train',
        n_triplets=args.num_traintriplets,
        transform=transforms.Compose([
            transforms.Scale(args.image_size),
            transforms.CenterCrop(args.image_size),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            normalize,
        ])),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               **kwargs)
    test_loader = torch.utils.data.DataLoader(TripletImageLoader(
        'data',
        'ut-zap50k-images',
        'filenames.json',
        conditions,
        'test',
        n_triplets=args.num_testtriplets,
        transform=transforms.Compose([
            transforms.Scale(args.image_size),
            transforms.CenterCrop(args.image_size),
            transforms.ToTensor(),
            normalize,
        ])),
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              **kwargs)
    val_loader = torch.utils.data.DataLoader(TripletImageLoader(
        'data',
        'ut-zap50k-images',
        'filenames.json',
        conditions,
        'val',
        n_triplets=args.num_valtriplets,
        transform=transforms.Compose([
            transforms.Scale(args.image_size),
            transforms.CenterCrop(args.image_size),
            transforms.ToTensor(),
            normalize,
        ])),
                                             batch_size=args.batch_size,
                                             shuffle=True,
                                             **kwargs)

    # model = Resnet_18.resnet18(pretrained=True, embedding_size=args.dim_embed)
    # model = Vgg_19.vgg19(pretrained=True, embedding_size=args.dim_embed,out_size =out_size)
    #embedding_size=64,output_size =8

    #### putting encoder instead of model
    encoder = _Encoder(ngpu, nc, nef, out_size, nz)
    encoder.apply(weights_init)
    # if args.encoder != '':
    #     encoder.load_state_dict(torch.load(args.encoder))
    print(encoder)
    if args.cuda:
        encoder = encoder.cuda()
    #changing csn input to encoder from model(vgg))
    tnet = Tripletnet(encoder)
    if args.cuda:
        tnet.cuda()

    decoder = _Decoder(ngpu, nc, ndf, out_size, nz)
    decoder.apply(weights_init)
    # if args.decoder != '':
    #     decoder.load_state_dict(torch.load(args.decoder))
    print(decoder)

    if args.cuda:
        decoder = decoder.cuda()

    descriptor = _VGG(ngpu)

    if args.cuda:
        descriptor = descriptor.cuda()
    print(descriptor)

    global train_loss_metric, train_loss_VAE, train_acc_metric, test_loss_metric, test_loss_VAE, test_acc_metric
    train_loss_metric = []
    train_loss_VAE = []
    train_acc_metric = []
    test_loss_metric = []
    test_loss_VAE = []
    test_acc_metric = []

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            train_loss_metric = checkpoint['train_loss_metric']
            train_loss_VAE = checkpoint['train_loss_VAE']
            train_acc_metric = checkpoint['train_acc_metric']
            test_loss_metric = checkpoint['test_loss_metric']
            test_loss_VAE = checkpoint['test_loss_VAE']
            test_acc_metric = checkpoint['test_acc_metric']

            tnet.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = False

    criterion = torch.nn.MarginRankingLoss(margin=args.margin)
    # parameters = filter(lambda p: p.requires_grad, tnet.parameters())
    parameters = list(tnet.parameters()) + list(decoder.parameters())
    optimizer = optim.Adam(parameters,
                           lr=args.lr,
                           betas=(args.beta1, args.beta2))

    n_parameters = sum([p.data.nelement() for p in tnet.parameters()])
    print('  + Number of params in tnet: {}'.format(n_parameters))

    #####
    #####

    ######
    if args.test:
        test_acc = test(test_loader, tnet, decoder, descriptor, criterion, 1)
        sys.exit()

    for epoch in range(args.start_epoch, args.epochs + 1):
        # update learning rate
        adjust_learning_rate(optimizer, epoch)
        # train for one epoch
        train(train_loader, tnet, decoder, descriptor, criterion, optimizer,
              epoch)
        # evaluate on validation set
        acc = test(val_loader, tnet, decoder, descriptor, criterion, epoch)

        # remember best acc and save checkpoint
        is_best = acc > best_acc
        best_acc = max(acc, best_acc)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'state_dict': tnet.state_dict(),
                'best_prec1': best_acc,
                'train_loss_metric': train_loss_metric,
                'train_loss_VAE': train_loss_VAE,
                'train_acc_metric': train_acc_metric,
                'test_loss_metric': test_loss_metric,
                'test_loss_VAE': test_loss_VAE,
                'test_acc_metric': test_acc_metric,
            }, is_best)
Ejemplo n.º 10
0
def main():
    global args, best_acc
    args = parser.parse_args()
    args.cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)

    kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}
    train_loader = torch.utils.data.DataLoader(TripletImageLoader(
        base_path='',
        filenames_filename="C:/Users/DELL/ds_info.txt",
        triplets_file_name=
        "C:/Users/DELL/triplet-network-pytorch-master/ds_distant.txt",
        transform=transforms.Compose([transforms.ToTensor()])),
                                               batch_size=1,
                                               shuffle=True,
                                               **kwargs)
    test_loader = torch.utils.data.DataLoader(TripletImageLoader(
        base_path='',
        filenames_filename="C:/Users/DELL/ds_info/ds_info(10, 14)_62.txt",
        triplets_file_name=
        "C:/Users/DELL/triplet-network-pytorch-master/ds_distant.txt",
        transform=transforms.Compose([transforms.ToTensor()])),
                                              batch_size=1,
                                              shuffle=True,
                                              **kwargs)

    class Net(nn.Module):
        def __init__(self):
            super(Net, self).__init__()
            self.conv1 = nn.Conv2d(3, 10, kernel_size=5)
            self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
            self.conv2_drop = nn.Dropout2d()
            self.fc1 = nn.Linear(10, 50)
            self.fc2 = nn.Linear(50, 10)

        def forward(self, x):
            x = F.relu(F.max_pool2d(self.conv1(x), 2))
            x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
            x = x.view(50, x.size(0) * 10)
            x = F.relu(self.fc1(x))
            x = F.dropout(x, training=self.training)
            return self.fc2(x)

    model = Net()
    tnet = Tripletnet(model)
    if args.cuda:
        tnet.cuda()

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            tnet.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    criterion = torch.nn.MarginRankingLoss(margin=args.margin)
    optimizer = optim.SGD(tnet.parameters(),
                          lr=args.lr,
                          momentum=args.momentum)

    n_parameters = sum([p.data.nelement() for p in tnet.parameters()])
    print('  + Number of params: {}'.format(n_parameters))

    for epoch in range(1, args.epochs + 1):
        # train for one epoch
        train(train_loader, tnet, criterion, optimizer, epoch)
        # evaluate on validation set
        acc = test(test_loader, tnet, criterion, epoch)
        # remember best acc and save checkpoint
        is_best = acc > best_acc
        best_acc = max(acc, best_acc)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'state_dict': tnet.state_dict(),
                'best_prec1': 100,
            }, is_best)

    test(test_loader, tnet, criterion, epoch)
Ejemplo n.º 11
0
def main():
    global args, best_acc
    global log_interval
    log_interval = 30
    args = parser.parse_args()
    print(args)
    nz = int(args.dim_embed)
    nef = int(args.nef)
    ndf = int(args.ndf)
    ngpu = int(args.ngpu)
    nc = int(args.nc)
    out_size = args.image_size // 16

    # Data Loaders
    args.cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)
    normalize = transforms.Normalize(mean=[0.0, 0.0, 0.0], std=[1, 1, 1])
    out_size = args.image_size // 16
    kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}
    train_loader = torch.utils.data.DataLoader(
        TripletImageLoader('data/FID-300/', [0, 250],
                           transform=transforms.Compose([
                               transforms.Resize((224, 112)),
                               transforms.CenterCrop((224, 112)),
                               transforms.ToTensor(),
                               normalize,
                           ])),
        batch_size=args.train_batch_size,
        shuffle=True,
        **kwargs)

    # Encoder Network
    encoder = _Encoder(ngpu, nc, nef, out_size, nz)
    encoder.apply(weights_init)
    if args.cuda:
        encoder = encoder.cuda()
    tnet = Tripletnet(encoder)
    if args.cuda:
        tnet.cuda()

    # Decoder
    decoder = _Decoder(ngpu, nc, ndf, out_size, nz)
    decoder.apply(weights_init)

    # Global Storage
    global train_loss_metric, train_loss_VAE, train_acc_metric, test_loss_metric, test_loss_VAE, test_acc_metric
    train_loss_metric = []
    train_loss_VAE = []
    train_acc_metric = []
    test_loss_metric = []
    test_loss_VAE = []
    test_acc_metric = []

    # Loss Functions and Params
    criterion = torch.nn.MarginRankingLoss(margin=args.margin)
    parameters = list(tnet.parameters()) + list(decoder.parameters())
    optimizer = optim.Adam(parameters,
                           lr=args.lr,
                           betas=(args.beta1, args.beta2))

    # Half points
    half_points = [50, 100, 150]

    for epoch in range(args.start_epoch, args.epochs + 1):
        # Half the LR based on above interval
        if epoch in half_points:
            half_lr(optimizer)
            # Print
            print(epoch)
            print("LR: " + str(optimizer.param_groups[0]['lr']))

        # Train
        train(train_loader, tnet, decoder, criterion, optimizer, epoch)

    print("OK")
Ejemplo n.º 12
0
def main():
    global args, best_acc
    args = parser.parse_args()
    args.cuda = not args.no_cuda and torch.cuda.is_available()
    torch.manual_seed(args.seed)
    if args.cuda:
        torch.cuda.manual_seed(args.seed)
    # global plotter
    # plotter = VisdomLinePlotter(env_name=args.name)

    kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}

    root_dir = "../caltech-data/"
    triplet_data_dir = os.path.join(root_dir, "triplet_data")
    train_triplet_path_file = os.path.join(triplet_data_dir,
                                           "triplet_paths_train.txt")
    train_triplet_idx_file = os.path.join(triplet_data_dir,
                                          "triplet_index_train.txt")
    val_triplet_path_file = os.path.join(triplet_data_dir,
                                         "triplet_paths_val.txt")
    val_triplet_idx_file = os.path.join(triplet_data_dir,
                                        "triplet_index_val.txt")

    train_loader = torch.utils.data.DataLoader(TripletImageLoader(
        filenames_filename=train_triplet_path_file,
        triplets_file_name=train_triplet_idx_file,
        transform=transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               **kwargs)

    test_loader = torch.utils.data.DataLoader(TripletImageLoader(
        filenames_filename=val_triplet_path_file,
        triplets_file_name=val_triplet_idx_file,
        transform=transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                              batch_size=args.batch_size,
                                              shuffle=True,
                                              **kwargs)

    embedingnet = Vgg_Net()
    tnet = Tripletnet(embedingnet)
    print(tnet)
    if args.cuda:
        tnet.cuda()

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            tnet.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})".format(
                args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    cudnn.benchmark = True

    criterion = torch.nn.MarginRankingLoss(margin=args.margin)
    optimizer = optim.SGD(tnet.parameters(),
                          lr=args.lr,
                          momentum=args.momentum)

    n_parameters = sum([p.data.nelement() for p in tnet.parameters()])
    print('  + Number of params: {}'.format(n_parameters))

    for epoch in range(1, args.epochs + 1):
        # train for one epoch
        train(train_loader, tnet, criterion, optimizer, epoch)
        # evaluate on validation set
        acc = test(test_loader, tnet, criterion, epoch)

        # remember best acc and save checkpoint
        is_best = acc > best_acc
        best_acc = max(acc, best_acc)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'state_dict': tnet.state_dict(),
                'best_prec1': best_acc,
            }, is_best)