예제 #1
0
def main():
    # Views the training images and displays the distance on anchor-negative and anchor-positive
    test_display_triplet_distance = True

    # print the experiment configuration
    print('\nparsed options:\n{}\n'.format(vars(args)))
    print('\nNumber of Classes:\n{}\n'.format(len(train_dir.classes)))

    # instantiate model and initialize weights
    model = FaceModel(embedding_size=args.embedding_size,
                      num_classes=len(train_dir.classes),
                      pretrained=False)

    model.to(device)
    triplet_loss = TripletMarginLoss(args.margin)
    optimizer = create_optimizer(model, args.lr)

    # 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']
            checkpoint = torch.load(args.resume)
            model.load_state_dict(checkpoint['state_dict'])
        else:
            print('=> no checkpoint found at {}'.format(args.resume))

    start = args.start_epoch
    end = start + args.epochs

    for epoch in range(start, end):
        print(80 * '=')
        print('Epoch [{}/{}]'.format(epoch, end - 1))
        time0 = time.time()
        own_train(train_loader, model, triplet_loss, optimizer, epoch,
                  data_size)
        print(f' Execution time    = {time.time() - time0}')
        print(80 * '=')

        if test_display_triplet_distance:
            display_triplet_distance(model, train_loader,
                                     LOG_DIR + "/train_{}".format(epoch))
    print(80 * '=')
    time0 = time.time()
    own_test(test_loader, model, epoch)
    print(f' Execution time    = {time.time() - time0}')
    print(80 * '=')
    if test_display_triplet_distance:
        display_triplet_distance_test(model, test_loader,
                                      LOG_DIR + "/test_{}".format(epoch))
def main():
    #img = Image.open('./1.jpg')

    #img_array=np.asarray(img)
    #print (img_array.shape)

    # Get cropped and prewhitened image tensor
    #print(img_cropped.shape)
    test_display_triplet_distance = True
    # print the experiment configuration
    print('\nparsed options:\n{}\n'.format(vars(args)))
    print('\nNumber of Classes:\n{}\n'.format(len(train_dir.classes)))
    # instantiate model and initialize weights

    # 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']
        else:
            checkpoint = None
            print('=> no checkpoint found at {}'.format(args.resume))

    model = InceptionResnetV1(fine_turn=1,
                              pretrained=None,
                              num_classes=len(train_dir.classes),
                              device=device)
    state_dict = {}
    cached_file1 = "./20180408-102900-casia-webface-logits.pt"
    cached_file2 = "20180408-102900-casia-webface-features.pt"
    state_dict.update(torch.load(cached_file1))
    state_dict.update(torch.load(cached_file2))
    model.load_state_dict(state_dict)
    if args.cuda:
        model.cuda()

    optimizer = create_optimizer(model, args.lr)

    start = args.start_epoch
    end = start + args.epochs
    test(test_loader, model)
    display_triplet_distance_test(model, test_loader,
                                  LOG_DIR + "/test_{}".format(1))
    """for epoch in range(start, end):
예제 #3
0
def main():
    #test_display_triplet_distance= True
    '''
    why test_display_triplet_distance= True in center loss.py?????
    '''
    test_display_triplet_distance = True
    # print the experiment configuration
    print('\nparsed options:\n{}\n'.format(vars(args)))
    print('\nNumber of Classes:\n{}\n'.format(str(6400)))
    num_classes = 6400
    # 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']
        else:
            checkpoint = None
            print('=> no checkpoint found at {}'.format(args.resume))
    #print(checkpoint)
    # instantiate model and initialize weights
    #model = FaceModelSoftmax(embedding_size=args.embedding_size,num_classes=len(train_dir.classes),checkpoint=checkpoint)
    model = FaceModelSoftmax(embedding_size=args.embedding_size,
                             num_classes=num_classes,
                             checkpoint=checkpoint)
    if args.cuda:
        #print("you are using gpu")
        model.cuda()

    optimizer = create_optimizer(model, args.lr)

    start = args.start_epoch
    end = start + args.epochs
    for epoch in range(start, end):
        train(train_loader, model, optimizer, epoch)
        test(test_loader, model, epoch)
        testaccuracy(testaccuracy_loader, model, epoch)
        testRecall(testaccuracy_loader, model, epoch)
        if test_display_triplet_distance:
            display_triplet_distance_test(model, test_loader,
                                          LOG_DIR + "/test_{}".format(epoch))
            display_triplet_distance(model, train_loader,
                                     LOG_DIR + "/train_{}".format(epoch))
예제 #4
0
def main():
    # Views the training images and displays the distance on anchor-negative and anchor-positive
    test_display_triplet_distance = True

    # print the experiment configuration
    print('\nparsed options:\n{}\n'.format(vars(args)))
    print('\nNumber of Classes:\n{}\n'.format(len(train_dir.classes)))

    # instantiate model and initialize weights
    # embedding_net = EmbeddingNetL2()
    # model = TripletNet(embedding_net)
    model = FaceModel(embedding_size=args.embedding_size,
                      num_classes=len(train_dir.classes),
                      pretrained=False)
    if args.cuda:
        model.cuda()

    optimizer = create_optimizer(model, args.lr)

    # 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']
            checkpoint = torch.load(args.resume)
            model.load_state_dict(checkpoint['state_dict'])
        else:
            print('=> no checkpoint found at {}'.format(args.resume))

    start = args.start_epoch
    end = start + args.epochs

    for epoch in range(start, end):
        train(train_loader, model, optimizer, epoch)
        test(test_loader, model, epoch)

        if test_display_triplet_distance:
            display_triplet_distance(model, train_loader, LOG_DIR + "/train_{}".format(epoch))
            display_triplet_distance_test(model, test_loader, LOG_DIR + "/test_{}".format(epoch))
예제 #5
0
def main_loop():
    test_display_triplet_distance= True
    # print the experiment configuration
    # print('\nparsed options:\n{}\n'.format(vars(args))
    # print('\nNumber of Classes:\n{}\n'.format(len(train_dir.classes)))

    # instantiate model and initialize weights



    # optionally resume from a checkpoint
    checkpoint = None
    if resume:
        if os.path.isfile(resume_path):
            print('=> loading checkpoint {}'.format(resume_path))
            checkpoint = torch.load(resume_path)
            args['start_epoch'] = checkpoint['epoch']
        else:
            checkpoint = None
            print('=> no checkpoint found at {}'.format(resume_path))

    model = FaceModelCenter(embedding_size=embedding_size,num_classes=len(train_dir.classes)
                            ,checkpoint=checkpoint)

    if cuda:
        model.cuda()

    optimizer = create_optimizer(model, lr)

    start = start_epoch
    end = start + int(epochs)
    print(model)
    for epoch in range(start, end):
        train(train_loader, model, optimizer, epoch)
        test(test_loader, model, epoch)
        if test_display_triplet_distance:
            display_triplet_distance_test(model,test_loader,LOG_DIR+"/test_{}".format(epoch))
예제 #6
0
    model = FaceModel(embedding_size=args.embedding_size,num_classes=len(train_dir.classes))

    if args.cuda:
        model.cuda()

    optimizer = create_optimizer(model, args.lr)

    start = args.start_epoch
    end = start + args.epochs

    for epoch in range(start, end):
        train(train_loader, model, optimizer, epoch)
        test(test_loader, model, epoch)
        testaccuracy(testaccuracy_loader, model, epoch)
        if test_display_triplet_distance:
            display_triplet_distance_test(model,test_loader,LOG_DIR+"/test_{}".format(epoch))




def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    for k in topk: