简 介: 对于AlexNet的网络基本结构进行总结。由于这个网络在实现过程中所需要的计算力比较大。再次并未对其进行测试。

关键词AlexNetPaddle

#mermaid-svg-96sB5aZmwVCy6qSl .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-96sB5aZmwVCy6qSl .label text{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .node rect,#mermaid-svg-96sB5aZmwVCy6qSl .node circle,#mermaid-svg-96sB5aZmwVCy6qSl .node ellipse,#mermaid-svg-96sB5aZmwVCy6qSl .node polygon,#mermaid-svg-96sB5aZmwVCy6qSl .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-96sB5aZmwVCy6qSl .node .label{text-align:center;fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .node.clickable{cursor:pointer}#mermaid-svg-96sB5aZmwVCy6qSl .arrowheadPath{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-96sB5aZmwVCy6qSl .flowchart-link{stroke:#333;fill:none}#mermaid-svg-96sB5aZmwVCy6qSl .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-96sB5aZmwVCy6qSl .edgeLabel rect{opacity:0.9}#mermaid-svg-96sB5aZmwVCy6qSl .edgeLabel span{color:#333}#mermaid-svg-96sB5aZmwVCy6qSl .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-96sB5aZmwVCy6qSl .cluster text{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-96sB5aZmwVCy6qSl .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-96sB5aZmwVCy6qSl text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-96sB5aZmwVCy6qSl .actor-line{stroke:grey}#mermaid-svg-96sB5aZmwVCy6qSl .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-96sB5aZmwVCy6qSl .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-96sB5aZmwVCy6qSl #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-96sB5aZmwVCy6qSl .sequenceNumber{fill:#fff}#mermaid-svg-96sB5aZmwVCy6qSl #sequencenumber{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl #crosshead path{fill:#333;stroke:#333}#mermaid-svg-96sB5aZmwVCy6qSl .messageText{fill:#333;stroke:#333}#mermaid-svg-96sB5aZmwVCy6qSl .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-96sB5aZmwVCy6qSl .labelText,#mermaid-svg-96sB5aZmwVCy6qSl .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-96sB5aZmwVCy6qSl .loopText,#mermaid-svg-96sB5aZmwVCy6qSl .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-96sB5aZmwVCy6qSl .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-96sB5aZmwVCy6qSl .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-96sB5aZmwVCy6qSl .noteText,#mermaid-svg-96sB5aZmwVCy6qSl .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-96sB5aZmwVCy6qSl .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-96sB5aZmwVCy6qSl .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-96sB5aZmwVCy6qSl .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-96sB5aZmwVCy6qSl .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl .section{stroke:none;opacity:0.2}#mermaid-svg-96sB5aZmwVCy6qSl .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-96sB5aZmwVCy6qSl .section2{fill:#fff400}#mermaid-svg-96sB5aZmwVCy6qSl .section1,#mermaid-svg-96sB5aZmwVCy6qSl .section3{fill:#fff;opacity:0.2}#mermaid-svg-96sB5aZmwVCy6qSl .sectionTitle0{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .sectionTitle1{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .sectionTitle2{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .sectionTitle3{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-96sB5aZmwVCy6qSl .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl .grid path{stroke-width:0}#mermaid-svg-96sB5aZmwVCy6qSl .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-96sB5aZmwVCy6qSl .task{stroke-width:2}#mermaid-svg-96sB5aZmwVCy6qSl .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl .taskText:not([font-size]){font-size:11px}#mermaid-svg-96sB5aZmwVCy6qSl .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-96sB5aZmwVCy6qSl .task.clickable{cursor:pointer}#mermaid-svg-96sB5aZmwVCy6qSl .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-96sB5aZmwVCy6qSl .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-96sB5aZmwVCy6qSl .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-96sB5aZmwVCy6qSl .taskText0,#mermaid-svg-96sB5aZmwVCy6qSl .taskText1,#mermaid-svg-96sB5aZmwVCy6qSl .taskText2,#mermaid-svg-96sB5aZmwVCy6qSl .taskText3{fill:#fff}#mermaid-svg-96sB5aZmwVCy6qSl .task0,#mermaid-svg-96sB5aZmwVCy6qSl .task1,#mermaid-svg-96sB5aZmwVCy6qSl .task2,#mermaid-svg-96sB5aZmwVCy6qSl .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-96sB5aZmwVCy6qSl .taskTextOutside0,#mermaid-svg-96sB5aZmwVCy6qSl .taskTextOutside2{fill:#000}#mermaid-svg-96sB5aZmwVCy6qSl .taskTextOutside1,#mermaid-svg-96sB5aZmwVCy6qSl .taskTextOutside3{fill:#000}#mermaid-svg-96sB5aZmwVCy6qSl .active0,#mermaid-svg-96sB5aZmwVCy6qSl .active1,#mermaid-svg-96sB5aZmwVCy6qSl .active2,#mermaid-svg-96sB5aZmwVCy6qSl .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-96sB5aZmwVCy6qSl .activeText0,#mermaid-svg-96sB5aZmwVCy6qSl .activeText1,#mermaid-svg-96sB5aZmwVCy6qSl .activeText2,#mermaid-svg-96sB5aZmwVCy6qSl .activeText3{fill:#000 !important}#mermaid-svg-96sB5aZmwVCy6qSl .done0,#mermaid-svg-96sB5aZmwVCy6qSl .done1,#mermaid-svg-96sB5aZmwVCy6qSl .done2,#mermaid-svg-96sB5aZmwVCy6qSl .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-96sB5aZmwVCy6qSl .doneText0,#mermaid-svg-96sB5aZmwVCy6qSl .doneText1,#mermaid-svg-96sB5aZmwVCy6qSl .doneText2,#mermaid-svg-96sB5aZmwVCy6qSl .doneText3{fill:#000 !important}#mermaid-svg-96sB5aZmwVCy6qSl .crit0,#mermaid-svg-96sB5aZmwVCy6qSl .crit1,#mermaid-svg-96sB5aZmwVCy6qSl .crit2,#mermaid-svg-96sB5aZmwVCy6qSl .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-96sB5aZmwVCy6qSl .activeCrit0,#mermaid-svg-96sB5aZmwVCy6qSl .activeCrit1,#mermaid-svg-96sB5aZmwVCy6qSl .activeCrit2,#mermaid-svg-96sB5aZmwVCy6qSl .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-96sB5aZmwVCy6qSl .doneCrit0,#mermaid-svg-96sB5aZmwVCy6qSl .doneCrit1,#mermaid-svg-96sB5aZmwVCy6qSl .doneCrit2,#mermaid-svg-96sB5aZmwVCy6qSl .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-96sB5aZmwVCy6qSl .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-96sB5aZmwVCy6qSl .milestoneText{font-style:italic}#mermaid-svg-96sB5aZmwVCy6qSl .doneCritText0,#mermaid-svg-96sB5aZmwVCy6qSl .doneCritText1,#mermaid-svg-96sB5aZmwVCy6qSl .doneCritText2,#mermaid-svg-96sB5aZmwVCy6qSl .doneCritText3{fill:#000 !important}#mermaid-svg-96sB5aZmwVCy6qSl .activeCritText0,#mermaid-svg-96sB5aZmwVCy6qSl .activeCritText1,#mermaid-svg-96sB5aZmwVCy6qSl .activeCritText2,#mermaid-svg-96sB5aZmwVCy6qSl .activeCritText3{fill:#000 !important}#mermaid-svg-96sB5aZmwVCy6qSl .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-96sB5aZmwVCy6qSl g.classGroup text .title{font-weight:bolder}#mermaid-svg-96sB5aZmwVCy6qSl g.clickable{cursor:pointer}#mermaid-svg-96sB5aZmwVCy6qSl g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-96sB5aZmwVCy6qSl g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-96sB5aZmwVCy6qSl .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-96sB5aZmwVCy6qSl .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-96sB5aZmwVCy6qSl .dashed-line{stroke-dasharray:3}#mermaid-svg-96sB5aZmwVCy6qSl #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl .commit-id,#mermaid-svg-96sB5aZmwVCy6qSl .commit-msg,#mermaid-svg-96sB5aZmwVCy6qSl .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-96sB5aZmwVCy6qSl g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-96sB5aZmwVCy6qSl g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-96sB5aZmwVCy6qSl g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-96sB5aZmwVCy6qSl .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-96sB5aZmwVCy6qSl .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-96sB5aZmwVCy6qSl .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-96sB5aZmwVCy6qSl .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-96sB5aZmwVCy6qSl .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-96sB5aZmwVCy6qSl .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-96sB5aZmwVCy6qSl .edgeLabel text{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-96sB5aZmwVCy6qSl .node circle.state-start{fill:black;stroke:black}#mermaid-svg-96sB5aZmwVCy6qSl .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-96sB5aZmwVCy6qSl #statediagram-barbEnd{fill:#9370db}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-state .divider{stroke:#9370db}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-96sB5aZmwVCy6qSl .note-edge{stroke-dasharray:5}#mermaid-svg-96sB5aZmwVCy6qSl .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-96sB5aZmwVCy6qSl .error-icon{fill:#522}#mermaid-svg-96sB5aZmwVCy6qSl .error-text{fill:#522;stroke:#522}#mermaid-svg-96sB5aZmwVCy6qSl .edge-thickness-normal{stroke-width:2px}#mermaid-svg-96sB5aZmwVCy6qSl .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-96sB5aZmwVCy6qSl .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-96sB5aZmwVCy6qSl .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-96sB5aZmwVCy6qSl .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-96sB5aZmwVCy6qSl .marker{fill:#333}#mermaid-svg-96sB5aZmwVCy6qSl .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-96sB5aZmwVCy6qSl {color: rgba(0, 0, 0, 0.75);font: ;}

