模型结构解读

Modeling 文件包含了fastreid主要的模块设计和结构蓝图。同时Fast_reid 设计了两个重要的中间件,通过这两个中间件实现了配置文件中的name到object的映射变换,将config中的字符串变换为实际的模型组件以调用结构蓝图。这两个中间件分别是fastreid文件夹下的engine和utils下的registry.py文件。

结构蓝图

1.Meta_arch就是模型的结构蓝图,将backbone,hesds,losses不同的积木组件拼接起来

t9fvd

引擎engine

2.Engine 作为中间件,设置了Trainerbase, Simpletrainer, DefaultTrainer三个类相互继承,使用DefaultTrainer可以使用特定的config加载特定的模型。

x1al8

 1 class TrainerBase:
 2    """
 3    Base class for iterative trainer with hooks.
 4    The only assumption we made here is: the training runs in a loop.
 5    A subclass can implement what the loop is.
 6    We made no assumptions about the existence of dataloader, optimizer, model, etc.
 7    Attributes:
 8        iter(int): the current iteration.
 9        epoch(int): the current epoch.
10        start_iter(int): The iteration to start with.
11            By convention the minimum possible value is 0.
12        max_epoch (int): The epoch to end training.
13        storage(EventStorage): An EventStorage that's opened during the course of training.
14    """
 1class SimpleTrainer(TrainerBase):
 2    """
 3    A simple trainer for the most common type of task:
 4    single-cost single-optimizer single-data-source iterative optimization.
 5    It assumes that every step, you:
 6    1. Compute the loss with a data from the data_loader.
 7    2. Compute the gradients with the above loss.
 8    3. Update the model with the optimizer.
 9    If you want to do anything fancier than this,
10    either subclass TrainerBase and implement your own `run_step`,
11    or write your own training loop.
12    """
 1class DefaultTrainer(TrainerBase):
 2    """
 3    A trainer with default training logic. Compared to `SimpleTrainer`, it
 4    contains the following logic in addition:
 5    1. Create model, optimizer, scheduler, dataloader from the given config.
 6    2. Load a checkpoint or `cfg.MODEL.WEIGHTS`, if exists.
 7    3. Register a few common hooks.
 8    It is created to simplify the **standard model training workflow** and reduce code boilerplate
 9    for users who only need the standard training workflow, with standard features.
10    It means this class makes *many assumptions* about your training logic that
11    may easily become invalid in a new research. In fact, any assumptions beyond those made in the
12    :class:`SimpleTrainer` are too much for research.
13    The code of this class has been annotated about restrictive assumptions it mades.
14    When they do not work for you, you're encouraged to:
15    1. Overwrite methods of this class, OR:
16    2. Use :class:`SimpleTrainer`, which only does minimal SGD training and
17       nothing else. You can then add your own hooks if needed. OR:
18    3. Write your own training loop similar to `tools/plain_train_net.py`.
19    Also note that the behavior of this class, like other functions/classes in
20    this file, is not stable, since it is meant to represent the "common default behavior".
21    It is only guaranteed to work well with the standard models and training workflow in fastreid.
22    To obtain more stable behavior, write your own training logic with other public APIs.
23    Attributes:
24        scheduler:
25        checkpointer:
26        cfg (CfgNode):
27    Examples:
28    .. code-block:: python
29        trainer = DefaultTrainer(cfg)
30        trainer.resume_or_load()  # load last checkpoint or MODEL.WEIGHTS
31        trainer.train()
32    """

registry登记仓库

3.registry是模型结构的登记仓库(也可以理解为不同结构模型的时使用接口),将模型的name映射为实际的module,通过registry实现了第三方模块的注册与创建

1s9ys

 1class Registry(object):
 2    """
 3    The registry that provides name -> object mapping, to support third-party
 4    users' custom modules.
 5    To create a registry (e.g. a backbone registry):
 6    .. code-block:: python
 7        BACKBONE_REGISTRY = Registry('BACKBONE')
 8    To register an object:
 9    .. code-block:: python
10        @BACKBONE_REGISTRY.register()
11        class MyBackbone():
12            ...
13    Or:
14    .. code-block:: python
15        BACKBONE_REGISTRY.register(MyBackbone)
16    """