Example #1
0
def load_data_iterator(config):
    tasks = ['train', 'test', 'valid']
    data_iterator = {}
    for task in tasks:
        data_iterator[task] = create_data_loader(config,
                                                 dtype=task,
                                                 shuffle=True)
    print('Data iterators have been created')
    return data_iterator
Example #2
0
            #print("accuracy : {}".format(accuracy))

        print(f"Got {num_correct}/{num_samples} with accuracy {float(num_correct)/float(num_samples)*100:.2f}")
    model_net.train()


if __name__ == "__main__":
    path = "/media/anand/polyglot/BraindataPY"
    label_dict = {"no":1,"yes":0}
    train_dirlist = dataloader.train_or_testset("train/")
    test_dirlist = dataloader.train_or_testset("test/")
    #extracting path,images and labels from training data
    label_train = []      ###declare a list for tracking target labels for training datas
    images_train = []      ####assigning a list and storing image arrays of training images
    image_paths_train = [] ###This is an optional step to keep a track on the train image datas path.
    dataloader.create_data_loader(train_dirlist,label_train,images_train,image_paths_train) # dirlist[:-1] includes no and yes directories
    print("train_set some samples")
    print(label_train[2])
    print(images_train[2])
    print(image_paths_train[2])
    for i in images_train:
        print(i)
    #extracting path,images and labels from testing data
    label_test = []      ###declare a list for tracking target labels for testing datas
    images_test = []      ####assigning a list and storing image arrays of testing images
    image_paths_test = [] ###This is an optional step to keep a track on the test image datas path
    dataloader.create_data_loader(test_dirlist,label_test,images_test,image_paths_test)
    print("test_set some samples")
    print(label_test[2])
    print(images_test[2])
    print(image_paths_test[2])
Example #3
0
def train_model(dataset=dataset, save_dir=save_dir, load_dir = load_dir, num_classes=num_classes, lr=lr,
                num_epochs=nEpochs, increment = increment, save_epoch=snapshot, useTest=useTest, test_interval=nTestInterval):
    """
        Args:
            num_classes (int): Number of classes in the data
            num_epochs (int, optional): Number of epochs to train for.
    """


    classifier = Classifier(num_classes = num_classes)
    generator = Modified_Generator(semantic_dim, noise_dim)
    discriminator = Discriminator(input_dim=input_dim)


    if args.resume_epoch is not None and args.train == 1:
        checkpoint = torch.load(os.path.join(load_dir, saveName + '_increment' '_epoch-' + str(args.resume_epoch - 1) + '.pth.tar'),
                       map_location=lambda storage, loc: storage)   # Load all tensors onto the CPU
        print("Initializing weights from: {}...".format(
            os.path.join(load_dir, 'models', saveName + '_epoch-' + str(resume_epoch - 1) + '.pth.tar')))
        classifier.load_state_dict(checkpoint['classifier_state_dict'])
        generator.load_state_dict(checkpoint['generator_state_dict'])
        discriminator.load_state_dict(checkpoint['discriminator_state_dict'])
        
    elif args.resume_epoch is not None and args.train == 0:
        checkpoint = torch.load(os.path.join(load_dir, saveName + '_increment' + '_epoch-' + str(args.resume_epoch - 1) + '.pth.tar'),
                       map_location=lambda storage, loc: storage)   # Load all tensors onto the CPU
        print("Initializing weights from: {}...".format(
            os.path.join(save_dir, 'models', saveName + '_epoch-' + str(resume_epoch - 1) + '.pth.tar')))
        
        classifier.load_state_dict(checkpoint['classifier_state_dict'])
        generator.load_state_dict(checkpoint['generator_state_dict'])
        discriminator.load_state_dict(checkpoint['discriminator_state_dict'])

    else:
        print("Training {} from scratch...".format(modelName))

    log_dir = os.path.join(save_dir)
    writer = SummaryWriter(log_dir=log_dir)

    iters = (total_classes - num_classes)/(increment_classes)

    for i in range(int(iters)):

        print('Training model on {} dataset...'.format(dataset))
       
        if (i != 0):
            num_classes = num_classes + increment_classes
            checkpoint = torch.load(os.path.join(save_dir, saveName + '_increment_' + str(i-1) + '_epoch-' + 'best' + '.pth.tar'),
                           map_location=lambda storage, loc: storage)   # Load all tensors onto the CPU
            print("Initializing weights from: {}...".format(
                os.path.join(save_dir, saveName + '_increment_' + str(i-1) + '_epoch-' + 'best' + '.pth.tar')))
            classifier.load_state_dict(checkpoint['classifier_state_dict'])
            generator.load_state_dict(checkpoint['generator_state_dict'])
            discriminator.load_state_dict(checkpoint['discriminator_state_dict'])


        #model1 = deepcopy(model)
        classifier1 = deepcopy(classifier)
        generator1 = deepcopy(generator)
        discriminator1 = deepcopy(discriminator)
        classifier1, generator1, discriminator1 = classifier1.cuda(), generator1.cuda(), discriminator1.cuda()
        print('Copied previous model')

        in_features = classifier.classifier_out.in_features
        weights = classifier.classifier_out.weight.data
        print('new out features: ', num_classes + increment_classes)
        classifier.classifier_out = nn.Linear(in_features, num_classes + increment_classes, bias = False)
        kaiming_normal_init(classifier.classifier_out.weight)
        classifier.classifier_out.weight.data[:num_classes] = weights
            
        print('Updated Classifier With Number Of Classes %d' % (num_classes + increment_classes))
            
        train_dataloader, test_dataloader, len_train, len_test = create_data_loader(feat_path, all_classes[num_classes:increment_classes+num_classes])

        print('Classes used in the new dataset: %d to %d' % (num_classes, num_classes+increment_classes))
        
        old_train_dataloader, old_test_dataloader, old_len_train, old_len_test = create_data_loader(feat_path, all_classes[:num_classes])

        optimizer = torch.optim.Adam(classifier.parameters(), lr=lr[0], betas=(b1,b2))
        optimizer_G = torch.optim.Adam(generator.parameters(), lr=lr[0], betas=(b1, b2))
        optimizer_D = torch.optim.Adam(discriminator.parameters(), lr=lr[0], betas=(b1, b2))

        print('Classes used in the old dataset: 0 to %d' % (num_classes))

        feats = sio.loadmat(att_path)
        att = feats['att']
        att = np.transpose(att, (1,0))
        att = torch.tensor(att).cuda()  

        adversarial_loss = torch.nn.BCELoss().to(device)    

        if cuda:
            classifier = classifier.to(device)
            classifier1 = classifier1.to(device)
            generator = generator.to(device)
            generator1 = generator1.to(device)
            discriminator = discriminator.to(device)
            discriminator1 = discriminator.to(device)

        num_epochs = num_epochs + increment
        num_lr_stages = num_epochs/len(lr)

        if args.train == 1:
            
            for epoch in range(num_epochs):
                start_time = timeit.default_timer()

                running_old_corrects = 0.0
                running_new_corrects = 0.0

                classifier.train()

                for (inputs, labels) in train_dataloader:
                    optimizer.zero_grad()

                    feats = Variable(inputs.to(device), requires_grad=True).float()
                    labels = Variable(labels.to(device), requires_grad=False).long()              
 
                    loop_batch_size = len(inputs)