AlexNet
文章目录
简介
网络结构
代码实现
总 结

§01 AlexNet


1.1 简介

  AlexNet2012ImageNet竞赛冠军获得者Hinton和他的学生Alex Krizhevsky设计的,因此得名叫做AlexNet

  相比于LeNextAlexNet增加了更多的是用于深度CNN网络的学习方法,这些方法之所以能够得以实施,依赖于:

  (1)数据,一个包含许多特征的深度模型它需要大量的有标签的数据才能表现得比其他经典方法更好;
  (2)硬件(算力),深度学习对计算资源要求很高,但是早期的硬件计算能力非常有限。

  直到2012年,这两点都得到了很大程度的改善,这也就造就了AlexNet模型的横空出世。它首次证明了学习到的特征可以超越手工设计的特征,一举打破了困扰计算机视觉研究的瓶颈。

  • 参考链接AlexNet学习
  • 论文原文ImageNet Classification with Deep ConvolutionalNeural Networks

1.2 网络结构

  AlexNet与LeNet的设计理念非常相似,但也有非常明显的区别:

  • 第一AlexNet包含5层卷积、2层全连接隐藏层以及1层全连接输出层;
  • 第二AlexNet模型将sigmoid激活函数更改为了更为简单的ReLU激活函数;
  • 第三AlexNet通过丢弃法(Dropout)有效的控制了全连接层的模型复杂度,防止引入过多的参数;
  • 第四AlexNet引入了大量的图像增广,如翻转、裁剪以及颜色变化等,有效的增加了数据样本的数量,从而缓解了过拟合现象的发生。

  由于ImageNet数据集中的图像要比MINST数据集大许多,所以需要更大的卷积核来捕捉物体,因此:

  • 第一层卷积核的窗口为11×11
  • 而第二层卷积核就减少到5×5,之后就一直采用3×3
  • 此外,第一、第二和第五个卷积层之后都使用了窗口形状为3×3、步幅为2的最大池化层。
  • 最后就是一般的全连接层。

  借用书中的图来表示一下

▲ 图1.2.1 AlexNet网络结构

输入图像大小:(batch_size,3,224,224)
第一层卷积:  (96,3,11,11) ,padding = 2,stride = 4     输出大小:(batch_size,96,55,55)ReLU(), Local Response Normalizationmax pooling(3,stride = 2)                        输出大小:(batch_size,96,27,27)
第二层卷积: (256,96,5,5) padding = 2,stride = 1     输出大小:(batch_size,256,27,27)ReLU(), Local Response Normalizationmax pooling(3,stride = 2)                        输出大小:(batch_size,256,13,13)
第三层卷积: (384,256,3,3) padding = 1,stride = 1     输出大小:(batch_size,384,13,13)ReLU()
第四层卷积: (384,384,3,3) padding = 1,stride = 1     输出大小:(batch_size,384,13,13)ReLU()
第五层卷积: (256,384,3,3) padding = 1,stride = 1     输出大小:(batch_size,256,13,13)ReLU()max pooling(3,stride = 2)                        输出大小:(batch_size,256,6,6)
第六/七/八层全连接层(256*6*6,4096)   ReLU()  Dropout()        输出大小:(batch_size,4096)(4096,4096)      ReLU()  Dropout()          输出大小:(batch_size,4096)(4096,1000)输出大小:(batch_size,1000)

  原论文中是采取了两块GPU进行交互,所以在图中有两条支路。AlexNet架构有6000万参数和650000个神经元,包含5层卷积网络,其中一些含有max pooling,还有3层全连接层,最后一层的节点数1000个,采用softmax分类

  但是呢,你只要细心一点就会发现,第一层卷积层的224×224是有问题的,应该是227×227才对,这一点在吴恩达老师的视频中也被提到,所以正确的图应该是下面这个。

