InterpretDL实现ResNet的可解释性分析
InterpretingResNetModelWithLRP(Layer-wiRelevancePropagation)
使⽤Layer-wiRelevancePropagation对ResNet和VGG⽹络进⾏可解释的分析。
StarandFork!
本项⽬的所有代码和tutorials均源于PaddlePaddle的仓库,欢迎⼤家Star和Fork!
InterpretDL:InterpretationofDeepLearningModelsbadonPaddlePaddle
InterpretDL,shortforinterpretationsofdeeplearningmodels,olkit
containsimplementationsofmanyinterpretationalgorithms,includingLIME,Grad-CAM,IntegratedGradientsandmore.
SomeSOTAandnewinterpretationalgorithmsarealsoimplemented.
InterpretDLisunderactiveconstructionandallcontributionsarewelcome!
克隆仓库
#下载⽐较慢附上了压缩包
#!gitclone/PaddlePaddle/
%cd~
!unzip-oqdata/data98915/
%cdInterpretDL/tutorials
/home/aistudio
/home/aistudio/InterpretDL/tutorials
#安装依赖
!pipinstallscikit-image
!all
Layer-wiRelevancePropagation(LRP)isanexplanationtechniqueapplicabletomodelsstructuredasneuralnetworks,
,videos,ortext.
LRPoperatesbypropagatingthepredictionbackwardsintheneuralnetwork,bymeansofpurpolydesignedlocal
propagationrules.
tice,state-of-the-artneuralnetworkssuch
asResNetmakeuofmorecomplexlayerssuchasconvolutionsandpooling.
Inthisca,LRPrulesaremoreconvenientlyimplementedbycastingtheoperationsofthefour-stepprocedureaboveas
perationsarereadilyavailableinneuralnetworkframeworkssuch
asPaddlePaddleandPytorch,andcanthereforebereudforthepurpoofimplementingLRP.
Here,wetaketheResNetpretrainednetworkforimageclassification.
LRPRulesforDeepRectifierNetworks
BasicRule(LRP-0)
Thisruleredistributesinproportionto:
EpsilonRule(LRP-)
f(x)
R=jRk
∑
aw∑0,jjjk
awjjk
k
ϵ
AfirstenhancementofthebasicLRP-0ruleconsistsofaddingasmallpositiveterminthedenominator:
Theroleofistoabsorbsomerelevancewhe
becomeslarger,picallyleadstoexplanationsthat
aresparrintermsofinputfeaturesandlessnoisy.
GammaRule(LRP-)
Anotherenhancementwhichweintroducehereisobtainedbyfavoringtheeffectofpositivecontributionsovernegative
contributions:
eas,negativecontributionsstartto
valenceofpositivecontributionshasalimitingeffectonhowlargepositiveandnegativerelevancecan
lpstodelivermorestableexplanations.
ImplementingLRPEfficiently
erthegenericrule
putationofthisropagationrulecanbedecompodinfoursteps:
KaTeXparerror:Nosuchenvironment:align*atposition8:begin{align*}forall_k:~z_k…
Thefirststepisaforwardpassonacopyofthelayerwheretheweightsandbiashavebeenappliedthemap,to
third
step,onenotesthatcanalsobeexpresdasthegradientcomputation:
whereisthevectoroflower-layeractivations,whereisafunctionofit,andwhereskisinsteadtreatedas
adientcanbecomputedviaautomaticdifferentiation,whichisavailableinmostneuralnetworkslibraries.
Inthisrepository,weurule:
Youcantpropervalueofandtogettheruleorruleifyouneed.
R=jRk
∑
ϵ+aw∑0,jjjk
awjjk
k
ϵ
ϵ
γ
R=jRk
∑
a⋅(w+γw)∑0,jjjkjk
+
a⋅(w+γw)jjkjk
+
k
γγ
R=jRk
∑
ϵ+aρ(w)∑0,jjjk
aρ(w)jjk
k
LRP−0/ϵ/γ
θ→ρ(θ)
ϵ
cj
c=j[∇(z(a)⋅s))]k
∑
kk
j
a=(a)jjzk
LRP−αβ
LRP−αβ
R=j(α−β)Rk
∑
(aw)∑0,jjjk
+
(aw)jjk
+
(aw)∑0,jjjk
−
(aw)jjk
−
k
αβLRP−0LRP−γ
importpaddle
importinterpretdlasit
Loadinputimage
fromPILimportImage
_simportread_image
img_path='asts/'
x=ray(read_image(img_path)[0])
x
Loadlrpmodel
Supportedlrpmodel
resnet18
resnet34
resnet50
resnet101
resnet152
vgg16
More…
Loadmodelresnet50
_modelimportresnet50
paddle_model=resnet50(pretrained=True)
#paddle_model
100%|██████████|151272/151272[00:03<00:00,47730.41it/s]
LoadLRPinterperter
lrp=nterpreter(paddle_model,u_cuda=Fal)
Gettheinterpretationresult
Gettheinterpretationresultfromthemaximumproblabel
R=ret(
img_path,
label=None,
visual=True,
save_path=None)
bullmastiff(243):0.429
Brabancongriffon(262):0.048
tiger,Pantheratigr(292):0.044
boxer(242):0.038
doormat,welcomemat(539):0.029
tabby,tabbycat(281):0.026
tigercat(282):0.023
pug,pug-dog(254):0.022
Egyptiancat(285):0.013
bloodhound,sleuthho(163):0.012
Ulabel:243,bullmastiff
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349:DeprecationWarning:UsingorimportingtheABC
sfrom'collections'insteadoffrom''isdeprecated,andin3.8itwillstopworking
ifisinstance(obj,or):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366:DeprecationWarning:UsingorimportingtheABC
sfrom'collections'insteadoffrom''isdeprecated,andin3.8itwillstopworking
returnlist(data)ifisinstance(data,gView)eldata
tlabeltotigercat(282)
R=ret(
img_path,
label=282,
visual=True,
save_path=None)
bullmastiff(243):0.429
Brabancongriffon(262):0.048
tiger,Pantheratigr(292):0.044
boxer(242):0.038
doormat,welcomemat(539):0.029
tabby,tabbycat(281):0.026
tigercat(282):0.023
pug,pug-dog(254):0.022
Egyptiancat(285):0.013
bloodhound,sleuthho(163):0.012
Ulabel:282,tigercat
Changemodeltoresnet101
_modelimportresnet101
paddle_model=resnet101(pretrained=True)
100%|██████████|263160/263160[00:04<00:00,63094.35it/s]
lrp=nterpreter(paddle_model,u_cuda=Fal)
R=ret(
img_path,
label=None,
visual=True,
save_path=None)
tigercat(282):0.483
tabby,tabbycat(281):0.212
tiger,Pantheratigr(292):0.171
Egyptiancat(285):0.083
bullmastiff(243):0.017
doormat,welcomemat(539):0.007
boxer(242):0.003
spaceheater(811):0.003
lynx,catamount(287):0.002
radiator(753):0.001
Ulabel:282,tigercat
tlabeltobullmastiff(243)
R=ret(
img_path,
label=243,
visual=True,
save_path=None)
tigercat(282):0.483
tabby,tabbycat(281):0.212
tiger,Pantheratigr(292):0.171
Egyptiancat(285):0.083
bullmastiff(243):0.017
doormat,welcomemat(539):0.007
boxer(242):0.003
spaceheater(811):0.003
lynx,catamount(287):0.002
radiator(753):0.001
Ulabel:243,bullmastiff
Changemodeltovgg16
_modelimportvgg16
paddle_model=vgg16(pretrained=True)
100%|██████████|817517/817517[00:13<00:00,62384.04it/s]
lrp=nterpreter(paddle_model,u_cuda=Fal)
R=ret(
img_path,
label=None,
visual=True,
save_path=None)
bullmastiff(243):0.620
tiger,Pantheratigr(292):0.306
GreatDane(246):0.071
pug,pug-dog(254):0.003
boxer(242):0.000
Leonberg(255):0.000
bloodhound,sleuthho(163):0.000
tigercat(282):0.000
Germanshepherd,Ger(235):0.000
Brabancongriffon(262):0.000
Ulabel:243,bullmastiff
tlabeltotigercat(282)
R=ret(
img_path,
label=282,
visual=True,
save_path=None)
bullmastiff(243):0.620
tiger,Pantheratigr(292):0.306
GreatDane(246):0.071
pug,pug-dog(254):0.003
boxer(242):0.000
Leonberg(255):0.000
bloodhound,sleuthho(163):0.000
tigercat(282):0.000
Germanshepherd,Ger(235):0.000
Brabancongriffon(262):0.000
Ulabel:282,tigercat
关于作者
袁廷翼
西安交通⼤学2020级研究⽣控制科学与⼯程
百度⼤数据实验室实习⽣
感兴趣的⽅向为:机器⼈,ml,cv,⽹络可解释性
AIstudio主页:
Wechat:yty___
有问题欢迎交流,喜欢可以三连⼀下!
本文发布于:2022-11-26 07:35:23,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/23708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |