Skip to content

Commit

Permalink
Merge 3b0e12d into af88d62
Browse files Browse the repository at this point in the history
  • Loading branch information
ly015 committed Mar 13, 2023
2 parents af88d62 + 3b0e12d commit f9bfd93
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 78 deletions.
17 changes: 14 additions & 3 deletions mmpose/apis/inferencers/pose2d_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import mmcv
import numpy as np
from mmdet.apis.det_inferencer import DetInferencer
from mmengine.config import Config, ConfigDict
from mmengine.infer.infer import ModelType
from mmengine.registry import init_default_scope
Expand All @@ -17,6 +16,12 @@
from .base_mmpose_inferencer import BaseMMPoseInferencer
from .utils import default_det_models

try:
from mmdet.apis.det_inferencer import DetInferencer
mmdet_available = True
except (ImportError, ModuleNotFoundError):
mmdet_available = False

InstanceList = List[InstanceData]
InputType = Union[str, np.ndarray]
InputsType = Union[InputType, Sequence[InputType]]
Expand Down Expand Up @@ -94,8 +99,14 @@ def __init__(self,
det_model, det_weights, det_cat_ids = det_info[
'model'], det_info['weights'], det_info['cat_ids']

self.detector = DetInferencer(
det_model, det_weights, device=device)
if mmdet_available:
self.detector = DetInferencer(
det_model, det_weights, device=device)
else:
raise RuntimeError(
'MMDetection is required to build inferencers '
'for top-down pose estimation models.')

if isinstance(det_cat_ids, (tuple, list)):
self.det_cat_ids = det_cat_ids
else:
Expand Down
2 changes: 1 addition & 1 deletion mmpose/models/pose_estimators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _load_metainfo(metainfo: dict = None) -> dict:

def forward(self,
inputs: torch.Tensor,
data_samples: OptSampleList,
data_samples: OptSampleList = None,
mode: str = 'tensor') -> ForwardResults:
"""The unified entry for a forward process in both training and test.
Expand Down
118 changes: 44 additions & 74 deletions tools/analysis_tools/get_flops.py
Original file line number Diff line number Diff line change
@@ -1,111 +1,81 @@
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
from functools import partial

import torch
from mmengine.config import DictAction
from mmengine.config import Config, DictAction

from mmpose.apis.inference import init_model
from mmpose.registry import MODELS
from mmpose.utils import register_all_modules

try:
from mmcv.cnn import get_model_complexity_info
from mmengine.analysis import get_model_complexity_info
except ImportError:
raise ImportError('Please upgrade mmcv to >0.6.2')


def parse_args():
parser = argparse.ArgumentParser(description='Train a recognizer')
parser = argparse.ArgumentParser(description='Train a detector')
parser.add_argument('config', help='train config file path')
parser.add_argument(
'--device',
default='cuda:0',
help='Device used for model initialization')
parser.add_argument(
'--cfg-options',
nargs='+',
action=DictAction,
default={},
help='override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file. For example, '
"'--cfg-options model.backbone.depth=18 model.backbone.with_cp=True'")
parser.add_argument(
'--shape',
type=int,
nargs='+',
default=[256, 192],
default=[1280, 800],
help='input image size')
parser.add_argument(
'--input-constructor',
'-c',
type=str,
choices=['none', 'batch'],
default='none',
help='If specified, it takes a callable method that generates '
'input. Otherwise, it will generate a random tensor with '
'input shape to calculate FLOPs.')
parser.add_argument(
'--batch-size', '-b', type=int, default=1, help='input batch size')
parser.add_argument(
'--not-print-per-layer-stat',
'-n',
action='store_true',
help='Whether to print complexity information'
'for each layer in a model')
'--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.')
args = parser.parse_args()
return args


def batch_constructor(flops_model, batch_size, input_shape):
"""Generate a batch of tensors to the model."""
batch = {}

inputs = torch.ones(()).new_empty(
(batch_size, *input_shape),
dtype=next(flops_model.parameters()).dtype,
device=next(flops_model.parameters()).device)

batch['inputs'] = inputs
return batch


def main():

register_all_modules()
args = parse_args()

if len(args.shape) == 1:
input_shape = (3, args.shape[0], args.shape[0])
h = w = args.shape[0]
elif len(args.shape) == 2:
input_shape = (3, ) + tuple(args.shape)
h, w = args.shape
else:
raise ValueError('invalid input shape')
input_shape = (3, h, w)

model = init_model(
args.config,
checkpoint=None,
device=args.device,
cfg_options=args.cfg_options)
cfg = Config.fromfile(args.config)
if args.cfg_options is not None:
cfg.merge_from_dict(args.cfg_options)

if args.input_constructor == 'batch':
input_constructor = partial(batch_constructor, model, args.batch_size)
else:
input_constructor = None
model = MODELS.build(cfg.model)
model.eval()

analysis_results = get_model_complexity_info(
model, input_shape, show_table=True, show_arch=False)

# ayalysis_results = {
# 'flops': flops,
# 'flops_str': flops_str,
# 'activations': activations,
# 'activations_str': activations_str,
# 'params': params,
# 'params_str': params_str,
# 'out_table': complexity_table,
# 'out_arch': complexity_arch
# }

if hasattr(model, '_forward'):
model.forward = model._forward
else:
raise NotImplementedError(
'FLOPs counter is currently not currently supported with {}'.
format(model.__class__.__name__))

flops, params = get_model_complexity_info(
model,
input_shape,
input_constructor=input_constructor,
print_per_layer_stat=(not args.not_print_per_layer_stat))
split_line = '=' * 30
input_shape = (args.batch_size, ) + input_shape
print(f'{split_line}\nInput shape: {input_shape}\n'
f'Flops: {flops}\nParams: {params}\n{split_line}')
f'Flops: {analysis_results["flops"]}\n'
f'Params: {analysis_results["params"]}\n{split_line}')

print(analysis_results['activations'])
# print(analysis_results['complexity_table'])
# print(complexity_str)
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.')
Expand Down
106 changes: 106 additions & 0 deletions tools/analysis_tools/get_flops1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright (c) OpenMMLab. All rights reserved.
import argparse

import torch
from mmengine.config import DictAction

from mmpose.apis.inference import init_model

try:
# from mmcv.cnn import get_model_complexity_info
from mmengine.analysis import get_model_complexity_info
except ImportError:
raise ImportError('Please upgrade mmcv to >0.6.2')


def parse_args():
parser = argparse.ArgumentParser(description='Train a recognizer')
parser.add_argument('config', help='train config file path')
parser.add_argument(
'--device', default='cpu', help='Device used for model initialization')
parser.add_argument(
'--cfg-options',
nargs='+',
action=DictAction,
default={},
help='override some settings in the used config, the key-value pair '
'in xxx=yyy format will be merged into config file. For example, '
"'--cfg-options model.backbone.depth=18 model.backbone.with_cp=True'")
parser.add_argument(
'--shape',
type=int,
nargs='+',
default=[256, 192],
help='input image size')
parser.add_argument(
'--input-constructor',
'-c',
type=str,
choices=['none', 'batch'],
default='none',
help='If specified, it takes a callable method that generates '
'input. Otherwise, it will generate a random tensor with '
'input shape to calculate FLOPs.')
parser.add_argument(
'--batch-size', '-b', type=int, default=1, help='input batch size')
parser.add_argument(
'--not-print-per-layer-stat',
'-n',
action='store_true',
help='Whether to print complexity information'
'for each layer in a model')
args = parser.parse_args()
return args


def batch_constructor(flops_model, batch_size, input_shape):
"""Generate a batch of tensors to the model."""
batch = {}

inputs = torch.ones(()).new_empty(
(batch_size, *input_shape),
dtype=next(flops_model.parameters()).dtype,
device=next(flops_model.parameters()).device)

batch['inputs'] = inputs
return batch


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')

model = init_model(
args.config,
checkpoint=None,
device=args.device,
cfg_options=args.cfg_options)

if hasattr(model, '_forward'):
model.forward = model._forward
else:
raise NotImplementedError(
'FLOPs counter is currently not currently supported with {}'.
format(model.__class__.__name__))

analysis_results = get_model_complexity_info(model, input_shape)
flops = analysis_results['flops_str']
params = analysis_results['params_str']
split_line = '=' * 30
input_shape = (args.batch_size, ) + input_shape
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 comments on commit f9bfd93

Please sign in to comment.