▲ 图1.2.2 AlexNet网络结构

1.3 代码实现

  下面的代码来自于: paddle实现AlexNet ,记录在这里,用于之后进行分析和利用。

# -*- coding: utf-8 -*-
# @Time : 2020/1/21 11:18
# @Author : Zhao HL
# @File : alexnet-paddle.py
import os, sys
from PIL import Image
import numpy as np
import pandas as pd
import paddle
from paddle import fluid
from paddle.fluid.layers import data, conv2d, pool2d, flatten, fc, cross_entropy, accuracy, mean
from my_utils import process_show, draw_loss_acc# region parameters
# region paths
Data_path = "./data/my_imagenet"
Data_csv_path = "./data/my_imagenet.csv"
Model_path = 'model/'
Model_file_tf = "model/alexnet_tf.ckpt"
Model_file_keras = "model/alexnet_keras.h5"
Model_file_torch = "model/alexnet_torch.pth"
Model_file_paddle = "model/alexnet_paddle.model"
# endregion# region image parameter
Img_size = 227
Img_chs = 3
Label_size = 1
Label_class = {'n02091244': 'Ibizan hound','n02114548': 'white wolf','n02138441': 'meerkat','n03584254': 'iPod','n03075370': 'combination lock','n09256479': 'coral reef','n03980874': 'poncho','n02174001': 'rhinoceros beetle','n03770439': 'miniskirt','n03773504': 'missile'}
Labels_nums = len(Label_class)
# endregion# region net parameter
Conv1_kernel_size = 11
Conv1_chs = 96
Conv2_kernel_size = 5
Conv2_chs = 256
Conv3_kernel_size = 3
Conv3_chs = 384
Conv4_kernel_size = 3
Conv4_chs = 384
Conv5_kernel_size = 3
Conv5_chs = 256
Flatten_size = 6 * 6 * 256
Fc1_size = 4096
Fc2_size = 4096
Fc3_size = Labels_nums
# endregion# region hpyerparameter
Learning_rate = 1e-4
Batch_size = 32
Buffer_size = 256
Infer_size = 1
Epochs = 10
Train_num = 700
Train_batch_num = Train_num // Batch_size
Val_num = 100
Val_batch_num = Val_num // Batch_size
Test_num = 200
Test_batch_num = Test_num // Batch_size
# endregion
place = fluid.CUDAPlace(0) if fluid.cuda_places() else fluid.CPUPlace()
# endregionclass MyDataset():def __init__(self, root_path, batch_size, files_list=None,):self.root_path = root_pathself.files_list = files_list if files_list else os.listdir(root_path)self.size = len(files_list)self.batch_size = batch_sizedef __len__(self):return self.sizedef dataset_reader(self):passfiles_list = self.files_list if self.files_list is not None else os.listdir(self.root_path)def reader():np.random.shuffle(files_list)for file_name in files_list:label_str = file_name[:9]label = list(Label_class.keys()).index(label_str)img = Image.open(os.path.join(self.root_path, file_name))yield img, labelreturn paddle.batch(paddle.reader.xmap_readers(self.transform, reader, 2, Buffer_size), batch_size=self.batch_size)def transform(self,sample):def Normalize(image, means, stds):for band in range(len(means)):image[:, :, band] = image[:, :, band] / 255.0image[:, :, band] = (image[:, :, band] - means[band]) / stds[band]image = np.transpose(image, [2, 1, 0])return imagepassimage, label = sampleimage = image.resize((Img_size, Img_size), Image.ANTIALIAS)image = Normalize(np.array(image).astype(np.float), [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])return image, labelclass AlexNet:def __init__(self, structShow=False):self.structShow = structShowself.image = data(shape=[Img_chs, Img_size, Img_size], dtype='float32', name='image')self.label = data(shape=[Label_size], dtype='int64', name='label')self.predict = self.get_alexNet()def get_alexNet(self):conv1 = conv2d(self.image, Conv1_chs, filter_size=Conv1_kernel_size, stride=4, padding=0, act='relu')pool1 = pool2d(conv1, 3, pool_stride=2, pool_type='max')conv2 = conv2d(pool1, Conv2_chs, filter_size=Conv2_kernel_size, padding=2, act='relu')pool2 = pool2d(conv2, 3, pool_stride=2, pool_type='max')conv3 = conv2d(pool2, Conv3_chs, filter_size=Conv3_kernel_size, padding=1, act='relu')conv4 = conv2d(conv3, Conv4_chs, filter_size=Conv4_kernel_size, padding=1, act='relu')conv5 = conv2d(conv4, Conv5_chs, filter_size=Conv5_kernel_size, padding=1, act='relu')pool3 = pool2d(conv5, 3, pool_stride=2, pool_type='max')flt = flatten(pool3, axis=1)fc1 = fc(flt, Fc1_size, act='relu')fc2 = fc(fc1, Fc2_size, act='relu')fc3 = fc(fc1, Fc3_size, act='softmax')if self.structShow:print(conv1.name, conv1.shape)print(pool1.name, pool1.shape)print(conv2.name, conv2.shape)print(pool2.name, pool2.shape)print(conv3.name, conv3.shape)print(conv4.name, conv4.shape)print(conv5.name, conv5.shape)print(pool3.name, pool3.shape)print(flt.name, flt.shape)print(fc1.name, fc1.shape)print(fc2.name, fc2.shape)print(fc3.name, fc3.shape)return fc3def train():net = AlexNet(structShow=True)image, label, predict = net.image, net.label, net.predictfeeder = fluid.DataFeeder(place=place, feed_list=[image, label])df = pd.read_csv(Data_csv_path, header=0, index_col=0)train_list = df[df['split'] == 'train']['filename'].tolist()val_list = df[df['split'] == 'val']['filename'].tolist()train_reader = MyDataset(Data_path, batch_size=Batch_size, files_list=train_list).dataset_reader()val_reader =  MyDataset(Data_path, batch_size=Batch_size, files_list=val_list).dataset_reader()loss = cross_entropy(input=predict, label=label)loss_mean = mean(loss)acc = accuracy(input=predict, label=label)optimizer = fluid.optimizer.AdamOptimizer(learning_rate=Learning_rate)optimizer.minimize(loss_mean)val_program = fluid.default_main_program().clone(for_test=True)exe = fluid.Executor(place)exe.run(fluid.default_startup_program())train_losses = np.ones(Epochs)train_accs = np.ones(Epochs)val_losses = np.ones(Epochs)val_accs = np.ones(Epochs)best_loss = float("inf")best_loss_epoch = 0for epoch in range(Epochs):print('Epoch %d/%d:' % (epoch + 1, Epochs))train_sum_loss = 0train_sum_acc = 0val_sum_loss = 0val_sum_acc = 0for batch_num, data in enumerate(train_reader()):train_loss, train_acc = exe.run(program=fluid.default_main_program(),  # 运行主程序feed=feeder.feed(data),  # 给模型喂入数据fetch_list=[loss_mean, acc])  # fetch 误差、准确率train_sum_loss += train_loss[0]train_sum_acc += train_acc[0]process_show(batch_num + 1, Train_num / Batch_size, train_acc, train_loss, prefix='train:')for batch_num, data in enumerate(val_reader()):val_loss, val_acc = exe.run(program=val_program,  # 执行训练程序feed=feeder.feed(data),  # 喂入数据fetch_list=[loss_mean, acc])  # fetch 误差、准确率val_sum_loss += val_loss[0]val_sum_acc += val_acc[0]process_show(batch_num + 1, Val_num / Batch_size, val_acc, val_loss, prefix='train:')train_sum_loss /= (Train_num // Batch_size)train_sum_acc /= (Train_num // Batch_size)val_sum_loss /= (Val_num // Batch_size)val_sum_acc /= (Val_num // Batch_size)train_losses[epoch] = train_sum_losstrain_accs[epoch] = train_sum_accval_losses[epoch] = val_sum_lossval_accs[epoch] = val_sum_accprint('average summary:\ntrain acc %.4f, loss %.4f ; val acc %.4f, loss %.4f'% (train_sum_acc, train_sum_loss, val_sum_acc, val_sum_loss))if val_sum_loss < best_loss:print('val_loss improve from %.4f to %.4f, model save to %s ! \n' % (best_loss, val_sum_loss, Model_file_paddle))best_loss = val_sum_lossbest_loss_epoch = epoch + 1fluid.io.save_inference_model(Model_file_paddle,  # 保存推理model的路径['image'],  # 推理(inference)需要 feed 的数据[predict],  # 保存推理(inference)结果的 Variablesexe)  # executor 保存 inference modelelse:print('val_loss do not improve from %.4f \n' % (best_loss))print('best loss %.4f at epoch %d \n' % (best_loss, best_loss_epoch))draw_loss_acc(train_losses, train_accs, 'train')draw_loss_acc(val_losses, val_accs, 'val')if __name__ == '__main__':pass# dataInfo_show(r'E:\_Python\01_deeplearning\03_AlexNet\data\my_imagenet',#               r'E:\_Python\01_deeplearning\03_AlexNet\data\my_imagenet.csv',#               r'E:\_Python\01_deeplearning\03_AlexNet\data\synset_words.txt')# dataset_divide(r'E:\_Python\01_deeplearning\03_AlexNet\data\my_imagenet.csv')train()

※ 总  结 ※


  对于AlexNet的网络基本结构进行总结。由于这个网络在实现过程中所需要的计算力比较大。再次并未对其进行测试。


■ 相关文献链接:

  • AlexNet学习
  • ImageNet Classification with Deep ConvolutionalNeural Networks
  • paddle实现AlexNet

● 相关图表链接:

  • 图1.2.1 AlexNet网络结构
  • 图1.2.2 AlexNet网络结构

在 AI Studio中利用Paddle实现经典的AlexNet相关推荐

  1. AI Studio : 利用Paddle框架中的极简框架识别MNIST

    简 介: ※通过测试网络上的这个极简的Paddle识别MNIST程序,也就是使用了一个非常简单的线性回归网络,初步熟悉了Paddle下的网络架构方式.对于如果从numpy到Paddle的tensor转 ...

  2. AI Studio中的视觉数据集合

    简 介: ※对于Paddle中的vision中的图片数据Cifar10, FashionMNIST进行显示与测试. 关键词: Cifar10,FashionMNIST #mermaid-svg-4a4 ...

  3. 搭建AI Studio 中Python开发环境

    简 介: 测试与完成了基于编写的python小程序"ais",可以直接直接将TEASOFT中的编辑的python程序通过Windows剪切板发送到AI Studio,自动运行.通过 ...

  4. 如何自动将AI Studio中的GIF文件上载到CSDN?

    ▲ 图1 旋转的数字 简 介: 本文给出了自动将AI Studio工作环境中的GIF目录下的动图图片完成压缩,下载并上载到CSDN的整个过程.给出了实现的详细代码.利用这个工具便可以完成更加丰富多彩的 ...

  5. android定制机测试,在 Android Studio 中利用 “产品定制” 进行封闭测试

    最近,我们借助 Android 开发者峰会的机会与开发者进行了交流,期间,我们以一个简单的 Notes 应用为例,探讨了它在 Android 上的测试情况.此示例应用是我们在测试 codelab 的过 ...

  6. 在Visual Studio中利用NTVS创建Pomelo项目

    刚看新闻,才知道微软发布了Node.js Tools for Visual Studio(NTVS),受够了WebStorm输入法Bug的困扰,这下终于可以解脱了.以Pomelo为例,运行命令:pom ...

  7. 在Paddle中利用AlexNet测试CIFAR10数据集合

    简 介: 利用Paddle框架搭建了AlexNet网络,并在AI Studio上利用其至尊版本测试了AlexNet对于Cifar10的分类效果. 基础的训练在测试集合上的分类效果没有能够超过60%,这 ...

  8. 【AI Studio】飞桨图像分类零基础训练营 - 03 - 卷积神经网络基础

    前言:第三天,老师结合ppt文图详细讲解了线性和卷积网络的构建,由简单到复杂的讲解卷积网络的发展.最后结合几个项目加深理解.愈发感觉老师讲的好了.第二天的课听完后还感觉自己什么都懂了,结果轮到自己动手 ...

  9. 在AI Studio上部署外部可以直接调用的云端服务

    简 介: 按照 百度上的快速部署的两个平台:EasyDL以及AI Studio体验馆 的例子,对于在云端部署可以直接调用的服务进行了测试.但最后没有能够测试成功.后面还需要通过具体的例子来测试完整进行 ...

