Network Dissection量化网络的可解释性(interpretability)

引言

作者提出了量化网络latent representation可解释性的框架:评估单一隐藏单元与语义概念的对齐关系。给定一个CNN,可以对某一卷积层的隐藏单元的语义进行打分。语义具有如下的标签:objects(目标),parts(物体的一部分),scenes(场景),textures(纹理),materials(材料),color(颜色)。 Broadly and Densely Labeled Dataset (Broden) 收集了多个数据集的图片并进行了密集的pixel-wise的标注,除了纹理(texture)和场景(scene)是对整体图像的标注,部分例子如下所示:
在这里插入图片描述
本文阐述了一些可解释性的结论:

  • 可解释性是与坐标轴对齐(axis-aligned)的,对表示(representation)进行翻转(rotate),网络的可解释能力会下降,但是分类性能不变。
  • 越深的结构可解释性越好,ResNet>VGGNet>GoogleNet>AlexNet
  • 对于训练数据集的可解释性,Places > ImageNet,因为一个场景(scene)会包含多个目标,因此有益于多个目标检测器(object detectors)出现来识别场景。
  • 对训练条件的可解释性,训练论数越多越好。与初始化无关,dropout会增强可解释性。Batch normalization会降低可解释性,百化(whiten)操作会平滑缩放问题并且rotate中间特征的轴。

方法

在这里插入图片描述
对于给定CNN衡量语义对齐的单元。上图中探测最后卷积层的一个单元的表现。
对于一个给定的输入图像 x x x,某一卷积层的第 k k k个单元的激活图是 A k ( x ) A_{k}(x) Ak(x)。单元的激活分布为 α k \alpha_{k} αk,上分位点 T k T_{k} Tk通过 P ( α k > T K ) = 0.005 P(\alpha_{k}>T_{K})=0.005 P(αk>TK)=0.005在每个spatial位置上。如上图所示,首先对低像素的单元激活图 A k ( x ) A_{k}(x) Ak(x)进行上采样到 S k ( x ) S_{k}(x) Sk(x)为输入图像像素大小,之后和输入像素的标注mask L c L_{c} Lc进行比较,其中 c c c是某一个语义概念。 S k ( x ) S_{k}(x) Sk(x)进行一个阈值的二值分割: M k ( x ) ≡ S k ( x ) ≥ T k ( x ) M_{k}(x)\equiv S_{k}(x)\geq T_{k}(x) Mk(x)Sk(x)Tk(x)。计算 M k ( x ) ∩ L c ( x ) M_{k}(x)\cap L_{c}(x) Mk(x)Lc(x)通过IoU来得到单元 k k k对于概念 c c c的分数:
I o U k , c = ∑ ∣ M k ( x ) ∩ L c ( x ) ∣ ∑ ∣ M k ( x ) ∪ L c ( x ) ∣ IoU_{k,c}=\frac{\sum{|M_{k}(x)\cap L_{c}(x)|}}{\sum|M_{k}(x)\cup L_{c}(x)|} IoUk,c=Mk(x)Lc(x)Mk(x)Lc(x)
我们认为单元 k k k对于概念 c c c作为一个检测器如果 I o U k , c IoU_{k,c} IoUk,c超过了阈值。文中作者使用阈值 0.04 0.04 0.04。注意单个单元可能是多个概念的检测器,于是作者选择排名最好的label。量化某层的可解释性,我们count对齐于单元的独特概念的数量,称这个指标为the number of unique detectors,将某层可以作为detector的单元数量定义为the number of detectors.

pytorch实现

作者提供了Github链接:https://github.com/CSAILVision/NetDissect-Lite
在这里重点说明如何使用自己的模型进行dissection,注意首先要得到pre-trained权重文件。
(1)将模型module代码放在loader/下,如新建文件lenet.py:

import torch.nn as nn
import torch.nn.functional as F
import torch


class LeNet(nn.Module):
    def __init__(self, num_classes=1000):
        super(LeNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, 3),
            nn.ReLU(inplace=True))

        self.fc = nn.Linear(64, num_classes)

    def forward(self, x):
        out = self.features(x)
        out = F.adaptive_avg_pool2d(out, (1, 1))
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out

(2)修改loader/下的model_loader.py,用于加载自己的lenet模型:
settings.py里加入IS_TORCHVISION变量,用于标识是不是torchvision中已有的模型。

import settings
import torch
import torchvision
from .lenet import LeNet

