Пример #1
0
def test(epoch):
    global best_acc, current_best_epoch
    #pdb.set_trace()
    net.eval()
    with torch.no_grad():
        all_embeddings = np.zeros((len(test_loader.dataset), 84))
        all_targets = np.zeros(len(test_loader.dataset))
        for batch_idx, (inputs, targets) in enumerate(test_loader):
            inputs, targets = inputs.to(device), targets.to(device)
            _, embeddings = net(inputs)
            all_embeddings[batch_idx *
                           args.batch_size:batch_idx * args.batch_size +
                           embeddings.shape[0], :] = embeddings.cpu().numpy()
            all_targets[batch_idx *
                        args.batch_size:batch_idx * args.batch_size +
                        targets.shape[0]] = targets.cpu().numpy()
            progress_bar(batch_idx, len(test_loader))

    # Save checkpoint.
    acc = KNNEvaluation(all_embeddings, all_targets)
    #pdb.set_trace()
    if acc > best_acc:
        print('Saving..')
        state = {
            'net': net.state_dict(),
            'acc': acc,
            'epoch': epoch,
        }
        if not os.path.isdir('checkpoint'):
            os.mkdir('checkpoint')
        torch.save(state, './checkpoint/lifted_ckpt.t7')
        best_acc = acc
        current_best_epoch = epoch
    print('1NN accuracy on test set:%.3f | current best epoch: %d' %
          (acc, current_best_epoch))
def train(epoch):
    print('\nEpoch: %d' % epoch)
    net.train()
    train_loss = 0
    for batch_idx, (inputs, targets) in enumerate(train_loader):
        inputs, targets = inputs.to(device), targets.to(device)
        optimizer.zero_grad()
        _,embeddings = net(inputs)
        loss = criterion(embeddings, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()

        progress_bar(batch_idx, len(train_loader), 'Loss: %.3f ' % (1.0*train_loss/(batch_idx+1)))
Пример #3
0
def train(epoch):
    print('\nEpoch: %d' % epoch)
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        inputs, targets = inputs.to(device), targets.to(device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()
        _, predicted = outputs.max(1)
        total += targets.size(0)
        correct += predicted.eq(targets).sum().item()

        progress_bar(
            batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
            (train_loss /
             (batch_idx + 1), 100. * correct / total, correct, total))
Пример #4
0
def test(epoch):
    global best_acc
    net.eval()
    test_loss = 0
    correct = 0
    total = 0
    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(testloader):
            inputs, targets = inputs.to(device), targets.to(device)
            outputs = net(inputs)
            loss = criterion(outputs, targets)

            test_loss += loss.item()
            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()

            progress_bar(
                batch_idx, len(testloader),
                'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
                (test_loss /
                 (batch_idx + 1), 100. * correct / total, correct, total))

    # Save checkpoint.
    acc = 100. * correct / total
    if acc > best_acc:
        print('Saving..')
        state = {
            'net': net.state_dict(),
            'acc': acc,
            'epoch': epoch,
        }
        if not os.path.isdir('checkpoint'):
            os.mkdir('checkpoint')
        torch.save(state, './checkpoint/classification_ckpt.t7')
        best_acc = acc
Пример #5
0
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    net = ResNet18()
    net = net.to(device)
    if device == 'cuda':
        net = torch.nn.DataParallel(net)
        cudnn.benchmark = True

    # fileList=file_name('checkpoint','.t7')
    fileList = ['checkpoint/Fisher2_ckpt.t7']
    for ckpt_file in fileList:
        print('==> Resuming from checkpoint ' + ckpt_file)
        assert os.path.isdir(
            'checkpoint'), 'Error: no checkpoint directory found!'
        checkpoint = torch.load(ckpt_file)
        net.load_state_dict(checkpoint['net'])
        best_acc = checkpoint['acc']
        net.eval()
        with torch.no_grad():
            all_embeddings = np.zeros((len(test_loader.dataset), 128))
            all_targets = np.zeros(len(test_loader.dataset))
            for batch_idx, (inputs, targets) in enumerate(test_loader):
                inputs, targets = inputs.to(device), targets.to(device)
                _, embeddings = net(inputs)
                all_embeddings[
                    batch_idx * bs:batch_idx * bs +
                    embeddings.shape[0], :] = embeddings.cpu().numpy()
                all_targets[batch_idx * bs:batch_idx * bs +
                            targets.shape[0]] = targets.cpu().numpy()
                progress_bar(batch_idx, len(test_loader))
        evaluate(ckpt_file, all_embeddings, all_targets)