############### Begin Incremental Training Of Conv-LSTM Model ############################
                    old_labels = Variable(LongTensor(np.random.randint(0, num_classes, loop_batch_size))).long().cuda()
                    noise = Variable(FloatTensor(np.random.normal(0, 1, (loop_batch_size, noise_dim)))).cuda()

                    old_semantic = att[old_labels]
                   
                    old_features = generator1(old_semantic.float(), noise)
                    new_features = feats
                    new_logits = classifier(new_features)
                    old_logits = classifier(old_features)

                    new_cls_loss = nn.CrossEntropyLoss()(new_logits, labels)
                    old_cls_loss = nn.CrossEntropyLoss()(old_logits, old_labels)

                    #loss = 10*nn.CrossEntropyLoss()(new_logits, labels) + nn.CrossEntropyLoss()(old_logits, old_labels) + nn.CrossEntropyLoss()(dataset_logits, dataset_labels)  + 0.25*CustomKLDiv(new_logits[:,:num_classes], expected_logits, 0.5)
                    #loss = dataset_cls_loss + 100*new_cls_loss + 7.5*CustomKLDiv(new_logits[:,:num_classes], expected_logits, 0.5)
                    loss = 100*new_cls_loss + 10*old_cls_loss        #Used for UCF101
                    #loss = 100*(new_cls_loss + old_cls_loss)

                    loss.backward()
                    optimizer.step()                    
    
                    _, old_predictions = torch.max(torch.softmax(old_logits, dim = 1), dim = 1, keepdim = False)
                    _, new_predictions = torch.max(torch.softmax(new_logits, dim = 1), dim = 1, keepdim = False) 
                    running_old_corrects += torch.sum(old_predictions == old_labels.data) 
                    running_new_corrects += torch.sum(new_predictions == labels.data)

                old_epoch_acc = running_old_corrects.item() / len_train
                new_epoch_acc = running_new_corrects.item() / len_train

                words = 'data/old_train_acc_epoch' + str(i)
                writer.add_scalar(words, old_epoch_acc, epoch)
                words = 'data/new_train_acc_epoch' + str(i)
                writer.add_scalar(words, new_epoch_acc, epoch)

                print("Set: {} Epoch: {}/{} Train Old Acc: {} Train New Acc: {}".format(i, epoch+1, num_epochs, old_epoch_acc, new_epoch_acc))
                #print("Set: {} Epoch: {}/{} Train New Acc: {}".format(i, epoch+1, num_epochs, new_epoch_acc))

                if useTest and epoch % test_interval == (test_interval - 1):
                    #classifier.eval()
                    
                    running_old_corrects = 0.0
                    running_new_corrects = 0.0
    
                    for (inputs, labels) in test_dataloader:
                        with torch.no_grad():
                            feats = inputs.to(device).float()
                            labels = labels.to(device).long()                
             
                            #print(labels)
                            loop_batch_size = len(feats)
                   
                            probs = classifier(feats)
     
                        _, predictions = torch.max(torch.softmax(probs, dim = 1), dim = 1, keepdim = False)
                        running_new_corrects += torch.sum(predictions == labels.data)

                    new_epoch_acc = running_new_corrects.item() / len_test
                    words = 'data/new_test_acc_epoch' + str(i)
                    writer.add_scalar(words, new_epoch_acc, epoch)


                    for (inputs, labels) in old_test_dataloader:
                        feats = inputs.to(device).float()
                        labels = labels.to(device).long()                

                        loop_batch_size = len(inputs)
      
                        old_logits = classifier(feats)
     
                        _, old_predictions = torch.max(torch.softmax(old_logits, dim = 1), dim = 1, keepdim = False)         
                        running_old_corrects += torch.sum(old_predictions == labels.data)

                    old_epoch_acc = running_old_corrects.item() / old_len_test

                    words = 'data/old_test_acc_epoch' + str(i)
                    writer.add_scalar(words, old_epoch_acc, epoch)

                print("Set: {} Epoch: {}/{} Test Old Acc: {} Test New Acc: {}".format(i, epoch+1, num_epochs, old_epoch_acc, new_epoch_acc))


            best_gan_acc = 0.0

            for epoch in range(num_epochs):
                start_time = timeit.default_timer()

                running_old_corrects = 0.0
                running_new_corrects = 0.0

                classifier.train()
                generator1.train()
                generator.train()
                discriminator1.train()
                discriminator.train()

                for (inputs, labels) in train_dataloader:
                    feats = Variable(inputs.to(device), requires_grad=True).float()
                    labels = Variable(labels.to(device), requires_grad=False).long()                

                    loop_batch_size = len(feats)

                    valid = Variable(FloatTensor(loop_batch_size, 1).fill_(1.0), requires_grad=False).cuda()
                    fake = Variable(FloatTensor(loop_batch_size, 1).fill_(0.0), requires_grad=False).cuda()

                    noise = Variable(FloatTensor(np.random.normal(0, 1, (loop_batch_size, noise_dim)))).cuda()
                    semantic_true = att[labels].cuda()
 
                    optimizer_D.zero_grad()