def loadmodel(hook_fn):
    if settings.IS_TORCHVISION:
        if settings.MODEL_FILE is None:
            model = torchvision.models.__dict__[settings.MODEL](pretrained=True)
        else:
            checkpoint = torch.load(settings.MODEL_FILE)
            if type(checkpoint).__name__ == 'OrderedDict' or type(checkpoint).__name__ == 'dict':
                model = torchvision.models.__dict__[settings.MODEL](num_classes=settings.NUM_CLASSES)
                if settings.MODEL_PARALLEL:
                    state_dict = {str.replace(k, 'module.', ''): v for k, v in checkpoint[
                        'state_dict'].items()}  # the data parallel layer will add 'module' before each layer name
                else:
                    state_dict = checkpoint
                model.load_state_dict(state_dict)
            else:
                model = checkpoint
    else:
        checkpoint = torch.load(settings.MODEL_FILE)
        model = LeNet(num_classes=settings.NUM_CLASSES)
        param_dict = {}  # 新的pre-trained权重文件,因为直接load checkpoint可能会引起组件名称不一致问题
        for k, v in zip(model.state_dict().keys(), checkpoint['net'].keys()):
            param_dict[k] = checkpoint['net'][v]
        model.load_state_dict(param_dict)

    for name in settings.FEATURE_NAMES:
        model._modules.get(name).register_forward_hook(hook_fn)
    if settings.GPU:
        model.cuda()
    model.eval()
    return model

(3)settings.py文件设置如下:

######### global settings  #########
GPU = True                                  # running on GPU is highly suggested
TEST_MODE = False                         # turning on the testmode means the code will run on a small dataset.
CLEAN = True                               # set to "True" if you want to clean the temporary large files after generating result
MODEL = 'lenet'                          # model arch: resnet18, alexnet, resnet50, densenet161
DATASET = 'imagenet'                       # model trained on: places365 or imagenet
QUANTILE = 0.005                            # the threshold used for activation
SEG_THRESHOLD = 0.04                        # the threshold used for visualization
SCORE_THRESHOLD = 0.04                      # the threshold used for IoU score (in HTML file)
TOPN = 10                                   # to show top N image with highest activation for each unit
PARALLEL = 1                                # how many process is used for tallying (Experiments show that 1 is the fastest)
CATAGORIES = ["object", "material", "part","scene","texture","color"] # concept categories that are chosen to detect: "object", "part", "scene", "material", "texture", "color"
OUTPUT_FOLDER = "result/pytorch_"+MODEL+"_"+DATASET # result will be stored in this folder

########### sub settings ###########
# In most of the case, you don't have to change them.
# DATA_DIRECTORY: where broaden dataset locates
# IMG_SIZE: image size, alexnet use 227x227
# NUM_CLASSES: how many labels in final prediction
# FEATURE_NAMES: the array of layer where features will be extracted
# MODEL_FILE: the model file to be probed, "None" means the pretrained model in torchvision
# MODEL_PARALLEL: some model is trained in multi-GPU, so there is another way to load them.
# WORKERS: how many workers are fetching images
# BATCH_SIZE: batch size used in feature extraction
# TALLY_BATCH_SIZE: batch size used in tallying
# INDEX_FILE: if you turn on the TEST_MODE, actually you should provide this file on your own

if MODEL != 'alexnet':
    DATA_DIRECTORY = 'dataset/broden1_224'
    IMG_SIZE = 224
else:
    DATA_DIRECTORY = 'dataset/broden1_227'
    IMG_SIZE = 227


NUM_CLASSES = 1000
IS_TORCHVISION = False

if MODEL == 'resnet50':
    FEATURE_NAMES = ['layer4']
    MODEL_FILE = None
    MODEL_PARALLEL = False
elif MODEL == 'densenet169':
    FEATURE_NAMES = ['features']
    MODEL_FILE = None
    MODEL_PARALLEL=False
elif MODEL == 'lenet':
    FEATURE_NAMES = ['features']
    MODEL_FILE = '/home/ws/winycg/mbagnet/checkpoint/LeNet.pth.tar' # 预训练权重文件


if TEST_MODE:
    WORKERS = 1
    BATCH_SIZE = 4
    TALLY_BATCH_SIZE = 2
    TALLY_AHEAD = 1
    INDEX_FILE = 'index_sm.csv'
    OUTPUT_FOLDER += "_test"
