예제 #1
0
def infer(valid_loader, model, criterion):
    """
targert
    valid loop and record loss ,accuracy 
parameters
    train_loader : training dataloader 
    model        : training model 
    criterion    : loss function 
    return 
    average top 1 accuracy 
    """
    total_loss = utils.AvgrageMeter()
    total_top1 = utils.AvgrageMeter()
    aux_nums = len(model.aux_ind)

    total_loss_aux = [utils.AvgrageMeter() for i in range(aux_nums)]
    total_top1_aux = [utils.AvgrageMeter() for i in range(aux_nums)]

    model.eval()

    for step, (data, target) in enumerate(valid_loader):

        data = data.cuda()
        target = target.cuda()
        with torch.no_grad():
            logits, auxs = model(data)
        loss = F.cross_entropy(logits, target).cuda()
        loss_aux = [F.cross_entropy(i, target).cuda() for i in auxs]

        prec1 = utils.accuracy(logits, target)[0]
        prec1_aux = [utils.accuracy(i, target)[0] for i in auxs]

        n = data.size(0)

        total_loss.update(loss.item(), n)
        total_top1.update(prec1.item(), n)

        [
            total_loss_aux[ind].update(i.item(), n)
            for ind, i in enumerate(loss_aux)
        ]
        [
            total_top1_aux[ind].update(i.item(), n)
            for ind, i in enumerate(prec1_aux)
        ]

        if (step + 1) % args.report_freq == 0:
            [
                logging.info('aux_{}  {:0>3d} {:.6f} {:.3f}'.format(
                    i, step, total_loss_aux[i].avg, total_top1_aux[i].avg))
                for i in range(aux_nums)
            ]
            logging.info('\nvalid  {:0>3d} {:.6f} {:.3f}'.format(
                step, total_loss.avg, total_top1.avg))

    del loss, logits, loss_aux

    return total_top1.avg
예제 #2
0
def validate(epoch, val_loader, model, criterion, args):
    batch_time = utils.AverageMeter('Time', ':6.3f')
    losses = utils.AverageMeter('Loss', ':.4e')
    top1 = utils.AverageMeter('Acc@1', ':6.2f')
    top5 = utils.AverageMeter('Acc@5', ':6.2f')

    if args.use_dali:
        num_iter = val_loader._size // args.batch_size
    else:
        num_iter = len(val_loader)

    model.eval()
    with torch.no_grad():
        end = time.time()
        if args.use_dali:
            for batch_idx, batch_data in enumerate(val_loader):
                images = batch_data[0]['data'].cuda()
                targets = batch_data[0]['label'].squeeze().long().cuda()

                # compute output
                logits = model(images)
                loss = criterion(logits, targets)

                # measure accuracy and record loss
                pred1, pred5 = utils.accuracy(logits, targets, topk=(1, 5))
                n = images.size(0)
                losses.update(loss.item(), n)
                top1.update(pred1[0], n)
                top5.update(pred5[0], n)

                # measure elapsed time
                batch_time.update(time.time() - end)
                end = time.time()
        else:
            for batch_idx, (images, targets) in enumerate(val_loader):
                images = images.cuda()
                targets = targets.cuda()

                # compute output
                logits = model(images)
                loss = criterion(logits, targets)

                # measure accuracy and record loss
                pred1, pred5 = utils.accuracy(logits, targets, topk=(1, 5))
                n = images.size(0)
                losses.update(loss.item(), n)
                top1.update(pred1[0], n)
                top5.update(pred5[0], n)

                # measure elapsed time
                batch_time.update(time.time() - end)
                end = time.time()

        logger.info(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'.format(
            top1=top1, top5=top5))

    return losses.avg, top1.avg, top5.avg
예제 #3
0
def infer(valid_queue, model, criterion):
    objs = utils.AverageMeter()
    top1 = utils.AverageMeter()
    top5 = utils.AverageMeter()

    model.eval()
    with torch.no_grad():
        for step, (data, target) in enumerate(valid_queue):
            data = data.cuda()
            target = target.cuda()

            logits, _ = model(data)
            loss = criterion(logits, target)

            prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
            n = data.size(0)
            objs.update(loss.item(), n)
            top1.update(prec1.item(), n)
            top5.update(prec5.item(), n)

            if step % args.report_freq == 0:
                logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg,
                             top5.avg)

    return top1.avg, top5.avg, objs.avg