############## New Dataset training #######################
                    true_features_2048 = feats
                    validity_real = discriminator(true_features_2048.detach()).view(-1)
                    validity_real_expected = discriminator1(true_features_2048.detach()).view(-1)
                    d_real_loss = adversarial_loss(validity_real, valid) + 0.25*CustomKLDiv(validity_real, validity_real_expected, 0.5, dim = 0)  #Used for UCF101
                    #d_real_loss = 5*adversarial_loss(validity_real, valid) + 0.25*CustomKLDiv(validity_real, validity_real_expected, 0.5, dim = 0)
                    d_real_loss.backward(retain_graph = True)

############## All Fake Batch Training #######################
                    gen_imgs = generator(semantic_true.float(), noise)
                    validity_fake = discriminator(gen_imgs.detach()).view(-1)
                    validity_fake_expected = discriminator1(gen_imgs.detach()).view(-1)
                    d_fake_loss = adversarial_loss(validity_fake, fake) + 0.25*CustomKLDiv(validity_fake, validity_fake_expected, 0.5, dim = 0)  #Used for UCF101
                    #d_fake_loss = 5*adversarial_loss(validity_fake, fake) + 0.25*CustomKLDiv(validity_fake, validity_fake_expected, 0.5, dim = 0)
                    d_fake_loss.backward(retain_graph = True)            
                    optimizer_D.step()

############## Generator training ########################
                    optimizer_G.zero_grad()
                    validity = discriminator(gen_imgs).view(-1)
                    new_logits = classifier(gen_imgs)            
                    g_loss = adversarial_loss(validity, valid) + 7.5*CustomKLDiv(gen_imgs, true_features_2048, 0.5) + 0.25*nn.CrossEntropyLoss()(new_logits, labels) + 10*nn.MSELoss()(gen_imgs, true_features)
                    #g_loss = adversarial_loss(validity, valid) + 7.5*CustomKLDiv(gen_imgs, true_features_2048, 0.5) + 0.25*nn.CrossEntropyLoss()(new_logits, labels) #Used for UCF101
                    #g_loss = 5*adversarial_loss(validity, valid) + 7.5*CustomKLDiv(gen_imgs, true_features_2048, 0.5) + 0.25*nn.CrossEntropyLoss()(new_logits, labels)
                    g_loss.backward(retain_graph = True)
                    optimizer_G.step()    


############## Old Dataset Training ###########################

                    old_labels = Variable(LongTensor(np.random.randint(0, num_classes, loop_batch_size))).cuda()
                    noise = Variable(FloatTensor(np.random.normal(0, 1, (loop_batch_size, noise_dim)))).cuda()
                    semantic_true = att[old_labels].cuda()
 
                    optimizer_D.zero_grad()

                    true_features_2048 = generator1(semantic_true.float(), noise)
                    validity_real = discriminator(true_features_2048.detach()).view(-1)
                    d_real_loss = adversarial_loss(validity_real, valid)
                    d_real_loss.backward(retain_graph = True)

############## All Fake Batch Training #######################
                    gen_imgs = generator(semantic_true.float(), noise)
                    validity_fake = discriminator(gen_imgs.detach()).view(-1)
                    d_fake_loss = adversarial_loss(validity_fake, fake)
                    d_fake_loss.backward(retain_graph = True)            
                    optimizer_D.step()