else:
    WORKERS = 12
    BATCH_SIZE = 16
    TALLY_BATCH_SIZE = 16
    TALLY_AHEAD = 4
    INDEX_FILE = 'index.csv'

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
1 Writing an ANSI C++ Program 1 1.1 Getting Ready to Program....... 2 1.1 A First Program......... 3 1.2 Problem Solving: Recipes....... . 7 1.2.1 Algorithms—Being Precise...... . . 8 1.3 Implementing Our Algorithm in C++.... . . . 10 1.4 Software Engineering: Style...... . . 12 1.5 Common Programming Errors...... 13 1.6 Writing and Running a C++ Program.... . . . 14 1.6.1 Interrupting a Program...... 16 1.6.2 Typing an End-of-File Signal..... 16 1.7 Dr. P’s Prescriptions....... . . . 16 1.8 C++ Compared with Java....... 17 Summary.......... 20 Review Questions........ . 21 Exercises.......... 22 2 Native Types and Statements 25 2.1 Program Elements........ . 26 2.1.1 Comments........ . 26 2.1.2 Keywords........ . . 27 2.1.3 Identifiers........ . . 27 2.1.4 Literals......... 29 2.1.5 Operators and Punctuators..... . 31 2.2 Input/Output......... . 31 2.3 Program Structure........ . 34 2.3.1 Redirection........ . 36 2.4 Simple Types......... . 37 2.4.1 Initialization........ 39 2.5 The Traditional Conversions...... . 40 2.6 Enumeration Types........ 43 2.6.1 typedef Declarations...... . 44 2.7 Expressions.......... . 44 2.7.1 Precedence and Associativity of Operators.. . 45 2.7.2 Relational, Equality, and Logical Operators.. . 47 2.8 Statements......... . . 50 2.8.1 Assignment and Expressions..... . . . 51 2.8.2 The Compound Statement...... . 52 2.8.3 The if and if-else Statements.... . 52 2.8.4 The while Statement...... . 55 2.8.5 The for Statement...... . . . 56 2.8.6 The do Statement....... 57 2.8.7 The break and continue Statements... . 58 2.8.8 The switch Statement...... 59 2.8.9 The goto Statement...... . . 62 2.9 Software Engineering: Debugging..... . 62 2.10 Dr. P’s Prescriptions....... . . . 65 2.11 C++ Compared with Java....... 67 Summary.......... 69 Review Questions........ . 70 Exercises.......... 71 3 Functions, Pointers, and Arrays 75 3.1 Functions......... . . . 75 3.2 Function Invocation........ 76 3.3 Function Definition........ 78 3.4 The return Statement........ . 79 3.5 Function Prototypes........ . . . 80 3.6 Call-By-Value......... . 81 3.7 Recursion......... . . . 83 3.8 Default Arguments........ 84 3.9 Functions as Arguments....... 86 3.10 Overloading Functions........ . 88 3.11 Inlining.......... . 89 3.11.1Software Engineering: Avoiding Macros... 89 3.12 Scope and Storage Class....... 90 3.12.1The Storage Class auto...... . . . 92 3.12.2The Storage Class extern..... . . 92 3.12.3The Storage Class register..... 93 3.12.4The Storage Class static..... . . 94 3.12.5Header Files and Linkage Mysteries... . . . 95 3.13 Namespaces......... . 98 3.14 Pointer Types......... 99 3.14.1Addressing and Dereferencing.... . 100 3.14.2Pointer-Based Call-By-Reference..... 100 3.15 Reference Declarations....... 102 3.16 The Uses of void......... 104 3.17 Arrays.......... . 105 3.17.1Subscripting....... . . . 106 3.17.2 Initialization....... . . . 106 3.18 Arrays and Pointers....... . . . 106 3.19 Passing Arrays to Functions....... 107 3.20 Problem Solving: Random Numbers.... . . . 108 3.21 Software Engineering: Structured Programming.. . . 111 3.22 Core Language ADT: char* String..... 114 3.23 Multidimensional Arrays...... . . . 117 3.24 Operators new and delete...... . 120 3.24.1 Vector Instead of Array..... . . . 123 3.24.2 String Instead of char*..... . . . 124 3.25 Software Engineering: Program Correctness.... 124 3.26 Dr. P’s Prescriptions....... . . 127 3.27 C++ Compared with Java...... . . . 128 Summary......... . . . 130 Review Questions........ 132 Exercises......... . . . 133 4 Classes and Abstract Data Types 139 4.1 The Aggregate Type class and struct.... . . . 140 4.2 Member Selection Operator...... . 141 4.3 Member Functions........ . . . 143 4.4 Access: Private and Public...... . . 146 4.5 Classes.......... 147 4.6 Class Scope......... . 150 4.6.1 Scope Resolution Operator..... 150 4.6.2 Nested Classes....... . 152 4.7 An Example: Flushing....... . 153 4.8 The this Pointer........ . 158 4.9 static Members......... 159 4.10 const Members......... . 161 4.10.1Mutable Members...... . . . 163 4.11 A Container Class Example: ch_stack..... 164 4.12 Software Engineering: Class Design.... . . . 166 4.12.1Trade-Offs in Design...... . 168 4.12.2Unified Modeling Language (UML) and Design. . . 169 4.13 Dr. P’s Prescriptions....... . . 170 4.14 C++ Compared with Java...... . . . 171 4.15 Advanced Topics........ . 172 4.15.1Pointer to Class Member..... . . 172 4.15.2Unions........ . . . 174 4.15.3Bit Fields........ . . 175 Summary......... . . . 177 Review Questions........ 178 Exercises......... . . . 179 5 Ctors, Dtors, Conversions, and Operator Overloading 183 5.1 Classes with Constructors...... . . 184 5.1.1 The Default Constructor..... . . 186 5.1.2 Constructor Initializer...... 187 5.1.3 Constructors as Conversions..... . . 187 5.1.4 Improving the point Class..... 189 5.1.5 Constructing a Stack...... . 190 5.1.6 The Copy Constructor...... 193 5.2 Classes with Destructors...... . . . 195 5.3 Members That Are Class Types..... . . 195 5.4 Example: A Singly Linked List..... . . . 196 5.5 Strings Using Reference Semantics.... . . . 201 5.6 Constructor Issues and Mysteries..... 204 5.6.1 Destructor Details...... . . . 205 5.6.2 Constructor Pragmatics...... . . 206 5.7 Polymorphism Using Function Overloading... . 206 5.8 ADT Conversions......... 207 5.9 Overloading and Signature Matching.... . . 208 5.10 Friend Functions........ . 211 5.11 Overloading Operators....... 213 5.12 Unary Operator Overloading...... 214 5.13 Binary Operator Overloading...... 217 5.14 Overloading the Assignment Operator.... . 219 5.15 Overloading the Subscript Operator..... . . 220 5.16 Overloading Operator () for Indexing.... 221 5.17 Overloading << and >>....... 222 5.18 Overloading ->........ . . 223 5.19 Overloading new and delete...... . . . 224 5.20 More Signature Matching....... . . 227 5.21 Software Engineering: When to Use Overloading.. . 228 5.22 Dr. P’s Prescriptions....... . . 229 5.23 C++ Compared with Java...... . . . 231 Summary......... . . . 235 Review Questions........ 236 Exercises......... . . . 237 6 Templates and Generic Programming 243 6.1 Template Class stack....... . 246 6.2 Function Templates....... . . . 248 6.2.1 Signature Matching and Overloading.... 250 6.2.2 How to Write a Simple Function: square().. . . . 252 6.3 Generic Code Development: Quicksort..... 253 6.3.1 Converting to a Generic quicksort()... . . . 256 6.4 Class Templates........ . 260 6.4.1 Friends........ . . . 260 6.4.2 Static Members....... . 260 6.4.3 Class Template Arguments..... 261 6.4.4 Default Template Arguments..... . . 261 6.4.5 Member Templates...... . . 262 6.5 Parameterizing the Class vector...... 262 6.6 Using STL: string, vector, and complex... . . 265 6.6.1 string and basic_string<>.... . . . 265 6.6.2 vector<> in STL....... 267 6.6.3 Using complex<>...... . . . 267 6.6.4 limits and Other Useful Templates.... . . 268 6.7 Software Engineering: Reuse and Generics.... . 269 6.7.1 Debugging Template Code..... 269 6.7.2 Special Considerations..... . . . 270 6.7.3 Using typename....... 271 6.8 Dr. P’s Prescriptions....... . . 272 6.9 C++ Compared with Java...... . . . 272 Summary......... . . . 276 Review Questions........ 277 Exercises......... . . . 278 7 Standard Template Library 280 7.1 A Simple STL Example....... . 280 7.2 Containers.......... . 283 7.2.1 Sequence Containers....... 285 7.2.2 Associative Containers..... . . . 288 7.2.3 Container Adaptors...... . . 293 7.3 Iterators......... . . . 296 7.3.1 Iterators for istream and ostream... . . . 297 7.3.2 Iterator Adaptors....... . . . 300 7.4 Algorithms......... . 302 7.4.1 Sorting Algorithms...... . . 302 7.4.2 Nonmutating Sequence Algorithms... . . 305 7.4.3 Mutating Sequence Algorithms.... . 307 7.4.4 Numerical Algorithms...... 310 7.5 Numerical Integration Made Easy..... . 311 7.6 STL: Function Objects....... . 315 7.6.1 Building a Function Object..... 317 7.6.2 Function Adaptors....... . . 318 7.7 Allocators......... . . 320 7.8 Software Engineering: STL Use...... . . 320 7.8.1 Syntax Bugs....... . . . 321 7.9 Dr. P’s Prescriptions....... . . 322 7.10 C++ Compared with Java...... . . . 323 Summary......... . . . 324 Review Questions........ 325 Exercises......... . . . 326 8 Inheritance and OOP 328 8.1 A Derived Class........ . . 329 8.1.1 More Unified Modeling Language (UML)... . . 333 8.2 A Student ISA Person....... . . 334 8.3 Virtual Functions: Dynamic Determination... . . 337 8.3.1 Overloading and Overriding Confusion.. . . . 340 8.3.2 A Canonical Example: Class shape... . . 342 8.4 Abstract Base Classes....... . 343 8.5 Templates and Inheritance....... . 350 8.6 Multiple Inheritance........ . . 351 8.7 RTTI and Other Fine Points...... . 353 8.7.1 Finer Points........ 354 8.8 Software Engineering: Inheritance and Design.. . . . 355 8.8.1 Subtyping Form........ 356 8.8.2 Code Reuse........ . . . 357 8.9 Dr. P’s Prescriptions....... . . 358 8.10 C++ Compared with Java...... . . . 358 Summary......... . . . 361 Review Questions........ 362 Exercises......... . . . 363 9 Input/Output 366 9.1 The Output Class ostream....... . 366 9.2 Formatted Output and iomanip..... . . 367 9.3 User-Defined Types: Output....... 372 9.4 The Input Class istream....... . . . 374 9.5 Files.......... . . 375 9.6 Using Strings as Streams...... . . . 379 9.7 The Functions and Macros in ctype.... . . . 380 9.8 Using Stream States........ . . 380 9.9 Mixing I/O Libraries........ . . 383 9.10 Software Engineering: I/O...... . . 384 9.11 Dr. P’s Prescriptions....... . . 386 9.12 C++ Compared with Java...... . . . 386 Summary......... . . . 389 Review Questions........ 390 Exercises......... . . . 391 10Exceptions and Program Correctness 394 10.1 Using the assert Library....... . . . 394 10.2 C++ Exceptions........ . . 397 10.3 Throwing Exceptions....... . . 398 10.3.1Rethrown Exceptions....... 400 10.3.2Exception Expressions..... . . . 401 10.4 try Blocks.......... . 404 10.5 Handlers......... . . . 405 10.6 Converting Assertions to Exceptions.... . . 405 10.7 Exception Specification....... 408 10.8 terminate() and unexpected()..... 409 10.9 Standard Exceptions and Their Uses.... . . 409 10.10 Software Engineering: Exception Objects... . . . 411 10.11 Dr. P’s Prescriptions....... . . 413 10.12 C++ Compared with Java...... . . . 414 Summary......... . . . 417 Review Questions........ 418 Exercises......... . . . 419 11OOP Using C++ 421 11.1 OOP Language Requirements...... . . . 422 11.1.1ADTs: Encapsulation and Data Hiding... 423 11.1.2Reuse and Inheritance...... 423 11.1.3Polymorphism........ . 424 11.2 OOP: The Dominant Programming Methodology.. . 425 11.3 Designing with OOP in Mind...... 432 11.4 Class-Responsibility-Collaborator...... 434 11.4.1CRC Cards......... 435 11.5 Design Patterns........ . . 436 11.6 A Further Assessment of C++..... . . . 437 11.6.1Why C++ Is Better Than Java.... . . . 438 11.6.2A Short Rebuttal....... 439 11.7 Software Engineering: Last Thoughts.... . . 439 11.8 Dr. P’s Prescriptions....... . . 440 11.9 C++ Compared with Java...... . . . 441 Summary......... . . . 447 Review Questions........ 448 Exercises......... . . . 449 AASCII Character Codes 451 B Operator Precedence and Associativity 453 C String Library 455 C.1 Constructors......... 456 C.2 Member Functions........ . . . 457 C.3 Global Operators........ . 460 DThe tio Library 462 D.1 Console.......... . . . 462 D.2 FormattedWriter........ . 463 D.3 PrintFileWriter........ . . . 472 D.4 ReadException......... . . 472 D.5 ReadInput......... . . 473 Index 482

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值