indices_test = get_indices_for_classes(testset, known_classes) test_loader = DataLoader(testset, batch_size=1000, sampler = SubsetRandomSampler(indices_test)) acc_real = test_classifier(classifier, test_loader) acc_fake = test_classifier_on_generator(classifier, gen_model, test_loader) 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)) # --------------------------------------------------- STREAM TRAINING ---------------------------------------------------------- stream_duration = 100 for interval in range(stream_duration): gen_model_old = copy.deepcopy(gen_model) stream_class = [np.random.randint(0, 10)] stream_indices = get_indices_for_classes(trainset, stream_class) # Preloading the historical data/stored codes fake_data = historical_buffer.make_tensor_dataset() real_data = real_buffer.make_tensor_dataset() stream_loader = DataLoader(trainset, batch_size=opts['batch_size'], sampler = SubsetRandomSampler(stream_indices), drop_last=True) fake_loader = DataLoader(fake_data, batch_size=opts['batch_size'], shuffle=True, drop_last=True) real_loader = DataLoader(real_data, batch_size=opts['batch_size'], shuffle=True, drop_last=True) known_classes = [int(a) for a in historical_buffer.dbuffer.keys()] # We will make an extra testing step with completed testset if a new class is coming from the stream if stream_class[0] not in known_classes: print('ADDED A NEW CLASS {}'.format(stream_class[0])) known_classes.append(stream_class[0]) indices_test = get_indices_for_classes(testset, known_classes) test_loader = DataLoader(testset, batch_size=1000, sampler = SubsetRandomSampler(indices_test)) acc_real = test_classifier(classifier, test_loader) acc_fake = test_classifier_on_generator(classifier, gen_model, test_loader)
results['known_classes'].append(len(known_classes)) # --------------------------------------------------- STREAM TRAINING ---------------------------------------------------------- stream_duration = 1000 for interval in range(stream_duration): gen_model_old = copy.deepcopy(gen_model) stream_classes = [ np.random.randint(0, nb_of_classes) for idx in range(classes_per_interval) ] # Preloading the historical data/stored codes print('Starting new interval, create tensors from storages') #sup_functions.memory_check() codes_storage.make_tensor_dataset() real_buffer.make_tensor_dataset() print('Fake and real tensors created') #sup_functions.memory_check() # Initializing loaders: stream, historical and codes to decode stream_loaders = {} #stream_loader = DataLoader(full_original_trainset, batch_size=opts['batch_size'], sampler = SubsetRandomSampler(stream_indices), drop_last=True) fake_loader = DataLoader(codes_storage.tensor_dataset, batch_size=opts['batch_size'], shuffle=True, drop_last=True) real_loader = DataLoader(real_buffer.tensor_dataset, batch_size=opts['batch_size'], shuffle=True, drop_last=True) known_classes = [int(a) for a in codes_storage.dbuffer.keys()]
test_classifier = sup_functions.test_classifier test_classifier_on_generator = sup_functions.test_classifier_on_generator get_indices_for_classes = sup_functions.get_indices_for_classes trainset = torch.load('../datasets/LSUN/trainset.pth') testset = torch.load('../datasets/LSUN/testset.pth') original_trainset = TensorDataset(trainset[0], trainset[1], torch.zeros(trainset[1].shape)) trainset = TensorDataset(trainset[0], trainset[1], torch.zeros(trainset[1].shape)) testset = TensorDataset(testset[0], testset[1]) # Loading the datasets print('Reshaping data into readable format') hist_buffer = Data_Buffer(real_batches_to_add or 1, opts['batch_size']) hist_buffer.load_from_tensor_dataset(trainset, list(range(30))) hist_buffer.make_tensor_dataset() hist_data = hist_buffer.tensor_dataset data_buffer = Data_Buffer(batches_per_class, opts['batch_size']) data_buffer.load_from_tensor_dataset(trainset, list(range(30))) data_buffer.cuda_device = cuda_device print('Ended reshaping') # Initializing data loaders for first 5 classes #train_loader = DataLoader(original_trainset, batch_size=opts['batch_size'], shuffle=True) test_loader = DataLoader(testset, batch_size=opts['batch_size'], shuffle=False) # Initializing classification model classifier = Classifier(nb_of_classes) classifier_dict = torch.load('./pretrained_models/classifier_30_LSUN.pth')
original_trainset = TensorDataset(trainset[0], trainset[1], torch.zeros(trainset[1].shape)) testset = TensorDataset(testset[0], testset[1]) # Loading the datasets print('Reshaping data into readable format') data_buffer = Data_Buffer(batches_per_class, opts['batch_size']) data_buffer.add_batches_from_dataset(original_trainset, list(range(30)), batches_per_class) data_buffer.cuda_device = cuda_device orig_buffer = Data_Buffer(real_batches_to_add, opts['batch_size']) orig_buffer.add_batches_from_dataset(original_trainset, list(range(30)), real_batches_to_add) orig_buffer.cuda_device = cuda_device original_trainset = orig_buffer.make_tensor_dataset() print('Ended reshaping') # Initializing data loaders for first 5 classes #train_loader = DataLoader(original_trainset, batch_size=opts['batch_size'], shuffle=True) test_loader = DataLoader(testset, batch_size=opts['batch_size'], shuffle=False) # Initializing classification model classifier = Classifier(nb_of_classes) classifier_dict = torch.load('./pretrained_models/full_classifier_LSUN.pth') classifier.load_state_dict(classifier_dict) classifier.cuda() gen_model = AE_models.AE_LSUN(code_size) gen_model_state = torch.load('./pretrained_models/AE_32_LSUN_data.pth')
# --------------------------------------------------- STREAM TRAINING ---------------------------------------------------------- stream_duration = 1000 for interval in range(stream_duration): #gen_model_old = copy.deepcopy(gen_model) stream_classes = [ np.random.randint(0, nb_of_classes) for idx in range(classes_per_interval) ] #stream_indices = sup_functions.get_indices_for_classes(full_original_trainset, stream_classes) # Preloading the historical data/stored codes print('Starting new interval, create tensors from storages') #sup_functions.memory_check() #codes_storage.make_tensor_dataset() real_buffer.make_tensor_dataset() print('Fake and real tensors created') #sup_functions.memory_check() # Initializing loaders: stream, historical and codes to decode stream_loaders = {} #stream_loader = DataLoader(full_original_trainset, batch_size=opts['batch_size'], sampler = SubsetRandomSampler(stream_indices), drop_last=True) #fake_loader = DataLoader(codes_storage.tensor_dataset, batch_size=opts['batch_size'], shuffle=True, drop_last=True) real_loader = DataLoader(real_buffer.tensor_dataset, batch_size=opts['batch_size'], shuffle=True, drop_last=True) known_classes = [int(a) for a in real_buffer.dbuffer.keys()] # We will make an extra testing step with completed testset if a new class is coming from the stream added_new_classes = 0 for stream_class in stream_classes:
generative_criterion_cl.cuda() generative_criterion_rec = nn.MSELoss() generative_criterion_rec.cuda() acc = test_classifier(classifier, test_loader) acc_rec = test_classifier_on_generator(classifier, gen_model, test_loader) print('Classification accuracy prior to training: {:.4f}'.format(acc)) print('Test accuracy on reconstructed testset: {}'.format(acc_rec)) accuracies = [] max_accuracy = 0 for epoch in range(training_epochs): # loop over the dataset multiple times if epoch % 1 == 0: print('Transforming data with the latest autoencoder') data_buffer.transform_data(gen_model) trainset = data_buffer.make_tensor_dataset() train_loader = DataLoader(trainset, batch_size=opts['batch_size'], shuffle=True, drop_last=True) for idx, (train_X, train_Y) in enumerate(train_loader): inputs = train_X.cuda() # ===================forward===================== #reconstructions = gen_model(inputs) #orig_classes = classifier(inputs) #classification_reconstructed = classifier(reconstructions) #loss_gen = generative_criterion(classification_reconstructed, orig_classes) #loss_gen.backward() #generative_optimizer.step() #generative_optimizer.zero_grad()