def run_one(names):
    class args:
        meta_lr = 1e-3
        dataset = "miniImageNet"
        epoch_len = 800
        n = 5
        k = 5
        q = 5
        meta_batch_size = 2
        n_models = len(names)
        eval_batches = 80
        pred_mode = 'mean'
        order = 2
        epochs = 1
        inner_train_steps = 5
        inner_val_steps = 10
        inner_lr = 0.01

    background = dataset_class('background')
    background_taskloader = DataLoader(background,
                                       batch_sampler=NShotTaskSampler(
                                           background,
                                           args.epoch_len,
                                           n=args.n,
                                           k=args.k,
                                           q=args.q,
                                           num_tasks=args.meta_batch_size),
                                       num_workers=8)
    evaluation = dataset_class('evaluation')
    evaluation_taskloader = DataLoader(evaluation,
                                       batch_sampler=NShotTaskSampler(
                                           evaluation,
                                           args.eval_batches,
                                           n=args.n,
                                           k=args.k,
                                           q=args.q,
                                           num_tasks=args.meta_batch_size),
                                       num_workers=8)

    ############
    # Training #
    ############
    print(f'Training MAML on {args.dataset}...')

    model_params = [num_input_channels, args.k, fc_layer_size]
    meta_models = [
        FewShotClassifier(num_input_channels, args.k,
                          fc_layer_size).to(device, dtype=torch.double)
        for _ in range(args.n_models)
    ]
    meta_optimisers = [
        torch.optim.Adam(meta_model.parameters(), lr=args.meta_lr)
        for meta_model in meta_models
    ]

    for i, (model, name) in enumerate(zip(meta_models, names)):
        model.load_state_dict(torch.load("../models/maml_ens/" + name))

    loss_fn = F.nll_loss if args.order > 0 else F.cross_entropy

    if args.order == 2:
        fit_fn = meta_gradient_ens_step_mgpu_2order
    elif args.order == 1:
        fit_fn = meta_gradient_ens_step_mgpu_1order
    else:
        fit_fn = meta_gradient_ens_step_mgpu_meanloss

    def prepare_meta_batch(n, k, q, meta_batch_size):
        def prepare_meta_batch_(batch):
            x, y = batch
            # Reshape to `meta_batch_size` number of tasks. Each task contains
            # n*k support samples to train the fast model on and q*k query samples to
            # evaluate the fast model on and generate meta-gradients
            x = x.reshape(meta_batch_size, n * k + q * k, num_input_channels,
                          x.shape[-2], x.shape[-1])
            # Move to device
            x = x.double().to(device)
            # Create label
            y = create_nshot_task_label(k, q).cuda().repeat(meta_batch_size)
            return x, y

        return prepare_meta_batch_

    callbacks = [
        SaveFewShot(
            eval_fn=fit_fn,
            num_tasks=args.eval_batches,
            n_shot=args.n,
            k_way=args.k,
            q_queries=args.q,
            taskloader=evaluation_taskloader,
            prepare_batch=prepare_meta_batch(args.n, args.k, args.q,
                                             args.meta_batch_size),
            # MAML kwargs
            inner_train_steps=args.inner_val_steps,
            inner_lr=args.inner_lr,
            device=device,
            order=args.order,
            pred_mode=args.pred_mode,
            model_params=model_params)
    ]
    print(names[0][:-7])
    save_res(
        meta_models,
        meta_optimisers,
        loss_fn,
        epochs=args.epochs,
        dataloader=background_taskloader,
        prepare_batch=prepare_meta_batch(args.n, args.k, args.q,
                                         args.meta_batch_size),
        callbacks=callbacks,
        metrics=['categorical_accuracy'],
        fit_function=fit_fn,
        name=names[0][:-7],
        fit_function_kwargs={
            'n_shot': args.n,
            'k_way': args.k,
            'q_queries': args.q,
            'train': False,
            'pred_mode': args.pred_mode,
            'order': args.order,
            'device': device,
            'inner_train_steps': args.inner_train_steps,
            'inner_lr': args.inner_lr,
            'model_params': model_params
        },
    )
Example #2
0
                                       evaluation,
                                       args.eval_batches,
                                       n=args.n,
                                       k=args.k,
                                       q=args.q,
                                       num_tasks=args.meta_batch_size),
                                   num_workers=8)

############
# Training #
############
print(f'Training MAML on {args.dataset}...')

model_params = [num_input_channels, args.k, fc_layer_size]
meta_models = [
    FewShotClassifier(num_input_channels, args.k,
                      fc_layer_size).to(device, dtype=torch.double)
    for _ in range(args.n_models)
]
meta_optimisers = [
    torch.optim.Adam(meta_model.parameters(), lr=args.meta_lr)
    for meta_model in meta_models
]

loss_fn = F.nll_loss if args.order > 0 else F.cross_entropy
if args.order == 2:
    fit_fn = meta_gradient_ens_step_mgpu_2order
elif args.order == 1:
    fit_fn = meta_gradient_ens_step_mgpu_1order
else:
    fit_fn = meta_gradient_ens_step_mgpu_meanloss
Example #3
0
evaluation = dataset_class('datasets/test')
evaluation_taskloader = DataLoader(evaluation,
                                   batch_sampler=NShotTaskSampler(
                                       evaluation,
                                       args.eval_batches,
                                       n=args.n,
                                       k=args.k,
                                       q=args.q,
                                       num_tasks=args.meta_batch_size),
                                   num_workers=8)

