mmdetection源码解读(一)

  一、安装测试:(官方github上很详细)

  https://github.com/open-mmlab/mmdetection

  测试:

  from mmdet.apis import init_detector, inference_detector, show_result

  if __name__ == '__main__':

  config_file = 'configs/faster_rcnn_r50_fpn_1x.py'

  checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'

  img_path = 'test.jpg'

  model = init_detector(config_file, checkpoint_file, device='cuda:0')

  result = inference_detector(model, img_path)

  show_result(img_path, result, model.CLASSES)

  测试注意事项:

  1. mmdetection目前不支持Windos系统,我使用Ubuntu16.04

  2.`checkpoint_file` 是我在mmdetection目录下创建了一个checkpoints文件夹,然后手动下载

  (链接:https://pan.baidu.com/s/1jC_9DJWrnwB8tm9LnGAHeQ 提取码:indv )的权值,也可以自动下载的应该。

  如果跑通应该没啥问题了。

  二:mmdetection/utils/registry.py 文档解读:

  import inspect

  import mmcv

  class Registry(object):

  """

  这里主要实现一个Registry类,用来规范注册网络的各个模块,比如backebone, neck,还有dataset,pipeline等等这些模块。

  """

  def __init__(self, name):

  self._name = name

  self._module_dict = dict()

  def __repr__(self):

  """这个函数主要是给类一个输出,我的理解是就是输出类相关信息。

  用下面的代码自己测试看看。

  #from mmdet.utils import Registry

  #print(Registry('backbone'))

  """

  format_str = self.__class__.__name__ + '(name={}, items={})'.format(

  self._name, list(self._module_dict.keys()))

  return format_str

  @property #负责修饰一个对象函数,让类生成成员变量对应的setter和getter函数

  def name(self):

  return self._name

  @property

  def module_dict(self):

  return self._module_dict

  def get(self, key):

  return self._module_dict.get(key, None)

  def _register_module(self, module_class):

  """Register a module.

  Args:

  module (:obj:`nn.Module`): Module to be registered.

  """

  if not inspect.isclass(module_class):

  raise TypeError('module must be a class, but got {}'.format(

  type(module_class)))

  module_name = module_class.__name__

  if module_name in self._module_dict:

  raise KeyError('{} is already registered in {}'.format(

  module_name, self.name))

  self._module_dict[module_name] = module_class

  def register_module(self, cls):

  self._register_module(cls)

  return cls

  def build_from_cfg(cfg, registry, default_args=None):

  """无锡人流医院哪家好 http://www.ytsg029.com/

  这个函数大意就是从config文件中cfg各个模块使用registry进行注册。

  Build a module from config dict.

  Args:

  cfg (dict): Config dict. It should at least contain the key "type".

  registry (:obj:`Registry`): The registry to search the type from.

  default_args (dict, optional): Default initialization arguments.

  Returns:

  obj: The constructed object.

  """

  assert isinstance(cfg, dict) and 'type' in cfg

  assert isinstance(default_args, dict) or default_args is None

  args = cfg.copy()

  obj_type = args.pop('type')

  if mmcv.is_str(obj_type):

  obj_cls = registry.get(obj_type)

  if obj_cls is None:

  raise KeyError('{} is not in the {} registry'.format(

  obj_type, registry.name))

  elif inspect.isclass(obj_type):

  obj_cls = obj_type

  else:

  raise TypeError('type must be a str or valid type, but got {}'.format(

  type(obj_type)))

  if default_args is not None:

  for name, value in default_args.items():

  args.setdefault(name, value)

  return obj_cls(**args)

  三:数据集介绍:

  mmdetection现在支持coco和voc数据集格式,数据集的格式使用官方介绍的

  mmdetection

  ----------------- data

  -----coco

  -----VOCdevkit

  主要嵌套关系是

  以mmdetection/mmdet/datasets/cityscapes.py为例:

  from .coco import CocoDataset

  from .registry import DATASETS

  @DATASETS.register_module

  class CityscapesDataset(CocoDataset):

  """

  自定义coco格式数据类<------CocoDataset <-----------CustomDataset <----------Dataset(torch.utils.data.Dataset)

  VOCDataset<-----------XMLDataset<----------CustomDataset<--------Dataset(同上)

  其他一些函数主要是用来读取分析coco的.json和voc的XML文件的,具体细节先不谈,先了解整体框架

  """

  CLASSES = ('person', 'rider', 'car', 'truck', 'bus', 'train', 'motorcycle', 'bicycle')

posted @ 2019-10-11 14:49  网管布吉岛  阅读(5014)  评论(0编辑  收藏  举报