コード例 #1
0
def evalEpoch(epoch, dataloader, model, criterion, evaluation, opt, writer):
    model.eval()
    total_loss, total_accuracy = Meter(), Meter()

    for batch in dataloader:
        if not opt.use_rcnn:
            img, ques, ques_mask, _, ans_idx = batch
        else:
            img, ques, img_mask, ques_mask, _, ans_idx = batch

        img = Variable(img, volatile=True)
        img_mask = Variable(img_mask, volatile=True) if opt.use_rcnn else None
        ques = Variable(ques, volatile=True)
        ques_mask = Variable(ques_mask, volatile=True)
        ans_idx = Variable(ans_idx, volatile=True)

        img, img_mask, ques, ques_mask, ans_idx = \
         move_to_cuda((img, img_mask, ques, ques_mask, ans_idx), devices=opt.gpus)
        ques = model.word_embedded(ques)

        score = model(img, ques, img_mask, ques_mask) if opt.use_rcnn else \
         model(img, ques, img_mask, ques_mask, is_train=False)
        accuracy = evaluation(score, ans_idx)
        loss = criterion(score, ans_idx)

        total_loss.update((loss.data[0] / opt.batch_size))
        total_accuracy.update(accuracy.data[0])

    writer.add_scalar("epoch/val_loss", total_loss.avg, global_step=epoch)
    writer.add_scalar("epoch/val_accuracy",
                      total_accuracy.avg,
                      global_step=epoch)

    return total_loss.avg, total_accuracy.avg
コード例 #2
0
ファイル: train.py プロジェクト: veda10/VQA
def evalEpoch(epoch, dataloader, model, criterion, evaluation, opt, writer):
    model.eval()
    total_loss, total_accuracy = Meter(), Meter()

    for i, batch in enumerate(dataloader):
        img, ques, ques_mask, _, ans_idx = batch

        img = torch.tensor(img, requires_grad=False)
        img_mask = None
        ques = torch.tensor(ques, requires_grad=False)
        ques_mask = torch.tensor(ques_mask, requires_grad=False)
        ans_idx = torch.tensor(ans_idx, requires_grad=False)

        img, img_mask, ques, ques_mask, ans_idx = move_to_cuda(
            (img, img_mask, ques, ques_mask, ans_idx), devices=opt['gpus'])
        ques = model.word_embedded(ques)

        score = model(img, ques, img_mask, ques_mask, is_train=False)
        accuracy = evaluation(score, ans_idx)
        loss = criterion(score, ans_idx)

        total_loss.update((loss.item() / opt['batch_size']))
        total_accuracy.update(accuracy.item())

        if (i + 1) % opt['log_interval'] == 0:
            print("VALIDATION: Epoch %5d; iter %6i" % (epoch, i + 1))
    writer.add_scalar("epoch/val_loss", total_loss.avg, global_step=epoch)
    writer.add_scalar("epoch/val_accuracy",
                      total_accuracy.avg,
                      global_step=epoch)

    return total_loss.avg, total_accuracy.avg
コード例 #3
0
def trainEpoch(epoch, dataloader, model, criterion, evaluation, optim, opt,
               writer):
    model.train()
    loss_record = [Meter() for _ in range(3)]
    accuracy_record = [Meter() for _ in range(3)]
    timer = Timer()

    timer.tic()
    optim.step_epoch()
    for i, batch in enumerate(dataloader):
        if not opt.use_rcnn:
            img, ques, ques_mask, _, ans_idx = batch
        else:
            img, ques, img_mask, ques_mask, _, ans_idx = batch

        img = Variable(img) if opt.use_rcnn else Variable(img, volatile=True)
        img_mask = Variable(img_mask) if opt.use_rcnn else None
        ques = Variable(ques, volatile=True)
        ques_mask = Variable(ques_mask)
        ans_idx = Variable(ans_idx)

        img, img_mask, ques, ques_mask, ans_idx = \
         move_to_cuda((img, img_mask, ques, ques_mask, ans_idx), devices=opt.gpus)
        ques = model.word_embedded(ques)
        ques = Variable(ques.data)

        optim.zero_grad()
        score = model(img, ques, img_mask, ques_mask) if opt.use_rcnn else \
         model(img, ques, img_mask, ques_mask, is_train=True)

        loss = criterion(score, ans_idx)
        loss.backward()
        accuracy = evaluation(Variable(score.data, volatile=True),
                              Variable(ans_idx.data, volatile=True))
        _, ratio, updates, params = optim.step()

        for j in range(3):
            loss_record[j].update((loss.data[0] / opt.batch_size))
            accuracy_record[j].update(accuracy.data[0])

        if ratio is not None:
            writer.add_scalar("statistics/update_to_param_ratio",
                              ratio,
                              global_step=(epoch * len(dataloader) + i))
            writer.add_scalar("statistics/absolute_updates",
                              updates,
                              global_step=(epoch * len(dataloader) + i))
            writer.add_scalar("statistics/absolute_params",
                              params,
                              global_step=(epoch * len(dataloader) + i))

        if (i + 1) % 10 == 0:
            writer.add_scalar("iter/train_loss",
                              loss_record[0].avg,
                              global_step=(epoch * len(dataloader) + i))
            writer.add_scalar("iter/train_accuracy",
                              accuracy_record[0].avg,
                              global_step=(epoch * len(dataloader) + i))
            loss_record[0].reset()
            accuracy_record[0].reset()

        if (i + 1) % opt.log_interval == 0:
            print(
                "Epoch %5d; iter %6i; loss: %8.2f; accuracy: %8.2f; %6.0fs elapsed"
                % (epoch, i + 1, loss_record[1].avg, accuracy_record[1].avg,
                   timer.toc(average=False)))
            loss_record[1].reset()
            accuracy_record[1].reset()
            timer.tic()

    writer.add_scalar("epoch/train_loss",
                      loss_record[2].avg,
                      global_step=epoch)
    writer.add_scalar("epoch/train_accuracy",
                      accuracy_record[2].avg,
                      global_step=epoch)

    return loss_record[2].avg, accuracy_record[2].avg