torchreid/engine/engine.py
def run(self, aim_sess, save_dir='log', max_epoch=0, start_epoch=0, fixbase_epoch=0, open_layers=None,
            start_eval=0, eval_freq=-1, test_only=False, print_freq=10,
            dist_metric='euclidean', normalize_feature=False, visrank=False, visrankactiv=False, visrankactivthr=False,
            maskthr=0.7, visrank_topk=10, use_metric_cuhk03=False, ranks=[1, 5, 10, 20], rerank=False, visactmap=False,
            vispartmap=False, visdrop=False, visdroptype='random'):
        """A unified pipeline for training and evaluating a model.
        :param aim_sess: aim recorder
        :param save_dir: directory to save model.
        :param max_epoch: maximum epoch.
        :param start_epoch: (int, optional) starting epoch. Default is 0.
        :param fixbase_epoch: (int, optional) number of epochs to train ``open_layers`` (new layers)
                              while keeping base layers frozen. Default is 0. ``fixbase_epoch`` is counted
                              in ``max_epoch``.
        :param open_layers: (str or list, optional) layers (attribute names) open for training.
                            start_eval (int, optional): from which epoch to start evaluation. Default is 0.
                            eval_freq (int, optional): evaluation frequency. Default is -1 (meaning evaluation
                            is only performed at the end of training).
        :param start_eval:
        :param eval_freq:
        :param test_only: (bool, optional) if True, only runs evaluation on test datasets. Default is False.
        :param print_freq: (int, optional) print_frequency. Default is 10.
        :param dist_metric: (str, optional) distance metric used to compute distance matrix between query and gallery.
                            Default is "euclidean".
        :param normalize_feature: (bool, optional) performs L2 normalization on feature vectors before
                                  computing feature distance. Default is False.
        :param visrank: (bool, optional) visualizes ranked results. Default is False. It is recommended to
                        enable ``visrank`` when ``test_only`` is True. The ranked images will be saved to
                        "save_dir/visrank_dataset", e.g. "save_dir/visrank_market1501".
        :param visrankactiv:
        :param visrankactivthr:
        :param maskthr:
        :param visrank_topk: (int, optional) top-k ranked images to be visualized. Default is 10.
                             use_metric_cuhk03 (bool, optional): use single-gallery-shot setting for cuhk03.
                             Default is False. This should be enabled when using cuhk03 classic split.
        :param use_metric_cuhk03:
        :param ranks: (list, optional) cmc ranks to be computed. Default is [1, 5, 10, 20].
        :param rerank: (bool, optional) uses person re-ranking (by Zhong et al. CVPR'17).
                       Default is False. This is only enabled when test_only=True.
        :param visactmap: (bool, optional) visualizes activation maps. Default is False.
        :param vispartmap:
        :param visdrop:
        :param visdroptype:
        :return:
        """
        if visrank and not test_only:
            raise ValueError('visrank=True is valid only if test_only=True')

        if visrankactiv and not test_only:
            raise ValueError('visrankactiv=True is valid only if test_only=True')

        if visrankactivthr and not test_only:
            raise ValueError('visrankactivthr=True is valid only if test_only=True')

        if visdrop and not test_only:
            raise ValueError('visdrop=True is valid only if test_only=True')

        if test_only:
            self.test(
                0,
                dist_metric=dist_metric,
                normalize_feature=normalize_feature,
                visrank=visrank,
                visrankactiv=visrankactiv,
                visrank_topk=visrank_topk,
                save_dir=save_dir,
                use_metric_cuhk03=use_metric_cuhk03,
                ranks=ranks,
                rerank=rerank,
                maskthr=maskthr,
                visrankactivthr=visrankactivthr,
                visdrop=visdrop,
                visdroptype=visdroptype
            )
            return

        if self.writer is None:
            self.writer = SummaryWriter(log_dir=save_dir)

        if visactmap:
            self.visactmap(self.test_loader, save_dir, self.datamanager.width, self.datamanager.height, print_freq)
            return

        if vispartmap:
            self.vispartmap(self.test_loader, save_dir, self.datamanager.width, self.datamanager.height, print_freq)
            return

        time_start = time.time()
        self.max_epoch = max_epoch
        print('=> Start training')

        for epoch in range(start_epoch, max_epoch):
            losses = self.train(print_freq=print_freq, fixbase_epoch=fixbase_epoch,
                                open_layers=open_layers, epoch=epoch)

            # AIM recorder (acc, loss)
            for key in losses.meters.keys():
                aim_sess.track(losses.meters[key].avg, name=key, epoch=epoch, subset='train')
            
            if (epoch+1) >= start_eval and eval_freq > 0 and (epoch+1) % eval_freq == 0 and (epoch+1) != max_epoch:
                rank1 = self.test(
                    epoch,
                    aim_sess=aim_sess,
                    dist_metric=dist_metric,
                    normalize_feature=normalize_feature,
                    visrank=visrank,
                    visrankactiv=visrankactiv,
                    visrank_topk=visrank_topk,
                    save_dir=save_dir,
                    use_metric_cuhk03=use_metric_cuhk03,
                    ranks=ranks,
                    rerank=rerank,
                    maskthr=maskthr,
                    visrankactivthr=visrankactivthr
                )
                self._save_checkpoint(epoch, rank1, save_dir)

        if max_epoch > 0:
            print('=> Final test')
            rank1 = self.test(
                epoch,
                dist_metric=dist_metric,
                normalize_feature=normalize_feature,
                visrank=visrank,
                visrankactiv=visrankactiv,
                visrank_topk=visrank_topk,
                save_dir=save_dir,
                use_metric_cuhk03=use_metric_cuhk03,
                ranks=ranks,
                rerank=rerank,
                maskthr=maskthr,
                visrankactivthr=visrankactivthr
            )
            self._save_checkpoint(epoch, rank1, save_dir)

        elapsed = round(time.time() - time_start)
        elapsed = str(datetime.timedelta(seconds=elapsed))
        print('Elapsed {}'.format(elapsed))
        if self.writer is None:
            self.writer.close()