예제 #4
0
파일: train_search.py 프로젝트: mktal/DrNAS
def infer(valid_queue, model, criterion):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    model.eval()

    with torch.no_grad():
        for step, (input, target) in enumerate(valid_queue):
            input = input.cuda()
            target = target.cuda(non_blocking=True)

            logits = model(input)
            loss = criterion(logits, target)

            prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
            n = input.size(0)
            objs.update(loss.data, n)
            top1.update(prec1.data, n)
            top5.update(prec5.data, n)

            if step % args.report_freq == 0:
                logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg,
                             top5.avg)
            if 'debug' in args.save:
                break

    return top1.avg, objs.avg
예제 #5
0
def train(train_queue, model, criterion, optimizer):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    model.train()

    for step, (input, target) in enumerate(train_queue):
        target = target.cuda(async=True)
        input = input.cuda()
        input = Variable(input)
        target = Variable(target)

        optimizer.zero_grad()
        logits, logits_aux = model(input)
        loss = criterion(logits, target)
        if args.auxiliary:
            loss_aux = criterion(logits_aux, target)
            loss += args.auxiliary_weight * loss_aux

        loss.backward()
        nn.utils.clip_grad_norm(model.parameters(), args.grad_clip)
        optimizer.step()

        prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
        n = input.size(0)
        objs.update(loss.data[0], n)
        top1.update(prec1.data[0], n)
        top5.update(prec5.data[0], n)

        if step % args.report_freq == 0:
            logging.info('train %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)

    return top1.avg, objs.avg
예제 #6
0
def train(train_queue, model, criterion, optimizer):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    model.train()

    for step, (input, target) in enumerate(train_queue):
        input = input.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)

        optimizer.zero_grad()
        logits, logits_aux = model(input)
        loss = criterion(logits, target)
        if args.auxiliary:
            loss_aux = criterion(logits_aux, target)
            loss += args.auxiliary_weight * loss_aux
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip)
        optimizer.step()

        prec1, _ = utils.accuracy(logits, target, topk=(1, 5))
        n = input.size(0)
        objs.update(loss.data.item(), n)
        top1.update(prec1.data.item(), n)

        if step % args.report_freq == 0:
            logging.info('Train Step: %03d Objs: %e Acc: %f', step, objs.avg,
                         top1.avg)

    return top1.avg, objs.avg
def train(train_queue, model, optimizer, gpu):
    objs = utils.AverageTracker()
    top1 = utils.AverageTracker()
    top5 = utils.AverageTracker()
    model.train()

    for step, (input, target) in enumerate(train_queue):
        # input = input.cuda(non_blocking=True)
        # target = target.cuda(non_blocking=True)
        if gpu:
            target = target.cuda(non_blocking=True)

        optimizer.zero_grad()
        dic = model(input, target)
        logits = dic['logits']
        loss = dic['loss']
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip)
        optimizer.step()

        prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
        n = input.size(0)
        objs.update(loss.item(), n)
        top1.update(prec1.item(), n)
        top5.update(prec5.item(), n)

        if step % args.report_freq == 0:
            logging.info('train %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)
        del loss, logits, prec1, prec5, input, target

    return top1.avg, objs.avg
예제 #8
0
def infer(valid_queue, model, criterion):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    model.eval()

    for step, (input, target) in enumerate(valid_queue):
        input = Variable(input, volatile=True).cuda()
        target = Variable(target, volatile=True).cuda(async=True)

        logits = model(input)
        loss = criterion(logits, target)

        prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
        n = input.size(0)
        objs.update(loss.data[0], n)
        top1.update(prec1.data[0], n)
        top5.update(prec5.data[0], n)

        if step % args.report_freq == 0:
            logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg,
                         top5.avg)
            if args.debug:
                break

    return top1.avg, objs.avg
예제 #9
0
def train(train_queue, model, criterion, optimizer):
    objs = utils.AverageMeter()
    top1 = utils.AverageMeter()
    model.train()

    for step, (inp, target) in enumerate(train_queue):
        inp = inp.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)

        model.zero_grad()

        logits = model(inp)
        loss = criterion(logits, target)
        loss.backward()
        optimizer.step()

        prec1, _ = utils.accuracy(logits, target, topk=(1, 5))
        n = inp.size(0)
        objs.update(loss.clone().item(), n)
        top1.update(prec1.clone().item(), n)
        step += 1

        if step % args.report_freq == 0:
            print('Train Step: {:3d} Objs: {:.4f} Acc: {:.2f}'.format(step, objs.avg, top1.avg))

    return top1.avg, objs.avg
