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.




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

  • 相关阅读:
    java8 新特性
    class类加载机制
    redis和memcache的区别
    Nginx,Apache,Tomcat区别
    java synchronized锁的理解
    java BIO,NIO在单服务器,多客户端通信上的应用
    理解spring任务调度timer,Quartz,spring task
    理解同步与异步,及java中实现同步机制的方法
    java范型的理解
    正向代理,反向代理的理解
  • 原文地址:https://www.cnblogs.com/sddai/p/14412014.html
Copyright © 2011-2022 走看看