############
# Training #
############
print(f'Training MAML on {args.dataset}...')
meta_model = FewShotClassifier(num_input_channels, args.k,
                               fc_layer_size).to(device, dtype=torch.double)
if args.retrain:
    meta_model.load_state_dict(
        torch.load(PATH + f'/models/maml/{param_str}.pth'))
meta_optimiser = torch.optim.Adam(meta_model.parameters(), lr=args.meta_lr)
loss_fn = nn.CrossEntropyLoss().to(device)

if args.retrain:
    param_str = f'{args.dataset}_order={args.order}_n={args.n}_k={args.k}_metabatch={args.meta_batch_size}_' \
                f'train_steps={args.inner_train_steps}_val_steps={args.inner_val_steps}_experiment={args.experiment_name}_'\
                f'retrain=True'


def prepare_meta_batch(n, k, q, meta_batch_size):
    def prepare_meta_batch_(batch):
        x, y = batch
Example #4
0
evaluation = dataset_class('evaluation')
evaluation_taskloader = DataLoader(evaluation,
                                   batch_sampler=NShotTaskSampler(
                                       evaluation,
                                       args.eval_batches,
                                       n=args.n,
                                       k=args.k,
                                       q=args.q,
                                       num_tasks=args.meta_batch_size),
                                   num_workers=8)

############
# Training #
############
print(f'Training MAML on {args.dataset}...')
meta_model = FewShotClassifier(num_input_channels, args.k,
                               fc_layer_size).to(device, dtype=torch.double)
meta_optimiser = torch.optim.Adam(meta_model.parameters(), lr=args.meta_lr)
loss_fn = nn.CrossEntropyLoss().to(device)


def prepare_meta_batch(n, k, q, meta_batch_size):
    def prepare_meta_batch_(batch):
        x, y = batch
        # Reshape to `meta_batch_size` number of tasks. Each task contains
        # n*k support samples to train the fast model on and q*k query samples to
        # evaluate the fast model on and generate meta-gradients
        x = x.reshape(meta_batch_size, n * k + q * k, num_input_channels,
                      x.shape[-2], x.shape[-1])
        # Move to device
        x = x.double().to(device)
        # Create label
    def test(self):

        img_filename = os.path.join( DATA_PATH ,'Omniglot',
                                     'images_background',
                                     'Latin.0',
                                     'character16',
                                     '0698_01.137602.png')
        img = io.imread(img_filename)
        img = img[np.newaxis, np.newaxis, :, :]
        img = (img - img.min()) / (img.max() - img.min())
        img = torch.from_numpy(img)
        print(img.size())
        n = 5
        k = 300
        setup_dirs()
        assert torch.cuda.is_available()

        device = torch.device('cpu')
        torch.backends.cudnn.benchmark = True

        model = FewShotClassifier(1, k).to(device, dtype=torch.double)
        model.load_state_dict(torch.load(os.path.join("models", "semantic_classifier",
                                                      "test_k=300_few_shot_classifier.pth")))

        conv_out = model(img)

        deconv_model = FewShotDeconv(model).to(device, dtype=torch.double)

        conv_layer_indices = model.get_conv_layer_indices()

        plt.ion()  # remove blocking
        plt.figure(figsize=(10, 5))

        done = False
        i=0
        while not done:
            layer = input('Layer to view (0-12, -1 to exit): ')
            try:
                layer = int(layer)
            except ValueError:
                continue

            if layer < 0:
                sys.exit(0)
            activ_map = model.feature_outputs[layer].data.numpy()
            activ_map = activ_map.transpose(1, 2, 3, 0)
            activ_map_grid = vis_grid(activ_map)
            vis_layer(activ_map_grid)

            # only transpose convolve from Conv2d or ReLU layers
            conv_layer = layer
            if conv_layer not in conv_layer_indices:
                conv_layer -= 1
                if conv_layer not in conv_layer_indices:
                    continue

            n_maps = activ_map.shape[0]

            marker = None
            while True:
                choose_map = True# input('Select map?  (y/[n]): ') == 'y'
                if marker != None:
                    marker.pop(0).remove()

                if not choose_map:
                    break

                _, map_x_dim, map_y_dim, _ = activ_map.shape
                map_img_x_dim, map_img_y_dim, _ = activ_map_grid.shape
                x_step = map_img_x_dim // (map_x_dim + 1)

                print('Click on an activation map to continue')
                x_pos, y_pos = plt.ginput(1)[0]
                x_index = x_pos // (map_x_dim + 1)
                y_index = y_pos // (map_y_dim + 1)
                map_idx = int(x_step * y_index + x_index)

                if map_idx >= n_maps:
                    print('Invalid map selected')
                    continue

                decon = deconv_model(model.feature_outputs[layer][0][map_idx][None, None, :, :], conv_layer, map_idx,
                                model.pool_indices)
                img = decon_img(decon)
                img = img.reshape((28,28))
                print(img.shape)
                plt.subplot(121)
                vis_layer(activ_map_grid)
                marker = plt.plot(x_pos, y_pos, marker='+', color='red')
                plt.subplot(122)
                plt.imshow(img)
                # plt.savefig('deconvnet' + str(x_pos) + '_' + str(y_pos) + '_hiragana13_layer=' + str(layer)+ '.png')
                i += 1