def infer(valid_queue, model, criterion):
    objs = utils.AverageMeter()
    top1 = utils.AverageMeter()
    model.eval()

    with torch.no_grad():
        for step, (input, target) in enumerate(valid_queue):
            input = Variable(input)
            target = Variable(target)
            if args.gpu != -1:
                input = input.cuda(non_blocking=True)
                target = target.cuda(non_blocking=True)

            logits = model(input)
            loss = criterion(logits, target)

            prec1 = utils.accuracy(logits, target, topk=(1, ))
            n = input.size(0)
            objs.update(loss.detach().item(), n)
            top1.update(prec1[0].item(), n)

            if step % args.report_freq == 0:
                logging.info('valid %03d %e %f', step, objs.avg, top1.avg)

    return top1.avg, objs.avg
예제 #11
0
def test(test_queue, model, criterion, logger):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()

    with torch.no_grad():
        for step, (input, target) in enumerate(test_queue):
            n = input.size(0)
            input = Variable(input.float()).to(device)
            target = Variable(target.long()).to(device)

            logits = model(input)
            loss = criterion(logits, target)

            prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))

            objs.update(loss.item(), n)
            top1.update(prec1.item(), n)

            if step % args.report_freq == 0:
                logger.info('time = %s, test %03d %e %f',
                            str(utils.get_unix_timestamp()), step, objs.avg,
                            top1.avg)
                print('time = {}, test {} {}'.format(
                    str(utils.get_unix_timestamp()), step, objs.avg))

    return objs.avg, top1.avg
예제 #12
0
def train(train_queue, model, criterion, optimizer, logger):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()

    for step, (input, target) in enumerate(train_queue):
        n = input.size(0)
        input = Variable(input.float(), requires_grad=False).to(device)
        target = Variable(target.long(), requires_grad=False).to(device)
        logits = model(input)
        loss = criterion(logits, target)
        optimizer.zero_grad()
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip)
        optimizer.step()

        prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))

        objs.update(loss.item(), n)
        top1.update(prec1.item(), n)

        if step % args.report_freq == 0:
            logger.info('time = %s, train %03d %e %f',
                        str(utils.get_unix_timestamp()), step, objs.avg,
                        top1.avg)
            print('time = {}, train {} {}'.format(
                str(utils.get_unix_timestamp()), step, objs.avg))

    return objs.avg, top1.avg
def infer(valid_queue, model, criterion, writer_dict):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    model.eval()

    # tensorboard logger
    writer = writer_dict['writer']
    val_step = writer_dict['val_steps']

    for step, (input, target) in enumerate(valid_queue):
        input = Variable(input, volatile=True).cuda()
        # target = Variable(target, volatile=True).cuda(async=True)
        target = Variable(target, volatile=True).cuda()

        logits = model(input)
        loss = criterion(logits, target)
        writer.add_scalar('val_loss', loss.data, val_step)

        prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
        writer.add_scalar('val_prec1', prec1.data, val_step)
        writer.add_scalar('val_prec5', prec5.data, val_step)
        n = input.size(0)
        objs.update(loss.data, n)
        top1.update(prec1.data, n)
        top5.update(prec5.data, n)

        val_step += 1
        writer_dict['val_steps'] += 1

        if step % args.report_freq == 0:
            logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg,
                         top5.avg)

    return top1.avg, objs.avg
예제 #14
0
def infer(valid_queue, model, criterion, temperature, logger=None, batch_num=-1,
          log_frequence=10):
  objs = utils.AvgrageMeter()
  top1 = utils.AvgrageMeter()
  top5 = utils.AvgrageMeter()
  model.eval()
  # logger.info("Start new epoch inference")

  tic = time.time()
  for step, (input, target) in enumerate(valid_queue):
    input = input.cuda()
    target = target.cuda()

    logits , _, _ = model(input , temperature)
    loss = criterion(logits, target)
    prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))

    n = input.size(0)
    objs.update(loss.data , n)
    top1.update(prec1.data , n)
    top5.update(prec5.data , n)
    if logger is not None:
      if step > 0 and step % log_frequence == 0:
        toc = time.time()
        speed = 1.0 * log_frequence * n / (toc - tic)
        if batch_num > 0:
          logger.info("Step[%d/%d] speed[%.4f samples/s] loss[%.6f] acc[%.4f]" % (step, batch_num, 
                          speed, loss.detach().cpu().numpy(), \
                          prec1.detach().cpu().numpy() / 100.0))
        else:
          logger.info("Step[%d] speed[%.4f samples/s] loss[%.6f] acc[%.4f]" % (step, 
                          speed, loss.detach().cpu().numpy(), \
                          prec1.detach().cpu().numpy() / 100.0))
        tic = time.time()
  return top1.avg, top5.avg, objs.avg
