您当前的位置: 首页 >  网络
  • 2浏览

    0关注

    417博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

行人重识别02-08:fast-reid(BoT)-pytorch编程规范(fast-reid为例)5-BoT网络模型构建

江南才尽,年少无知! 发布时间:2020-08-18 19:05:29 ,浏览量:2

以下链接是个人关于fast-reid(BoT行人重识别) 所有见解,如有错误欢迎大家指出,我会第一时间纠正。有兴趣的朋友可以加微信:17575010159 相互讨论技术。若是帮助到了你什么,一定要记得点赞!因为这是对我最大的鼓励。 文末附带 \color{blue}{文末附带} 文末附带 公众号 − \color{blue}{公众号 -} 公众号− 海量资源。 \color{blue}{ 海量资源}。 海量资源。

行人重识别02-00:fast-reid(BoT)-目录-史上最新无死角讲解

极度推荐的商业级项目: \color{red}{极度推荐的商业级项目:} 极度推荐的商业级项目:这是本人落地的行为分析项目,主要包含(1.行人检测,2.行人追踪,3.行为识别三大模块):行为分析(商用级别)00-目录-史上最新无死角讲解

前言

在 fastreid\engine\train_loop.py 文件中,找到类 class SimpleTrainer(TrainerBase),可以看到如下代码:

class SimpleTrainer(TrainerBase):
	......
    def run_step(self):
	    # 进行前向传播
        outputs, targets = self.model(data)

        # Compute loss,计算loss
        if isinstance(self.model, DistributedDataParallel):
            loss_dict = self.model.module.losses(outputs, targets)
        else:
            loss_dict = self.model.losses(outputs, targets)

这里看到,首先把数据送入到构建的模型之中,然后进行前向传播,获得预测的结果之后计算loos。那么这里的模型是那个模型?是如何构建的呢?其实,这里的 model 就是 fastreid\modeling\meta_arch\baseline.py 文件中 class Baseline(nn.Module) 创建的对象。

Baseline

本人对于 class Baseline(nn.Module) 的注释如下:

@META_ARCH_REGISTRY.register()
class Baseline(nn.Module):
    def __init__(self, cfg):
        super().__init__()
        self._cfg = cfg
        # 获得数据预处理的参数
        assert len(cfg.MODEL.PIXEL_MEAN) == len(cfg.MODEL.PIXEL_STD)
        self.register_buffer("pixel_mean", torch.tensor(cfg.MODEL.PIXEL_MEAN).view(1, -1, 1, 1))
        self.register_buffer("pixel_std", torch.tensor(cfg.MODEL.PIXEL_STD).view(1, -1, 1, 1))

        # backbone,根据参数构建主干网络,如Resnet50等等
        self.backbone = build_backbone(cfg)

        # head,获得头部模型 pool 的类型,然后构建对应的 pool 方式
        pool_type = cfg.MODEL.HEADS.POOL_LAYER
        if pool_type == 'fastavgpool':  pool_layer = FastGlobalAvgPool2d()
        elif pool_type == 'avgpool':    pool_layer = nn.AdaptiveAvgPool2d(1)
        elif pool_type == 'maxpool':    pool_layer = nn.AdaptiveMaxPool2d(1)
        elif pool_type == 'gempool':    pool_layer = GeneralizedMeanPoolingP()
        elif pool_type == "avgmaxpool": pool_layer = AdaptiveAvgMaxPool2d()
        elif pool_type == "identity":   pool_layer = nn.Identity()
        else:
            raise KeyError(f"{pool_type} is invalid, please choose from "
                           f"'avgpool', 'maxpool', 'gempool', 'avgmaxpool' and 'identity'.")

        # 获得头部模型的输入通道数,以及全链接层输出的类别数目
        in_feat = cfg.MODEL.HEADS.IN_FEAT
        num_classes = cfg.MODEL.HEADS.NUM_CLASSES
        # 根据参数构建头部模型
        self.heads = build_reid_heads(cfg, in_feat, num_classes, pool_layer)

    @property
    def device(self):
        return self.pixel_mean.device

    def forward(self, batched_inputs):
        # 进行数据预处理
        images = self.preprocess_image(batched_inputs)
        # 通过主干网络提取特征
        features = self.backbone(images)

        # 如果是进行训练
        if self.training:
            assert "targets" in batched_inputs, "Person ID annotation are missing in training!"
            # 获取标签
            targets = batched_inputs["targets"].long().to(self.device)

            # PreciseBN flag, When do preciseBN on different dataset, the number of classes in new dataset
            # may be larger than that in the original dataset, so the circle/arcface will
            # throw an error. We just set all the targets to 0 to avoid this problem.
            if targets.sum()             
关注
打赏
1592542134
查看更多评论
0.0410s