mmdetection源码阅读

2021-11-23号更新

mmdetection中的hook函数

重难点总结:

# step1: 根据官方文档,getattr(self,'name')等同于self.name
# sept2: 这是23中设计模式中的观察者模块式,即主类可以监听其他类的

案例分析

import sys
class HOOK:

    def before_breakfirst(self, runner):
        print('{}:吃早饭之前晨练30分钟'.format(sys._getframe().f_code.co_name))
        print(runner.name)

    def after_breakfirst(self, runner):
        print('{}:吃早饭之前晨练30分钟'.format(sys._getframe().f_code.co_name))

    def before_lunch(self, runner):
        print('{}:吃午饭之前跑上实验'.format(sys._getframe().f_code.co_name))

    def after_lunch(self, runner):
        print('{}:吃完午饭午休30分钟'.format(sys._getframe().f_code.co_name))

    def before_dinner(self, runner):
        print('{}: 没想好做什么'.format(sys._getframe().f_code.co_name))

    def after_dinner(self, runner):
        print('{}: 没想好做什么'.format(sys._getframe().f_code.co_name))

    def after_finish_work(self, runner, are_you_busy=False):
        if are_you_busy:
            print('{}:今天事贼多,还是加班吧'.format(sys._getframe().f_code.co_name))
        else:
            print('{}:今天没啥事,去锻炼30分钟'.format(sys._getframe().f_code.co_name))


class Runner(object):
    def __init__(self, name):
        self.name = name
        self._hooks = []  # 将hook对象放进这里  <__main__.Runner object at 0x7f626e>

    def register_hook(self, hook):
        # 这里不做优先级判断,直接在头部插入HOOK
        self._hooks.insert(0, hook)

    def call_hook(self, hook_name):
        for hook in self._hooks:  # 这里的hook是HOOK实例出来的对象
            # print(getattr(hook, hook_name))  # 这里可以反射出before_breakfirst 方法
            # print(self)  # self是runner对象
            getattr(hook, hook_name)(self)  # 这里 传进去的self其实是runner对象
            # getattr(hook, hook_name)('haha')

    def run(self):
        print('开始启动我的一天')
        self.call_hook('before_breakfirst')
        self.call_hook('after_breakfirst')
        self.call_hook('before_lunch')
        self.call_hook('after_lunch')
        self.call_hook('before_dinner')
        self.call_hook('after_dinner')
        self.call_hook('after_finish_work')
        print('~~睡觉~~')


# 实例化
hook = HOOK()
runner = Runner('小明')

runner.register_hook(hook)
runner.run()

错误一: 由于coco 数据集错误导致的报错 --时间2021年6月9号

  • 报错信息:
fatal: not a git repository (or any parent up to mount point /media/yuqing)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
loading annotations into memory...
Traceback (most recent call last):
  File "/home/yuqing/anaconda3/envs/yolodet/lib/python3.7/site-packages/mmcv/utils/registry.py", line 51, in build_from_cfg
    return obj_cls(**args)
  File "/media/yuqing/My Passport/cv_coder/mmdetection-2.12.0/mmdet/datasets/custom.py", line 88, in __init__
    self.data_infos = self.load_annotations(self.ann_file)
  File "/media/yuqing/My Passport/cv_coder/mmdetection-2.12.0/mmdet/datasets/coco.py", line 47, in load_annotations
    self.coco = COCO(ann_file)
  File "/media/yuqing/My Passport/cv_coder/mmdetection-2.12.0/mmdet/datasets/api_wrappers/coco_api.py", line 22, in __init__
    super().__init__(annotation_file=annotation_file)
  File "/home/yuqing/anaconda3/envs/yolodet/lib/python3.7/site-packages/pycocotools/coco.py", line 85, in __init__
    dataset = json.load(f)
  File "/home/yuqing/anaconda3/envs/yolodet/lib/python3.7/json/__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/home/yuqing/anaconda3/envs/yolodet/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/home/yuqing/anaconda3/envs/yolodet/lib/python3.7/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tools/train.py", line 188, in <module>
    main()
  File "tools/train.py", line 184, in main
    meta=meta)
  File "/media/yuqing/My Passport/cv_coder/mmdetection-2.12.0/mmdet/apis/train.py", line 140, in train_detector
    val_dataset = build_dataset(cfg.data.val, dict(test_mode=True))
  File "/media/yuqing/My Passport/cv_coder/mmdetection-2.12.0/mmdet/datasets/builder.py", line 71, in build_dataset
    dataset = build_from_cfg(cfg, DATASETS, default_args)
  File "/home/yuqing/anaconda3/envs/yolodet/lib/python3.7/site-packages/mmcv/utils/registry.py", line 54, in build_from_cfg
    raise type(e)(f'{obj_cls.__name__}: {e}')
TypeError: __init__() missing 2 required positional arguments: 'doc' and 'pos'

解决方法:

  • 正确放置coco数据集,出现此类错误时想到是数据集错误
原文地址:https://www.cnblogs.com/zranguai/p/14867716.html