############## Generator training ########################
                    optimizer_G.zero_grad()
                    validity = discriminator(gen_imgs).view(-1)
                    old_logits = classifier(gen_imgs)   
                    g_loss = adversarial_loss(validity, valid) + 7.5*CustomKLDiv(gen_imgs, true_features_2048, 0.5) + 0.25*nn.CrossEntropyLoss()(old_logits, old_labels) + 10*nn.MSELoss()(gen_imgs, true_features_2048)
                    #g_loss = adversarial_loss(validity, valid) + 7.5*CustomKLDiv(gen_imgs, true_features_2048, 0.5) + 0.25*nn.CrossEntropyLoss()(old_logits, old_labels)
                    g_loss.backward(retain_graph = True)
                    optimizer_G.step()   

                    _, old_predictions = torch.max(torch.softmax(old_logits, dim = 1), dim = 1, keepdim = False)
                    _, new_predictions = torch.max(torch.softmax(new_logits, dim = 1), dim = 1, keepdim = False)         
                    running_old_corrects += torch.sum(old_predictions == old_labels.data)
                    running_new_corrects += torch.sum(new_predictions == labels.data)

                old_epoch_acc = running_old_corrects.item() / len_train
                new_epoch_acc = running_new_corrects.item() / len_train

                words = 'data/gen_old_train_acc_epoch' + str(i)
                writer.add_scalar(words, old_epoch_acc, epoch)
                words = 'data/gen_new_train_acc_epoch' + str(i)
                writer.add_scalar(words, new_epoch_acc, epoch)

                print("Set: {} Epoch: {}/{} Train GAN Old Acc: {} Train GAN New Acc: {}".format(i, epoch+1, num_epochs, old_epoch_acc, new_epoch_acc))




                if useTest and epoch % test_interval == (test_interval - 1):
                    classifier.train()
                    generator.train()
                    
                    running_old_corrects = 0.0
                    running_new_corrects = 0.0
    
                    for (inputs, labels) in test_dataloader:
                        labels = Variable(labels.to(device), requires_grad=False).long()                
                        loop_batch_size = len(inputs)
                        noise = Variable(FloatTensor(np.random.normal(0, 1, (loop_batch_size, noise_dim)))).cuda()
                   
                        semantic = att[labels].cuda()
                        new_features = generator(semantic.float(), noise)
                        new_logits = classifier(new_features)
     
                        _, new_predictions = torch.max(torch.softmax(new_logits, dim = 1), dim = 1, keepdim = False)         
                        running_new_corrects += torch.sum(new_predictions == labels.data)

                    new_epoch_acc = running_new_corrects.item() / len_test

                    words = 'data/gen_new_test_acc_epoch' + str(i)
                    writer.add_scalar(words, new_epoch_acc, epoch)


                    for (inputs, labels) in old_test_dataloader:
                        labels = Variable(labels.to(device), requires_grad=False).long()                
                        loop_batch_size = len(inputs)
                        noise = Variable(FloatTensor(np.random.normal(0, 1, (loop_batch_size, noise_dim)))).cuda()
                   
                        semantic = att[labels].cuda()
                        old_features = generator(semantic.float(), noise)
                        old_logits = classifier(old_features)
     
                        _, old_predictions = torch.max(torch.softmax(old_logits, dim = 1), dim = 1, keepdim = False)         
                        running_old_corrects += torch.sum(old_predictions == labels.data)

                    old_epoch_acc = running_old_corrects.item() / old_len_test

                    words = 'data/gen_old_test_acc_epoch' + str(i)
                    writer.add_scalar(words, old_epoch_acc, epoch)

                print("Set: {} Epoch: {}/{} GAN Test Old Acc: {} GAN Test New Acc: {}".format(i, epoch+1, num_epochs, old_epoch_acc, new_epoch_acc))



                if ((best_gan_acc < new_epoch_acc+old_epoch_acc) and (epoch > 150)):
                    save_path = os.path.join(save_dir, saveName + '_increment_' + str(i) + '_epoch-' + 'best' + '.pth.tar')
                    torch.save({
                        'classifier_state_dict': classifier.state_dict(),
                        'generator_state_dict': generator.state_dict(),
                        'discriminator_state_dict': discriminator.state_dict(),
                    }, save_path)
                    best_gan_acc = new_epoch_acc + old_epoch_acc
                    print("Save model at {}\n".format(save_path))



        else:
            for epoch in range(num_epochs):
                start_time = timeit.default_timer()

                running_loss = 0.0
                running_corrects = 0.0

                #classifier.eval()

                for indices, inputs, labels in test_dataloader:
                    inputs = inputs.permute(0,2,1,3,4)
                    image_sequences = Variable(inputs.to(device), requires_grad=True)
                    labels = Variable(labels.to(device), requires_grad=False)                

                    model.lstm.reset_hidden_state()
                    loop_batch_size = len(inputs)

                    true_features_2048 = model(image_sequences)
                    true_features_2048 = true_features_2048.view(true_features_2048.size(0), -1)
                    logits = classifier(true_features_2048)

                    _, predictions = torch.max(torch.softmax(logits, dim = 1), dim = 1, keepdim = False)
                                  
                    running_loss += loss.item() * inputs.size(0)
                    running_corrects += torch.sum(predictions == labels.data)

                epoch_loss = running_loss / len(test_dataloader)
                epoch_acc = running_corrects.item() / len(test_dataloader)

                writer.add_scalar('data/test_acc {}'.format(i), epoch_loss, epoch)
                print("[test] Epoch: {}/{} Testing Acc: {}".format(epoch+1, num_epochs, epoch_acc))


                running_loss = 0.0
                running_corrects = 0.0

                #classifier.eval()

                for indices, inputs, labels in old_dataloader:
                    inputs = inputs.permute(0,2,1,3,4)
                    image_sequences = Variable(inputs.to(device), requires_grad=True)
                    labels = Variable(labels.to(device), requires_grad=False)                

                    model.lstm.reset_hidden_state()
                    loop_batch_size = len(inputs)

                    true_features_2048 = model(image_sequences)
                    true_features_2048 = true_features_2048.view(true_features_2048.size(0), -1)
                    logits = classifier(true_features_2048)

                    _, predictions = torch.max(torch.softmax(logits, dim = 1), dim = 1, keepdim = False)
                                  
                    running_loss += loss.item() * inputs.size(0)
                    running_corrects += torch.sum(predictions == labels.data)

                epoch_loss = running_loss / len(old_dataloader)
                epoch_acc = running_corrects.item() / len(old_dataloader)

                writer.add_scalar('data/old_acc {}'.format(i), epoch_loss, epoch)
                print("[test] Epoch: {}/{} Old Acc: {}".format(epoch+1, num_epochs, epoch_acc))
        
    writer.close()
