Exemplo n.º 1
0
data = get_CIFAR10_data()

model = TwoLayerNet(hidden_dim=150, reg=1e-3, weight_scale=1e-3)
solver = None

##############################################################################
# TODO: Use a Solver instance to train a TwoLayerNet that achieves at least  #
# 50% accuracy on the validation set.                                        #
##############################################################################
solver = Solver(
    model,
    data,
    #update_rule='sgd',
    optim_config={
        'learning_rate': 1e-3,
    },
    lr_decay=0.95,
    num_epochs=10,
    batch_size=100,
    print_every=100)

pass
##############################################################################
#                             END OF YOUR CODE                               #
##############################################################################

# Run this cell to visualize training loss and train / val accuracy

plt.subplot(2, 1, 1)
plt.title('Training loss')
Exemplo n.º 2
0
def evaluate(config):
    """ Configure evaluation with or without cuda """

    if config['use_cuda'] and torch.cuda.is_available():
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")
        torch.set_default_tensor_type('torch.FloatTensor')

    z = None
    mu = None

    def get_model_solver_paths(save_path, epoch):
        print("Getting model and solver paths")
        model_paths = []
        solver_paths = []

        for _, _, fnames in os.walk(save_path):
            model_paths = [fname for fname in fnames if 'model' in fname]
            solver_paths = [fname for fname in fnames if 'solver' in fname]

        if not model_paths or not solver_paths:
            raise Exception('Model or solver not found.')

        if not epoch:
            model_path = os.path.join(
                save_path,
                sorted(model_paths,
                       key=lambda s: int(s.split("model")[1]))[-1])
            solver_path = os.path.join(
                save_path,
                sorted(solver_paths,
                       key=lambda s: int(s.split("solver")[1]))[-1])
        else:
            model_path = os.path.join(save_path, 'model' + str(epoch))
            solver_path = os.path.join(save_path, 'solver' + str(epoch))

        return model_path, solver_path

    print("Loading dataset")

    dataset = CustomDataset(config['data_path'],
                            transform=transforms.Compose([
                                transforms.Grayscale(),
                                transforms.ToTensor()
                            ]),
                            len_inp_sequence=config['len_inp_sequence'],
                            len_out_sequence=config['len_out_sequence'],
                            load_ground_truth=True,
                            question=config['use_question'],
                            load_to_ram=False,
                            load_config=True)
    dataset_config = pickle.load(
        open(os.path.join(config['data_path'], 'config.p'), 'rb'))

    if config['num_samples'] is not None:
        if config['num_show_images'] > len(dataset):
            raise Exception(
                'Dataset does not contain {} images to show'.format(
                    config['num_show_images']))

        # Sample equidistantly from dataset
        indices = np.linspace(0,
                              len(dataset) - 1,
                              config['num_samples'],
                              dtype=int).tolist()

        dataset_list = [dataset[i] for i in indices]
        ground_truth = [dataset.get_ground_truth(i) for i in indices]
    else:
        dataset_list = dataset
        ground_truth = [
            dataset.get_ground_truth(i) for i in range(len(dataset))
        ]

    model_path, solver_path = get_model_solver_paths(config['save_path'],
                                                     config['epoch'])

    print("Loading model and solver")
    solver = Solver()
    solver.load(solver_path, device=device, only_history=True)
    model = torch.load(model_path, map_location=device)
    model.eval()

    if config['analyze_dataset']:
        print("Analysing dataset")
        if config['num_samples'] is not None:
            indices = np.linspace(0,
                                  len(dataset) - 1,
                                  config['num_samples'],
                                  dtype=int).tolist()
        else:
            indices = range(len(dataset))

        trajectories = np.array([dataset.get_ground_truth(i) for i in indices])

        analyze_dataset(trajectories,
                        window_size_x=dataset_config.window_size_x,
                        window_size_y=dataset_config.window_size_y,
                        mode='lines')

    if config['show_solver_history']:
        print("Showing solver history")
        show_solver_history(solver)

    if config['print_training_config']:
        print_traning_config(solver)

    if config['show_latent_variables']:
        print("Using {} samples to show latent variables".format(
            config['num_samples']))
        z, mu = show_latent_variables(model, dataset_list)

    if config['show_model_output']:
        print("Showing model output")
        indices = np.linspace(0,
                              len(dataset) - 1,
                              config['num_show_images'],
                              dtype=int).tolist()
        show_model_output(model, dataset, indices, dataset.len_out_sequence)

    if config['eval_correlation']:
        print("Evaluating correlation")

        if z is None:
            z, mu = show_latent_variables(model, dataset_list, show=False)

        show_correlation(model, dataset_list, solver, z, ground_truth)

        # if model.use_physics:
        #     show_correlation_after_physics(model, dataset_list)
        # else:
        #     print("Model without physics layer")

    if config['latent_walk_gifs']:
        print("Creating GIFs for walks over latent variables")
        if config['num_samples'] is not None:
            # Sample equidistantly from dataset
            indices = np.linspace(0,
                                  len(dataset) - 1,
                                  config['num_samples'],
                                  dtype=int).tolist()

            dataset_list = [dataset[i] for i in indices]
            ground_truth = [dataset.get_ground_truth(i) for i in indices]
        else:
            dataset_list = dataset
            ground_truth = [
                dataset.get_ground_truth(i) for i in range(len(dataset))
            ]

        if mu is None:
            z, mu = show_latent_variables(model, dataset_list, show=False)

        show_latent_walk_gifs(model,
                              mu,
                              question=config['use_question'],
                              len_out_sequence=dataset.len_out_sequence)

    if config['walk_over_question']:
        print("Walk over questions")
        walk_over_question(model, dataset)

    if config['eval_disentanglement']:
        print("Evaluating disentanglement")
        # load eval dataset to list
        paths = [
            os.path.join(config['eval_data_path'], path)
            for path in dataset_config.latent_names
        ]
        eval_datasets = [
            CustomDataset(
                path,
                transform=transforms.Compose(
                    [transforms.Grayscale(),
                     transforms.ToTensor()]),
                len_inp_sequence=config['len_inp_sequence'],
                len_out_sequence=config['len_out_sequence'],
                load_ground_truth=True,
                question=config['use_question'],
                load_to_ram=False,
                load_config=True,
            ) for path in paths
        ]

        eval_disentanglement(model, eval_datasets, device, num_epochs=100)

    if config['mutual_information_gap']:
        print("Computing mutual information gap")
        MIG(model, dataset, config['num_samples'], discrete=True)