A unified pipeline for training and evaluating a model.
        :param aim_sess: aim recorder
        :param save_dir: directory to save model.
        :param max_epoch: maximum epoch.
        :param start_epoch: (int, optional) starting epoch. Default is 0.
        :param fixbase_epoch: (int, optional) number of epochs to train ``open_layers`` (new layers)
                              while keeping base layers frozen. Default is 0. ``fixbase_epoch`` is counted
                              in ``max_epoch``.
        :param open_layers: (str or list, optional) layers (attribute names) open for training.
                            start_eval (int, optional): from which epoch to start evaluation. Default is 0.
                            eval_freq (int, optional): evaluation frequency. Default is -1 (meaning evaluation
                            is only performed at the end of training).
        :param start_eval:
        :param eval_freq:
        :param test_only: (bool, optional) if True, only runs evaluation on test datasets. Default is False.
        :param print_freq: (int, optional) print_frequency. Default is 10.
        :param dist_metric: (str, optional) distance metric used to compute distance matrix between query and gallery.
                            Default is "euclidean".
        :param normalize_feature: (bool, optional) performs L2 normalization on feature vectors before
                                  computing feature distance. Default is False.
        :param visrank: (bool, optional) visualizes ranked results. Default is False. It is recommended to
                        enable ``visrank`` when ``test_only`` is True. The ranked images will be saved to
                        "save_dir/visrank_dataset", e.g. "save_dir/visrank_market1501".
        :param visrankactiv:
        :param visrankactivthr:
        :param maskthr:
        :param visrank_topk: (int, optional) top-k ranked images to be visualized. Default is 10.
                             use_metric_cuhk03 (bool, optional): use single-gallery-shot setting for cuhk03.
                             Default is False. This should be enabled when using cuhk03 classic split.
        :param use_metric_cuhk03:
        :param ranks: (list, optional) cmc ranks to be computed. Default is [1, 5, 10, 20].
        :param rerank: (bool, optional) uses person re-ranking (by Zhong et al. CVPR'17).
                       Default is False. This is only enabled when test_only=True.
        :param visactmap: (bool, optional) visualizes activation maps. Default is False.
        :param vispartmap:
        :param visdrop:
        :param visdroptype:
        :return:
        
A unified pipeline for training and evaluating a model.
A unified pipeline for training and evaluating a model .
def run(self, aim_sess, save_dir='log', max_epoch=0, start_epoch=0, fixbase_epoch=0, open_layers=None,
            start_eval=0, eval_freq=-1, test_only=False, print_freq=10,
            dist_metric='euclidean', normalize_feature=False, visrank=False, visrankactiv=False, visrankactivthr=False,
            maskthr=0.7, visrank_topk=10, use_metric_cuhk03=False, ranks=[1, 5, 10, 20], rerank=False, visactmap=False,
            vispartmap=False, visdrop=False, visdroptype='random'):
        if visrank and not test_only:
            raise ValueError('visrank=True is valid only if test_only=True')
        if visrankactiv and not test_only:
            raise ValueError('visrankactiv=True is valid only if test_only=True')
        if visrankactivthr and not test_only:
            raise ValueError('visrankactivthr=True is valid only if test_only=True')
        if visdrop and not test_only:
            raise ValueError('visdrop=True is valid only if test_only=True')
        if test_only:
            self.test(
                0,
                dist_metric=dist_metric,
                normalize_feature=normalize_feature,
                visrank=visrank,
                visrankactiv=visrankactiv,
                visrank_topk=visrank_topk,
                save_dir=save_dir,
                use_metric_cuhk03=use_metric_cuhk03,
                ranks=ranks,
                rerank=rerank,
                maskthr=maskthr,
                visrankactivthr=visrankactivthr,
                visdrop=visdrop,
                visdroptype=visdroptype
            )
            return
        if self.writer is None:
            self.writer = SummaryWriter(log_dir=save_dir)
        if visactmap:
            self.visactmap(self.test_loader, save_dir, self.datamanager.width, self.datamanager.height, print_freq)
            return
        if vispartmap:
            self.vispartmap(self.test_loader, save_dir, self.datamanager.width, self.datamanager.height, print_freq)
            return
        time_start = time.time()
        self.max_epoch = max_epoch
        print('=> Start training')
        for epoch in range(start_epoch, max_epoch):
            losses = self.train(print_freq=print_freq, fixbase_epoch=fixbase_epoch,
                                open_layers=open_layers, epoch=epoch)
            for key in losses.meters.keys():
                aim_sess.track(losses.meters[key].avg, name=key, epoch=epoch, subset='train')
            if (epoch+1) >= start_eval and eval_freq > 0 and (epoch+1) % eval_freq == 0 and (epoch+1) != max_epoch:
                rank1 = self.test(
                    epoch,
                    aim_sess=aim_sess,
                    dist_metric=dist_metric,
                    normalize_feature=normalize_feature,
                    visrank=visrank,
                    visrankactiv=visrankactiv,
                    visrank_topk=visrank_topk,
                    save_dir=save_dir,
                    use_metric_cuhk03=use_metric_cuhk03,
                    ranks=ranks,
                    rerank=rerank,
                    maskthr=maskthr,
                    visrankactivthr=visrankactivthr
                )
                self._save_checkpoint(epoch, rank1, save_dir)
        if max_epoch > 0:
            print('=> Final test')
            rank1 = self.test(
                epoch,
                dist_metric=dist_metric,
                normalize_feature=normalize_feature,
                visrank=visrank,
                visrankactiv=visrankactiv,
                visrank_topk=visrank_topk,
                save_dir=save_dir,
                use_metric_cuhk03=use_metric_cuhk03,
                ranks=ranks,
                rerank=rerank,
                maskthr=maskthr,
                visrankactivthr=visrankactivthr
            )
            self._save_checkpoint(epoch, rank1, save_dir)
        elapsed = round(time.time() - time_start)
        elapsed = str(datetime.timedelta(seconds=elapsed))
        print('Elapsed {}'.format(elapsed))
        if self.writer is None:
            self.writer.close()
def run ( self , aim_sess , save_dir = 'log' , max_epoch = 0 , start_epoch = 0 , fixbase_epoch = 0 , open_layers = None , start_eval = 0 , eval_freq = - 1 , test_only = False , print_freq = 10 , dist_metric = 'euclidean' , normalize_feature = False , visrank = False , visrankactiv = False , visrankactivthr = False , maskthr = 0.7 , visrank_topk = 10 , use_metric_cuhk03 = False , ranks = [ 1 , 5 , 10 , 20 ] , rerank = False , visactmap = False , vispartmap = False , visdrop = False , visdroptype = 'random' ) : if visrank and not test_only : raise ValueError ( 'visrank=True is valid only if test_only=True' ) if visrankactiv and not test_only : raise ValueError ( 'visrankactiv=True is valid only if test_only=True' ) if visrankactivthr and not test_only : raise ValueError ( 'visrankactivthr=True is valid only if test_only=True' ) if visdrop and not test_only : raise ValueError ( 'visdrop=True is valid only if test_only=True' ) if test_only : self . test ( 0 , dist_metric = dist_metric , normalize_feature = normalize_feature , visrank = visrank , visrankactiv = visrankactiv , visrank_topk = visrank_topk , save_dir = save_dir , use_metric_cuhk03 = use_metric_cuhk03 , ranks = ranks , rerank = rerank , maskthr = maskthr , visrankactivthr = visrankactivthr , visdrop = visdrop , visdroptype = visdroptype ) return if self . writer is None : self . writer = SummaryWriter ( log_dir = save_dir ) if visactmap : self . visactmap ( self . test_loader , save_dir , self . datamanager . width , self . datamanager . height , print_freq ) return if vispartmap : self . vispartmap ( self . test_loader , save_dir , self . datamanager . width , self . datamanager . height , print_freq ) return time_start = time . time ( ) self . max_epoch = max_epoch print ( '=> Start training' ) for epoch in range ( start_epoch , max_epoch ) : losses = self . train ( print_freq = print_freq , fixbase_epoch = fixbase_epoch , open_layers = open_layers , epoch = epoch ) for key in losses . meters . keys ( ) : aim_sess . track ( losses . meters [ key ] . avg , name = key , epoch = epoch , subset = 'train' ) if ( epoch + 1 ) >= start_eval and eval_freq > 0 and ( epoch + 1 ) % eval_freq == 0 and ( epoch + 1 ) != max_epoch : rank1 = self . test ( epoch , aim_sess = aim_sess , dist_metric = dist_metric , normalize_feature = normalize_feature , visrank = visrank , visrankactiv = visrankactiv , visrank_topk = visrank_topk , save_dir = save_dir , use_metric_cuhk03 = use_metric_cuhk03 , ranks = ranks , rerank = rerank , maskthr = maskthr , visrankactivthr = visrankactivthr ) self . _save_checkpoint ( epoch , rank1 , save_dir ) if max_epoch > 0 : print ( '=> Final test' ) rank1 = self . test ( epoch , dist_metric = dist_metric , normalize_feature = normalize_feature , visrank = visrank , visrankactiv = visrankactiv , visrank_topk = visrank_topk , save_dir = save_dir , use_metric_cuhk03 = use_metric_cuhk03 , ranks = ranks , rerank = rerank , maskthr = maskthr , visrankactivthr = visrankactivthr ) self . _save_checkpoint ( epoch , rank1 , save_dir ) elapsed = round ( time . time ( ) - time_start ) elapsed = str ( datetime . timedelta ( seconds = elapsed ) ) print ( 'Elapsed {}' . format ( elapsed ) ) if self . writer is None : self . writer . close ( )
A unified pipeline for training and evaluating a model.
A unified pipeline for training and evaluating a model .
"""A unified pipeline for training and evaluating a model.
        :param aim_sess: aim recorder
        :param save_dir: directory to save model.
        :param max_epoch: maximum epoch.
        :param start_epoch: (int, optional) starting epoch. Default is 0.
        :param fixbase_epoch: (int, optional) number of epochs to train ``open_layers`` (new layers)
                              while keeping base layers frozen. Default is 0. ``fixbase_epoch`` is counted
                              in ``max_epoch``.
        :param open_layers: (str or list, optional) layers (attribute names) open for training.
                            start_eval (int, optional): from which epoch to start evaluation. Default is 0.
                            eval_freq (int, optional): evaluation frequency. Default is -1 (meaning evaluation
                            is only performed at the end of training).
        :param start_eval:
        :param eval_freq:
        :param test_only: (bool, optional) if True, only runs evaluation on test datasets. Default is False.
        :param print_freq: (int, optional) print_frequency. Default is 10.
        :param dist_metric: (str, optional) distance metric used to compute distance matrix between query and gallery.
                            Default is "euclidean".
        :param normalize_feature: (bool, optional) performs L2 normalization on feature vectors before
                                  computing feature distance. Default is False.
        :param visrank: (bool, optional) visualizes ranked results. Default is False. It is recommended to
                        enable ``visrank`` when ``test_only`` is True. The ranked images will be saved to
                        "save_dir/visrank_dataset", e.g. "save_dir/visrank_market1501".
        :param visrankactiv:
        :param visrankactivthr:
        :param maskthr:
        :param visrank_topk: (int, optional) top-k ranked images to be visualized. Default is 10.
                             use_metric_cuhk03 (bool, optional): use single-gallery-shot setting for cuhk03.
                             Default is False. This should be enabled when using cuhk03 classic split.
        :param use_metric_cuhk03:
        :param ranks: (list, optional) cmc ranks to be computed. Default is [1, 5, 10, 20].
        :param rerank: (bool, optional) uses person re-ranking (by Zhong et al. CVPR'17).
                       Default is False. This is only enabled when test_only=True.
        :param visactmap: (bool, optional) visualizes activation maps. Default is False.
        :param vispartmap:
        :param visdrop:
        :param visdroptype:
        :return:
        """ # AIM recorder (acc, loss)
None
self
aim_sess
None
save_dir
directory to save model.
directory to save model .
max_epoch
None
start_epoch
(int, optional) starting epoch. Default is 0.
( int optional ) starting epoch . Default is 0 .
fixbase_epoch
(int, optional) number of epochs to train ``open_layers`` (new layers)
while keeping base layers frozen. Default is 0. ``fixbase_epoch`` is counted
in ``max_epoch``.
( int optional ) number of epochs to train ` ` open_layers ` ` ( new layers ) while keeping base layers frozen . Default is 0 . ` ` fixbase_epoch ` ` is counted in ` ` max_epoch ` ` .
open_layers
(str or list, optional) layers (attribute names) open for training.
start_eval (int, optional): from which epoch to start evaluation. Default is 0.
eval_freq (int, optional): evaluation frequency. Default is -1 (meaning evaluation
is only performed at the end of training).
( str or list optional ) layers ( attribute names ) open for training . start_eval ( int optional ) : from which epoch to start evaluation . Default is 0 . eval_freq ( int optional ) : evaluation frequency . Default is - 1 ( meaning evaluation is only performed at the end of training ) .
start_eval
None
eval_freq
None
test_only
(bool, optional) if True, only runs evaluation on test datasets. Default is False.
( bool optional ) if True only runs evaluation on test datasets . Default is False .
print_freq
(int, optional) print_frequency. Default is 10.
( int optional ) print_frequency . Default is 10 .
dist_metric
(str, optional) distance metric used to compute distance matrix between query and gallery.
Default is "euclidean".
( str optional ) distance metric used to compute distance matrix between query and gallery . Default is " euclidean " .
normalize_feature
(bool, optional) performs L2 normalization on feature vectors before
computing feature distance. Default is False.
( bool optional ) performs L2 normalization on feature vectors before computing feature distance . Default is False .
visrank
(bool, optional) visualizes ranked results. Default is False. It is recommended to
enable ``visrank`` when ``test_only`` is True. The ranked images will be saved to
"save_dir/visrank_dataset", e.g.
( bool optional ) visualizes ranked results . Default is False . It is recommended to enable ` ` visrank ` ` when ` ` test_only ` ` is True . The ranked images will be saved to " save_dir / visrank_dataset " e . g .
visrankactiv
None
visrankactivthr
None
maskthr
None
visrank_topk
(int, optional) top-k ranked images to be visualized. Default is 10.
use_metric_cuhk03 (bool, optional): use single-gallery-shot setting for cuhk03.
Default is False. This should be enabled when using cuhk03 classic split.
( int optional ) top - k ranked images to be visualized . Default is 10 . use_metric_cuhk03 ( bool optional ) : use single - gallery - shot setting for cuhk03 . Default is False . This should be enabled when using cuhk03 classic split .
use_metric_cuhk03
None
ranks
(list, optional) cmc ranks to be computed.
( list optional ) cmc ranks to be computed .
rerank
(bool, optional) uses person re-ranking (by Zhong et al.
( bool optional ) uses person re - ranking ( by Zhong et al .
visactmap
(bool, optional) visualizes activation maps. Default is False.
( bool optional ) visualizes activation maps . Default is False .
vispartmap
None
visdrop
None
visdroptype
None