import{s as Qe,n as et,o as tt}from"../chunks/scheduler.7bc62968.js";import{S as nt,i as it,g as s,s as a,r as h,A as at,h as r,f as n,c as o,j as Ze,u as m,x as l,k as Je,y as ot,a as i,v as c,d as f,t as p,w as u}from"../chunks/index.2f8492b0.js";import{H as d,E as st}from"../chunks/EditOnGithub.2a9ce03a.js";function rt(Re){let g,Z,W,J,v,Q,$,ee,y,He=`Recently, the breakthrough of Vision Transformers (ViTs) quickly superseded pure CNN models as the new state-of-the-art for image recognition.
Intriguingly, research has found that CNNs could adopt a significant portion of the design choices in Vision Transformers.
ConvNext represents a significant improvement to pure convolution models by incorporating techniques inspired by ViTs and achieving results comparable to ViTs in accuracy and scalability.`,te,w,ne,x,Se=`The author of the ConvNeXT paper starts building the model with a regular ResNet (ResNet-50), then modernizes and improves the architecture step-by-step to imitate the hierarchical structure of Vision Transformers.
The key improvements are:`,ie,b,Ie=`<li>Training Techniques</li> <li>Macro Design</li> <li>ResNeXt-ify</li> <li>Inverted Bottleneck</li> <li>Large Kernel Sizes</li> <li>Micro Design
We will go through each of the key improvements.
These designs are not novel in itself. However, you can learn how researchers adapt and modify designs systematically to improve existing models.
To show the effectiveness of each improvement, we will compare the model’s accuracy before and after the modification on ImageNet-1K.</li>`,ae,T,qe='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/block_comparison.png" alt="Block Comparison"/>',oe,C,se,N,Ee=`The researchers first discerned that, while architectural design choices are crucial, the quality of the training procedure also plays a pivotal role in influencing performance outcomes.
Inspired by DeiT and Swin Transformers, ConvNext closely adapts their training techniques. Some of the notable changes are:`,re,k,Ae=`<li>Epochs: Extending the epochs from the original 90 epochs to 300 epochs.</li> <li>Optimizer: Using AdamW optimizer instead of Adam optimizer, which differs in how it handles weight decay.</li> <li>Regularization: Using Stochastic Depth and Label Smoothing as regularization techniques.</li> <li>Mixup (generates a weighted combination of random image pairs), Cutmix (cuts part of an image and replace it with a patch from another image), RandAugment (applies a series of random augmentations such as rotation, translation, and shear), and Random Erasing (randomly selects a rectangle region in an image and erases its pixels with random values) to increase training data.
Modifying these training procedures has improved ResNet-50’s accuracy from 76.1% to 78.8%.</li> <li>Regularization: Using Stochastic Depth and Label Smoothing as regularization techniques.
Modifying these training procedures has improved ResNet-50’s accuracy from 76.1% to 78.8%.</li>`,le,L,he,_,De=`Macro design refers to the high-level structural decisions and considerations made in a system or model, such as the arrangement of layers, the distribution of computational load across different stages, and the overall structure.
Examining Swin Transformers’ macro network, the authors have identified two noteworthy design considerations that benefit the performance of ConvNext.`,me,M,ce,P,Ue=`The stage compute ratio refers to the distribution of computational load among the stages of a neural network model.
ResNet-50 has four main stages with (3, 4, 6, 3) blocks, which means it has a compute ratio of 3:4:6:3.
To follow Swin Transformer’s compute ratio of 1:1:3:1, the researchers adjusted the number of blocks on each stage of the ResNet from (3, 4, 6, 3) to (3, 3, 9, 3) instead.
Changing the stage compute ratio improves the model accuracy from 78.8% to 79.4%.`,fe,z,pe,R,Be=`Typically, at the start of ResNet’s architecture, the input is fed to a stem 7×7 convolution layer with stride 2, followed by a max pool, used to downsample the image by a factor of 4.
However, the authors discovered that substituting the stem with a convolutional layer featuring a 4×4 kernel size and a stride of 4 is more effective, effectively convolving them through non-overlapping 4x4 patches.
Patchify serves the same purpose of downsampling the image by a factor of 4 while reducing the number of layers.
This Patchifying step slightly improves the model accuracy from 79.4% to 79.5%.`,ue,H,de,S,Ve=`ConvNext also adopts the idea of ResNeXt, explained in the previous section.
ResNeXt demonstrates an improved trade-off between the number of floating-point operations (FLOPs) and accuracy compared to a standard ResNet.
By using depthwise convolution and 1 × 1 convolutions, we would have the separation of spatial and channel mixing - a characteristic also found in vision Transformers.
Using depthwise convolutions reduces the number of FLOPS and the accuracy.
However, by increasing the channels from 64 to 96, the accuracy is higher than the original ResNet-50 while maintaining a similar number of FLOPs.
This modification improves the model accuracy from 79.5% to 80.5%.`,ge,I,ve,q,Fe=`One common idea in every Transformer block is the usage of an inverted bottleneck, where the hidden layers are much bigger than the input dimension.
This idea has also been used and popularized in Computer Vision by MobileNetV2.
ConvNext adopts this idea, having input layers with 96 channels and increasing the hidden layers to 384 channels.
By using this technique, it improves the model accuracy from 80.5% to 80.6%.`,$e,E,Xe='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/inverted_bottleneck.png" alt="Inverted Bottleneck Comparison"/>',ye,A,we,D,je=`A key factor contributing to the exceptional performance of Vision Transformer is its non-local self-attention, allowing for a broader receptive field of image features.
In Swin Transformers, the attention block window size is set to at least 7×7, surpassing the 3x3 kernel size of ResNext.
However, before adjusting the kernel size, it is necessary to reposition the depthwise convolution layer, as shown in the image below.
This repositioning enables the 1x1 layers to efficiently handle computational tasks, while the depthwise convolution layer functions as a more non-local receptor.
With this, the network can harness the advantages of incorporating bigger kernel-sized convolutions.
Implementing a 7x7 kernel size maintains the accuracy at 80.6% but reduces the overall FLOPs efficiency of the model.`,xe,U,Oe='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/depthwise_moveup.png" alt="Moving up the Depth Conv Layer"/>',be,B,Te,V,Ke=`In addition to the modifications stated, the author adds some micro-design changes to the model.
Mico design refers to low-level structural decisions such as the choice of activation functions and layer details.
Some of the notable micro changes are:`,Ce,F,Ge=`<li>Activation: Replacing ReLU activation with GELU (Gaussian Error Linear Unit) and eliminating all GELU layers from the residual block except for one between two 1×1 layers.</li> <li>Normalization: Fewer normalization layers by removing two BatchNorm layers and substituting BatchNorm with LayerNorm, leaving only one LayerNorm layer before the conv 1 × 1 layers.</li> <li>Downsampling Layer: Add a separate downsample layer in between ResNet stages
These final modifications improve the ConvNext accuracy from 80.6% to 82.0%.
The final ConvNext model exceeds Swin Transformer’s accuracy of 81.3%.</li>`,Ne,X,ke,j,We='You can go to <a href="https://huggingface.co/docs/transformers/model_doc/convnext" rel="nofollow">this HuggingFace documentation</a> to learn how to integrate the ConvNext pipeline into your code.',Le,O,_e,K,Ye=`The paper “A ConvNet for the 2020s” proposed the ConvNext architecture in 2022 by a team of researchers from Facebook AI Research, including Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, and Saining Xie.
The paper can be found <a href="https://arxiv.org/abs/2201.03545" rel="nofollow">here</a> and the GitHub repo can be found <a href="https://github.com/facebookresearch/ConvNeXt" rel="nofollow">here</a>.`,Me,G,Pe,Y,ze;return v=new d({props:{title:"ConvNext - A ConvNet for the 2020s (2022)",local:"convnext---a-convnet-for-the-2020s-2022",headingTag:"h1"}}),$=new d({props:{title:"Introduction",local:"introduction",headingTag:"h2"}}),w=new d({props:{title:"Key Improvements",local:"key-improvements",headingTag:"h2"}}),C=new d({props:{title:"Training Techniques",local:"training-techniques",headingTag:"h2"}}),L=new d({props:{title:"Macro Design",local:"macro-design",headingTag:"h2"}}),M=new d({props:{title:"The stage compute ratio",local:"the-stage-compute-ratio",headingTag:"h3"}}),z=new d({props:{title:"Changing stem to Patchify",local:"changing-stem-to-patchify",headingTag:"h3"}}),H=new d({props:{title:"ResNeXt-ify",local:"resnext-ify",headingTag:"h2"}}),I=new d({props:{title:"Inverted Bottleneck",local:"inverted-bottleneck",headingTag:"h2"}}),A=new d({props:{title:"Large Kernel Sizes",local:"large-kernel-sizes",headingTag:"h2"}}),B=new d({props:{title:"Micro Design",local:"micro-design",headingTag:"h2"}}),X=new d({props:{title:"Model Code",local:"model-code",headingTag:"h2"}}),O=new d({props:{title:"References",local:"references",headingTag:"h2"}}),G=new st({props:{source:"https://github.com/johko/computer-vision-course/blob/main/chapters/en/unit2/cnns/convnext.mdx"}}),{c(){g=s("meta"),Z=a(),W=s("p"),J=a(),h(v.$$.fragment),Q=a(),h($.$$.fragment),ee=a(),y=s("p"),y.textContent=He,te=a(),h(w.$$.fragment),ne=a(),x=s("p"),x.textContent=Se,ie=a(),b=s("ul"),b.innerHTML=Ie,ae=a(),T=s("p"),T.innerHTML=qe,oe=a(),h(C.$$.fragment),se=a(),N=s("p"),N.textContent=Ee,re=a(),k=s("ul"),k.innerHTML=Ae,le=a(),h(L.$$.fragment),he=a(),_=s("p"),_.textContent=De,me=a(),h(M.$$.fragment),ce=a(),P=s("p"),P.textContent=Ue,fe=a(),h(z.$$.fragment),pe=a(),R=s("p"),R.textContent=Be,ue=a(),h(H.$$.fragment),de=a(),S=s("p"),S.textContent=Ve,ge=a(),h(I.$$.fragment),ve=a(),q=s("p"),q.textContent=Fe,$e=a(),E=s("p"),E.innerHTML=Xe,ye=a(),h(A.$$.fragment),we=a(),D=s("p"),D.textContent=je,xe=a(),U=s("p"),U.innerHTML=Oe,be=a(),h(B.$$.fragment),Te=a(),V=s("p"),V.textContent=Ke,Ce=a(),F=s("ul"),F.innerHTML=Ge,Ne=a(),h(X.$$.fragment),ke=a(),j=s("p"),j.innerHTML=We,Le=a(),h(O.$$.fragment),_e=a(),K=s("p"),K.innerHTML=Ye,Me=a(),h(G.$$.fragment),Pe=a(),Y=s("p"),this.h()},l(e){const t=at("svelte-u9bgzb",document.head);g=r(t,"META",{name:!0,content:!0}),t.forEach(n),Z=o(e),W=r(e,"P",{}),Ze(W).forEach(n),J=o(e),m(v.$$.fragment,e),Q=o(e),m($.$$.fragment,e),ee=o(e),y=r(e,"P",{"data-svelte-h":!0}),l(y)!=="svelte-1xbb17w"&&(y.textContent=He),te=o(e),m(w.$$.fragment,e),ne=o(e),x=r(e,"P",{"data-svelte-h":!0}),l(x)!=="svelte-3zp0ah"&&(x.textContent=Se),ie=o(e),b=r(e,"UL",{"data-svelte-h":!0}),l(b)!=="svelte-zwd32u"&&(b.innerHTML=Ie),ae=o(e),T=r(e,"P",{"data-svelte-h":!0}),l(T)!=="svelte-hpkh9n"&&(T.innerHTML=qe),oe=o(e),m(C.$$.fragment,e),se=o(e),N=r(e,"P",{"data-svelte-h":!0}),l(N)!=="svelte-x3eulp"&&(N.textContent=Ee),re=o(e),k=r(e,"UL",{"data-svelte-h":!0}),l(k)!=="svelte-2xgspu"&&(k.innerHTML=Ae),le=o(e),m(L.$$.fragment,e),he=o(e),_=r(e,"P",{"data-svelte-h":!0}),l(_)!=="svelte-jme7tz"&&(_.textContent=De),me=o(e),m(M.$$.fragment,e),ce=o(e),P=r(e,"P",{"data-svelte-h":!0}),l(P)!=="svelte-164n12x"&&(P.textContent=Ue),fe=o(e),m(z.$$.fragment,e),pe=o(e),R=r(e,"P",{"data-svelte-h":!0}),l(R)!=="svelte-q3frac"&&(R.textContent=Be),ue=o(e),m(H.$$.fragment,e),de=o(e),S=r(e,"P",{"data-svelte-h":!0}),l(S)!=="svelte-19t3tih"&&(S.textContent=Ve),ge=o(e),m(I.$$.fragment,e),ve=o(e),q=r(e,"P",{"data-svelte-h":!0}),l(q)!=="svelte-f2hrwg"&&(q.textContent=Fe),$e=o(e),E=r(e,"P",{"data-svelte-h":!0}),l(E)!=="svelte-1xhu8a2"&&(E.innerHTML=Xe),ye=o(e),m(A.$$.fragment,e),we=o(e),D=r(e,"P",{"data-svelte-h":!0}),l(D)!=="svelte-odbjli"&&(D.textContent=je),xe=o(e),U=r(e,"P",{"data-svelte-h":!0}),l(U)!=="svelte-1vjwuxs"&&(U.innerHTML=Oe),be=o(e),m(B.$$.fragment,e),Te=o(e),V=r(e,"P",{"data-svelte-h":!0}),l(V)!=="svelte-1t3cn42"&&(V.textContent=Ke),Ce=o(e),F=r(e,"UL",{"data-svelte-h":!0}),l(F)!=="svelte-1404rzr"&&(F.innerHTML=Ge),Ne=o(e),m(X.$$.fragment,e),ke=o(e),j=r(e,"P",{"data-svelte-h":!0}),l(j)!=="svelte-1dfpi5t"&&(j.innerHTML=We),Le=o(e),m(O.$$.fragment,e),_e=o(e),K=r(e,"P",{"data-svelte-h":!0}),l(K)!=="svelte-15wicxb"&&(K.innerHTML=Ye),Me=o(e),m(G.$$.fragment,e),Pe=o(e),Y=r(e,"P",{}),Ze(Y).forEach(n),this.h()},h(){Je(g,"name","hf:doc:metadata"),Je(g,"content",lt)},m(e,t){ot(document.head,g),i(e,Z,t),i(e,W,t),i(e,J,t),c(v,e,t),i(e,Q,t),c($,e,t),i(e,ee,t),i(e,y,t),i(e,te,t),c(w,e,t),i(e,ne,t),i(e,x,t),i(e,ie,t),i(e,b,t),i(e,ae,t),i(e,T,t),i(e,oe,t),c(C,e,t),i(e,se,t),i(e,N,t),i(e,re,t),i(e,k,t),i(e,le,t),c(L,e,t),i(e,he,t),i(e,_,t),i(e,me,t),c(M,e,t),i(e,ce,t),i(e,P,t),i(e,fe,t),c(z,e,t),i(e,pe,t),i(e,R,t),i(e,ue,t),c(H,e,t),i(e,de,t),i(e,S,t),i(e,ge,t),c(I,e,t),i(e,ve,t),i(e,q,t),i(e,$e,t),i(e,E,t),i(e,ye,t),c(A,e,t),i(e,we,t),i(e,D,t),i(e,xe,t),i(e,U,t),i(e,be,t),c(B,e,t),i(e,Te,t),i(e,V,t),i(e,Ce,t),i(e,F,t),i(e,Ne,t),c(X,e,t),i(e,ke,t),i(e,j,t),i(e,Le,t),c(O,e,t),i(e,_e,t),i(e,K,t),i(e,Me,t),c(G,e,t),i(e,Pe,t),i(e,Y,t),ze=!0},p:et,i(e){ze||(f(v.$$.fragment,e),f($.$$.fragment,e),f(w.$$.fragment,e),f(C.$$.fragment,e),f(L.$$.fragment,e),f(M.$$.fragment,e),f(z.$$.fragment,e),f(H.$$.fragment,e),f(I.$$.fragment,e),f(A.$$.fragment,e),f(B.$$.fragment,e),f(X.$$.fragment,e),f(O.$$.fragment,e),f(G.$$.fragment,e),ze=!0)},o(e){p(v.$$.fragment,e),p($.$$.fragment,e),p(w.$$.fragment,e),p(C.$$.fragment,e),p(L.$$.fragment,e),p(M.$$.fragment,e),p(z.$$.fragment,e),p(H.$$.fragment,e),p(I.$$.fragment,e),p(A.$$.fragment,e),p(B.$$.fragment,e),p(X.$$.fragment,e),p(O.$$.fragment,e),p(G.$$.fragment,e),ze=!1},d(e){e&&(n(Z),n(W),n(J),n(Q),n(ee),n(y),n(te),n(ne),n(x),n(ie),n(b),n(ae),n(T),n(oe),n(se),n(N),n(re),n(k),n(le),n(he),n(_),n(me),n(ce),n(P),n(fe),n(pe),n(R),n(ue),n(de),n(S),n(ge),n(ve),n(q),n($e),n(E),n(ye),n(we),n(D),n(xe),n(U),n(be),n(Te),n(V),n(Ce),n(F),n(Ne),n(ke),n(j),n(Le),n(_e),n(K),n(Me),n(Pe),n(Y)),n(g),u(v,e),u($,e),u(w,e),u(C,e),u(L,e),u(M,e),u(z,e),u(H,e),u(I,e),u(A,e),u(B,e),u(X,e),u(O,e),u(G,e)}}}const lt='{"title":"ConvNext - A ConvNet for the 2020s (2022)","local":"convnext---a-convnet-for-the-2020s-2022","sections":[{"title":"Introduction","local":"introduction","sections":[],"depth":2},{"title":"Key Improvements","local":"key-improvements","sections":[],"depth":2},{"title":"Training Techniques","local":"training-techniques","sections":[],"depth":2},{"title":"Macro Design","local":"macro-design","sections":[{"title":"The stage compute ratio","local":"the-stage-compute-ratio","sections":[],"depth":3},{"title":"Changing stem to Patchify","local":"changing-stem-to-patchify","sections":[],"depth":3}],"depth":2},{"title":"ResNeXt-ify","local":"resnext-ify","sections":[],"depth":2},{"title":"Inverted Bottleneck","local":"inverted-bottleneck","sections":[],"depth":2},{"title":"Large Kernel Sizes","local":"large-kernel-sizes","sections":[],"depth":2},{"title":"Micro Design","local":"micro-design","sections":[],"depth":2},{"title":"Model Code","local":"model-code","sections":[],"depth":2},{"title":"References","local":"references","sections":[],"depth":2}],"depth":1}';function ht(Re){return tt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class pt extends nt{constructor(g){super(),it(this,g,ht,rt,Qe,{})}}export{pt as component};
