Beispiel #1
0
def main(opts):
    pprint(vars(opts))
    device = torch.device(opts.device)
    print('using device: {}'.format(device))

    encoder = Convnet().to(device)
    encoder.load_state_dict(torch.load(opts.weights))
    encoder.eval()
    pipeline = protoPipe(encoder, opts.shot_k, opts.query_k)

    test_set = MNIST(mode='test')
    test_sampler = TaskSampler(test_set.label, 1000, opts.n_way,
                               opts.shot_k + opts.query_k)
    test_loader = DataLoader(dataset=test_set,
                             batch_sampler=test_sampler,
                             num_workers=8,
                             pin_memory=True)

    test_acc = []
    for episode, batch in enumerate(test_loader, 0):
        task = batch[0].view(opts.n_way * (opts.shot_k + opts.query_k), 3, 84,
                             84)
        loss, acc = pipeline(task.to(device), opts.n_way)
        test_acc.append(acc)

    m, h = mean_confidence_interval(test_acc)
    print('TEST set  acc: {:.4f}, h: {:.4f}'.format(m, h))
Beispiel #2
0
def main(opts):
    pprint(vars(opts))
    device = torch.device(opts.device)
    print('using device: {}'.format(device))

    encoder = Convnet().to(device)
    encoder.load_state_dict(torch.load(opts.weights))
    encoder.eval()
    pipeline = protoPipe(encoder, opts.shot_k, opts.query_k)

    if opts.seed is not None:
        torch.manual_seed(opts.seed)
        torch.cuda.manual_seed_all(opts.seed)
        np.random.seed(opts.seed)
        print("\nrandom seed: {}".format(opts.seed))

    if opts.dataset == 'mini':
        test_set = MiniImageNet(mode='test')
    elif opts.dataset == 'MNIST':
        test_set = MNIST(mode='test')
    elif opts.dataset == 'CIFAR':
        test_set = CIFAR(mode='test')
    elif opts.dataset == 'FashionMNIST':
        test_set = Fashion_MNIST(mode='test')

    test_sampler = TaskSampler(test_set.label, 1000, opts.n_way,
                               opts.shot_k + opts.query_k)
    test_loader = DataLoader(dataset=test_set,
                             batch_sampler=test_sampler,
                             num_workers=8,
                             pin_memory=True)

    test_acc = []
    for episode, batch in enumerate(test_loader, 0):
        task = batch[0].view(opts.n_way * (opts.shot_k + opts.query_k), 3, 84,
                             84)
        loss, acc = pipeline(task.to(device), opts.n_way)
        test_acc.append(acc)

    m, h = mean_confidence_interval(test_acc)
    print('TEST set  acc: {:.4f}, h: {:.4f}'.format(m, h))
    parser.add_argument('--query', type=int, default=30)
    parser.add_argument('--folds', type=int, default=2)
    args = parser.parse_args()
    pprint(vars(args))

    set_gpu(args.gpu)

    dataset = MiniImageNet('test')
    sampler = CategoriesSampler(dataset.label,
                                args.batch, args.way, args.folds * args.shot + args.query)
    loader = DataLoader(dataset, batch_sampler=sampler,
                        num_workers=8, pin_memory=True)

    model = Convnet().cuda()
    model.load_state_dict(torch.load(args.load))
    model.eval()

    ave_acc = Averager()
    s_label = torch.arange(args.train_way).repeat(args.shot).view(args.shot * args.train_way)
    s_onehot = torch.zeros(s_label.size(0), 20)
    s_onehot = s_onehot.scatter_(1, s_label.unsqueeze(dim=1), 1).cuda()

    for i, batch in enumerate(loader, 1):
        data, _ = [_.cuda() for _ in batch]
        k = args.way * args.shot
        data_shot, meta_support, data_query = data[:k], data[k:2*k], data[2*k:]

        #p = inter_fold(model, args, data_shot)

        x = model(data_shot)
        x = x.reshape(args.shot, args.way, -1).mean(dim=0)