generator.load_state_dict(checkpoint['generator_state_dict'])
print("Training {} saved model...".format("UCF101"))

print('Total params: %.2fM' %
      (sum(p.numel() for p in classifier.parameters()) / 1000000.0))

if cuda:
    generator = generator.to(device)
    classifier = classifier.to(device)

classifier.eval()
generator.eval()

att_path = 'ucf101_i3d/split_1/att_splits.mat'
feat_path = 'ucf101_i3d/i3d.mat'
train_dataloader, _, len_train, len_test = create_data_loader(
    feat_path, range(num_class))

feats = sio.loadmat(att_path)
att = feats['att']
att = np.transpose(att, (1, 0))
att = torch.tensor(att).cuda()

start_time = timeit.default_timer()

#running_corrects = 0.0

for (inputs, labels) in train_dataloader:
    feats = Variable(inputs, requires_grad=False).float().cuda()
    labels = Variable(labels, requires_grad=False).long().cuda()
    loop_batch_size = len(feats)
    probs = feats
 def test_create_datafolder4test(self):
     create_data_loader(test_dirlist,label_test,images_test,image_paths_test)
     assert len(image_paths_test)== 57
 def test_create_dataloader4train(self):
     create_data_loader(train_dirlist,label_train,images_train,image_paths_train)
     assert len(label_train) == 196
Example #7
0
            batch_id += 1
            step_idx += 1
            scheduler.step()

        train_epoch_cost = time.time() - epoch_start
        ce_time.append(train_epoch_cost)
        logger.info("train epoch: %d, epoch_cost: %.5f s" %
                    (pass_id, train_epoch_cost))

    if args.save_model and rank == 0:
        model_dir = os.path.join(args.save_model, "step_final")
        if not os.path.exists(model_dir):
            os.makedirs(model_dir)
        paddle.save(transformer.state_dict(),
                    os.path.join(model_dir, "transformer.pdparams"))
        paddle.save(optimizer.state_dict(),
                    os.path.join(model_dir, "transformer.pdopt"))


if __name__ == '__main__':
    # 读入参数
    yaml_file = './transformer.base.yaml'
    with open(yaml_file, 'rt') as f:
        args = AttrDict(yaml.safe_load(f))
        pprint(args)

    # Define data loader
    (train_loader), (eval_loader) = create_data_loader(args)

    print('training the model')
    do_train(args, train_loader, eval_loader)
