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.




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

  • 相关阅读:
    SpringBoot简单项目学习笔记08(servlet的内置容器的切换(tomcat、jetty、undertow))
    读《大家看的设计书(第三版)》有感
    百度比赛任务二收获
    百度前端任务一学习的知识
    学习git的内容
    codeforces #588 ABCD
    codeforces #597 div2 ABCD !F
    codeforces #589 div2 ABCD E待补
    codeforces #590 div3 BCDF E待补
    codeforces #591 div2 ABCD
  • 原文地址:https://www.cnblogs.com/sddai/p/14412014.html
Copyright © 2011-2022 走看看