Beispiel #4
0
            optimizer_global2.step()

            proto = None
            proto_final = None
            logits = None
            loss = None

        tl1 = tl1.item()
        tl2 = tl2.item()
        ta1 = ta1.item()
        ta2 = ta2.item()
        #log('epoch {}, train, loss={:.4f} acc={:.4f}'.format(epoch, tl, ta))
        log('epoch {}, train, loss1={:.4f} loss2={:.4f} acc1={:.4f} acc2={:.4f}'
            .format(epoch, tl1, tl2, ta1, ta2))

        model_cnn.eval()
        model_reg.eval()

        vl1 = Averager()
        vl2 = Averager()
        va1 = Averager()
        va2 = Averager()

        for i, batch in enumerate(val_loader, 1):
            data, lab = [_.cuda() for _ in batch]

            p = args.shot * args.test_way
            data_shot, data_query = data[:p], data[p:]
            data_shot = data_shot[:, 3:, :]
            data_query = data_query[:, 3:, :]
Beispiel #5
0
def main(args):
    device = torch.device(args.device)
    ensure_path(args.save_path)

    data = Data(args.dataset, args.n_batches, args.train_way, args.test_way, args.shot, args.query)
    train_loader = data.train_loader
    val_loader = data.valid_loader

    model = Convnet(x_dim=2).to(device)
    
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.5)

    def save_model(name):
        torch.save(model.state_dict(), osp.join(args.save_path, name + '.pth'))
    
    trlog = dict(
        args=vars(args),
        train_loss=[],
        val_loss=[],
        train_acc=[],
        val_acc=[],
        max_acc=0.0,
    )

    timer = Timer()

    for epoch in range(1, args.max_epoch + 1):
        lr_scheduler.step()

        model.train()

        tl = Averager()
        ta = Averager()

        for i, batch in enumerate(train_loader, 1):
            data, _ = [_.to(device) for _ in batch]
            data = data.reshape(-1, 2, 105, 105)
            p = args.shot * args.train_way
            embedded = model(data)
            embedded_shot, embedded_query = embedded[:p], embedded[p:]

            proto = embedded_shot.reshape(args.shot, args.train_way, -1).mean(dim=0)

            label = torch.arange(args.train_way).repeat(args.query).to(device)

            logits = euclidean_metric(embedded_query, proto)
            loss = F.cross_entropy(logits, label)
            acc = count_acc(logits, label)
            print('epoch {}, train {}/{}, loss={:.4f} acc={:.4f}'
                  .format(epoch, i, len(train_loader), loss.item(), acc))

            tl.add(loss.item())
            ta.add(acc)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        tl = tl.item()
        ta = ta.item()

        model.eval()

        vl = Averager()
        va = Averager()

        for i, batch in enumerate(val_loader, 1):
            data, _ = [_.cuda() for _ in batch]
            data = data.reshape(-1, 2, 105, 105)
            p = args.shot * args.test_way
            data_shot, data_query = data[:p], data[p:]

            proto = model(data_shot)
            proto = proto.reshape(args.shot, args.test_way, -1).mean(dim=0)

            label = torch.arange(args.test_way).repeat(args.query).to(device)

            logits = euclidean_metric(model(data_query), proto)
            loss = F.cross_entropy(logits, label)
            acc = count_acc(logits, label)

            vl.add(loss.item())
            va.add(acc)

        vl = vl.item()
        va = va.item()
        print('epoch {}, val, loss={:.4f} acc={:.4f}'.format(epoch, vl, va))

        if va > trlog['max_acc']:
            trlog['max_acc'] = va
            save_model('max-acc')

        trlog['train_loss'].append(tl)
        trlog['train_acc'].append(ta)
        trlog['val_loss'].append(vl)
        trlog['val_acc'].append(va)

        torch.save(trlog, osp.join(args.save_path, 'trlog'))

        save_model('epoch-last')

        if epoch % args.save_epoch == 0:
            save_model('epoch-{}'.format(epoch))

        print('ETA:{}/{}'.format(timer.measure(), timer.measure(epoch / args.max_epoch)))