def infer(valid_queue, model, criterion, args, gpu):
  '''
  在最后一个epoch后打印验证集计算结果
  '''
  print('正在进行测试!!!!')
  objs = utils.AvgrageMeter()
  top1 = utils.AvgrageMeter()
  top5 = utils.AvgrageMeter()
  model.eval()

  for step, (input, target) in enumerate(valid_queue):
    input = input.cuda(non_blocking=True)
    target = target.cuda(non_blocking=True)
    logits = model(input) # 计算预测结果
    loss = criterion(logits, target)

    prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
    n = input.size(0)
    objs.update(loss.item(), n)
    top1.update(prec1.item(), n)
    top5.update(prec5.item(), n)
    if gpu == 0:
      if step % args.report_freq == 0:
        logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)
        print('valid %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)
  return top1.avg, objs.avg
예제 #16
0
def train(train_queue, model, criterion, optimizer):
    global is_multi_gpu
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    model.train()

    for step, (input, target) in enumerate(train_queue):
        n = input.size(0)
        input = input.cuda()
        target = target.cuda(non_blocking=True)

        optimizer.zero_grad()
        logits, logits_aux = model(input)
        loss = criterion(logits, target)
        if args.auxiliary:
            loss_aux = criterion(logits_aux, target)
            loss += args.auxiliary_weight * loss_aux
        loss.backward()
        parameters = model.module.parameters() if is_multi_gpu else model.parameters()
        nn.utils.clip_grad_norm_(parameters, args.grad_clip)
        optimizer.step()

        prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
        objs.update(loss.item(), n)
        top1.update(prec1.item(), n)
        top5.update(prec5.item(), n)

        if step % args.report_freq == 0:
            logging.info('train %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)
            wandb.log({"evaluation_train_accuracy_avg": objs.avg}, step=step)
            wandb.log({"evaluation_train_accuracy_top1": top1.avg}, step=step)
            wandb.log({"evaluation_train_accuracy_top5": top5.avg}, step=step)

    return top1.avg, objs.avg
예제 #17
0
def infer(valid_queue, model, criterion):
    global is_multi_gpu

    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()

    # nn.BatchNorm layers will use their running stats (in the default mode) and nn.Dropout will be deactivated.
    model.eval()

    for step, (input, target) in enumerate(valid_queue):
        with torch.no_grad():
            input = input.cuda()
            target = target.cuda(non_blocking=True)

            logits, _ = model(input)
            loss = criterion(logits, target)

            prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
            n = input.size(0)
            objs.update(loss.item(), n)
            top1.update(prec1.item(), n)
            top5.update(prec5.item(), n)

            if step % args.report_freq == 0:
                logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)

    return top1.avg, objs.avg
예제 #18
0
def train(train_loader, model, controller, optimizer, args):
    total_loss = search_policies.cnn.utils.AverageMeter()
    total_top1 = search_policies.cnn.utils.AverageMeter()

    for step, (data, target) in enumerate(train_loader):
        if args.debug and step > 10:
            logging.warning("Breaking after 10 steps")
            break

        model.train()
        n = data.size(0)

        data = data.cuda()
        target = target.cuda()

        optimizer.zero_grad()

        controller.eval()
        dag, _, _ = controller()

        logits, _ = model(data, dag)
        loss = F.cross_entropy(logits, target)

        loss.backward()
        optimizer.step()

        prec1 = utils.accuracy(logits, target)[0]
        total_loss.update(loss.item(), n)
        total_top1.update(prec1.item(), n)

        if step % args.report_freq == 0:
            logging.info('train %03d %e %f', step, total_loss.avg,
                         total_top1.avg)

    return total_top1.avg
예제 #19
0
def validate(epoch, val_loader, model, criterion, args):
    batch_time = utils.AverageMeter('Time', ':6.3f')
    losses = utils.AverageMeter('Loss', ':.4e')
    top1 = utils.AverageMeter('Acc@1', ':6.2f')
    top5 = utils.AverageMeter('Acc@5', ':6.2f')

    # switch to evaluation mode
    model.eval()
    with torch.no_grad():
        end = time.time()
        for i, (images, target) in enumerate(val_loader):
            images = images.cuda()
            target = target.cuda()

            # compute output
            logits = model(images)
            loss = criterion(logits, target)

            # measure accuracy and record loss
            pred1, pred5 = utils.accuracy(logits, target, topk=(1, 5))
            n = images.size(0)
            losses.update(loss.item(), n)
            top1.update(pred1[0], n)
            top5.update(pred5[0], n)

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

        logger.info(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'
              .format(top1=top1, top5=top5))

    return losses.avg, top1.avg, top5.avg
예제 #20
0
    def child_test(self, test_queue, model, arch, criterion, verbose=True):
        utils = self.utils
        objs = search_policies.cnn.utils.AverageMeter()
        top1 = search_policies.cnn.utils.AverageMeter()
        top5 = search_policies.cnn.utils.AverageMeter()
        model.eval()
        # arch_l = arch
        arch = self.process_arch(arch)
        with torch.no_grad():
            for step, (input, target) in enumerate(test_queue):
                if self.args.debug:
                    if step > 10:
                        print("Break after 10 batch")
                        break
                input = input.cuda()
                target = target.cuda()
                logits, _ = model(input, arch, bn_train=False)
                loss = criterion(logits, target)

                prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
                n = input.size(0)
                objs.update(loss.item(), n)
                top1.update(prec1.item(), n)
                top5.update(prec5.item(), n)

                if step % self.args.report_freq == 0 and step > 0 and verbose:
                    logging.info(
                        'test | step %03d | loss %e | acc %f | acc-5 %f', step,
                        objs.avg, top1.avg, top5.avg)

        return top1.avg, top5.avg, objs.avg
def infer(valid_queue, model, gpu):
    objs = utils.AverageTracker()
    top1 = utils.AverageTracker()
    top5 = utils.AverageTracker()
    model.eval()
    with torch.no_grad():
        for step, (input, target) in enumerate(valid_queue):
            # input = input.cuda(non_blocking=True)
            if gpu:
                target = target.cuda(non_blocking=True)

            dic = model(input, target)
            logits = dic['logits']
            loss = dic['loss']

            prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
            n = input.size(0)
            assert isinstance(n, int)
            objs.update(loss.item(), n)
            top1.update(prec1.item(), n)
            top5.update(prec5.item(), n)

            if step % args.report_freq == 0:
                logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)
            del loss, logits, prec1, prec5, input, target

    return top1.avg, top5.avg, objs.avg
