mirror of
https://github.com/open-mmlab/mmfewshot.git
synced 2025-06-03 14:49:43 +08:00
* bug fix * fix comments * fix doc str * seprate the cls and det * update script * fix commnets * fix commnets * fix commnets test 0 loss fix commnets * update tools * rename detectors * rename detectors * rename detectors * rename detectors * del frcnn config * update doc str * update config * fix comments * fix comments * update config * update config * update config * update config * fix comments * fix comments * fix comments * fix comments * add mpsr * update config * update config * test bug voc data * mv vis to misc * classification framwork * add fewshot classification methods * set before method static * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * add blank line for bar * update config * update config * update config * update config * update config * update config * update config * update config * Revert "update config" This reverts commit a71c6aae288f282887c8d75875b82333ada20658. * Revert "update config" This reverts commit d21d5bcccf00195968ccd7ea1952672a54e13ac1. * Revert "update config" This reverts commit 1786c07902ec885f0f456cca39cd47872c5eaf64. * Revert "update config" This reverts commit 27efc60b04aa96c45c5ee02307014d51518b6805. * update config * update config * update config * update config * update config * update config * update config * update config * add resnet12 * update config * update config * update config * update config * update config * update config and backbone * update config and backbone * update config and backbone * update config and backbone * update config and backbone * update config and backbone * fix dataset sampling * fix dataset sampling * add baseline config * update baseline config * update baseline++ config * update baseline++ config * update baseline++ config * update baseline config * fix pipeline * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update test backbone * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * fix maml bug * fix maml bug * update config * fix maml bug * update config * update config * update config * fix maml bug * update config * update config * update config * update config * update config * update config * add tiered imagenet * update config * update config * update config * update config * add dataloader * add dataloader * add dataloader * add dataloader * add dataloader * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * add doc * add doc * fix tiered imagenet * fix tiered imagenet * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * fix comments * fix comments * update config * update config * update config * update config * update config * update config * fix eval prior * update relation net * update config * update config * update config * update config * update config * update config * update config * update config * update config * update config * update doc str * update doc str * update doc str * update config * update config * update config * update config * update doc str * add few shot classification * update meta baseline config * add few shot classification * update script * update script * update script * update script * update script * update config * update config * update config * update config * update margin to 0 * update config * fix comments * fix comments * update config * update config * update config * fix comments * update config * fix meta baseline * fix meta baseline * update config * fix meta baseline * update config * update config * fix comments * fix comments * update config * add config Co-authored-by: zhangshilong <2392587229zsl@gmail.com>
169 lines
6.1 KiB
Python
169 lines
6.1 KiB
Python
import argparse
|
|
import os
|
|
import os.path as osp
|
|
import time
|
|
|
|
import mmcv
|
|
import torch
|
|
from mmcls.models import build_classifier
|
|
from mmcls.utils import get_root_logger
|
|
from mmcv import DictAction
|
|
from mmcv.parallel import MMDataParallel, MMDistributedDataParallel
|
|
from mmcv.runner import (get_dist_info, init_dist, load_checkpoint,
|
|
wrap_fp16_model)
|
|
|
|
from mmfewshot.classification.apis import (multi_gpu_meta_test,
|
|
set_random_seed,
|
|
single_gpu_meta_test)
|
|
from mmfewshot.classification.datasets import (build_dataset,
|
|
build_meta_test_dataloader)
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser(description='mmcls test model')
|
|
parser.add_argument('config', help='test config file path')
|
|
parser.add_argument('checkpoint', help='checkpoint file')
|
|
parser.add_argument('--work-dir', help='the dir to save logs and models')
|
|
parser.add_argument(
|
|
'--metrics',
|
|
type=str,
|
|
nargs='+',
|
|
help='evaluation metrics, which depends on the dataset, e.g., '
|
|
'"accuracy", "precision", "recall", "f1_score", "support" for single '
|
|
'label dataset, and "mAP", "CP", "CR", "CF1", "OP", "OR", "OF1" for '
|
|
'multi-label dataset')
|
|
parser.add_argument(
|
|
'--show-task-results',
|
|
action='store_true',
|
|
help='whether to show results of each task')
|
|
parser.add_argument('--tmpdir', help='tmp dir for writing some results')
|
|
parser.add_argument(
|
|
'--options',
|
|
nargs='+',
|
|
action=DictAction,
|
|
help='override some settings in the used config, the key-value pair '
|
|
'in xxx=yyy format will be merged into config file.')
|
|
parser.add_argument(
|
|
'--metric-options',
|
|
nargs='+',
|
|
action=DictAction,
|
|
default={},
|
|
help='custom options for evaluation, the key-value pair in xxx=yyy '
|
|
'format will be parsed as a dict metric_options for dataset.evaluate()'
|
|
' function.')
|
|
parser.add_argument(
|
|
'--launcher',
|
|
choices=['none', 'pytorch', 'slurm', 'mpi'],
|
|
default='none',
|
|
help='job launcher')
|
|
parser.add_argument('--seed', type=int, default=None, help='random seed')
|
|
parser.add_argument(
|
|
'--deterministic',
|
|
action='store_true',
|
|
help='whether to set deterministic options for CUDNN backend.')
|
|
parser.add_argument('--local_rank', type=int, default=0)
|
|
parser.add_argument(
|
|
'--device',
|
|
choices=['cpu', 'cuda'],
|
|
default='cuda',
|
|
help='device used for testing')
|
|
args = parser.parse_args()
|
|
if 'LOCAL_RANK' not in os.environ:
|
|
os.environ['LOCAL_RANK'] = str(args.local_rank)
|
|
return args
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
cfg = mmcv.Config.fromfile(args.config)
|
|
if args.options is not None:
|
|
cfg.merge_from_dict(args.options)
|
|
# set cudnn_benchmark
|
|
if cfg.get('cudnn_benchmark', False):
|
|
torch.backends.cudnn.benchmark = True
|
|
|
|
# work_dir is determined in this priority: CLI > segment in file > filename
|
|
if args.work_dir is not None:
|
|
# update configs according to CLI args if args.work_dir is not None
|
|
cfg.work_dir = args.work_dir
|
|
elif cfg.get('work_dir', None) is None:
|
|
# use config filename as default work_dir if cfg.work_dir is None
|
|
cfg.work_dir = osp.join('./work_dirs',
|
|
osp.splitext(osp.basename(args.config))[0])
|
|
|
|
# init distributed env first, since logger depends on the dist info.
|
|
if args.launcher == 'none':
|
|
distributed = False
|
|
else:
|
|
distributed = True
|
|
init_dist(args.launcher, **cfg.dist_params)
|
|
rank, _ = get_dist_info()
|
|
# create work_dir
|
|
mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))
|
|
# dump config
|
|
cfg.dump(osp.join(cfg.work_dir, osp.basename(args.config)))
|
|
# init the logger before other steps
|
|
timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime())
|
|
log_file = osp.join(cfg.work_dir, f'{timestamp}_test.log')
|
|
logger = get_root_logger(log_file=log_file, log_level=cfg.log_level)
|
|
|
|
# set random seeds
|
|
if args.seed is not None:
|
|
logger.info(f'Set random seed to {args.seed}, '
|
|
f'deterministic: {args.deterministic}')
|
|
set_random_seed(args.seed, deterministic=args.deterministic)
|
|
|
|
dataset = build_dataset(cfg.data.test)
|
|
meta_test_cfg = cfg.data.test.meta_test_cfg
|
|
(support_data_loader, query_data_loader,
|
|
all_data_loader) = build_meta_test_dataloader(dataset, meta_test_cfg)
|
|
|
|
# build the model and load checkpoint
|
|
model = build_classifier(cfg.model)
|
|
fp16_cfg = cfg.get('fp16', None)
|
|
if fp16_cfg is not None:
|
|
wrap_fp16_model(model)
|
|
if rank == 0:
|
|
logger.info(f'load from checkpoint: {args.checkpoint} ')
|
|
load_checkpoint(model, args.checkpoint, map_location='cpu')
|
|
if not distributed:
|
|
if args.device == 'cpu':
|
|
model = model.cpu()
|
|
else:
|
|
model = MMDataParallel(model, device_ids=[0])
|
|
meta_eval_results = single_gpu_meta_test(
|
|
model,
|
|
meta_test_cfg.num_episodes,
|
|
support_data_loader,
|
|
query_data_loader,
|
|
all_data_loader,
|
|
meta_test_cfg=meta_test_cfg,
|
|
logger=logger,
|
|
eval_kwargs=dict(metric=cfg.evaluation.metric),
|
|
show_task_results=args.show_task_results)
|
|
else:
|
|
model = MMDistributedDataParallel(
|
|
model.cuda(),
|
|
device_ids=[torch.cuda.current_device()],
|
|
broadcast_buffers=False)
|
|
meta_eval_results = multi_gpu_meta_test(
|
|
model,
|
|
meta_test_cfg.num_episodes,
|
|
support_data_loader,
|
|
query_data_loader,
|
|
all_data_loader,
|
|
meta_test_cfg=meta_test_cfg,
|
|
logger=logger,
|
|
eval_kwargs=dict(metric=cfg.evaluation.metric),
|
|
show_task_results=args.show_task_results)
|
|
|
|
if rank == 0:
|
|
logger.info(f'Checkpoint: {args.checkpoint}')
|
|
for k, v in meta_eval_results.items():
|
|
logger.info(f'{k} : {v:.2f}')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|