File size: 8,760 Bytes
ee66a83 fa626df ee66a83 fa626df ee66a83 fa626df ee66a83 5d666d5 ee66a83 9634dc5 ee66a83 9634dc5 ee66a83 fa626df ee66a83 fa626df ee66a83 fa626df ee66a83 fa626df 5d666d5 ee66a83 fa626df ee66a83 69d2ee4 ee66a83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import imp
import os
import warnings
from numbers import Number
from collections import defaultdict
import os.path as osp
import platform
import multiprocessing as mp
import cv2
import mmcv
import mmengine
import numpy as np
import torch
from torch.nn import DataParallel
from mmengine.config import DictAction
from mmengine.runner import load_checkpoint
from mmengine.registry import DefaultScope
from mmengine.runner import Runner
from mmpretrain.models import build_classifier
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('out', help='output result file')
parser.add_argument('--threshold', default=None, type=float, help='open-set threshold')
parser.add_argument('--no-scores', action='store_true', help='don\'t write score .csv file')
parser.add_argument(
'--gpu-collect',
action='store_true',
help='whether to use gpu to collect results')
parser.add_argument('--tmpdir', help='tmp dir for writing some results')
parser.add_argument(
'--cfg-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. If the value to '
'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
'Note that the quotation marks are necessary and that no white space '
'is allowed.')
parser.add_argument(
'--device', default=None, help='device used for testing. (Deprecated)')
parser.add_argument(
'--gpu-ids',
type=int,
nargs='+',
help='(Deprecated, please use --gpu-id) ids of gpus to use '
'(only applicable to non-distributed testing)')
parser.add_argument(
'--gpu-id',
type=int,
default=0,
help='id of gpu to use '
'(only applicable to non-distributed testing)')
args = parser.parse_args()
#assert args.metrics or args.out, \
# 'Please specify at least one of output path and evaluation metrics.'
return args
def single_gpu_test(model,
data_loader,
show=False,
out_dir=None,
**show_kwargs):
"""Test model with local single gpu.
This method tests model with a single gpu and supports showing results.
Args:
model (:obj:`torch.nn.Module`): Model to be tested.
data_loader (:obj:`torch.utils.data.DataLoader`): Pytorch data loader.
show (bool): Whether to show the test results. Defaults to False.
out_dir (str): The output directory of result plots of all samples.
Defaults to None, which means not to write output files.
**show_kwargs: Any other keyword arguments for showing results.
Returns:
list: The prediction results.
"""
model.eval()
results = []
dataset = data_loader.dataset
prog_bar = mmengine.ProgressBar(len(dataset))
observation_ids = []
for i, data in enumerate(data_loader):
with torch.no_grad():
data = model.module.data_preprocessor(data, training=False)
imgs = data['inputs'].cuda()
result = model.module.extract_feat(imgs)
filenames = [x.img_path for x in data['data_samples']]
obs_ids = [osp.basename(x).split('.')[0].split('-')[1] for x in filenames]
result = list(zip(result[0], obs_ids))
batch_size = len(result)
results.extend(result)
prog_bar.update(batch_size)
return results
def setup_multi_processes(cfg):
"""Setup multi-processing environment variables."""
# set multi-process start method as `fork` to speed up the training
if platform.system() != 'Windows':
mp_start_method = cfg.get('mp_start_method', 'fork')
current_method = mp.get_start_method(allow_none=True)
if current_method is not None and current_method != mp_start_method:
warnings.warn(
f'Multi-processing start method `{mp_start_method}` is '
f'different from the previous setting `{current_method}`.'
f'It will be force set to `{mp_start_method}`. You can change '
f'this behavior by changing `mp_start_method` in your config.')
mp.set_start_method(mp_start_method, force=True)
# disable opencv multithreading to avoid system being overloaded
opencv_num_threads = cfg.get('opencv_num_threads', 0)
cv2.setNumThreads(opencv_num_threads)
# setup OMP threads
# This code is referred from https://github.com/pytorch/pytorch/blob/master/torch/distributed/run.py # noqa
if 'OMP_NUM_THREADS' not in os.environ and cfg.test_dataloader.num_workers > 1:
omp_num_threads = 1
warnings.warn(
f'Setting OMP_NUM_THREADS environment variable for each process '
f'to be {omp_num_threads} in default, to avoid your system being '
f'overloaded, please further tune the variable for optimal '
f'performance in your application as needed.')
os.environ['OMP_NUM_THREADS'] = str(omp_num_threads)
# setup MKL threads
if 'MKL_NUM_THREADS' not in os.environ and cfg.test_dataloader.num_workers > 1:
mkl_num_threads = 1
warnings.warn(
f'Setting MKL_NUM_THREADS environment variable for each process '
f'to be {mkl_num_threads} in default, to avoid your system being '
f'overloaded, please further tune the variable for optimal '
f'performance in your application as needed.')
os.environ['MKL_NUM_THREADS'] = str(mkl_num_threads)
def main():
args = parse_args()
default_scope = DefaultScope.get_instance('test', scope_name='mmpretrain')
cfg = mmengine.Config.fromfile(args.config) #mmcv.Config.fromfile(args.config)
if args.cfg_options is not None:
cfg.merge_from_dict(args.cfg_options)
# set multi-process settings
setup_multi_processes(cfg)
# set cudnn_benchmark
if cfg.get('cudnn_benchmark', False):
torch.backends.cudnn.benchmark = True
cfg.model.pretrained = None
if args.gpu_ids is not None:
cfg.gpu_ids = args.gpu_ids[0:1]
warnings.warn('`--gpu-ids` is deprecated, please use `--gpu-id`. '
'Because we only support single GPU mode in '
'non-distributed testing. Use the first GPU '
'in `gpu_ids` now.')
else:
cfg.gpu_ids = [args.gpu_id]
data_loader = Runner.build_dataloader(cfg.test_dataloader)
# build the model and load checkpoint
model = build_classifier(cfg.model)
checkpoint = load_checkpoint(model, args.checkpoint, map_location='cpu')
if args.device == 'cpu':
model = model.cpu()
else:
model = DataParallel(model, device_ids=cfg.gpu_ids)
if not model.device_ids:
assert mmcv.digit_version(mmcv.__version__) >= (1, 4, 4), \
'To test with CPU, please confirm your mmcv version ' \
'is not lower than v1.4.4'
outputs = single_gpu_test(model, data_loader)
results = defaultdict(list)
for result, obs_id in outputs:
results[obs_id].append(result)
if not args.no_scores:
with open(args.out + '.scores.csv', 'w') as f2:
for obs_id, result in results.items():
avg_feats = torch.mean(torch.stack(result, dim=0), dim=0, keepdim=True)
scores = model.module.head(avg_feats)
f2.write(f'{obs_id}')
for s in scores:
f2.write(f',{s}')
f2.write('\n')
dropped = 0
total = 0
with open(args.out, 'w') as f:
f.write('observation_id,class_id\n')
for obs_id, result in results.items():
avg_feats = torch.mean(torch.stack(result, dim=0), dim=0, keepdim=True)
scores = model.module.head(avg_feats)
scores = scores.detach().cpu().numpy()
class_id = np.argmax(scores)
if args.threshold:
max_score = float(torch.max(torch.softmax(torch.from_numpy(scores), dim=0)))
if max_score < args.threshold:
class_id = -1
dropped += 1
total += 1
f.write(f'{obs_id},{int(class_id)}\n')
print(f'dropped {dropped} out of {total}')
if __name__ == '__main__':
main()
|