def test_model(dataset=dataset,
               load_dir=load_dir,
               only_classifier=only_classifier):

    if (only_classifier == 0):
        generator = Modified_Generator(semantic_dim, noise_dim)

        if (increment is None):
            checkpoint = torch.load(os.path.join(
                load_dir, saveName + '_increment' + '_epoch-' +
                str(resume_epoch - 1) + '.pth.tar'),
                                    map_location=lambda storage, loc: storage
                                    )  # Load all tensors onto the CPU
            print("Initializing weights from: {}...".format(
                os.path.join(
                    load_dir, 'models', saveName + '_epoch-' +
                    str(resume_epoch - 1) + '.pth.tar')))
            classifier = Classifier(num_classes=num_class, bias=True)

        else:
            checkpoint = torch.load(os.path.join(
                load_dir, saveName + '_increment_' + str(increment) +
                '_epoch-' + 'best' + '.pth.tar'),
                                    map_location=lambda storage, loc: storage
                                    )  # Load all tensors onto the CPU
            print("Initializing weights from: {}...".format(
                os.path.join(load_dir, 'models',
                             saveName + '_epoch-' + 'best' + '.pth.tar')))
            classifier = Classifier(num_classes=num_class, bias=False)

        classifier.load_state_dict(checkpoint['classifier_state_dict'])
        generator.load_state_dict(checkpoint['generator_state_dict'])
        print("Training {} saved model...".format(modelName))

        print('Total params: %.2fM' %
              (sum(p.numel() for p in classifier.parameters()) / 1000000.0))

        print('Training model on {} dataset...'.format(dataset))

        _, test_dataloader, _, len_test = create_data_loader(
            feat_path, test_classes)

        if cuda:
            generator = generator.to(device)
            classifier = classifier.to(device)

        classifier.train()
        generator.train()

        feats = sio.loadmat(att_path)
        att = feats['att']
        att = np.transpose(att, (1, 0))
        att = torch.tensor(att).cuda()

        start_time = timeit.default_timer()

        running_corrects = 0.0

        for (inputs, labels) in test_dataloader:
            feats = Variable(inputs, requires_grad=False).float().cuda()
            labels = Variable(labels, requires_grad=False).long().cuda()
            loop_batch_size = len(feats)
            probs = classifier(feats)
            _, predictions = torch.max(torch.softmax(probs, dim=1),
                                       dim=1,
                                       keepdim=False)

            running_corrects += torch.sum(predictions == labels.data)

        epoch_acc = running_corrects.item() / len_test
        print("[test] Classifier Testing Acc: {}".format(epoch_acc))

        start_time = timeit.default_timer()
        running_corrects = 0.0

        for (inputs, labels) in test_dataloader:
            feats = Variable(inputs, requires_grad=True).float().cuda()
            labels = Variable(labels, requires_grad=False).long().cuda()
            loop_batch_size = len(feats)

            noise = Variable(
                FloatTensor(
                    np.random.normal(0, 1, (loop_batch_size, noise_dim))))
            semantic_true = att[labels]

            with torch.no_grad():
                features_2048 = generator(semantic_true.float(), noise)
                probs = classifier(features_2048)

            _, predictions = torch.max(torch.softmax(probs, dim=1),
                                       dim=1,
                                       keepdim=False)
            running_corrects += torch.sum(predictions == labels.data)

        real_epoch_acc = running_corrects.item() / len_test

        print("[test] Epoch: Test Dataset Generator Acc: {}".format(
            real_epoch_acc))
        stop_time = timeit.default_timer()
        print("Execution time: " + str(stop_time - start_time) + "\n")

    else:
        if (increment is None):
            checkpoint = torch.load(os.path.join(
                load_dir, saveName + '_increment' + '_epoch-' +
                str(resume_epoch - 1) + '.pth.tar'),
                                    map_location=lambda storage, loc: storage
                                    )  # Load all tensors onto the CPU
            print("Initializing weights from: {}...".format(
                os.path.join(
                    load_dir, 'models', saveName + '_epoch-' +
                    str(resume_epoch - 1) + '.pth.tar')))
            classifier = Classifier(num_classes=num_class, bias=False)

        else:
            checkpoint = torch.load(os.path.join(
                load_dir, saveName + '_increment_' + str(increment) +
                '_epoch-' + str(resume_epoch - 1) + '.pth.tar'),
                                    map_location=lambda storage, loc: storage
                                    )  # Load all tensors onto the CPU
            print("Initializing weights from: {}...".format(
                os.path.join(
                    load_dir, 'models', saveName + '_epoch-' +
                    str(resume_epoch - 1) + '.pth.tar')))
            classifier = Classifier(num_classes=num_class, bias=False)

        classifier.load_state_dict(checkpoint['classifier_state_dict'])
        print("Training {} saved model...".format(modelName))

        print('Total params: %.2fM' %
              (sum(p.numel() for p in classifier.parameters()) / 1000000.0))

        print('Training model on {} dataset...'.format(dataset))

        _, test_dataloader, _, len_test = create_data_loader(
            feat_path, test_classes)

        if cuda:
            classifier = classifier.to(device)

        classifier.train()

        feats = sio.loadmat(att_path)
        att = feats['att']
        att = np.transpose(att, (1, 0))
        att = torch.tensor(att).cuda()

        start_time = timeit.default_timer()

        running_corrects = 0.0

        for (inputs, labels) in test_dataloader:
            feats = Variable(inputs, requires_grad=False).float().cuda()
            labels = Variable(labels, requires_grad=False).long().cuda()
            loop_batch_size = len(feats)
            probs = classifier(feats)
            _, predictions = torch.max(torch.softmax(probs, dim=1),
                                       dim=1,
                                       keepdim=False)

            running_corrects += torch.sum(predictions == labels.data)

        epoch_acc = running_corrects.item() / len_test
        print("[test] Classifier Testing Acc: {}".format(epoch_acc))
Example #9
0
att = feats['att']
att = np.transpose(att, (1, 0))
att = torch.tensor(att).cuda()

classes = 0

for i in range(increments):

    if (i == 0):
        if (not os.path.exists("gen_features")):
            os.mkdir("gen_features")
        if (not os.path.exists(f"gen_features/{args.save_name}")):
            os.mkdir(f"gen_features/{args.save_name}")

    if split == 'train':
        dataloader, _, _, _ = create_data_loader(
            feat_path, all_classes[classes:classes + args.increment_class])

    elif split == 'test':
        _, dataloader, _, _ = create_data_loader(
            feat_path, all_classes[classes:classes + args.increment_class])

    else:
        dataloader, _, _, _ = create_data_loader(
            feat_path,
            all_classes[classes:classes + args.increment_class],
            percent=0.00001)

    for j, (inputs, labels) in enumerate(dataloader):
        batch_size = inputs.size(0)
        labels = Variable(labels, requires_grad=False).long().cuda()
        noise = Variable(
def train_model(dataset=dataset,
                save_dir=save_dir,
                load_dir=load_dir,
                num_classes=total_classes,
                lr=lr,
                num_epochs=nEpochs,
                save_epoch=snapshot,
                useTest=useTest,
                test_interval=nTestInterval):
    """
        Args:
            num_classes (int): Number of classes in the data
            num_epochs (int, optional): Number of epochs to train for.
    """

    generator = Modified_Generator(semantic_dim, noise_dim)
    discriminator = Discriminator(input_dim=input_dim)
    classifier = Classifier(num_classes=num_classes)

    if args.resume_epoch is not None:
        checkpoint = torch.load(os.path.join(
            load_dir, saveName + '_increment'
            '_epoch-' + str(args.resume_epoch - 1) + '.pth.tar'),
                                map_location=lambda storage, loc: storage
                                )  # Load all tensors onto the CPU
        print("Initializing weights from: {}...".format(
            os.path.join(
                save_dir, 'models',
                saveName + '_epoch-' + str(resume_epoch - 1) + '.pth.tar')))
        classifier.load_state_dict(checkpoint['classifier_state_dict'])
        generator.load_state_dict(checkpoint['generator_state_dict'])
        discriminator.load_state_dict(checkpoint['discriminator_state_dict'])

        print("Training {} saved model...".format(modelName))

    else:
        print("Training {} from scratch...".format(modelName))

    print('Total params: %.2fM' %
          (sum(p.numel() for p in classifier.parameters()) / 1000000.0))

    log_dir = os.path.join(save_dir)
    writer = SummaryWriter(log_dir=log_dir)

    print('Training model on {} dataset...'.format(dataset))

    train_dataloader, test_dataloader, len_train, len_test = create_data_loader(
        feat_path, all_classes)

    trainval_loaders = {'train': train_dataloader, 'test': test_dataloader}

    if cuda:
        generator = generator.to(device)
        discriminator = discriminator.to(device)
        classifier = classifier.to(device)

    optimizer_G = torch.optim.Adam(generator.parameters(),
                                   lr=lr,
                                   betas=(b1, b2))
    optimizer_D = torch.optim.Adam(discriminator.parameters(),
                                   lr=lr,
                                   betas=(b1, b2))
    optimizer = torch.optim.Adam(list(classifier.parameters()), lr=lr)

    adversarial_loss = torch.nn.BCELoss().to(device)

    feats = sio.loadmat(att_path)
    att = feats['att']
    att = np.transpose(att, (1, 0))
    att = torch.tensor(att).cuda()

    if args.train == 1:
        if args.only_gan == 0:
            for epoch in range(num_epochs):
                start_time = timeit.default_timer()

                running_loss = 0.0
                running_corrects = 0.0

                classifier.train()

                for (inputs, labels) in (trainval_loaders["train"]):
                    feats = Variable(inputs, requires_grad=True).float().cuda()
                    labels = Variable(labels,
                                      requires_grad=False).long().cuda()

                    optimizer.zero_grad()

                    #model.lstm.reset_hidden_state()
                    loop_batch_size = len(feats)

                    probs = classifier(feats)
                    _, predictions = torch.max(torch.softmax(probs, dim=1),
                                               dim=1,
                                               keepdim=False)

                    loss = nn.CrossEntropyLoss()(probs, labels)
                    loss.backward()
                    optimizer.step()

                    running_loss += loss.item() * feats.size(0)
                    running_corrects += torch.sum(predictions == labels.data)

                epoch_loss = running_loss / len_train
                epoch_acc = running_corrects.item() / len_train

                writer.add_scalar('data/train_acc_epoch_benchmark', epoch_acc,
                                  epoch)

                print(
                    "[train] Epoch: {}/{} Training Acc: {} Training Loss: {}".
                    format(epoch + 1, num_epochs, epoch_acc, epoch_loss))

                if useTest and epoch % test_interval == (test_interval - 1):
                    classifier.train()
                    start_time = timeit.default_timer()

                    running_corrects = 0.0

                    for (inputs, labels) in (trainval_loaders["test"]):
                        feats = Variable(inputs,
                                         requires_grad=True).float().cuda()
                        labels = Variable(labels,
                                          requires_grad=False).long().cuda()
                        loop_batch_size = len(feats)

                        with torch.no_grad():
                            probs = classifier(feats)

                        _, predictions = torch.max(torch.softmax(probs, dim=1),
                                                   dim=1,
                                                   keepdim=False)
                        running_corrects += torch.sum(
                            predictions == labels.data)

                    #print(len_test)

                    real_epoch_acc = running_corrects.item() / len_test

                    writer.add_scalar('data/test_acc_epoch_benchmark',
                                      real_epoch_acc, epoch)

                    print("[test] Epoch: {}/{} Test Dataset Acc: {}".format(
                        epoch + 1, num_epochs, real_epoch_acc))
                    stop_time = timeit.default_timer()
                    print("Execution time: " + str(stop_time - start_time) +
                          "\n")

                if (epoch % save_epoch == save_epoch - 1):
                    save_path = os.path.join(
                        save_dir, saveName + '_increment'
                        '_epoch-' + str(epoch) + '.pth.tar')
                    torch.save(
                        {
                            'generator_state_dict': generator.state_dict(),
                            'discriminator_state_dict':
                            discriminator.state_dict(),
                            'classifier_state_dict': classifier.state_dict(),
                        }, save_path)
                    print("Save model at {}\n".format(save_path))

        for epoch in range(num_epochs):
            start_time = timeit.default_timer()

            running_d_loss = 0.0
            running_g_loss = 0.0
            gen_running_corrects = 0.0

            generator.train()
            discriminator.train()
            classifier.train()

            for (inputs, labels) in (trainval_loaders["train"]):
                feats = Variable(inputs, requires_grad=True).float().cuda()
                labels = Variable(labels, requires_grad=False).long().cuda()

                loop_batch_size = len(feats)

                valid = Variable(FloatTensor(loop_batch_size, 1).fill_(1.0),
                                 requires_grad=False)
                fake = Variable(FloatTensor(loop_batch_size, 1).fill_(0.0),
                                requires_grad=False)

                noise = Variable(
                    FloatTensor(
                        np.random.normal(0, 1, (loop_batch_size, noise_dim))))
                semantic_true = att[labels]

                optimizer_D.zero_grad()

                ############## All real batch training #######################
                true_features_2048 = feats
                validity_real = discriminator(true_features_2048).view(-1)
                d_real_loss = adversarial_loss(validity_real, valid)
                d_real_loss.backward(retain_graph=True)

                ############## All Fake Batch Training #######################
                gen_imgs = generator(semantic_true.float(), noise)
                validity_fake = discriminator(gen_imgs.detach()).view(-1)
                d_fake_loss = adversarial_loss(validity_fake, fake)
                d_fake_loss.backward(retain_graph=True)
                optimizer_D.step()

                ############## Generator training ########################
                optimizer_G.zero_grad()
                validity = discriminator(gen_imgs).view(-1)
                generated_preds = classifier(gen_imgs)

                gen_adv = adversarial_loss(validity, valid)
                KL_loss = CustomKLDiv(gen_imgs, true_features_2048, 0.5)
                #l1_loss = nn.L1Loss()(gen_imgs, true_features_2048)
                mse_loss = nn.MSELoss()(gen_imgs, true_features_2048)
                cls_loss = nn.CrossEntropyLoss()(generated_preds, labels)

                g_loss = gen_adv + 7.5 * KL_loss + 0.25 * cls_loss + 200 * mse_loss
                #g_loss = gen_adv + 7.5*KL_loss + 0.25*cls_loss + 1000*l1_loss
                #g_loss = gen_adv + 7.5*KL_loss + 0.25*cls_loss
                #g_loss = gen_adv + 100*l1_loss
                #g_loss = gen_adv + 7.5*KL_loss
                g_loss.backward(retain_graph=True)
                optimizer_G.step()

                _, gen_predictions = torch.max(torch.softmax(generated_preds,
                                                             dim=1),
                                               dim=1,
                                               keepdim=False)

                running_d_loss = running_d_loss + (
                    d_real_loss.item() + d_fake_loss.item()) * feats.size(0)
                running_g_loss = running_g_loss + g_loss.item() * feats.size(0)
                gen_running_corrects += torch.sum(
                    gen_predictions == labels.data)

            epoch_d_loss = running_d_loss / len_train
            epoch_g_loss = running_g_loss / len_train
            gen_epoch_acc = gen_running_corrects.item() / len_train

            writer.add_scalar('data/gen_train_acc_epoch_benchmark',
                              gen_epoch_acc, epoch)
            writer.add_scalar('data/d_loss_epoch_benchmark', epoch_d_loss,
                              epoch)
            writer.add_scalar('data/g_loss_epoch_benchmark', epoch_g_loss,
                              epoch)

            print(
                "[train] Epoch: {}/{} Generator Loss {} Discriminator Loss {} Generator Acc: {} "
                .format(epoch + 1, num_epochs, epoch_g_loss, epoch_d_loss,
                        gen_epoch_acc))

            if useTest and epoch % test_interval == (test_interval - 1):
                classifier.train()
                generator.train()
                start_time = timeit.default_timer()

                running_corrects = 0.0

                for (inputs, labels) in (trainval_loaders["test"]):
                    feats = Variable(inputs, requires_grad=True).float().cuda()
                    labels = Variable(labels,
                                      requires_grad=False).long().cuda()

                    loop_batch_size = len(feats)

                    noise = Variable(
                        FloatTensor(
                            np.random.normal(0, 1,
                                             (loop_batch_size, noise_dim))))
                    semantic_true = att[labels]

                    with torch.no_grad():
                        features_2048 = generator(semantic_true.float(), noise)
                        probs = classifier(features_2048)

                    _, predictions = torch.max(torch.softmax(probs, dim=1),
                                               dim=1,
                                               keepdim=False)
                    running_corrects += torch.sum(predictions == labels.data)

                real_epoch_acc = running_corrects.item() / len_test

                writer.add_scalar('data/gen_test_acc_epoch_benchmark',
                                  real_epoch_acc, epoch)

                print("[test] Epoch: {}/{} Test Dataset Generator Acc: {}".
                      format(epoch + 1, num_epochs, real_epoch_acc))
                stop_time = timeit.default_timer()
                print("Execution time: " + str(stop_time - start_time) + "\n")

            if (epoch % save_epoch == save_epoch - 1):
                save_path = os.path.join(
                    save_dir, saveName + '_increment'
                    '_epoch-' + str(epoch) + '.pth.tar')
                torch.save(
                    {
                        'epoch': epoch + 1,
                        'generator_state_dict': generator.state_dict(),
                        'discriminator_state_dict': discriminator.state_dict(),
                        'classifier_state_dict': classifier.state_dict(),
                    }, save_path)
                print("Save model at {}\n".format(save_path))

    writer.close()