mirror of
https://github.com/wjlin0/wjlin0.github.io.git
synced 2026-10-28 19:40:49 +00:00
1 line
43 KiB
JavaScript
1 line
43 KiB
JavaScript
"use strict";(globalThis.webpackChunkblog=globalThis.webpackChunkblog||[]).push([[2360],{3850:(n,e,s)=>{s.d(e,{A:()=>l});const l=s.p+"assets/images/expansion-0109d5db605c33d1ee3b5a2b18a95927.jpg"},28453:(n,e,s)=>{s.d(e,{R:()=>o,x:()=>r});var l=s(96540);const t={},i=l.createContext(t);function o(n){const e=l.useContext(i);return l.useMemo(function(){return"function"==typeof n?n(e):{...e,...n}},[e,n])}function r(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(t):n.components||t:o(n.components),l.createElement(i.Provider,{value:e},n.children)}},56974:(n,e,s)=>{s.d(e,{A:()=>l});const l=s.p+"assets/images/block-bd525508c9ef693cb5a45c56b07519d3.jpg"},58706:(n,e,s)=>{s.r(e),s.d(e,{assets:()=>a,contentTitle:()=>r,default:()=>p,frontMatter:()=>o,metadata:()=>l,toc:()=>c});const l=JSON.parse('{"id":"\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u5341\u7ae0/ResNet\u6e90\u7801\u89e3\u8bfb","title":"ResNet\u6e90\u7801\u89e3\u8bfb.md","description":"|\u4f5c\u8005|\u4fee\u8ba2\u65f6\u95f4|","source":"@site/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/10.AI/01.PyTorch/10.\u7b2c\u5341\u7ae0/ResNet\u6e90\u7801\u89e3\u8bfb.md","sourceDirName":"\u4e2a\u4eba\u77e5\u8bc6\u5e93/10.AI/01.PyTorch/10.\u7b2c\u5341\u7ae0","slug":"/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u5341\u7ae0/ResNet\u6e90\u7801\u89e3\u8bfb","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u5341\u7ae0/ResNet\u6e90\u7801\u89e3\u8bfb","draft":false,"unlisted":false,"editUrl":"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/10.AI/01.PyTorch/10.\u7b2c\u5341\u7ae0/ResNet\u6e90\u7801\u89e3\u8bfb.md","tags":[],"version":"current","frontMatter":{},"sidebar":"tutorialSidebar","previous":{"title":"RNN\u8be6\u89e3\u53ca\u5176\u5b9e\u73b0.md","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u5341\u7ae0/RNN\u8be6\u89e3\u53ca\u5176\u5b9e\u73b0"},"next":{"title":"Swin-Transformer\u89e3\u8bfb.md","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u5341\u7ae0/Swin-Transformer\u89e3\u8bfb"}}');var t=s(74848),i=s(28453);const o={},r="ResNet\u6e90\u7801\u89e3\u8bfb.md",a={},c=[{value:"1 \u57fa\u672c\u4ecb\u7ecd",id:"1-\u57fa\u672c\u4ecb\u7ecd",level:2},{value:"2 \u6e90\u7801\u89e3\u8bfb",id:"2-\u6e90\u7801\u89e3\u8bfb",level:2},{value:"2.1 \u5377\u79ef\u6838\u7684\u5c01\u88c5",id:"21-\u5377\u79ef\u6838\u7684\u5c01\u88c5",level:3},{value:"2.2 \u57fa\u672c\u6a21\u5757\u7684\u8bbe\u8ba1",id:"22-\u57fa\u672c\u6a21\u5757\u7684\u8bbe\u8ba1",level:3},{value:"2.2.1 Shortcut Connection.",id:"221-shortcut-connection",level:4},{value:"2.2.2 BasicBlock",id:"222-basicblock",level:4},{value:"2.2.3 BottleNeck",id:"223-bottleneck",level:4},{value:"2.3 \u7f51\u7edc\u6574\u4f53\u7ed3\u6784",id:"23-\u7f51\u7edc\u6574\u4f53\u7ed3\u6784",level:3},{value:"3 \u603b\u7ed3",id:"3-\u603b\u7ed3",level:2}];function d(n){const e={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",h4:"h4",header:"header",img:"img",li:"li",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,i.R)(),...n.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(e.header,{children:(0,t.jsx)(e.h1,{id:"resnet\u6e90\u7801\u89e3\u8bfbmd",children:"ResNet\u6e90\u7801\u89e3\u8bfb.md"})}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,t.jsxs)(e.table,{children:[(0,t.jsx)(e.thead,{children:(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.th,{style:{textAlign:"left"},children:"\u4f5c\u8005"}),(0,t.jsx)(e.th,{style:{textAlign:"left"},children:"\u4fee\u8ba2\u65f6\u95f4"})]})}),(0,t.jsx)(e.tbody,{children:(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{style:{textAlign:"left"},children:(0,t.jsx)(e.img,{src:"https://img.shields.io/badge/wjlin0-%E4%B8%AA%E4%BA%BA%E7%9F%A5%E8%AF%86%E5%BA%93-green",alt:"wjlin0"})}),(0,t.jsx)(e.td,{style:{textAlign:"left"},children:"2026-06-07 20:53:41"})]})})]}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,t.jsxs)(e.table,{children:[(0,t.jsx)(e.thead,{children:(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.th,{style:{textAlign:"left"},children:"\u4f5c\u8005"}),(0,t.jsx)(e.th,{style:{textAlign:"left"},children:"\u4fee\u8ba2\u65f6\u95f4"})]})}),(0,t.jsx)(e.tbody,{children:(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{style:{textAlign:"left"},children:(0,t.jsx)(e.img,{src:"https://img.shields.io/badge/wjlin0-%E4%B8%AA%E4%BA%BA%E7%9F%A5%E8%AF%86%E5%BA%93-green",alt:"wjlin0"})}),(0,t.jsx)(e.td,{style:{textAlign:"left"},children:"2025-10-31 15:44:29"})]})})]}),"\n",(0,t.jsx)(e.h1,{id:"resnet\u6e90\u7801\u89e3\u8bfb",children:"ResNet\u6e90\u7801\u89e3\u8bfb"}),"\n",(0,t.jsx)(e.p,{children:"\u672c\u6587\u5bf9\u6b8b\u5dee\u795e\u7ecf\u7f51\u7edc\uff08ResNet\uff09\u7684\u6e90\u7801\u8fdb\u884c\u89e3\u8bfb\u3002\u6b8b\u5dee\u795e\u7ecf\u7f51\u7edc\u662f\u7531\u5fae\u8f6f\u7814\u7a76\u9662\u7684\u4f55\u607a\u660e\u3001\u5f20\u7965\u96e8\u3001\u4efb\u5c11\u537f\u3001\u5b59\u5251\u7b49\u4eba\u63d0\u51fa\u7684\u3002\u5b83\u7684\u4e3b\u8981\u8d21\u732e\u662f\u53d1\u73b0\u4e86\u5728\u589e\u52a0\u7f51\u7edc\u5c42\u6570\u7684\u8fc7\u7a0b\u4e2d\uff0c\u200e\u968f\u7740\u8bad\u7ec3\u7cbe\u5ea6(Training accuracy)\u200e\u9010\u6e10\u8d8b\u4e8e\u9971\u548c\uff0c\u7ee7\u7eed\u589e\u52a0\u5c42\u6570\uff0ctraining accuracy \u5c31\u4f1a\u51fa\u73b0\u4e0b\u964d\u7684\u73b0\u8c61\uff0c\u800c\u8fd9\u79cd\u4e0b\u964d\u4e0d\u662f\u7531\u8fc7\u62df\u5408\u9020\u6210\u7684\u3002\u4ed6\u4eec\u5c06\u8fd9\u4e00\u73b0\u8c61\u79f0\u4e4b\u4e3a\u201c\u9000\u5316\u73b0\u8c61\uff08Degradation\uff09\u201d\uff0c\u5e76\u9488\u5bf9\u9000\u5316\u73b0\u8c61\u53d1\u660e\u4e86 \u201c\u5feb\u6377\u8fde\u63a5\uff08Shortcut connection\uff09\u201d\uff0c\u6781\u5927\u7684\u6d88\u9664\u4e86\u6df1\u5ea6\u8fc7\u5927\u7684\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u56f0\u96be\u95ee\u9898\u3002\u795e\u7ecf\u7f51\u7edc\u7684\u201c\u6df1\u5ea6\u201d\u9996\u6b21\u7a81\u7834\u4e86100\u5c42\u3001\u6700\u5927\u7684\u795e\u7ecf\u7f51\u7edc\u751a\u81f3\u8d85\u8fc7\u4e861000\u5c42\u3002\uff08\u5728\u6b64\uff0c\u5411\u5df2\u6545\u7684\u5b59\u5251\u535a\u58eb\u8868\u793a\u5d07\u9ad8\u7684\u656c\u610f\uff09"}),"\n",(0,t.jsx)(e.p,{children:"\u901a\u8fc7\u672c\u6587\u4f60\u5c06\u5b66\u4e60\u5230\uff1a"}),"\n",(0,t.jsxs)(e.ul,{children:["\n",(0,t.jsxs)(e.li,{children:["\n",(0,t.jsx)(e.p,{children:"\u68af\u5ea6\u6d88\u5931/\u7206\u70b8\u7684\u7b80\u4ecb"}),"\n"]}),"\n",(0,t.jsxs)(e.li,{children:["\n",(0,t.jsx)(e.p,{children:"\u4ee3\u7801\u91cc\u9762\u4e3a\u4ec0\u4e48\u8981\u8bbe\u8ba1BasicBlock\u548cBottleneck\u4e24\u79cd\u7ed3\u6784"}),"\n"]}),"\n",(0,t.jsxs)(e.li,{children:["\n",(0,t.jsx)(e.p,{children:"\u4ee3\u7801\u91cc\u9762\u7684expansion\u4f5c\u7528"}),"\n"]}),"\n"]}),"\n",(0,t.jsx)(e.h2,{id:"1-\u57fa\u672c\u4ecb\u7ecd",children:"1 \u57fa\u672c\u4ecb\u7ecd"}),"\n",(0,t.jsxs)(e.p,{children:["\u968f\u7740\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u7684\u51fa\u73b0\uff0c\u4eba\u4eec\u53d1\u73b0\u591a\u5c42\u5377\u79ef\u6216\u8005\u5168\u8fde\u63a5\u7f51\u7edc\u7684\u6548\u679c\u5927\u4e8e\u5355\u5c42\u5377\u79ef\u6216\u8005\u5168\u8fde\u63a5\u7f51\u7edc\u3002\u4e8e\u662f\u5f88\u591a\u4eba\u6f5c\u610f\u8bc6\u8ba4\u4e3a\u7f51\u7edc\u7684\u5c42\u6570\u8d8a\u591a\uff0c\u5176\u6548\u679c\u5c31\u4f1a\u8d8a\u597d\u3002\u4f46\u662f\u5f53\u65f6\u5fae\u8f6f\u7814\u7a76\u9662\u7684\u4f55\u607a\u660e\u3001\u5f20\u7965\u96e8\u3001\u4efb\u5c11\u537f\u3001\u5b59\u5251\u7b49\u4eba\u53d1\u73b0\u52a0\u6df1\u7f51\u7edc\u7684\u6df1\u5ea6\u540e\uff0c\u6574\u4e2a\u7f51\u7edc\u7684\u6548\u679c\u53cd\u800c\u53d8\u5dee\u4e86\u8bb8\u591a\u3002\u4ed6\u4eec\u8ba4\u4e3a\u5f88\u6df1\u7684\u7f51\u7edc\u65e0\u6cd5\u8bad\u7ec3\u7684\u539f\u56e0\u53ef\u80fd\u662f\u7f51\u7edc\u5728\u4fe1\u606f\u4f20\u9012\u7684\u65f6\u5019\u6216\u591a\u6216\u5c11\u4f1a\u5b58\u5728\u4fe1\u606f\u4e22\u5931\uff0c\u635f\u8017\u7b49\u95ee\u9898\uff0c\u540c\u65f6\u8fd8\u53ef\u80fd\u51fa\u73b0\u68af\u5ea6\u6d88\u5931\u6216\u8005\u68af\u5ea6\u7206\u70b8\u73b0\u8c61\u3002\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u4eec\u63d0\u51fa\u4e86ResNet\u4ee5\u671f\u671b\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cResNet\u7684\u51fa\u73b0\u4e5f\u8ba9\u795e\u7ecf\u7f51\u7edc\u9010\u6e10\u771f\u6b63\u8d70\u5411\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\u3002ResNet\u6700\u5927\u7684\u8d21\u732e\u5728\u4e8e\u6dfb\u52a0\u4e86shortcut connection\u5c06\u8f93\u5165\u76f4\u63a5\u8fde\u63a5\u5230\u540e\u9762\u7684\u5c42\uff0c\u4e00\u5b9a\u7a0b\u5ea6\u7f13\u89e3\u4e86\u68af\u5ea6\u6d88\u5931\u548c\u68af\u5ea6\u7206\u70b8\u5e76\u63d0\u9ad8\u4e86\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\u7684\u6548\u679c\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u7684\u89e3\u91ca\u4e00\u4e0b",(0,t.jsx)(e.strong,{children:"\u68af\u5ea6\u6d88\u5931"}),"\u548c",(0,t.jsx)(e.strong,{children:"\u68af\u5ea6\u7206\u70b8"}),"\u3002"]}),"\n",(0,t.jsxs)(e.p,{children:["\u68af\u5ea6\u6d88\u5931\u548c\u68af\u5ea6\u7206\u70b8\u7684\u6839\u6e90\u4e3b\u8981\u662f\u56e0\u4e3a\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\u7ed3\u6784\u4ee5\u53ca\u53cd\u5411\u4f20\u64ad\u7b97\u6cd5\uff0c\u76ee\u524d\u4f18\u5316\u795e\u7ecf\u7f51\u7edc\u7684\u65b9\u6cd5\u90fd\u662f\u57fa\u4e8e\u53cd\u5411\u4f20\u64ad\u7684\u601d\u60f3\uff0c\u5373\u6839\u636e\u635f\u5931\u51fd\u6570\u8ba1\u7b97\u7684\u8bef\u5dee\u901a\u8fc7\u53cd\u5411\u4f20\u64ad\u7684\u65b9\u5f0f\uff0c\u6307\u5bfc\u6df1\u5ea6\u7f51\u7edc\u6743\u503c\u7684\u66f4\u65b0\u3002\u8bef\u5dee\u68af\u5ea6\u662f\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u8ba1\u7b97\u7684\u65b9\u5411\u548c\u6570\u91cf\uff0c\u7528\u4e8e\u4ee5\u6b63\u786e\u7684\u65b9\u5411\u548c\u5408\u9002\u7684\u91cf\u66f4\u65b0\u7f51\u7edc\u6743\u91cd\u3002 \u5728\u6df1\u5c42\u7f51\u7edc\u6216\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u4e2d\uff0c\u8bef\u5dee\u68af\u5ea6\u53ef\u5728\u66f4\u65b0\u4e2d\u7d2f\u79ef\uff0c\u53d8\u6210\u975e\u5e38\u5927\u7684\u68af\u5ea6\uff0c\u7136\u540e\u5bfc\u81f4\u7f51\u7edc\u6743\u91cd\u7684\u5927\u5e45\u66f4\u65b0\uff0c\u5e76\u56e0\u6b64\u4f7f\u7f51\u7edc\u53d8\u5f97\u4e0d\u7a33\u5b9a\u3002\u5728\u6781\u7aef\u60c5\u51b5\u4e0b\uff0c\u6743\u91cd\u7684\u503c\u53d8\u5f97\u975e\u5e38\u5927\uff0c\u4ee5\u81f3\u4e8e\u6ea2\u51fa\uff0c\u5bfc\u81f4 NaN \u503c\u3002 ",(0,t.jsx)(e.strong,{children:"\u7f51\u7edc\u5c42\u4e4b\u95f4\u7684\u68af\u5ea6\uff08\u503c\u5927\u4e8e 1.0\uff09\u91cd\u590d\u76f8\u4e58\u5bfc\u81f4\u7684\u6307\u6570\u7ea7\u589e\u957f\u4f1a\u4ea7\u751f\u68af\u5ea6\u7206\u70b8\u3002"})," \u5728\u6df1\u5ea6\u591a\u5c42\u611f\u77e5\u673a\u7f51\u7edc\u4e2d\uff0c\u68af\u5ea6\u7206\u70b8\u4f1a\u5f15\u8d77\u7f51\u7edc\u4e0d\u7a33\u5b9a\uff0c\u6700\u597d\u7684\u7ed3\u679c\u662f\u65e0\u6cd5\u4ece\u8bad\u7ec3\u6570\u636e\u4e2d\u5b66\u4e60\uff0c\u800c\u6700\u574f\u7684\u7ed3\u679c\u662f\u51fa\u73b0\u65e0\u6cd5\u518d\u66f4\u65b0\u7684 NaN \u6743\u91cd\u503c\u3002"]}),"\n",(0,t.jsxs)(e.p,{children:["\u800c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u68af\u5ea6\u4f1a\u53d8\u5f97\u975e\u5e38\u5c0f\uff0c ",(0,t.jsx)(e.strong,{children:"\u7f51\u7edc\u5c42\u4e4b\u95f4\u7684\u68af\u5ea6\uff08\u503c\u5c0f\u4e8e 1.0\uff09\u91cd\u590d\u76f8\u4e58\u5bfc\u81f4\u7684\u6307\u6570\u7ea7\u53d8\u5c0f\u4f1a\u4ea7\u751f\u68af\u5ea6\u6d88\u5931"}),"\u3002\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5b8c\u5168\u505c\u6b62\u795e\u7ecf\u7f51\u7edc\u7684\u8fdb\u4e00\u6b65\u8bad\u7ec3\u3002\u4f8b\u5982\uff0c\u4f20\u7edf\u7684\u6fc0\u6d3b\u51fd\u6570(\u5982\u53cc\u66f2\u6b63\u5207\u51fd\u6570)\u5177\u6709\u8303\u56f4(0,1)\u5185\u7684\u68af\u5ea6\uff0c\u53cd\u5411\u4f20\u64ad\u901a\u8fc7\u94fe\u5f0f\u6cd5\u5219\u8ba1\u7b97\u68af\u5ea6\u3002\u8fd9\u6837\u505a\u7684\u6548\u679c\u662f\uff0c\u7528\u8fd9\u4e9b\u5c0f\u6570\u5b57\u7684n\u4e58\u4ee5n\u6765\u8ba1\u7b97n\u5c42\u7f51\u7edc\u4e2d\u201c\u524d\u7aef\u201d\u5c42\u7684\u68af\u5ea6\uff0c\u8fd9\u610f\u5473\u7740\u68af\u5ea6(\u8bef\u5dee\u4fe1\u53f7)\u968fn\u5448\u6307\u6570\u9012\u51cf\uff0c\u800c\u524d\u7aef\u5c42\u7684\u8bad\u7ec3\u975e\u5e38\u7f13\u6162\u3002\u6700\u7ec8\u5bfc\u81f4\u66f4\u65b0\u505c\u6ede\u3002"]}),"\n",(0,t.jsx)(e.h2,{id:"2-\u6e90\u7801\u89e3\u8bfb",children:"2 \u6e90\u7801\u89e3\u8bfb"}),"\n",(0,t.jsxs)(e.p,{children:["\u4e3a\u4e86\u5e2e\u52a9\u5927\u5bb6\u5bf9ResNet\u6709\u66f4\u597d\u7684\u7406\u89e3\uff0c\u6211\u4eec\u4f7f\u7528",(0,t.jsx)(e.a,{href:"https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py",children:"torchvision\u7684ResNet\u6e90\u7801"}),"\u8fdb\u884c\u89e3\u8bfb\u3002"]}),"\n",(0,t.jsx)(e.h3,{id:"21-\u5377\u79ef\u6838\u7684\u5c01\u88c5",children:"2.1 \u5377\u79ef\u6838\u7684\u5c01\u88c5"}),"\n",(0,t.jsx)(e.p,{children:"\u5728\u4ee3\u7801\u7684\u5f00\u59cb\uff0c\u9996\u5148\u5c01\u88c5\u4e863x3\u548c1x1\u7684\u5377\u79ef\u6838\uff0c\u8fd9\u6837\u53ef\u4ee5\u589e\u52a0\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u9664\u4e86\u8fd9\u79cd\u4ee3\u7801\u5199\u6cd5\u5916\uff0c\u8fd8\u6709\u8bb8\u591a\u6df1\u5ea6\u5b66\u4e60\u4ee3\u7801\u5728\u5f00\u59cb\u4e5f\u4f1a\u5c06\u5377\u79ef\u5c42\uff0c\u6fc0\u6d3b\u51fd\u6570\u5c42\u548cBN\u5c42\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u540c\u6837\u662f\u4e3a\u4e86\u589e\u52a0\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:'def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> nn.Conv2d:\n """3x3 convolution with padding"""\n return nn.Conv2d(\n in_planes,\n out_planes,\n kernel_size=3,\n stride=stride,\n padding=dilation,\n groups=groups,\n bias=False,\n dilation=dilation,\n )\n\n\ndef conv1x1(in_planes: int, out_planes: int, stride: int = 1) -> nn.Conv2d:\n """1x1 convolution"""\n return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)\n'})}),"\n",(0,t.jsx)(e.h3,{id:"22-\u57fa\u672c\u6a21\u5757\u7684\u8bbe\u8ba1",children:"2.2 \u57fa\u672c\u6a21\u5757\u7684\u8bbe\u8ba1"}),"\n",(0,t.jsx)(e.p,{children:"ResNet\u7f51\u7edc\u662f\u7531\u5f88\u591a\u76f8\u540c\u7684\u6a21\u5757\u5806\u53e0\u8d77\u6765\u7684\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u4ee3\u7801\u5177\u6709\u53ef\u8bfb\u6027\u548c\u53ef\u6269\u5c55\u6027\uff0cResNet\u5728\u8bbe\u8ba1\u65f6\u91c7\u7528\u4e86\u6a21\u5757\u5316\u8bbe\u8ba1\uff0c\u9488\u5bf9\u4e0d\u540c\u5927\u5c0f\u7684ResNet\uff0c\u4e66\u5199\u4e86BasicBlock\u548cBottleNeck\u4e24\u4e2a\u57fa\u672c\u6a21\u5757\u3002\u8fd9\u79cd\u6a21\u5757\u5316\u7684\u8bbe\u8ba1\u5728\u73b0\u5728\u8bb8\u591a\u5e38\u89c1\u7684\u6df1\u5ea6\u5b66\u4e60\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u7ecf\u5e38\u770b\u5230\u3002"}),"\n",(0,t.jsx)(e.p,{children:"ResNet\u5e38\u89c1\u7684\u5927\u5c0f\u6709\u4e0b\u56fe\u7684ResNet-18\uff0cResNet-34\uff0cResNet-50\u3001ResNet-101\u548cResNet-152\uff0c\u5176\u4e2d\u7f51\u7edc\u540e\u9762\u7684\u6570\u5b57\u4ee3\u8868\u7684\u662f\u7f51\u7edc\u7684\u5c42\u6570\u3002"}),"\n",(0,t.jsx)(e.p,{children:(0,t.jsx)(e.img,{alt:"expansion",src:s(3850).A+"",width:"875",height:"377"})}),"\n",(0,t.jsx)(e.p,{children:"\u4e3a\u4e86\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u7684\u7406\u89e3\uff0c\u6211\u4eec\u4ee5ResNet101\u4e3a\u4f8b\u3002"}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,t.jsxs)(e.table,{children:[(0,t.jsx)(e.thead,{children:(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.th,{children:"layer_name"}),(0,t.jsx)(e.th,{children:"\u6b21\u6570"})]})}),(0,t.jsxs)(e.tbody,{children:[(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"conv1"}),(0,t.jsx)(e.td,{children:"\u5377\u79ef1\u6b21"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"conv2_x"}),(0,t.jsx)(e.td,{children:"\u5377\u79ef3 x 3 = 9\u6b21"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"conv3_x"}),(0,t.jsx)(e.td,{children:"\u5377\u79ef4 x 3 = 12\u6b21"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"conv4_x"}),(0,t.jsx)(e.td,{children:"\u5377\u79ef23 x 3 = 69\u6b21"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"conv5_x"}),(0,t.jsx)(e.td,{children:"\u5377\u79ef3 x 3 = 9\u6b21"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"fc"}),(0,t.jsx)(e.td,{children:"average pool 1\u6b21"})]}),(0,t.jsxs)(e.tr,{children:[(0,t.jsx)(e.td,{children:"\u5408\u8ba1"}),(0,t.jsx)(e.td,{children:"1 + 9 + 12 + 69 + 9 + 1 = 101\u6b21"})]})]})]}),"\n",(0,t.jsx)(e.p,{children:"\u89c2\u5bdf\u4e0a\u9762\u5404\u4e2aResNet\u7684\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0ResNet-18\u548cResNet-34\u6bcf\u4e00\u5c42\u5185\uff0c\u6570\u636e\u7684\u5927\u5c0f\u4e0d\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u4f46\u662fResNet-50\u3001ResNet-101\u548cResNet-152\u4e2d\u7684\u6bcf\u4e00\u5c42\u5185\u8f93\u5165\u548c\u8f93\u51fa\u7684channel\u6570\u76ee\u4e0d\u4e00\u6837\uff0c\u8f93\u51fa\u7684channel\u6269\u5927\u4e3a\u8f93\u5165channel\u76844\u500d\uff0c\u9664\u6b64\u4e4b\u5916\uff0c\u6bcf\u4e00\u5c42\u7684\u5377\u79ef\u7684\u5927\u5c0f\u4e5f\u53d8\u6362\u4e3a1\uff0c3\uff0c1\u7684\u7ed3\u6784\u3002\u57fa\u4e8e\u8fd9\u4e2a\u53d1\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06ResNet-18\u548cResNet-34\u7684\u6784\u6210\u6a21\u5757\u5f53\u4f5c\u4e00\u7c7b\uff0cResNet-50\u3001ResNet-101\u548cResNet-152\u8fd9\u4e09\u7c7b\u7f51\u7edc\u7684\u6784\u6210\u6a21\u5757\u5f53\u4f5c\u4e00\u7c7b\u3002\u4e8b\u5b9e\u4e0a\uff0ctorchvision\u7684\u6e90\u7801\u4e5f\u662f\u57fa\u4e8e\u8fd9\u79cd\u8bbe\u8ba1\u601d\u60f3\uff0c\u4f7f\u7528\u5982\u4e0b\u56fe\u7684BasicBlock\uff08\u5de6\uff09\u548cBottleNeck\uff08\u53f3\uff09\u6a21\u5757\uff0c\u5e76\u4e14\u4e3a\u4e86\u63a7\u5236\u8f93\u5165\u548c\u8f93\u51fa\u7684\u901a\u9053\u6570\u76ee\u7684\u53d8\u5316\uff0c\u5728\u4ee3\u7801\u4e2d\u8f93\u51fa\u7684\u901a\u9053\u7ef4\u5ea6\u4e5f\u901a\u8fc7expansion\u8fdb\u884c\u63a7\u5236\uff0c\u4e24\u4e2ablock\u7c7b\u8f93\u5165\u4e00\u4e2a\u901a\u9053\u4e3ain_planes\u7ef4\u7684\u5ea6\u7279\u5f81\u56fe\uff0c\u8f93\u51fa\u4e00\u4e2aplanes*block.expansion\u7ef4\u7684\u7279\u5f81\u56fe\uff0c\u5176\u4e2dplanes\u7684\u6570\u76ee\u5927\u5c0f\u7b49\u4e8ein_planes\u3002\u9664\u6b64\u4ee5\u5916\uff0c\u4ee3\u7801\u53f3\u4fa7\u7684\u66f2\u7ebf\u5c31\u662f\u672c\u6587\u6700\u91cd\u8981\u7684shortcut\u652f\u8def\uff0c\u8be5\u652f\u8def\u4e0a\u7684downsample\u64cd\u4f5c\u662f\u4e3a\u4e86\u5bf9shortcut\u652f\u8def\u8fdb\u884c\u5927\u5c0f\u6216\u7ef4\u5ea6\u4e0a\u7684\u8c03\u6574\uff0c\u4ee5\u5e0c\u671b\u6267\u884c\u76f8\u52a0\u64cd\u4f5c"}),"\n",(0,t.jsx)(e.p,{children:(0,t.jsx)(e.img,{alt:"block",src:s(56974).A+"",width:"928",height:"372"})}),"\n",(0,t.jsx)(e.h4,{id:"221-shortcut-connection",children:"2.2.1 Shortcut Connection."}),"\n",(0,t.jsx)(e.p,{children:"\u8fd9\u91cc\u518d\u5206\u6790\u4e00\u4e0bshortcut connection\uff1a"}),"\n",(0,t.jsx)(e.p,{children:(0,t.jsx)(e.img,{alt:"shortcut",src:s(88723).A+"",width:"463",height:"273"})}),"\n",(0,t.jsx)(e.p,{children:"shortcut connection\u4e5f\u5c31\u662f\u6240\u8c13\u7684\u201c\u6284\u8fd1\u9053\u201d\uff0c\u5b83\u6709\u4e24\u79cd\u65b9\u5f0f\uff0c\u5176\u4e00\u4e3a\u540c\u7b49\u7ef4\u5ea6\u7684\u6620\u5c04\uff0c\u5373\u8f93\u5165\u8f93\u51fa\u76f4\u63a5\u76f8\u52a0\uff08\u5373\u4e0a\u56fe\u4e2d\u7684F(x) + x\uff09\uff0c\u53e6\u4e00\u79cd\u4e3a\u4e0d\u540c\u7ef4\u5ea6\u7684\u6620\u5c04\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u7ed9x\u8865\u5145\u4e00\u4e2a\u7ebf\u6027\u6620\u5c04\u6765\u5339\u914d\u7ef4\u5ea6\u3002"}),"\n",(0,t.jsx)(e.p,{children:"\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e2a\u56fe\uff1a"}),"\n",(0,t.jsx)(e.img,{src:"./figures/shortcut2.jpg"}),"\n",(0,t.jsx)(e.p,{children:"\u5de6\uff1aVGG-19\u6a21\u578b\uff0c\u4f5c\u4e3a\u53c2\u8003\u3002 \u4e2d\uff1a\u4e00\u4e2a\u670934\u4e2a\u53c2\u6570\u5c42\u7684\u666e\u901a\u7f51\u7edc\u3002 \u53f3\uff1a\u4e00\u4e2a\u670934\u4e2a\u53c2\u6570\u5c42\u7684\u6b8b\u5dee\u7f51\u7edc\uff08\u5373resnet34\uff09"}),"\n",(0,t.jsx)(e.p,{children:"\u5728\u4e0a\u56fe\u6700\u53f3\u4fa7\u7684\u8def\u5f84\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u660e\u663e\u7684\u770b\u5230shortcut connection\u52a0\u5165\u4e86\u7f51\u7edc\u4e4b\u4e2d\uff0c\u540c\u65f6\uff0c\u56fe\u4e2d\u4e5f\u5f88\u660e\u663e\u7684\u53ef\u4ee5\u770b\u5230\uff0c\u5b9e\u7ebf\u90e8\u5206\u5c31\u662f\u8fdb\u884c\u4e86\u5355\u7eaf\u7684F(x)+x\u64cd\u4f5c\uff0c\u800c\u865a\u7ebf\u90e8\u5206\uff0c\u7b2c\u4e00\u4e2a\u5377\u79ef\u5c42\u7684stride\u662f2\uff08\u90a3\u4e2a/2\u7684\u610f\u601d\u5c31\u662fstride\u662f2\uff09\uff1b\u540c\u65f6\u6ce8\u610f\u5230\u6df1\u5ea6\u4e5f\u53d1\u751f\u4e86\u53d8\u6362\uff0cchannel\u6570\u76ee\u589e\u52a0\u4e00\u500d\uff08\u6269\u5927\u4e24\u500d\uff09\uff0c\u8fd9\u6837F(x)\u7684\u5206\u8fa8\u7387\u6bd4x\u5c0f\u4e00\u534a\uff0c\u539a\u5ea6\u6bd4x\u5927\u4e00\u500d\u3002\u5728\u8fd9\u6837\u7684shortcut connection\u4e2d\uff0c\u5c31\u9700\u8981\u8865\u5145\u7ebf\u6027\u6620\u5c04\u6765\u589e\u52a0\u7ef4\u5ea6\u3002\u5728ResNet\u4e2d\uff0c\u4f5c\u8005\u4f7f\u7528\u4e861 x 1\u7684\u5377\u79ef\u6838\u6765\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\u3002"}),"\n",(0,t.jsx)(e.p,{children:"\u53e6\u5916\uff0c\u8bba\u6587\u4e2d\u53c8\u63d0\u5230\u8bf4\uff1a\u201c\u2026\u2026where both designs have similar time complexity.\u201d \u65e2\u7136BasicBlock\u548cBottleneck\u4e8c\u8005\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u4f3c\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8fd8\u8981\u989d\u5916\u8bbe\u8ba1\u4e00\u4e2aBottleneck\u7ed3\u6784\u5462\uff1f"}),"\n",(0,t.jsx)(e.p,{children:"\u6839\u636e\u524d\u9762\u7684\u53d9\u8ff0\u6211\u4eec\u77e5\u9053\uff0cBasicBlock\u7ed3\u6784\u6bd4\u4f20\u7edf\u7684\u5377\u79ef\u7ed3\u6784\u591a\u4e86\u4e00\u4e2ashortcut\u652f\u8def\uff0c\u7528\u4e8e\u4f20\u9012\u4f4e\u5c42\u7684\u4fe1\u606f\uff0c\u4f7f\u5f97\u7f51\u7edc\u80fd\u591f\u8bad\u7ec3\u5730\u5f88\u6df1\u3002\u800cBottleNeck\u5148\u901a\u8fc7\u4e00\u4e2a1x1\u7684\u5377\u79ef\u51cf\u5c11\u901a\u9053\u6570\uff0c\u4f7f\u5f97\u4e2d\u95f4\u5377\u79ef\u7684\u901a\u9053\u6570\u51cf\u5c11\u4e3a1/4\uff1b\u4e2d\u95f4\u7684\u666e\u901a\u5377\u79ef\u505a\u5b8c\u5377\u79ef\u540e\u8f93\u51fa\u901a\u9053\u6570\u7b49\u4e8e\u8f93\u5165\u901a\u9053\u6570\uff1b\u7b2c\u4e09\u4e2a\u5377\u79ef\u7528\u4e8e\u6062\u590d\u901a\u9053\u6570\uff0c\u4f7f\u5f97BottleNeck\u7684\u8f93\u51fa\u901a\u9053\u6570\u7b49\u4e8eBottleNeck\u7684\u8f93\u5165\u901a\u9053\u6570\u3002**\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd9\u4e24\u4e2a1x1\u5377\u79ef\u6709\u6548\u5730\u51cf\u5c11\u4e86\u5377\u79ef\u7684\u53c2\u6570\u4e2a\u6570\u548c\u8ba1\u7b97\u91cf\uff0c\u540c\u65f6\u51cf\u5c11\u4e86\u4e2d\u95f4\u7279\u5f81\u56fe\u7684\u901a\u9053\u6570\uff0c\u4f7f\u5355\u4e2aBlock\u6d88\u8017\u7684\u663e\u5b58\u66f4\u5c11\uff0c\u5728\u8f83\u6df1\u7684\u7f51\u7edc\u4e2dBottleNeck\u4f1a\u5728\u53c2\u6570\u4e0a\u66f4\u52a0\u8282\u7ea6\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u5229\u4e8e\u6784\u5efa\u5c42\u6570\u66f4\u591a\u7684\u7f51\u7edc\uff0c\u540c\u65f6\u8fd8\u80fd\u4fdd\u6301\u6027\u80fd\u7684\u63d0\u5347\u3002**\u6240\u4ee5resnet50, resnet101\u548cresnet152\u4f7f\u7528\u4e86\u53e6\u5916\u8bbe\u8ba1\u7684BottleNeck\u7ed3\u6784\u3002"}),"\n",(0,t.jsx)(e.h4,{id:"222-basicblock",children:"2.2.2 BasicBlock"}),"\n",(0,t.jsx)(e.p,{children:"BasicBlock\u6a21\u5757\u7528\u6765\u6784\u5efaresnet18\u548cresnet34"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:'class BasicBlock(nn.Module):\n expansion: int = 1\n\n def __init__(\n self,\n inplanes: int,\n planes: int,\n stride: int = 1,\n downsample: Optional[nn.Module] = None,\n groups: int = 1,\n base_width: int = 64,\n dilation: int = 1,\n norm_layer: Optional[Callable[..., nn.Module]] = None,\n ) -> None:\n super().__init__()\n if norm_layer is None:\n norm_layer = nn.BatchNorm2d\n if groups != 1 or base_width != 64:\n raise ValueError("BasicBlock only supports groups=1 and base_width=64")\n if dilation > 1:\n raise NotImplementedError("Dilation > 1 not supported in BasicBlock")\n # Both self.conv1 and self.downsample layers downsample the input when stride != 1\n self.conv1 = conv3x3(inplanes, planes, stride)\n self.bn1 = norm_layer(planes)\n self.relu = nn.ReLU(inplace=True)\n self.conv2 = conv3x3(planes, planes)\n self.bn2 = norm_layer(planes)\n self.downsample = downsample\n self.stride = stride\n\n def forward(self, x: Tensor) -> Tensor:\n identity = x # x \u7ed9\u81ea\u5df1\u5148\u5907\u4efd\u4e00\u4efd\n\n out = self.conv1(x) # \u5bf9x\u505a\u5377\u79ef \n out = self.bn1(out) # \u5bf9x\u5f52\u4e00\u5316 \n out = self.relu(out) # \u5bf9x\u7528\u6fc0\u6d3b\u51fd\u6570\n\n out = self.conv2(out) # \u5bf9x\u505a\u5377\u79ef\n out = self.bn2(out) # \u5f52\u4e00\u5316\n\n if self.downsample is not None:\n identity = self.downsample(x)\n\n out += identity # \u8fdb\u884cdownsample\n out = self.relu(out)\n\n return out\n'})}),"\n",(0,t.jsx)(e.h4,{id:"223-bottleneck",children:"2.2.3 BottleNeck"}),"\n",(0,t.jsx)(e.p,{children:"BottleNeck\u6a21\u5757\u7528\u6765\u6784\u5efaresnet50\uff0cresnet101\u548cresnet152"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:'class Bottleneck(nn.Module):\n # Bottleneck in torchvision places the stride for downsampling at 3x3 convolution(self.conv2)\n # while original implementation places the stride at the first 1x1 convolution(self.conv1)\n # according to "Deep residual learning for image recognition"https://arxiv.org/abs/1512.03385.\n # This variant is also known as ResNet V1.5 and improves accuracy according to\n # https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch.\n\n expansion: int = 4 # \u5bf9\u8f93\u51fa\u901a\u9053\u8fdb\u884c\u500d\u589e\n\n def __init__(\n self,\n inplanes: int,\n planes: int,\n stride: int = 1,\n downsample: Optional[nn.Module] = None,\n groups: int = 1,\n base_width: int = 64,\n dilation: int = 1,\n norm_layer: Optional[Callable[..., nn.Module]] = None,\n ) -> None:\n super().__init__()\n if norm_layer is None:\n norm_layer = nn.BatchNorm2d\n width = int(planes * (base_width / 64.0)) * groups\n # Both self.conv2 and self.downsample layers downsample the input when stride != 1\n self.conv1 = conv1x1(inplanes, width)\n self.bn1 = norm_layer(width)\n self.conv2 = conv3x3(width, width, stride, groups, dilation)\n self.bn2 = norm_layer(width)\n self.conv3 = conv1x1(width, planes * self.expansion)\n self.bn3 = norm_layer(planes * self.expansion)\n self.relu = nn.ReLU(inplace=True)\n self.downsample = downsample\n self.stride = stride\n\n # Bottleneckd forward\u51fd\u6570\u548cBasicBlock\u7c7b\u4f3c\uff0c\u4e0d\u518d\u989d\u5916\u6ce8\u91ca\n def forward(self, x: Tensor) -> Tensor:\n identity = x\n\n out = self.conv1(x)\n out = self.bn1(out)\n out = self.relu(out)\n\n out = self.conv2(out)\n out = self.bn2(out)\n out = self.relu(out)\n\n out = self.conv3(out)\n out = self.bn3(out)\n\n if self.downsample is not None:\n identity = self.downsample(x)\n\n out += identity\n out = self.relu(out)\n\n return out\n'})}),"\n",(0,t.jsxs)(e.p,{children:["\u6211\u4eec\u5728\u8fd9\u91cc\u518d\u5bf9\u4ee3\u7801\u4e2d",(0,t.jsx)(e.strong,{children:"expansion"}),"\u7684\u4f5c\u7528\u505a\u4e00\u4e2a\u8bf4\u660e\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u65b0\u56de\u987e\u4e00\u4e0b\u4e0b\u9762\u8fd9\u5f20\u56fe\u3002"]}),"\n",(0,t.jsx)(e.p,{children:(0,t.jsx)(e.img,{src:s(3850).A+"",width:"875",height:"377"})}),"\n",(0,t.jsx)(e.p,{children:"expansion\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u5bf9\u8f93\u51fa\u901a\u9053\u7684\u500d\u4e58\u3002\u5728BasicBlock\u548cBottleNeck\u4e2d\uff0c\u201c__init__\u201d\u51fd\u6570\u4e2d\u6709\u4e09\u4e2a\u6bd4\u8f83\u5173\u952e\u7684\u53c2\u6570\uff1ainplanes,planes\u548cstride\uff0c\u8fd9\u4e09\u8005\u5206\u522b\u8868\u793a\u8f93\u5165\u7684\u901a\u9053\u6570\uff0c\u8f93\u51fa\u7684\u901a\u9053\u6570\u548c\u6b65\u5e45\u3002\u5728\u4e24\u4e2a\u6a21\u5757\u4e2d\uff0c__init__\u4f20\u5165\u7684planes\u90fd\u662f64,128,156,512\uff0c\u4f46\u6211\u4eec\u89c2\u5bdf\u4e0a\u9762\u7684\u8868\u683c\uff0c\u4f1a\u53d1\u73b0\u5bf9\u4e8eResNet-50\uff0cResNet-101\u548cResNet-152\u800c\u8a00\uff0c\u5b83\u4eec\u9700\u8981\u7684\u8f93\u51fa\u901a\u9053\u5e94\u8be5\u4e3a256,512,1024,2048\u624d\u5bf9\u3002\u56e0\u6b64\u5728\u8fd9\u91cc\u8bbe\u7f6eexpansion=4\uff0c\u5bf9\u5e94\u4e0a\u9762BottleNeck\u4ee3\u7801\u4e2d\u768430\u884c\u548c31\u884c\uff0c\u5c06\u6bcf\u4e00\u4e2aplanes\u90fd\u4e58\u4e0a\u8fd9\u4e2aexpansion\uff0c\u5c31\u5f97\u5230\u4e86\u9700\u8981\u7684\u901a\u9053\u6570\uff1b\u800c\u5bf9\u4e8eResNet-18\u548cResNet-34\u800c\u8a00\uff0c\u8f93\u5165\u901a\u9053\u548c\u8f93\u51fa\u901a\u9053\u7684\u7ef4\u5ea6\u4e0a\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0c\u56e0\u6b64expansion\u4e5f\u8bbe\u7f6e\u4e3a1\u3002"}),"\n",(0,t.jsx)(e.h3,{id:"23-\u7f51\u7edc\u6574\u4f53\u7ed3\u6784",children:"2.3 \u7f51\u7edc\u6574\u4f53\u7ed3\u6784"}),"\n",(0,t.jsx)(e.p,{children:"\u5728\u5b9a\u4e49\u597d\u6700\u57fa\u672c\u7684Bottlenneck\u548cBasicBlock\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6784\u5efaResNet\u7f51\u7edc\u4e86\u3002"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:'class ResNet(nn.Module):\n def __init__(\n self,\n block: Type[Union[BasicBlock, Bottleneck]], # \u9009\u62e9\u57fa\u672c\u6a21\u5757\n layers: List[int], # \u6bcf\u4e00\u5c42block\u7684\u6570\u76ee\u6784\u6210 -> [3,4,6,3]\n num_classes: int = 1000, # \u5206\u7c7b\u6570\u76ee\n zero_init_residual: bool = False, # \u521d\u59cb\u5316\n \n #######\u5176\u4ed6\u5377\u79ef\u6784\u6210\uff0c\u4e0e\u672c\u6587ResNet\u65e0\u5173######\n groups: int = 1,\n width_per_group: int = 64,\n replace_stride_with_dilation: Optional[List[bool]] = None,\n #########################################\n \n norm_layer: Optional[Callable[..., nn.Module]] = None, # norm\u5c42\n ) -> None:\n super().__init__()\n _log_api_usage_once(self)\n if norm_layer is None:\n norm_layer = nn.BatchNorm2d\n self._norm_layer = norm_layer\n\t\t\n self.inplanes = 64 # \u8f93\u5165\u901a\u9053\n \n #######\u5176\u4ed6\u5377\u79ef\u6784\u6210\uff0c\u4e0e\u672c\u6587ResNet\u65e0\u5173######\n self.dilation = 1 # \u7a7a\u6d1e\u5377\u79ef\n if replace_stride_with_dilation is None:\n # each element in the tuple indicates if we should replace\n # the 2x2 stride with a dilated convolution instead\n replace_stride_with_dilation = [False, False, False]\n if len(replace_stride_with_dilation) != 3:\n raise ValueError(\n "replace_stride_with_dilation should be None "\n f"or a 3-element tuple, got {replace_stride_with_dilation}"\n )\n self.groups = groups\n self.base_width = width_per_group\n #########################################\n \n self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False)\n self.bn1 = norm_layer(self.inplanes)\n self.relu = nn.ReLU(inplace=True)\n self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)\n # \u901a\u8fc7_make_layer\u5e26\u5230\u5c42\u6b21\u5316\u8bbe\u8ba1\u7684\u6548\u679c\n self.layer1 = self._make_layer(block, 64, layers[0]) # \u5bf9\u5e94\u7740conv2_x\n self.layer2 = self._make_layer(block, 128, layers[1], stride=2, dilate=replace_stride_with_dilation[0]) # \u5bf9\u5e94\u7740conv3_x\n self.layer3 = self._make_layer(block, 256, layers[2], stride=2, dilate=replace_stride_with_dilation[1]) # \u5bf9\u5e94\u7740conv4_x\n self.layer4 = self._make_layer(block, 512, layers[3], stride=2, dilate=replace_stride_with_dilation[2]) # \u5bf9\u5e94\u7740conv5_x\n # \u5206\u7c7b\u5934\n self.avgpool = nn.AdaptiveAvgPool2d((1, 1))\n self.fc = nn.Linear(512 * block.expansion, num_classes)\n\t\t\n # \u6a21\u578b\u521d\u59cb\u5316\n for m in self.modules():\n if isinstance(m, nn.Conv2d):\n nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")\n elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):\n nn.init.constant_(m.weight, 1)\n nn.init.constant_(m.bias, 0)\n\n # Zero-initialize the last BN in each residual branch,\n # so that the residual branch starts with zeros, and each residual block behaves like an identity.\n # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677\n if zero_init_residual:\n for m in self.modules():\n if isinstance(m, Bottleneck) and m.bn3.weight is not None:\n nn.init.constant_(m.bn3.weight, 0) # type: ignore[arg-type]\n elif isinstance(m, BasicBlock) and m.bn2.weight is not None:\n nn.init.constant_(m.bn2.weight, 0) # type: ignore[arg-type]\n\t# \u5c42\u6b21\u5316\u8bbe\u8ba1\n def _make_layer(\n self,\n block: Type[Union[BasicBlock, Bottleneck]], # \u57fa\u672c\u6784\u6210\u6a21\u5757\u9009\u62e9\n planes: int, # \u8f93\u5165\u7684\u901a\u9053\n blocks: int, # \u6a21\u5757\u6570\u76ee\n stride: int = 1, # \u6b65\u957f\n dilate: bool = False, # \u7a7a\u6d1e\u5377\u79ef\uff0c\u4e0e\u672c\u6587\u65e0\u5173\n ) -> nn.Sequential:\n norm_layer = self._norm_layer\n downsample = None # \u662f\u5426\u91c7\u7528\u4e0b\u91c7\u6837\n ####################\u65e0\u5173#####################\n previous_dilation = self.dilation \n if dilate:\n self.dilation *= stride\n stride = 1\n #############################################\n if stride != 1 or self.inplanes != planes * block.expansion:\n downsample = nn.Sequential(\n conv1x1(self.inplanes, planes * block.expansion, stride),\n norm_layer(planes * block.expansion),\n )\n\t\t\n # \u4f7f\u7528layers\u5b58\u50a8\u6bcf\u4e2alayer\n layers = []\n layers.append(\n block(\n self.inplanes, planes, stride, downsample, self.groups, self.base_width, previous_dilation, norm_layer\n )\n )\n self.inplanes = planes * block.expansion\n for _ in range(1, blocks):\n layers.append(\n block(\n self.inplanes,\n planes,\n groups=self.groups,\n base_width=self.base_width,\n dilation=self.dilation,\n norm_layer=norm_layer,\n )\n )\n\t\t# \u5c06layers\u901a\u8fc7nn.Sequential\u8f6c\u5316\u4e3a\u7f51\u7edc\n return nn.Sequential(*layers)\n\n def _forward_impl(self, x: Tensor) -> Tensor:\n # See note [TorchScript super()]\n x = self.conv1(x) # conv1 x shape [1 64 112 112]\n x = self.bn1(x) # \u5f52\u4e00\u5316\u5904\u7406 \n x = self.relu(x) # \u6fc0\u6d3b\u51fd\u6570\n x = self.maxpool(x) # conv2_x\u76843x3 maxpool x shape [1 64 56 56]\n\n x = self.layer1(x) # layer 1\n x = self.layer2(x) # layer 2\n x = self.layer3(x) # layer 3\n x = self.layer4(x) # layer 4\n\n x = self.avgpool(x) # \u81ea\u9002\u5e94\u6c60\u5316\n x = torch.flatten(x, 1) \n x = self.fc(x) # \u5206\u7c7b\n\n return x\n\n def forward(self, x: Tensor) -> Tensor:\n return self._forward_impl(x) \n'})}),"\n",(0,t.jsx)(e.p,{children:"\u89c2\u5bdf\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u4e0d\u96be\u770b\u5230\uff0c\u9996\u5148\u662f\u4e00\u4e2a7 x 7\u7684\u5377\u79ef\u4f5c\u7528\u5728\u8f93\u5165\u76843\u7ef4\u56fe\u7247\u4e0a\uff0c\u5e76\u8f93\u5165\u4e00\u4e2a64\u7ef4\u7684\u7279\u5f81\u56fe\uff08\u5373self.inplanes\u7684\u521d\u59cb\u503c\uff09\uff0c\u901a\u8fc7BatchNorm\u5c42\uff0cReLU\u5c42\uff0cMaxPool\u5c42\uff1b\u7136\u540e\u7ecf\u8fc7_make_layer()\u51fd\u6570\u6784\u5efa\u76844\u5c42layer\uff0c\u6700\u540e\u7ecf\u8fc7\u4e00\u4e2aAveragePooling\u5c42\uff0c\u518d\u7ecf\u8fc7\u4e00\u4e2afc\u5c42\u5f97\u5230\u5206\u7c7b\u8f93\u51fa\u3002\u5728\u7f51\u7edc\u642d\u5efa\u8d77\u6765\u540e\uff0c\u8fd8\u5bf9\u6a21\u578b\u7684\u53c2\u6570(Conv2d\u3001BatchNorm2d\u3001last BN)\u8fdb\u884c\u4e86\u521d\u59cb\u5316\u3002"}),"\n",(0,t.jsx)(e.p,{children:"\u800c\u5bf9\u4e8e_make_layer\u51fd\u6570\uff0c\u4e00\u4e2a_make_layer()\u6784\u5efa\u4e00\u4e2alayer\u5c42\uff0c\u6bcf\u4e00\u4e2alayer\u5c42\u662f\u4e0a\u8ff0\u4e24\u79cd\u57fa\u672c\u6a21\u5757\u7684\u5806\u53e0\u3002\u8f93\u5165\u53c2\u6570\u4e2dblock\u4ee3\u8868\u8be5layer\u5806\u53e0\u6a21\u5757\u7684\u7c7b\u578b\uff0c\u53ef\u9009BasicBlock\u6216\u8005BottleNeck\uff1bblocks\u4ee3\u8868\u8be5layer\u4e2d\u5806\u53e0\u7684block\u7684\u6570\u76ee\uff1bplanes\u4e0e\u8be5layer\u6700\u7ec8\u8f93\u51fa\u7684\u7ef4\u5ea6\u6570\u6709\u5173\uff0c\u6ce8\u610f\u6700\u7ec8\u8f93\u51fa\u7684\u7ef4\u5ea6\u6570\u4e3aplanes * block.expansion\u3002\u9664\u6b64\u4e4b\u5916\uff0c _make_layer()\u662f\u7528\u6765\u751f\u6210\u6b8b\u5dee\u5757\u7684\uff0c\u8fd9\u5c31\u7275\u626f\u5230\u5b83\u7684\u7b2c\u56db\u4e2a\u53c2\u6570\uff1astride\uff0c\u5373\u5377\u79ef\u6b65\u5e45\u3002\u8be5\u51fd\u6570\u4e2d\u9996\u5148\u5b9a\u4e49\u4e86\u5982\u679cstride\u4e0d\u7b49\u4e8e1\u6216\u8005\u7ef4\u5ea6\u4e0d\u5339\u914d\uff08\u5373\u8f93\u5165\u901a\u9053\u4e0d\u6ee1\u8db3\u5bf9\u5e94\u5173\u7cfb\uff09\u7684\u65f6\u5019\u7684downsample\uff0c\u7136\u540e\u5bf9\u5176\u8fdb\u884c\u4e00\u6b21BN\u64cd\u4f5c\u3002\u63a5\u7740\u5bf9inplanes\u548cplanes\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u8fdb\u884c\u4e86\u4e00\u6b21downsample \uff0c\u5373\u5c06\u5e26downsample\u7684block\u6dfb\u52a0\u81f3layers\u3002\u8fd9\u6837\u4fdd\u8bc1\u4e86x\u548cout\u7684\u7ef4\u5ea6\u4e00\u81f4\uff0c\u63a5\u4e0b\u6765\u901a\u8fc7\u4e00\u4e2a\u5faa\u73af\u6dfb\u52a0\u4e86\u6307\u5b9a\u4e2a\u6570\u7684Block\uff0c\u7531\u4e8ex\u5df2\u7ecf\u7ef4\u5ea6\u4e00\u81f4\u4e86\uff0c\u8fd9\u6837\u6dfb\u52a0\u7684\u5176\u4ed6\u7684Block\u5c31\u53ef\u4ee5\u4e0d\u7528\u964d\u7ef4\u4e86\uff0c\u6240\u4ee5\u5faa\u73af\u6dfb\u52a0\u4e0d\u542bDownsample\u7684Block\u3002\u6b63\u5982\u4e0b\u9762\u4ee3\u7801\u6240\u793a"}),"\n",(0,t.jsx)(e.pre,{children:(0,t.jsx)(e.code,{className:"language-python",children:"if stride != 1 or self.inplanes != planes * block.expansion:\n downsample = nn.Sequential(\n conv1x1(self.inplanes, planes * block.expansion, stride),\n norm_layer(planes * block.expansion),\n )\n"})}),"\n",(0,t.jsx)(e.p,{children:"\u5f53\u4e00\u4e2alayer\u5305\u542b\u591a\u4e2ablock\u65f6\uff0c\u662f\u901a\u8fc7\u5411layers\u5217\u8868\u4e2d\u4f9d\u6b21\u52a0\u5165\u6bcf\u4e2ablock\uff0c\u6765\u5b9e\u73b0block\u7684\u5806\u53e0\u7684\u3002\u7b2c\u4e00\u4e2ablock\u9700\u8981\u7279\u6b8a\u5904\u7406\uff0c\u8be5block\u4f9d\u636e\u4f20\u5165\u7684self.inplanes, planes\u4ee5\u53castride\u5224\u65ad\uff0c\u53ef\u80fd\u542b\u6709downsample\u652f\u8def\uff1b\u8fd9\u4e2ablock\u7684\u8f93\u51fa\u7ef4\u5ea6\u662fplanes*block.expansion\u3002\u7d27\u63a5\u7740\u4fbf\u628aself.inplanes\u66f4\u65b0\u4e3a\u6b64\u503c\u4f5c\u4e3a\u540e\u7eedblock\u7684\u8f93\u5165\u7ef4\u5ea6\u3002\u540e\u9762\u7684block\u7684stride\u4e3a\u9ed8\u8ba4\u503c1\uff0c\u540c\u65f6\uff0c\u7531\u4e8e\u8f93\u5165\u4e3aself.inplanes\uff0c\u8f93\u51fa\u4e3aplanes*block.expansion\uff0c\u800cself.inplanes = planes * block.expansion\uff0c\u56e0\u6b64\u4e0d\u4f1a\u51fa\u73b0\u7279\u5f81\u56fe\u5927\u5c0f\u6216\u8005\u5c3a\u5bf8\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\uff0c\u4e0d\u53ef\u80fd\u51fa\u73b0downsample\u64cd\u4f5c\u3002"}),"\n",(0,t.jsx)(e.h2,{id:"3-\u603b\u7ed3",children:"3 \u603b\u7ed3"}),"\n",(0,t.jsx)(e.p,{children:"\u4e0e\u666e\u901a\u7684\u7f51\u7edc\u76f8\u6bd4\uff0cResNet\u6700\u5927\u7684\u4f18\u52bf\u5c31\u662f\u5f15\u5165\u4e86Shortcut\u8fd9\u4e2a\u652f\u8def\uff0c\u8ba9\u67d0\u4e00\u5c42\u53ef\u4ee5\u76f4\u63a5\u8fde\u63a5\u5230\u540e\u9762\u7684\u5c42\uff0c\u4f7f\u5f97\u540e\u9762\u7684\u5c42\u53ef\u4ee5\u76f4\u63a5\u5b66\u4e60\u6b8b\u5dee\u3002\u4f20\u7edf\u7684\u5377\u79ef\u5c42\u6216\u5168\u8fde\u63a5\u5c42\u5728\u4fe1\u606f\u4f20\u9012\u65f6\uff0c\u6216\u591a\u6216\u5c11\u4f1a\u5b58\u5728\u4fe1\u606f\u4e22\u5931\u3001\u635f\u8017\u7b49\u95ee\u9898\u3002ResNet \u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u901a\u8fc7\u76f4\u63a5\u5c06\u8f93\u5165\u4fe1\u606f\u7ed5\u9053\u4f20\u5230\u8f93\u51fa\uff0c\u4fdd\u62a4\u4fe1\u606f\u7684\u5b8c\u6574\u6027\uff0c\u6574\u4e2a\u7f51\u7edc\u5219\u53ea\u9700\u8981\u5b66\u4e60\u8f93\u5165\u3001\u8f93\u51fa\u5dee\u522b\u7684\u90a3\u4e00\u90e8\u5206\uff0c\u7b80\u5316\u5b66\u4e60\u76ee\u6807\u548c\u96be\u5ea6\u3002"}),"\n",(0,t.jsx)(e.p,{children:"ResNet\u7684\u51fa\u73b0\uff0c\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u89e3\u51b3\u4e86\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u968f\u6df1\u5ea6\u7684\u589e\u52a0\uff0c\u4f46\u662f\u6a21\u578b\u6548\u679c\u5374\u53d8\u5dee\u7684\u95ee\u9898\uff0c\u7528\u4f5c\u8005\u7684\u8bdd\u8bf4\uff0c\u5c31\u662f: \u201cOur deep residual nets can easily enjoy accuracy gains from greatly increased depth, producing results substantially better than previous networks.\u201d\u3002\u539f\u59cb\u7684ResNet\u5bf9\u4e8e\u8bad\u7ec3\u5377\u79ef\u795e\u7ecf\u7f51\u8def\u505a\u51fa\u4e86\u5f88\u5927\u7684\u8d21\u732e\uff0c\u4f46\u662f\u540c\u6837\u4e5f\u6709\u7740\u8bb8\u591a\u53ef\u4ee5\u6539\u8fdb\u7684\u5730\u65b9\u3002\u968f\u7740\u65f6\u4ee3\u7684\u53d1\u5c55\uff0c\u539f\u7248\u7684ResNet\u5728\u4e00\u6b21\u53c8\u4e00\u6b21\u7684\u7814\u7a76\u4e2d\u5f97\u5230\u4e86\u4e30\u5bcc\u548c\u5b8c\u5584\uff0c\u884d\u751f\u51fa\u4e86\u4e30\u5bcc\u7684\u6539\u8fdb\u7684\u6a21\u578b\uff0c\u5982ResNeXt\u3002\u5b83\u63d0\u51fa\u4e86\u4e00\u79cd\u4ecb\u4e8e\u666e\u901a\u5377\u79ef\u6838\u6df1\u5ea6\u53ef\u5206\u79bb\u5377\u79ef\u7684\u8fd9\u79cd\u7b56\u7565\uff1a\u5206\u7ec4\u5377\u79ef\u3002\u901a\u8fc7\u63a7\u5236\u5206\u7ec4\u7684\u6570\u91cf\uff08\u57fa\u6570\uff09\u6765\u8fbe\u5230\u4e24\u79cd\u7b56\u7565\u7684\u5e73\u8861\u3002\u5206\u7ec4\u5377\u79ef\u7684\u601d\u60f3\u662f\u6e90\u81eaInception\uff0c\u4e0d\u540c\u4e8eInception\u7684\u9700\u8981\u4eba\u5de5\u8bbe\u8ba1\u6bcf\u4e2a\u5206\u652f\uff0cResNeXt\u7684\u6bcf\u4e2a\u5206\u652f\u7684\u62d3\u6251\u7ed3\u6784\u662f\u76f8\u540c\u7684\u3002\u6700\u540e\u518d\u7ed3\u5408\u6b8b\u5dee\u7f51\u7edc\uff0c\u5f97\u5230\u7684\u4fbf\u662f\u6700\u7ec8\u7684ResNeXt\u3002"}),"\n",(0,t.jsx)(e.p,{children:"\u9664\u6b64\u4e4b\u5916\uff0cResNet\u8fd8\u6709\u5176\u5b83\u53d8\u4f53\u5982Wider ResNet\uff0cDarkNet53\u7b49\u3002\u5b83\u4eec\u7684\u6539\u8fdb\u76f8\u5bf9\u8f83\u5927\uff0c\u5c24\u5176\u662fDarkNet53\uff0c\u5b83\u548cResNet\u5df2\u7ecf\u6709\u5f88\u5927\u4e0d\u540c\u4e86\uff0c\u53ea\u662f\u4f7f\u7528\u5230\u4e86\u6b8b\u5dee\u8fde\u63a5\u4ece\u800c\u590d\u7528\u7279\u5f81\u800c\u5df2\u3002\u603b\u800c\u8a00\u4e4b\uff0cResNet\u4f5c\u4e3a\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u7684\u4e00\u4e2a\u91cc\u7a0b\u7891\u5f0f\u7684\u6a21\u578b\u4e00\u76f4\u5728\u5404\u4e2a\u9886\u57df\u88ab\u5e94\u7528\uff0c\u56e0\u6b64\u6211\u4eec\u5b66\u4e60\u8fd9\u6837\u4e00\u4e2a\u6a21\u578b\u67b6\u6784\u5f88\u6709\u5fc5\u8981\u3002\u6beb\u4e0d\u5938\u5f20\u7684\u8bf4\uff0cResNet\u503c\u5f97\u6bcf\u4e00\u4e2a\u5b66\u4e60\u6df1\u5ea6\u5b66\u4e60\u7684\u5b66\u5b50\u8ba4\u771f\u7814\u7a76\u3002"})]})}function p(n={}){const{wrapper:e}={...(0,i.R)(),...n.components};return e?(0,t.jsx)(e,{...n,children:(0,t.jsx)(d,{...n})}):d(n)}},88723:(n,e,s)=>{s.d(e,{A:()=>l});const l=s.p+"assets/images/shortcut-332ff3aeabe351ba9b4c933bfdebea1f.jpg"}}]); |