Пример #1
0
classification_optimizer = optim.Adam(classifier.parameters(), lr=opts.lr, betas=(opts.beta1, 0.999), weight_decay=1e-5)
max_acc = 0
for epoch in range(opts.n_epochs):
  for idx in range(opts.batches_per_epoch):
    inputs, labels = sample_gen_batch(int(opts.batch_size/opts.nb_of_classes))
    orig_classes = classifier(inputs.cuda())
    classification_loss = classification_criterion(orig_classes, labels.long().cuda())
    classification_loss.backward()
    classification_optimizer.step()
    classification_optimizer.zero_grad()
    
    if idx%10==0:
      print('epoch [{}/{}], classification loss: {:.4f}'.format(epoch, opts.n_epochs,  classification_loss.item()))
    
    
  test_acc = sup_functions.test_classifier(classifier, test_loader)
  
  if test_acc > max_acc
    max_acc = test_acc
  print("Accuracy on the testset: " + str(test_acc) + "; Best accuracy: " + str(max_acc))

#  print("Testing the quality of generated samples:")
#  test_epoch = 100
#  cm = [[0 for i in range(opts.nb_of_classes)] for j in range(opts.nb_of_classes)] 
#  for idx in range(test_epoch):
#    gen_samples = sample_image(n_row=10)
#    res = classifier(gen_samples[0]).max(1)[1]
#    cm += confusion_matrix(gen_samples[1], res)
#  print("Average accuracy: " + str(cm.diagonal().mean()*10/test_epoch))
#  print('Confusion matrix')
#  print(cm*10/test_epoch)
Пример #2
0
                                       pretrained_on_classes,
                                       gen_model.encoder)

print('Historical data successfully loaded from the datasets')

max_accuracy = 0
print('Testing already acquired knowledge prior to stream training')

known_classes = [int(a) for a in codes_storage.dbuffer.keys()]
indices_test = sup_functions.get_indices_for_classes(full_original_testset,
                                                     known_classes)
test_loader = DataLoader(full_original_testset,
                         batch_size=1000,
                         sampler=SubsetRandomSampler(indices_test))

acc_real = sup_functions.test_classifier(classifier, test_loader)
acc_fake = sup_functions.test_classifier_on_generator(classifier, gen_model,
                                                      test_loader)

results = {}
results['accuracies'] = []
results['known_classes'] = []
print('Real test accuracy on known classes prior to stream training: {:.8f}'.
      format(acc_real))
print(
    'Reconstructed test accuracy on known classes prior to stream training: {:.8f}'
    .format(acc_fake))
results['accuracies'].append(acc_real)
results['known_classes'].append(len(known_classes))

# --------------------------------------------------- STREAM TRAINING ----------------------------------------------------------
classifier = sup_functions.init_classifier(opts)
#classifier.eval()
gen_model = sup_functions.init_generative_model(opts)

criterion_AE = nn.MSELoss()
criterion_classif = nn.MSELoss()
optimizer_gen = torch.optim.Adam(gen_model.parameters(), lr=opts.lr*opts.betta2, betas=(0.9, 0.999), weight_decay=1e-5)
optimizer_classif = torch.optim.Adam(gen_model.parameters(), lr=opts.lr*opts.betta1, betas=(0.9, 0.999), weight_decay=1e-5)

if opts.cuda:
  gen_model = gen_model.cuda()
  criterion_AE = criterion_AE.cuda()
  criterion_classif = criterion_classif.cuda()


print('Classification accuracy on the original testset: ' + str(sup_functions.test_classifier(classifier, test_loader)))
max_test_acc = 0
accuracies = []
#TODO add score computation as in the paper
for epoch in range(opts.niter):  # loop over the dataset multiple times
  opts.epoch = epoch
  print('Training epoch ' + str(epoch))
#  bar = Bar('Training: ', max=int(opts['nb_classes']*opts['samples_per_class_train']/opts['batch_size']))
  for idx, (train_X, train_Y) in enumerate(train_loader):
    inputs = train_X.float()
    labels = train_Y
    if opts.cuda:
      inputs = inputs.cuda()
      labels = labels.cuda()
    #if opts.cuda:
      #inputs = inputs.cuda()
Пример #4
0
criterion = nn.CrossEntropyLoss()
if opts.cuda: criterion = criterion.cuda()
optimizer = optim.SGD(classifier.parameters(), lr=opts.lr, momentum=0.99)
if opts.optimizer == 'Adam':
    optimizer = optim.Adam(classifier.parameters(),
                           lr=opts.lr,
                           betas=(opts.beta1, 0.999),
                           weight_decay=1e-5)

max_test_acc = 0
accuracies = torch.FloatTensor(opts.niter)
for epoch in range(opts.niter):  # loop over the dataset multiple times
    print('Training epoch ' + str(epoch))
    sup_functions.train_classifier(classifier, train_loader, optimizer,
                                   criterion)
    accuracies[epoch] = sup_functions.test_classifier(classifier, test_loader)

    if accuracies[epoch] > max_test_acc:
        max_test_acc = accuracies[epoch]
        best_classifier = classifier
        torch.save(
            best_classifier.state_dict(),
            opts.root + 'pretrained_models/batch_classifier_' +
            opts.experiment_name + '.pth')

    print('Test accuracy: ' + str(accuracies[epoch]))

    torch.save(
        accuracies, opts.root + 'results/batch_classification_accuracy_' +
        opts.experiment_name + '.pth')
print('Finished Training')