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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM