Add two useful tools from mmdet
parent
d967b94439
commit
cf3a530c73
|
@ -0,0 +1,54 @@
|
|||
import argparse
|
||||
|
||||
from mmcv import Config
|
||||
from mmcv.cnn.utils import get_model_complexity_info
|
||||
|
||||
from mmcls.models import build_classifier
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='Get model flops and params')
|
||||
parser.add_argument('config', help='config file path')
|
||||
parser.add_argument(
|
||||
'--shape',
|
||||
type=int,
|
||||
nargs='+',
|
||||
default=[224, 224],
|
||||
help='input image size')
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
args = parse_args()
|
||||
|
||||
if len(args.shape) == 1:
|
||||
input_shape = (3, args.shape[0], args.shape[0])
|
||||
elif len(args.shape) == 2:
|
||||
input_shape = (3, ) + tuple(args.shape)
|
||||
else:
|
||||
raise ValueError('invalid input shape')
|
||||
|
||||
cfg = Config.fromfile(args.config)
|
||||
model = build_classifier(cfg.model)
|
||||
model.eval()
|
||||
|
||||
if hasattr(model, 'extract_feat'):
|
||||
model.forward = model.extract_feat
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
'FLOPs counter is currently not currently supported with {}'.
|
||||
format(model.__class__.__name__))
|
||||
|
||||
flops, params = get_model_complexity_info(model, input_shape)
|
||||
split_line = '=' * 30
|
||||
print(f'{split_line}\nInput shape: {input_shape}\n'
|
||||
f'Flops: {flops}\nParams: {params}\n{split_line}')
|
||||
print('!!!Please be cautious if you use the results in papers. '
|
||||
'You may need to check if all ops are supported and verify that the '
|
||||
'flops computation is correct.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -0,0 +1,39 @@
|
|||
import argparse
|
||||
import subprocess
|
||||
|
||||
import torch
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Process a checkpoint to be published')
|
||||
parser.add_argument('in_file', help='input checkpoint filename')
|
||||
parser.add_argument('out_file', help='output checkpoint filename')
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
def process_checkpoint(in_file, out_file):
|
||||
checkpoint = torch.load(in_file, map_location='cpu')
|
||||
# remove optimizer for smaller file size
|
||||
if 'optimizer' in checkpoint:
|
||||
del checkpoint['optimizer']
|
||||
# if it is necessary to remove some sensitive data in checkpoint['meta'],
|
||||
# add the code here.
|
||||
torch.save(checkpoint, out_file)
|
||||
sha = subprocess.check_output(['sha256sum', out_file]).decode()
|
||||
if out_file.endswith('.pth'):
|
||||
out_file_name = out_file[:-4]
|
||||
else:
|
||||
out_file_name = out_file
|
||||
final_file = out_file_name + f'-{sha[:8]}.pth'
|
||||
subprocess.Popen(['mv', out_file, final_file])
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
process_checkpoint(args.in_file, args.out_file)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue