ó šÄïYc@ sdZddlmZddddddd d d d d ddddddgZddlmZddlmZddlm Z d„Z defd„ƒYZ defd„ƒYZ defd„ƒYZ defd„ƒYZdefd„ƒYZdefd„ƒYZid d!d!d!d!gd"d"d#d$d%gfd&6d ddd'dgd"d"d#d$d%gfd(6d)ddd'dgd"d$d%d*d+gfd,6d)ddd-dgd"d$d%d*d+gfd.6d)dd/d0dgd"d$d%d*d+gfd16ZeegZie d 6e d)6ie d 6ed)6gZeeƒd2„Zd3„Zd4„Zd5„Zd6„Zd7„Zd8„Zd9„Zd:„Zd;„Zd<„Zd=S(>sResNets, implemented in Gluon.iÿÿÿÿ(tdivisiontResNetV1tResNetV2t BasicBlockV1t BasicBlockV2t BottleneckV1t BottleneckV2t resnet18_v1t resnet34_v1t resnet50_v1t resnet101_v1t resnet152_v1t resnet18_v2t resnet34_v2t resnet50_v2t resnet101_v2t resnet152_v2t get_resneti(tcpui(t HybridBlock(tnnc C s+tj|ddd|dddtd|ƒS(Nt kernel_sizeitstridestpaddingituse_biast in_channels(RtConv2DtFalse(tchannelststrideR((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyt_conv3x3#scB s&eZdZedd„Zd„ZRS(sÛBasicBlock V1 from `"Deep Residual Learning for Image Recognition" `_ paper. This is used for ResNet V1 for 18, 34 layers. Parameters ---------- channels : int Number of output channels. stride : int Stride size. downsample : bool, default False Whether to downsample the input. in_channels : int, default 0 Number of input channels. Default is 0, to infer from the graph. ic K stt|ƒj|tjddƒ|_|jjt|||ƒƒ|jjtjƒƒ|jjtj dƒƒ|jjt|d|ƒƒ|jjtjƒƒ|r tjddƒ|_ |j jtj |ddd|dt d|ƒƒ|j jtjƒƒn d|_ dS( NtprefixttreluiRRRR(tsuperRt__init__RtHybridSequentialtbodytaddRt BatchNormt Activationt downsampleRRtNone(tselfRRR)Rtkwargs((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR#9s!cC sM|}|j|ƒ}|jr0|j|ƒ}n|j||ddƒ}|S(Ntact_typeR!(R%R)R((R+tFtxtresidual((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pythybrid_forwardIs  (t__name__t __module__t__doc__RR#R1(((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR)scB s&eZdZedd„Zd„ZRS(sáBottleneck V1 from `"Deep Residual Learning for Image Recognition" `_ paper. This is used for ResNet V1 for 50, 101, 152 layers. Parameters ---------- channels : int Number of output channels. stride : int Stride size. downsample : bool, default False Whether to downsample the input. in_channels : int, default 0 Number of input channels. Default is 0, to infer from the graph. ic K sƒtt|ƒj|tjddƒ|_|jjtj|dddddƒƒ|jjtjƒƒ|jjtj dƒƒ|jjt |d||dƒƒ|jjtjƒƒ|jjtj dƒƒ|jjtj|ddddƒƒ|jjtjƒƒ|rvtjddƒ|_ |j jtj|ddd|dt d |ƒƒ|j jtjƒƒn d|_ dS( NRR iRiRR!RR(R"RR#RR$R%R&RR'R(RR)RR*(R+RRR)RR,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR#fs )$%!cC sM|}|j|ƒ}|jr0|j|ƒ}n|j||ddƒ}|S(NR-R!(R%R)R((R+R.R/R0((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR1ys  (R2R3R4RR#R1(((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRVscB s&eZdZedd„Zd„ZRS(sßBasicBlock V2 from `"Identity Mappings in Deep Residual Networks" `_ paper. This is used for ResNet V2 for 18, 34 layers. Parameters ---------- channels : int Number of output channels. stride : int Stride size. downsample : bool, default False Whether to downsample the input. in_channels : int, default 0 Number of input channels. Default is 0, to infer from the graph. icK s˜tt|ƒj|tjƒ|_t|||ƒ|_tjƒ|_t|d|ƒ|_ |r‹tj |d|dt d|ƒ|_ n d|_ dS(NiRR(R"RR#RR'tbn1Rtconv1tbn2tconv2RRR)R*(R+RRR)RR,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR#–scC s|}|j|ƒ}|j|ddƒ}|jrE|j|ƒ}n|j|ƒ}|j|ƒ}|j|ddƒ}|j|ƒ}||S(NR-R!(R5R(R)R6R7R8(R+R.R/R0((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR1¢s (R2R3R4RR#R1(((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR…s cB s&eZdZedd„Zd„ZRS(såBottleneck V2 from `"Identity Mappings in Deep Residual Networks" `_ paper. This is used for ResNet V2 for 50, 101, 152 layers. Parameters ---------- channels : int Number of output channels. stride : int Stride size. downsample : bool, default False Whether to downsample the input. in_channels : int, default 0 Number of input channels. Default is 0, to infer from the graph. icK sætt|ƒj|tjƒ|_tj|ddddddtƒ|_tjƒ|_ t |d||dƒ|_ tjƒ|_ tj|dddddtƒ|_ |rÙtj|d|dtd|ƒ|_n d|_dS(NiRiRRR(R"RR#RR'R5RRR6R7RR8tbn3tconv3R)R*(R+RRR)RR,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR#Âs($cC sÂ|}|j|ƒ}|j|ddƒ}|jrE|j|ƒ}n|j|ƒ}|j|ƒ}|j|ddƒ}|j|ƒ}|j|ƒ}|j|ddƒ}|j|ƒ}||S(NR-R!(R5R(R)R6R7R8R9R:(R+R.R/R0((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR1Ðs (R2R3R4RR#R1(((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR±scB s2eZdZded„Zdd„Zd„ZRS(s>ResNet V1 model from `"Deep Residual Learning for Image Recognition" `_ paper. Parameters ---------- block : HybridBlock Class for the residual block. Options are BasicBlockV1, BottleneckV1. layers : list of int Numbers of layers in each block channels : list of int Numbers of channels in each block. Length should be one larger than layers list. classes : int, default 1000 Number of classification classes. thumbnail : bool, default False Enable thumbnail. ièc K sátt|ƒj|t|ƒt|ƒdks8t‚|jƒ—tjddƒ|_|rƒ|jj t |dddƒƒn€|jj tj |dddddt d dƒƒ|jj tj ƒƒ|jj tjd ƒƒ|jj tjdddƒƒxmt|ƒD]_\}}|dkr.dnd} |jj |j||||d| |dd ||ƒƒqWtjddƒ|_|jj tjƒƒ|jj tjƒƒ|jj tj|d |d ƒƒWdQXdS( NiRR iiiiRRR!tin_unitsiÿÿÿÿ(R"RR#tlentAssertionErrort name_scopeRR$tfeaturesR&RRRR'R(t MaxPool2Dt enumeratet _make_layert classifiertGlobalAvgPool2DtFlattentDense( R+tblocktlayersRtclassest thumbnailR,tit num_layerR((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR#ös&" #(  ic C s˜tjdd|ƒ}|jƒp|j|||||kd|ddƒƒx=t|dƒD]+}|j||dtd|ddƒƒq_WWdQX|S(NRsstage%d_RR i(RR$R>R&trangeR( R+RGRHRRt stage_indexRtlayert_((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRBs ! /cC s"|j|ƒ}|j|ƒ}|S(N(R?RC(R+R.R/((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR1s(R2R3R4RR#RBR1(((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRäs cB s2eZdZded„Zdd„Zd„ZRS(s>ResNet V2 model from `"Identity Mappings in Deep Residual Networks" `_ paper. Parameters ---------- block : HybridBlock Class for the residual block. Options are BasicBlockV1, BottleneckV1. layers : list of int Numbers of layers in each block channels : list of int Numbers of channels in each block. Length should be one larger than layers list. classes : int, default 1000 Number of classification classes. thumbnail : bool, default False Enable thumbnail. ièc K sBtt|ƒj|t|ƒt|ƒdks8t‚|jƒøtjddƒ|_|jj tj dt dt ƒƒ|r¥|jj t |dddƒƒn€|jj tj |ddd dd t d dƒƒ|jj tj ƒƒ|jj tjd ƒƒ|jj tjdd dƒƒ|d}xwt|ƒD]i\}} |dkrZdnd } |jj |j|| ||d| |dd |ƒƒ||d}q<Wtjddƒ|_|jj tj ƒƒ|jj tjd ƒƒ|jj tjƒƒ|jj tjƒƒ|jj tj|d |ƒƒWdQXdS(NiRR tscaletcenteriiiiRRR!R;(R"RR#R<R=R>RR$R?R&R'RRRR(R@RARBRCRDRERF( R+RGRHRRIRJR,RRKRLR((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR#0s0" "#(   ic C s˜tjdd|ƒ}|jƒp|j|||||kd|ddƒƒx=t|dƒD]+}|j||dtd|ddƒƒq_WWdQX|S(NRsstage%d_RR i(RR$R>R&RMR( R+RGRHRRRNRRORP((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRBMs ! /cC s"|j|ƒ}|j|ƒ}|S(N(R?RC(R+R.R/((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR1Vs(R2R3R4RR#RBR1(((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRs t basic_blockii@i€iiiii"t bottle_neckiii2iieii$i˜c K sˆt|\}}}t|d}t|d|} || |||} |r„ddlm} | j| d||fƒd|ƒn| S(s[ResNet V1 model from `"Deep Residual Learning for Image Recognition" `_ paper. ResNet V2 model from `"Identity Mappings in Deep Residual Networks" `_ paper. Parameters ---------- version : int Version of ResNet. Options are 1, 2. num_layers : int Numbers of layers. Options are 18, 34, 50, 101, 152. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii(tget_model_files resnet%d_v%dtctx(t resnet_spectresnet_net_versionstresnet_block_versionst model_storeRUt load_params( tversiont num_layerst pretrainedRVR,t block_typeRHRt resnet_classt block_classtnetRU((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRis&cK stdd|S(sUResNet-18 V1 model from `"Deep Residual Learning for Image Recognition" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRƒs cK stdd|S(sUResNet-34 V1 model from `"Deep Residual Learning for Image Recognition" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii"(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRs cK stdd|S(sUResNet-50 V1 model from `"Deep Residual Learning for Image Recognition" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii2(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR s cK stdd|S(sVResNet-101 V1 model from `"Deep Residual Learning for Image Recognition" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. iie(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR ªs cK stdd|S(sVResNet-152 V1 model from `"Deep Residual Learning for Image Recognition" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii˜(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR ·s cK stdd|S(sUResNet-18 V2 model from `"Identity Mappings in Deep Residual Networks" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR Äs cK stdd|S(sUResNet-34 V2 model from `"Identity Mappings in Deep Residual Networks" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii"(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyR Ñs cK stdd|S(sUResNet-50 V2 model from `"Identity Mappings in Deep Residual Networks" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii2(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRÞs cK stdd|S(sVResNet-101 V2 model from `"Identity Mappings in Deep Residual Networks" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. iie(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRës cK stdd|S(sVResNet-152 V2 model from `"Identity Mappings in Deep Residual Networks" `_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. ii˜(R(R,((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pyRøs N( R4t __future__Rt__all__tcontextRRGRR RRRRRRRRRWRXRYRRRRR R R R R RRR(((sCbuild/bdist.linux-armv7l/egg/mxnet/gluon/model_zoo/vision/resnet.pytsH  -/,3:?.+++.