最新文章

  1. 风暴航路的地图编号_砖厂风暴航路已倒闭,新的搬砖圣地诞生
  2. Vector Enumeration
  3. 阿里云飞天论文获国际架构顶会 ATC 2021最佳论文:全球仅三篇
  4. c语言动态内存分配数组,【C】动态内存分配
  5. java所有单词汇总
  6. 一文详解中英文在NLP上的10大差异点
  7. html如何制作斜线表头表格,2种方法教你怎样在WPS excel中绘制表格斜线表头
  8. Linux 上如何清除 RAM 内存高速缓存,缓存和交换空间
  9. Vue Router 路由(路由守卫)---route
  10. 高新技术企业认定中科技成果转化数量的认定
  11. 软件工程——数据流图
  12. 第13届中国生物特征识别大会即将盛大开幕,中科博宏期待您的到来!
  13. ARCGIS水文分析:水库容量和蓄水区计算
  14. 分布式电商平台架构实践
  15. python3实现PAT乙级算法题库全集
  16. BZOJ 3036 绿豆蛙的归宿 期望DP
  17. vscode中使用prettier后html,css格式化不生效的问题
  18. 解决win10系统,pdf无法预览的问题
  19. GeoServer+mapbox发布矢量瓦片(一):创建美国区域显示
  20. 摩王软件OA+系统功能简介

热门文章

  1. [你必须知道的.NET]第二十二回:字符串驻留(上)---带着问题思考
  2. .NET程序员,我们应该更专业点
  3. linux 加载u盘、光盘、软盘 mount使用指南
  4. C# CAD对象 构造时应把它的父对象也加进它的属性里
  5. github中SSH的Key
  6. logback:用slf4j+logback实现多功能日志解决方案
  7. 《易学Python》——第6章 类与面向对象编程 6.1 类是什么
  8. 【TCP/IP详解 卷一:协议】第十二章 广播和多播
  9. 中大型网站静态资源优化及存储
  10. UF_DRAW_set_view_display 使用问题