Team Ai
Apppublic

Arulkumar03/Fox_Sheep_Detector_Computer_Vision_model

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
analyze_model.py160 linesDownload Raw Back to tools
1# -*- coding: utf-8 -*-2# Copyright (c) Facebook, Inc. and its affiliates.3 4import logging5import numpy as np6from collections import Counter7import tqdm8from fvcore.nn import flop_count_table  # can also try flop_count_str9 10from detectron2.checkpoint import DetectionCheckpointer11from detectron2.config import CfgNode, LazyConfig, get_cfg, instantiate12from detectron2.data import build_detection_test_loader13from detectron2.engine import default_argument_parser14from detectron2.modeling import build_model15from detectron2.utils.analysis import (16    FlopCountAnalysis,17    activation_count_operators,18    parameter_count_table,19)20from detectron2.utils.logger import setup_logger21 22logger = logging.getLogger("detectron2")23 24 25def setup(args):26    if args.config_file.endswith(".yaml"):27        cfg = get_cfg()28        cfg.merge_from_file(args.config_file)29        cfg.DATALOADER.NUM_WORKERS = 030        cfg.merge_from_list(args.opts)31        cfg.freeze()32    else:33        cfg = LazyConfig.load(args.config_file)34        cfg = LazyConfig.apply_overrides(cfg, args.opts)35    setup_logger(name="fvcore")36    setup_logger()37    return cfg38 39 40def do_flop(cfg):41    if isinstance(cfg, CfgNode):42        data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0])43        model = build_model(cfg)44        DetectionCheckpointer(model).load(cfg.MODEL.WEIGHTS)45    else:46        data_loader = instantiate(cfg.dataloader.test)47        model = instantiate(cfg.model)48        model.to(cfg.train.device)49        DetectionCheckpointer(model).load(cfg.train.init_checkpoint)50    model.eval()51 52    counts = Counter()53    total_flops = []54    for idx, data in zip(tqdm.trange(args.num_inputs), data_loader):  # noqa55        flops = FlopCountAnalysis(model, data)56        if idx > 0:57            flops.unsupported_ops_warnings(False).uncalled_modules_warnings(False)58        counts += flops.by_operator()59        total_flops.append(flops.total())60 61    logger.info("Flops table computed from only one input sample:\n" + flop_count_table(flops))62    logger.info(63        "Average GFlops for each type of operators:\n"64        + str([(k, v / (idx + 1) / 1e9) for k, v in counts.items()])65    )66    logger.info(67        "Total GFlops: {:.1f}±{:.1f}".format(np.mean(total_flops) / 1e9, np.std(total_flops) / 1e9)68    )69 70 71def do_activation(cfg):72    if isinstance(cfg, CfgNode):73        data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0])74        model = build_model(cfg)75        DetectionCheckpointer(model).load(cfg.MODEL.WEIGHTS)76    else:77        data_loader = instantiate(cfg.dataloader.test)78        model = instantiate(cfg.model)79        model.to(cfg.train.device)80        DetectionCheckpointer(model).load(cfg.train.init_checkpoint)81    model.eval()82 83    counts = Counter()84    total_activations = []85    for idx, data in zip(tqdm.trange(args.num_inputs), data_loader):  # noqa86        count = activation_count_operators(model, data)87        counts += count88        total_activations.append(sum(count.values()))89    logger.info(90        "(Million) Activations for Each Type of Operators:\n"91        + str([(k, v / idx) for k, v in counts.items()])92    )93    logger.info(94        "Total (Million) Activations: {}±{}".format(95            np.mean(total_activations), np.std(total_activations)96        )97    )98 99 100def do_parameter(cfg):101    if isinstance(cfg, CfgNode):102        model = build_model(cfg)103    else:104        model = instantiate(cfg.model)105    logger.info("Parameter Count:\n" + parameter_count_table(model, max_depth=5))106 107 108def do_structure(cfg):109    if isinstance(cfg, CfgNode):110        model = build_model(cfg)111    else:112        model = instantiate(cfg.model)113    logger.info("Model Structure:\n" + str(model))114 115 116if __name__ == "__main__":117    parser = default_argument_parser(118        epilog="""119Examples:120 121To show parameters of a model:122$ ./analyze_model.py --tasks parameter \\123    --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml124 125Flops and activations are data-dependent, therefore inputs and model weights126are needed to count them:127 128$ ./analyze_model.py --num-inputs 100 --tasks flop \\129    --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml \\130    MODEL.WEIGHTS /path/to/model.pkl131"""132    )133    parser.add_argument(134        "--tasks",135        choices=["flop", "activation", "parameter", "structure"],136        required=True,137        nargs="+",138    )139    parser.add_argument(140        "-n",141        "--num-inputs",142        default=100,143        type=int,144        help="number of inputs used to compute statistics for flops/activations, "145        "both are data dependent.",146    )147    args = parser.parse_args()148    assert not args.eval_only149    assert args.num_gpus == 1150 151    cfg = setup(args)152 153    for task in args.tasks:154        {155            "flop": do_flop,156            "activation": do_activation,157            "parameter": do_parameter,158            "structure": do_structure,159        }[task](cfg)160