def train(epoch, x, adj, labels):
    optimizer.zero_grad()
    output = model(x, adj)
    #output = model(x)
    loss_train = F.nll_loss(output[idx_train], labels[idx_train])
    #print("Model Out ",output[idx_train])
    #print("Model Out ",labels[idx_train])
    acc_train = accuracy(output[idx_train], labels[idx_train])
    #print(epoch,acc_train.item())
    loss_train.backward()
    optimizer.step()
    return loss_train.item()
Ejemplo n.º 2
0
def train(net, train_data, valid_data, num_epochs, lr, wd, ctx, lr_period,
          lr_decay):
    trainer = gluon.Trainer(net.collect_params(), 'sgd', {
        'learning_rate': lr,
        'momentum': 0.9,
        'wd': wd
    })

    prev_time = datetime.datetime.now()
    for epoch in range(num_epochs):
        train_loss = 0.0
        train_acc = 0.0
        if epoch > 0 and epoch % lr_period == 0:
            trainer.set_learning_rate(trainer.learning_rate * lr_decay)
        for data, label in train_data:
            label = label.as_in_context(ctx)
            with autograd.record():
                output = net(data.as_in_context(ctx))
                loss = softmax_cross_entropy(output, label)
            loss.backward()
            trainer.step(batch_size)
            train_loss += nd.mean(loss).asscalar()
            train_acc += my_utils.accuracy(output, label)
        cur_time = datetime.datetime.now()
        h, remainder = divmod((cur_time - prev_time).seconds, 3600)
        m, s = divmod(remainder, 60)
        time_str = "Time %02d:%02d:%02d" % (h, m, s)
        if valid_data is not None:
            valid_acc = my_utils.evaluate_accuracy(valid_data, net, ctx)
            epoch_str = ("Epoch %d. Loss: %f, Train acc %f, Valid acc %f, " %
                         (epoch, train_loss / len(train_data),
                          train_acc / len(train_data), valid_acc))
        else:
            epoch_str = ("Epoch %d. Loss: %f, Train acc %f, " %
                         (epoch, train_loss / len(train_data),
                          train_acc / len(train_data)))
        prev_time = cur_time
        print(epoch_str + time_str + ', lr ' + str(trainer.learning_rate))
def test(x, adj, labels):
    output = model(x, adj)
    #output = model(x)
    acc_test = accuracy(output[idx_test], labels[idx_test])
    print("Test set results:", acc_test.item())
Ejemplo n.º 4
0
embed= embed.cpu().detach()
idx_train = idx_train.cpu().detach()
idx_test = idx_test.cpu().detach()
labels = labels.cpu().detach()


train_feats = embed[idx_train]

num_class = torch.max(labels)+1

Model = SimpleModel(128,64,torch.max(labels).item()+1)


Loss = torch.nn.NLLLoss()
optimizer = torch.optim.Adam(Model.parameters(), lr=0.01,weight_decay=0.0)

for _ in range(200):
    optimizer.zero_grad()
    y_hat = Model(embed)
    output = Loss(y_hat[idx_train],labels[idx_train])
    output.backward()
    optimizer.step()


y_hat = Model(embed[idx_test])
lbl = labels[idx_test]

print(accuracy(y_hat,lbl).item())

