zoukankan      html  css  js  c++  java
  • Detectron2 获取网络中间层结果的途径

    Partially execute a model:

    Sometimes you may want to obtain an intermediate tensor inside a model, such as the input of certain layer, the output before post-processing. Since there are typically hundreds of intermediate tensors, there isn’t an API that provides you the intermediate result you need. You have the following options:

    1. Write a (sub)model. Following the tutorial, you can rewrite a model component (e.g. a head of a model), such that it does the same thing as the existing component, but returns the output you need.

    2. Partially execute a model. You can create the model as usual, but use custom code to execute it instead of its forward(). For example, the following code obtains mask features before mask head.

      images = ImageList.from_tensors(...)  # preprocessed input tensor
      model = build_model(cfg)
      model.eval()
      features = model.backbone(images.tensor)
      proposals, _ = model.proposal_generator(images, features)
      instances, _ = model.roi_heads(images, features, proposals)
      mask_features = [features[f] for f in model.roi_heads.in_features]
      mask_features = model.roi_heads.mask_pooler(mask_features, [x.pred_boxes for x in instances])
      

        

    3. Use forward hooks. Forward hooks can help you obtain inputs or outputs of a certain module. If they are not exactly what you want, they can at least be used together with partial execution to obtain other tensors.

    All options require you to read documentation and sometimes code of the existing models to understand the internal logic, in order to write code to obtain the internal tensors.




    如果这篇文章帮助到了你,你可以请作者喝一杯咖啡

  • 相关阅读:
    链表_单链表(插入删除查询)
    OceanBase架构浅析(一)
    电商商品搜索现状
    ASP.NET MVC 源码分析(二) —— 从 IRouteBuilder认识路由构建
    ASP.NET MVC 源码分析(一)
    RPC框架分析
    RPC简介
    Performance Metrics(性能指标2)
    Performance Metrics(性能指标1)
    Introduction(本书简介)
  • 原文地址:https://www.cnblogs.com/sddai/p/14412014.html
Copyright © 2011-2022 走看看