예제 #22
0
def train(train_queue, model, optimizer, global_step, criterion):
    objs = search_policies.cnn.utils.AverageMeter()
    top1 = search_policies.cnn.utils.AverageMeter()
    top5 = search_policies.cnn.utils.AverageMeter()
    model.train()
    for step, (input, target) in enumerate(train_queue):
        input = input.cuda().requires_grad_()
        target = target.cuda()
    
        optimizer.zero_grad()
        logits, aux_logits = model(input, global_step)
        global_step += 1
        loss = criterion(logits, target)
        if aux_logits is not None:
            aux_loss = criterion(aux_logits, target)
            loss += 0.4 * aux_loss
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), args.grad_bound)
        optimizer.step()
    
        prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
        n = input.size(0)
        objs.update(loss.data, n)
        top1.update(prec1.data, n)
        top5.update(prec5.data, n)
    
        if (step+1) % 100 == 0:
            logging.info('train %03d loss %e top1 %f top5 %f', step+1, objs.avg, top1.avg, top5.avg)

    return top1.avg, objs.avg, global_step
예제 #23
0
def infer(valid_loader, model, controller, criterion):
    total_loss = utils.AvgrageMeter()
    total_top1 = utils.AvgrageMeter()
    model.eval()
    controller.eval()

    with torch.no_grad():
        for step in range(20):
            data, target = valid_loader.next_batch()
            data = data.cuda()
            target = target.cuda()

            dag, _, _ = controller()

            logits, auxs = model(dag, data)
            loss = criterion(logits, target).cuda()

            prec1 = utils.accuracy(logits, target)[0]
            n = data.size(0)
            total_loss.update(loss.item(), n)
            total_top1.update(prec1.item(), n)

            logging.info('valid  {:0>3d} {:.6f} {:.3f}'.format(
                step, loss.item(), prec1.item()))
            logging.info('{}'.format([i for i in dag]))
            with open(os.path.join(args.save, 'dag_all.txt'), 'a') as f:
                f.write('{:.3f} {} infer\n'.format(prec1.item(),
                                                   [i for i in dag]))  #
            del loss, logits

    return total_top1.avg