Exemplo n.º 3
0
def train(config):
    """ Add a seed to have reproducible results """

    seed = 456
    torch.manual_seed(seed)
    """ Configure training with or without cuda """

    if config['use_cuda'] and torch.cuda.is_available():
        device = torch.device("cuda")
        torch.cuda.manual_seed(seed)
        kwargs = {'pin_memory': True}
        print("GPU available. Training on {}.".format(device))
    else:
        device = torch.device("cpu")
        torch.set_default_tensor_type('torch.FloatTensor')
        kwargs = {}
        print("No GPU. Training on {}.".format(device))
    """ Load dataset """

    print(
        "Loading dataset with input sequence length {} and output sequence length {}..."
        .format(config['len_inp_sequence'], config['len_out_sequence']))

    dataset = CustomDataset(config['data_path'],
                            transform=transforms.Compose([
                                transforms.Grayscale(),
                                transforms.ToTensor()
                            ]),
                            len_inp_sequence=config['len_inp_sequence'],
                            len_out_sequence=config['len_out_sequence'],
                            load_to_ram=config['load_data_to_ram'],
                            question=config['use_question'],
                            load_ground_truth=False,
                            load_config=True)

    if config['batch_size'] > len(dataset):
        raise Exception('Batch size bigger than the dataset.')

    if config['do_overfitting']:
        print("Overfitting on a subset of {} samples".format(
            config['num_train_overfit']))
        if config['batch_size'] > config['num_train_overfit']:
            raise Exception(
                'Batchsize for overfitting bigger than the number of samples for overfitting.'
            )
        else:
            train_data_sampler = SequentialSampler(
                range(config['num_train_overfit']))
            val_data_sampler = SequentialSampler(
                range(config['num_train_overfit']))

    else:
        print("Training on {} samples".format(config['num_train_regular']))
        if config['num_train_regular'] + config['num_val_regular'] > len(
                dataset):
            raise Exception(
                'Trying to use more samples for training and validation than len(dataset), {} > {}.'
                .format(
                    config['num_train_regular'] + config['num_val_regular'],
                    len(dataset)))
        else:
            train_data_sampler = SubsetRandomSampler(
                range(config['num_train_regular']))
            val_data_sampler = SubsetRandomSampler(
                range(config['num_train_regular'],
                      config['num_train_regular'] + config['num_val_regular']))

    train_data_loader = torch.utils.data.DataLoader(
        dataset=dataset,
        batch_size=config['batch_size'],
        num_workers=config['num_workers'],
        sampler=train_data_sampler,
        drop_last=True,
        **kwargs)
    val_data_loader = torch.utils.data.DataLoader(
        dataset=dataset,
        batch_size=config['batch_size'],
        num_workers=config['num_workers'],
        sampler=val_data_sampler,
        drop_last=True,
        **kwargs)
    """ Initialize model and solver """

    if config['continue_training']:
        print("Continuing training with model: {} and solver: {}".format(
            config['model_path'], config['solver_path']))

        model = torch.load(config['model_path'])
        model.to(device)
        solver = Solver()
        solver.optim = torch.optim.Adam(model.parameters(),
                                        lr=config['learning_rate'])
        solver.load(config['solver_path'], device=device)
        optimizer = None

    else:
        print("Initializing model...")
        model = VariationalAutoEncoder(
            len_in_sequence=config['len_inp_sequence'],
            len_out_sequence=config['len_out_sequence'],
            z_dim_encoder=config['z_dim_encoder'],
            z_dim_decoder=config['z_dim_decoder'],
            use_physics=config['use_physics'])
        solver = Solver()
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=config['learning_rate'])
    """ Perform training """
    solver.train(model=model,
                 train_config=config,
                 dataset_config=dataset.config,
                 tensorboard_path=config['tensorboard_log_dir'],
                 optim=optimizer,
                 num_epochs=config['num_epochs'],
                 max_train_time_s=config['max_train_time_s'],
                 train_loader=train_data_loader,
                 val_loader=val_data_loader,
                 log_after_iters=config['log_interval'],
                 save_after_epochs=config['save_interval'],
                 save_path=config['save_path'],
                 device=device,
                 C_offset=config['C_offset'],
                 C_max=config['C_max'],
                 C_stop_iter=config['C_stop_iter'],
                 gamma=config['gamma'],
                 target_var=config['target_var'],
                 log_reconstructed_images=config['log_reconstructed_images'],
                 beta=config['beta'])
Exemplo n.º 4
0
print('ur', 'lr', 'ne', 'ld','rs','dp', 'valAcc', 'valTest')

for ur in upadate_rules:
    for lr in learning_rates:
        for ne in num_epochs:
            for ld in l_decays:
                for rs in regularization_strengths:
                    for dp in dropout_choices:

                        model = FullyConnectedNet(hidden_dims, dropout=dp, reg=rs, weight_scale=5e-2, use_batchnorm=True)
                        solver = Solver(model, data,
                        lr_decay=ld,
                        num_epochs=ne,
                        batch_size=100,
                        update_rule=ur,
                        optim_config={
                        'learning_rate': lr
                        },
                        verbose=False)
                        solver.train()

                        y_val_pred = np.argmax(model.loss(X_val), axis=1)
                        y_test_pred = np.argmax(model.loss(X_test), axis=1)
                        val_acc = (y_val_pred == y_val).mean()
                        val_test = (y_test_pred == y_test).mean()

                        if val_acc > best_val:
                            best_model = model
                            best_val = val_acc
Exemplo n.º 5
0
        plt.title("Input image")

    # target
    plt.subplot(num_example_imgs, 2, i * 2 + 2)
    plt.imshow(label_img_to_rgb(target.numpy()))
    plt.axis('off')
    if i == 0:
        plt.title("Target image")
plt.show()

#from dl4cv.classifiers.segmentation_nn import SegmentationNN
from dl4cv.classifiers.fcn16s import FCN16VGG as SegmentationNN
from dl4cv.solver import Solver
import torch.nn.functional as F

########################################################################
#                             YOUR CODE                                #
########################################################################
train_loader = torch.utils.data.DataLoader(train_data,
                                           batch_size=1,
                                           shuffle=False,
                                           num_workers=1,
                                           sampler=OverfitSampler(20))
val_loader = torch.utils.data.DataLoader(val_data,
                                         batch_size=1,
                                         shuffle=False,
                                         num_workers=1,
                                         sampler=OverfitSampler(20))
model = SegmentationNN()
solver = Solver(optim_args={"lr": 1e-3})
solver.train(model, train_loader, val_loader, log_nth=1, num_epochs=1)
Exemplo n.º 6
0
    'y_val': data['y_val'],
}
weight_scale = 1.

bn_model = FullyConnectedNet(hidden_dims,
                             weight_scale=weight_scale,
                             use_batchnorm=True)
model = FullyConnectedNet(hidden_dims,
                          weight_scale=weight_scale,
                          use_batchnorm=False)

bn_solver = Solver(bn_model,
                   small_data,
                   num_epochs=10,
                   batch_size=50,
                   update_rule='adam',
                   optim_config={
                       'learning_rate': 1.,
                   },
                   verbose=True,
                   print_every=200)
bn_solver.train()

solver = Solver(model,
                small_data,
                num_epochs=10,
                batch_size=50,
                update_rule='adam',
                optim_config={
                    'learning_rate': 1.,
                },
                verbose=True,
#torch.set_default_tensor_type('torch.FloatTensor')

#%matplotlib inline

#Load data
train_data = SegmentationData(
    image_paths_file='datasets/segmentation_data/train.txt')
val_data = SegmentationData(
    image_paths_file='datasets/segmentation_data/val.txt')

########################################################################
#                             YOUR CODE                                #
########################################################################
#model = SegmentationNN()
model = torch.load("models/segmentation_nn5.model")
solver = Solver()

train_loader = torch.utils.data.DataLoader(train_data,
                                           batch_size=3,
                                           shuffle=False,
                                           num_workers=4)
val_loader = torch.utils.data.DataLoader(val_data,
                                         batch_size=3,
                                         shuffle=False,
                                         num_workers=1)
solver.train(model, train_loader, val_loader, log_nth=4, num_epochs=3)

#### TEST
test_data = SegmentationData(
    image_paths_file='datasets/segmentation_data_test/test.txt')
test_loader = torch.utils.data.DataLoader(test_data,