Ejemplo n.º 5
0
def main(args):

    # torch.manual_seed(222)
    # torch.cuda.manual_seed_all(222)
    # np.random.seed(222)

    print(args)

    device = torch.device('cuda')
    trush_num = len(os.listdir('/data/dataset/NIR-VIS-2.0/trush'))
    net = vgg_n.vgg16_dcf_db(num_classes=725 - trush_num).cuda()
    # pdb.set_trace()
    net.load_state_dict(
        torch.load('/home/jacobwang/torch_model/DCFNet/vgg13_face_dcf.pth'),
        strict=False)

    tmp = filter(lambda x: x.requires_grad, net.parameters())
    num = sum(map(lambda x: np.prod(x.shape), tmp))
    log_string(str(net.extra_repr))
    log_string('Total trainable tensors: %d' % num)

    # optimizer = torch.optim.Adam(net.parameters(), args.lr, weight_decay=0.00025)
    optimizer = torch.optim.SGD(net.parameters(),
                                args.lr,
                                momentum=0.9,
                                weight_decay=0.00025)
    # optimizer = torch.optim.RMSprop(net.parameters(), args.lr, weight_decay=0.0001)

    loss_ma = MovingAverage(100)
    acc_ma = MovingAverage(100)
    timer = Timer()

    crite = nn.CrossEntropyLoss()
    transform_train = transforms.Compose([
        # random_rot(60),
        # torchvision.transforms.Resize(size=256),  # Let smaller edge match
        # torchvision.transforms.Resize(size=512),  # Let smaller edge match
        # random_resize(0.8, 1.2, 224),
        torchvision.transforms.RandomHorizontalFlip(),
        # torchvision.transforms.RandomCrop(size=256),
        # torchvision.transforms.RandomCrop(size=448),
        torchvision.transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    ])

    transform_test = transforms.Compose([
        # torchvision.transforms.Resize(size=256),
        # torchvision.transforms.CenterCrop(size=256),
        # torchvision.transforms.Resize(size=512),
        torchvision.transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    ])

    root_path = '/data/dataset/NIR-VIS-2.0/merged_imgs'
    coco = NIRVIS(root_path, 'train', transform=transform_train)
    coco_test = NIRVIS(root_path, 'test', transform=transform_test)

    for epoch in range(args.epoch):
        # log_string('Epoch %d' %epoch)

        if epoch == 10000:
            optimizer.param_groups[0][
                'lr'] = optimizer.param_groups[0]['lr'] * 0.1
            log_string('Decaying learning rate.')
        # fetch meta_batchsz num of episode each time
        db = DataLoader(coco,
                        args.batch_size,
                        shuffle=True,
                        num_workers=8,
                        pin_memory=True,
                        worker_init_fn=worker_init_fn)

        timer.reset()
        for step, x in enumerate(db):

            x = [xx.cuda() for xx in x]
            [nir, vis, label] = x

            pred = net(torch.cat([nir, vis], 0))
            loss = crite(pred, label)

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

            loss_ma.update(loss.item())

            acc = accuracy(pred, label)[0].item()
            acc_ma.update(acc)

            # if step % 100 == 0:
        TI = timer.time(True)
        log_string('Epoch: %d, loss: %f, acc: %2.2f%%, time: %2.2f' %
                   (epoch, loss_ma.avg, acc_ma.avg, TI))

        if (epoch + 1) % 20 == 0:  # evaluation
            # pdb.set_trace()
            test_size = args.batch_size
            db_test = DataLoader(coco_test,
                                 test_size,
                                 shuffle=False,
                                 num_workers=8,
                                 pin_memory=True,
                                 drop_last=True)

            net.eval()
            cc = 0
            log_string('Validating at Epoch: %d' % epoch)

            acc = []
            acc_nir = []
            acc_vis = []

            with torch.no_grad():
                for jj, x in enumerate(db_test):
                    x = [xx.cuda() for xx in x]
                    [nir, vis, label] = x
                    # pdb.set_trace()
                    pred, p_nir, p_vis = net.infer(torch.cat([nir, vis], 0))

                    acc.append(accuracy(pred, label)[0].item())
                    acc_nir.append(accuracy(p_nir, label)[0].item())
                    acc_vis.append(accuracy(p_vis, label)[0].item())

            log_string(
                'TEsting at Epoch: %d, acc: %2.2f%%, acc: %2.2f%%, acc: %2.2f%%'
                % (epoch, np.mean(acc), np.mean(acc_nir), np.mean(acc_vis)))
            timer.reset()