예제 #24
0
def infer(valid_queue, model, criterion):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    model.eval()

    with torch.no_grad():
        for step, (input, target) in enumerate(valid_queue):
            input = input.cuda()
            target = target.cuda(non_blocking=True)

            logits, _ = model(input)
            loss = criterion(logits, target)

            prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
            n = input.size(0)
            objs.update(loss.data, n)
            top1.update(prec1.data, n)
            top5.update(prec5.data, n)

            if step % args.report_freq == 0:
                end_time = time.time()
                if step == 0:
                    duration = 0
                    start_time = time.time()
                else:
                    duration = end_time - start_time
                    start_time = time.time()
                logging.info(
                    'VALID Step: %03d Objs: %e R1: %f R5: %f Duration: %ds',
                    step, objs.avg, top1.avg, top5.avg, duration)

    return top1.avg, top5.avg, objs.avg
예제 #25
0
def infer(valid_queue, model, epoch, criterion, writer, model_name):
    batch_time = utils.AverageMeters('Time', ':6.3f')
    losses = utils.AverageMeters('Loss', ':.4e')
    top1 = utils.AverageMeters('Acc@1', ':6.2f')
    top5 = utils.AverageMeters('Acc@5', ':6.2f')
    model.eval()

    progress = utils.ProgressMeter(len(valid_queue),
                                   batch_time,
                                   losses,
                                   top1,
                                   top5,
                                   prefix='Test: ')
    cur_step = epoch * len(valid_queue)

    end = time.time()
    for step, (input, target) in enumerate(valid_queue):
        #input = input.cuda()
        #target = target.cuda(non_blocking=True)
        input = Variable(input, volatile=True).cuda()
        #target = Variable(target, volatile=True).cuda(async=True)
        target = Variable(target, volatile=True).cuda()
        if model_name == "ShuffleNas":
            block_choices = model.module.random_block_choices(
                select_predefined_block=False)
            ignore_first_two_cs = True  # 0.2 and 0.4 scales are ignored
            if config.cs_warm_up:
                full_channel_mask, _ = model.module.random_channel_mask(
                    select_all_channels=config.use_all_channels,
                    epoch_after_cs=epoch - config.epoch_start_cs,
                    ignore_first_two_cs=ignore_first_two_cs)
            else:
                full_channel_mask, _ = model.module.random_channel_mask(
                    select_all_channels=config.use_all_channels)
            logits = model(input, block_choices, full_channel_mask)
        else:
            logits = model(input, None, None)
        loss = criterion(logits, target)
        acc1, acc5 = utils.accuracy(logits, target, topk=(1, 5))
        n = input.size(0)
        reduced_loss = reduce_tensor(loss.data, world_size=config.world_size)
        acc1 = reduce_tensor(acc1, world_size=config.world_size)
        acc5 = reduce_tensor(acc5, world_size=config.world_size)
        losses.update(to_python_float(reduced_loss), n)
        top1.update(to_python_float(acc1), n)
        top5.update(to_python_float(acc5), n)

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if step % config.print_freq == 0:
            progress.print(step)
            logger.info('valid %03d %e %f %f', step, losses.avg, top1.avg,
                        top5.avg)

    writer.add_scalar('val/loss', losses.avg, cur_step)
    writer.add_scalar('val/top1', top1.avg, cur_step)
    writer.add_scalar('val/top5', top5.avg, cur_step)
    return top1.avg, losses.avg
예제 #26
0
def train(train_queue, model, criterion, optimizer):
    objs = utils.AverageMeter()
    top1 = utils.AverageMeter()
    model.train()

    for step, (input, target) in enumerate(train_queue):
        input = input.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)

        model.zero_grad()
        model.generate_share_alphas()

        logits = model(input)
        loss = criterion(logits, target)
        loss.backward()
        if args.grad_clip > 0:
            nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip)
        optimizer.step()

        prec1, _ = utils.accuracy(logits, target, topk=(1, 5))
        n = input.size(0)
        objs.update(loss.clone().item(), n)
        top1.update(prec1.clone().item(), n)

        if (step + 1) % args.report_freq == 0:
            logging.info('Train Step: %03d Objs: %e Acc: %.2f', step + 1,
                         objs.avg, top1.avg)

    return top1.avg, objs.avg
