trainer.py
def _valid_epoch(self, epoch, phase='valid'):
        """Runs validation phase on either the validation or test set"""
        total_loss = utils.AveTracker()
        top1_acc = utils.AveTracker()
        top5_acc = utils.AveTracker()
        self.model.eval()

        if phase == 'test':
            self.logger.info('Running test set inference...')

        loader = self.valid_loader if phase == 'valid' else self.test_loader

        with torch.no_grad():
            for step, (x, y) in enumerate(loader):
                x = x.to(self.device, non_blocking=True)
                y = y.to(self.device, non_blocking=True)

                logits = self.model(x)
                loss = self.criterion(logits, y)

                prec1, prec5 = utils.accuracy(logits, y, topk=(1, 5))
                n = x.size(0)
                total_loss.update(loss.item(), n)
                top1_acc.update(prec1.item(), n)
                top5_acc.update(prec5.item(), n)

                if step % 100 == 0:
                    self.logger.info(
                        '{} {:04d} {:e} {:f} {:f}'.format(
                            phase, step, total_loss.average, top1_acc.average, top5_acc.average
                        )
                    )

        return top1_acc.average, top5_acc.average, total_loss.average
Runs validation phase on either the validation or test set
Runs validation phase on either the validation or test set
Runs validation phase on either the validation or test set
def _valid_epoch(self, epoch, phase='valid'):
        total_loss = utils.AveTracker()
        top1_acc = utils.AveTracker()
        top5_acc = utils.AveTracker()
        self.model.eval()
        if phase == 'test':
            self.logger.info('Running test set inference...')
        loader = self.valid_loader if phase == 'valid' else self.test_loader
        with torch.no_grad():
            for step, (x, y) in enumerate(loader):
                x = x.to(self.device, non_blocking=True)
                y = y.to(self.device, non_blocking=True)
                logits = self.model(x)
                loss = self.criterion(logits, y)
                prec1, prec5 = utils.accuracy(logits, y, topk=(1, 5))
                n = x.size(0)
                total_loss.update(loss.item(), n)
                top1_acc.update(prec1.item(), n)
                top5_acc.update(prec5.item(), n)
                if step % 100 == 0:
                    self.logger.info(
                        '{} {:04d} {:e} {:f} {:f}'.format(
                            phase, step, total_loss.average, top1_acc.average, top5_acc.average
                        )
                    )
        return top1_acc.average, top5_acc.average, total_loss.average
def _valid_epoch ( self , epoch , phase = 'valid' ) : total_loss = utils . AveTracker ( ) top1_acc = utils . AveTracker ( ) top5_acc = utils . AveTracker ( ) self . model . eval ( ) if phase == 'test' : self . logger . info ( 'Running test set inference...' ) loader = self . valid_loader if phase == 'valid' else self . test_loader with torch . no_grad ( ) : for step , ( x , y ) in enumerate ( loader ) : x = x . to ( self . device , non_blocking = True ) y = y . to ( self . device , non_blocking = True ) logits = self . model ( x ) loss = self . criterion ( logits , y ) prec1 , prec5 = utils . accuracy ( logits , y , topk = ( 1 , 5 ) ) n = x . size ( 0 ) total_loss . update ( loss . item ( ) , n ) top1_acc . update ( prec1 . item ( ) , n ) top5_acc . update ( prec5 . item ( ) , n ) if step % 100 == 0 : self . logger . info ( '{} {:04d} {:e} {:f} {:f}' . format ( phase , step , total_loss . average , top1_acc . average , top5_acc . average ) ) return top1_acc . average , top5_acc . average , total_loss . average
Runs validation phase on either the validation or test set
Runs validation phase on either the validation or test set
"""Runs validation phase on either the validation or test set"""
self
epoch
phase