def infer(valid_queue, model, criterion):
    global is_multi_gpu

    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    model.eval()

    for step, (input, target) in enumerate(valid_queue):
        input = input.cuda()
        target = target.cuda()

        logits = model(input)
        loss = criterion(logits, target)

        prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
        n = input.size(0)
        objs.update(loss.item(), n)
        top1.update(prec1.item(), n)
        top5.update(prec5.item(), n)

        if step % args.report_freq == 0:
            logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg,
                         top5.avg)

    return top1.avg, objs.avg, loss
예제 #28
0
파일: train.py 프로젝트: chomin/BayesNAS
def infer(valid_queue, model, criterion):
    objs = utils.AvgrageMeter()
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    model.eval()

    with torch.no_grad():
        for step, (input, target) in enumerate(valid_queue):
            # input = Variable(input, volatile=True).cuda()
            # target = Variable(target, volatile=True).cuda(async=True)
            input = input.to(device)
            target = target.to(device)

            logits, _ = model(input)
            loss = criterion(logits, target)

            prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
            n = input.size(0)
            loss_data = loss.data[0] if loss.dim() != 0 else loss.item()
            prec1_data = prec1.data[0] if prec1.dim() != 0 else prec1.item()
            prec5_data = prec5.data[0] if prec5.dim() != 0 else prec5.item()
            objs.update(loss_data, n)
            top1.update(prec1_data, n)
            top5.update(prec5_data, n)

            if step % args.report_freq == 0:
                logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg,
                             top5.avg)

    return top1.avg, objs.avg
def train(train_queue, model, criterion, optimizer):
    objs = utils.AverageMeter()
    top1 = utils.AverageMeter()
    model.train()

    for step, (inp, target) in enumerate(train_queue):
        global global_step
        global_step += 1
        inp = inp.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)

        model.zero_grad()

        logits = model(inp)
        loss = criterion(logits, target)
        loss.backward()
        if args.grad_clip > 0:
            nn.utils.clip_grad_norm_(model.parameters(), args.grad_clip)
        optimizer.step()

        prec1, _ = utils.accuracy(logits, target, topk=(1, 5))
        n = inp.size(0)
        objs.update(loss.clone().item(), n)
        top1.update(prec1.clone().item(), n)
        writer_tr.add_scalar('loss', loss.item(), global_step)
        writer_tr.add_scalar('acc', prec1.item(), global_step)
        decomp_loss_1 = torch.mean(torch.log(torch.sum(torch.exp(logits - torch.max(logits, 1)[0].view(-1, 1)), 1))).item()
        writer_tr.add_scalar('decomp_loss_lgsmex', decomp_loss_1, global_step)
        decomp_loss_2 = torch.mean(torch.max(logits, 1)[0] - logits[range(len(inp)), target.cpu().numpy()]).item()
        writer_tr.add_scalar('decomp_loss_max', decomp_loss_2, global_step)

        if (step + 1) % args.report_freq == 0:
            logging.info('Train Step: %03d Objs: %e Acc: %.2f', step + 1, objs.avg, top1.avg)

    return top1.avg, objs.avg
예제 #30
0
def infer(valid_queue, model, alpha, criterion):
    """Run model in eval only mode."""
    objs = utils.AverageMeter()
    top1 = utils.AverageMeter()
    top5 = utils.AverageMeter()
    # model.eval()

    with torch.no_grad():
        for step, (data, target) in enumerate(valid_queue):
            n = data.size(0)
            data = data.cuda()
            target = target.cuda()

            weights = alpha(data.size(0))
            logits = model(data, weights)
            loss = criterion(logits, target)

            # Calculate the accuracy.
            prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))

            objs.update(loss.data, n)
            top1.update(prec1.data, n)
            top5.update(prec5.data, n)

            if step % args.report_freq == 0 or step == len(valid_queue) - 1:
                objs_avg = utils.reduce_tensor(objs.avg, args.world_size)
                top1_avg = utils.reduce_tensor(top1.avg, args.world_size)
                top5_avg = utils.reduce_tensor(top5.avg, args.world_size)
                logging.info('valid %03d %e %f %f', step, objs_avg, top1_avg,
                             top5_avg)

    return top1_avg, objs_avg