import{s as rt,n as lt,o as mt}from"../chunks/scheduler.35aab934.js";import{S as ht,i as ft,e as s,s as a,c as m,h as ct,a as r,d as n,b as o,f as ot,g as h,j as l,k as st,l as pt,m as i,n as f,t as c,o as p,p as u}from"../chunks/index.de17b971.js";import{C as ut}from"../chunks/CopyLLMTxtMenu.48bc5cc2.js";import{H as g,E as gt}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8fdece89.js";function dt(Ae){let d,ee,J,te,v,ne,$,ie,y,ae,x,Ue=`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.`,oe,w,se,C,De=`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:`,re,b,Be="<li>Training techniques</li> <li>Macro design</li> <li>ResNeXt-ify</li> <li>Inverted Bottleneck</li> <li>Large Kernel Sizes</li> <li>Micro Design</li>",le,T,Ve=`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.`,me,N,Fe='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/block_comparison.png" alt="Block Comparison"/>',he,_,fe,L,Xe=`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:`,ce,k,je="<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>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.</li> <li>Regularization: Using Stochastic Depth and Label Smoothing as regularization techniques.</li>",pe,M,Oe="Modifying these training procedures has improved ResNet-50’s accuracy from 76.1% to 78.8%.",ue,P,ge,z,Ke=`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.`,de,H,ve,R,Ge=`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%.`,$e,S,ye,I,We=`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%.`,xe,q,we,E,Ye=`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%.`,Ce,A,be,U,Ze=`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%.`,Te,D,Je='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/inverted_bottleneck.png" alt="Inverted Bottleneck Comparison"/>',Ne,B,_e,V,Qe=`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.`,Le,F,et='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/depthwise_moveup.png" alt="Moving up the Depth Conv Layer"/>',ke,X,Me,j,tt=`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:`,Pe,O,nt=`<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>`,ze,K,He,G,it='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.',Re,W,Se,Y,at=`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>.`,Ie,Z,qe,Q,Ee;return v=new ut({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),$=new g({props:{title:"ConvNext - A ConvNet for the 2020s (2022)",local:"convnext---a-convnet-for-the-2020s-2022",headingTag:"h1"}}),y=new g({props:{title:"Introduction",local:"introduction",headingTag:"h2"}}),w=new g({props:{title:"Key Improvements",local:"key-improvements",headingTag:"h2"}}),_=new g({props:{title:"Training Techniques",local:"training-techniques",headingTag:"h2"}}),P=new g({props:{title:"Macro Design",local:"macro-design",headingTag:"h2"}}),H=new g({props:{title:"The stage compute ratio",local:"the-stage-compute-ratio",headingTag:"h3"}}),S=new g({props:{title:"Changing stem to Patchify",local:"changing-stem-to-patchify",headingTag:"h3"}}),q=new g({props:{title:"ResNeXt-ify",local:"resnext-ify",headingTag:"h2"}}),A=new g({props:{title:"Inverted Bottleneck",local:"inverted-bottleneck",headingTag:"h2"}}),B=new g({props:{title:"Large Kernel Sizes",local:"large-kernel-sizes",headingTag:"h2"}}),X=new g({props:{title:"Micro Design",local:"micro-design",headingTag:"h2"}}),K=new g({props:{title:"Model Code",local:"model-code",headingTag:"h2"}}),W=new g({props:{title:"References",local:"references",headingTag:"h2"}}),Z=new gt({props:{source:"https://github.com/huggingface/computer-vision-course/blob/main/chapters/en/unit2/cnns/convnext.mdx"}}),{c(){d=s("meta"),ee=a(),J=s("p"),te=a(),m(v.$$.fragment),ne=a(),m($.$$.fragment),ie=a(),m(y.$$.fragment),ae=a(),x=s("p"),x.textContent=Ue,oe=a(),m(w.$$.fragment),se=a(),C=s("p"),C.textContent=De,re=a(),b=s("ul"),b.innerHTML=Be,le=a(),T=s("p"),T.textContent=Ve,me=a(),N=s("p"),N.innerHTML=Fe,he=a(),m(_.$$.fragment),fe=a(),L=s("p"),L.textContent=Xe,ce=a(),k=s("ul"),k.innerHTML=je,pe=a(),M=s("p"),M.textContent=Oe,ue=a(),m(P.$$.fragment),ge=a(),z=s("p"),z.textContent=Ke,de=a(),m(H.$$.fragment),ve=a(),R=s("p"),R.textContent=Ge,$e=a(),m(S.$$.fragment),ye=a(),I=s("p"),I.textContent=We,xe=a(),m(q.$$.fragment),we=a(),E=s("p"),E.textContent=Ye,Ce=a(),m(A.$$.fragment),be=a(),U=s("p"),U.textContent=Ze,Te=a(),D=s("p"),D.innerHTML=Je,Ne=a(),m(B.$$.fragment),_e=a(),V=s("p"),V.textContent=Qe,Le=a(),F=s("p"),F.innerHTML=et,ke=a(),m(X.$$.fragment),Me=a(),j=s("p"),j.textContent=tt,Pe=a(),O=s("ul"),O.innerHTML=nt,ze=a(),m(K.$$.fragment),He=a(),G=s("p"),G.innerHTML=it,Re=a(),m(W.$$.fragment),Se=a(),Y=s("p"),Y.innerHTML=at,Ie=a(),m(Z.$$.fragment),qe=a(),Q=s("p"),this.h()},l(e){const t=ct("svelte-u9bgzb",document.head);d=r(t,"META",{name:!0,content:!0}),t.forEach(n),ee=o(e),J=r(e,"P",{}),ot(J).forEach(n),te=o(e),h(v.$$.fragment,e),ne=o(e),h($.$$.fragment,e),ie=o(e),h(y.$$.fragment,e),ae=o(e),x=r(e,"P",{"data-svelte-h":!0}),l(x)!=="svelte-1xbb17w"&&(x.textContent=Ue),oe=o(e),h(w.$$.fragment,e),se=o(e),C=r(e,"P",{"data-svelte-h":!0}),l(C)!=="svelte-3zp0ah"&&(C.textContent=De),re=o(e),b=r(e,"UL",{"data-svelte-h":!0}),l(b)!=="svelte-1surxjp"&&(b.innerHTML=Be),le=o(e),T=r(e,"P",{"data-svelte-h":!0}),l(T)!=="svelte-1xn2fwb"&&(T.textContent=Ve),me=o(e),N=r(e,"P",{"data-svelte-h":!0}),l(N)!=="svelte-hpkh9n"&&(N.innerHTML=Fe),he=o(e),h(_.$$.fragment,e),fe=o(e),L=r(e,"P",{"data-svelte-h":!0}),l(L)!=="svelte-x3eulp"&&(L.textContent=Xe),ce=o(e),k=r(e,"UL",{"data-svelte-h":!0}),l(k)!=="svelte-1p5xkp1"&&(k.innerHTML=je),pe=o(e),M=r(e,"P",{"data-svelte-h":!0}),l(M)!=="svelte-2ffe9x"&&(M.textContent=Oe),ue=o(e),h(P.$$.fragment,e),ge=o(e),z=r(e,"P",{"data-svelte-h":!0}),l(z)!=="svelte-jme7tz"&&(z.textContent=Ke),de=o(e),h(H.$$.fragment,e),ve=o(e),R=r(e,"P",{"data-svelte-h":!0}),l(R)!=="svelte-164n12x"&&(R.textContent=Ge),$e=o(e),h(S.$$.fragment,e),ye=o(e),I=r(e,"P",{"data-svelte-h":!0}),l(I)!=="svelte-q3frac"&&(I.textContent=We),xe=o(e),h(q.$$.fragment,e),we=o(e),E=r(e,"P",{"data-svelte-h":!0}),l(E)!=="svelte-19t3tih"&&(E.textContent=Ye),Ce=o(e),h(A.$$.fragment,e),be=o(e),U=r(e,"P",{"data-svelte-h":!0}),l(U)!=="svelte-f2hrwg"&&(U.textContent=Ze),Te=o(e),D=r(e,"P",{"data-svelte-h":!0}),l(D)!=="svelte-1xhu8a2"&&(D.innerHTML=Je),Ne=o(e),h(B.$$.fragment,e),_e=o(e),V=r(e,"P",{"data-svelte-h":!0}),l(V)!=="svelte-odbjli"&&(V.textContent=Qe),Le=o(e),F=r(e,"P",{"data-svelte-h":!0}),l(F)!=="svelte-1vjwuxs"&&(F.innerHTML=et),ke=o(e),h(X.$$.fragment,e),Me=o(e),j=r(e,"P",{"data-svelte-h":!0}),l(j)!=="svelte-1t3cn42"&&(j.textContent=tt),Pe=o(e),O=r(e,"UL",{"data-svelte-h":!0}),l(O)!=="svelte-1404rzr"&&(O.innerHTML=nt),ze=o(e),h(K.$$.fragment,e),He=o(e),G=r(e,"P",{"data-svelte-h":!0}),l(G)!=="svelte-1dfpi5t"&&(G.innerHTML=it),Re=o(e),h(W.$$.fragment,e),Se=o(e),Y=r(e,"P",{"data-svelte-h":!0}),l(Y)!=="svelte-15wicxb"&&(Y.innerHTML=at),Ie=o(e),h(Z.$$.fragment,e),qe=o(e),Q=r(e,"P",{}),ot(Q).forEach(n),this.h()},h(){st(d,"name","hf:doc:metadata"),st(d,"content",vt)},m(e,t){pt(document.head,d),i(e,ee,t),i(e,J,t),i(e,te,t),f(v,e,t),i(e,ne,t),f($,e,t),i(e,ie,t),f(y,e,t),i(e,ae,t),i(e,x,t),i(e,oe,t),f(w,e,t),i(e,se,t),i(e,C,t),i(e,re,t),i(e,b,t),i(e,le,t),i(e,T,t),i(e,me,t),i(e,N,t),i(e,he,t),f(_,e,t),i(e,fe,t),i(e,L,t),i(e,ce,t),i(e,k,t),i(e,pe,t),i(e,M,t),i(e,ue,t),f(P,e,t),i(e,ge,t),i(e,z,t),i(e,de,t),f(H,e,t),i(e,ve,t),i(e,R,t),i(e,$e,t),f(S,e,t),i(e,ye,t),i(e,I,t),i(e,xe,t),f(q,e,t),i(e,we,t),i(e,E,t),i(e,Ce,t),f(A,e,t),i(e,be,t),i(e,U,t),i(e,Te,t),i(e,D,t),i(e,Ne,t),f(B,e,t),i(e,_e,t),i(e,V,t),i(e,Le,t),i(e,F,t),i(e,ke,t),f(X,e,t),i(e,Me,t),i(e,j,t),i(e,Pe,t),i(e,O,t),i(e,ze,t),f(K,e,t),i(e,He,t),i(e,G,t),i(e,Re,t),f(W,e,t),i(e,Se,t),i(e,Y,t),i(e,Ie,t),f(Z,e,t),i(e,qe,t),i(e,Q,t),Ee=!0},p:lt,i(e){Ee||(c(v.$$.fragment,e),c($.$$.fragment,e),c(y.$$.fragment,e),c(w.$$.fragment,e),c(_.$$.fragment,e),c(P.$$.fragment,e),c(H.$$.fragment,e),c(S.$$.fragment,e),c(q.$$.fragment,e),c(A.$$.fragment,e),c(B.$$.fragment,e),c(X.$$.fragment,e),c(K.$$.fragment,e),c(W.$$.fragment,e),c(Z.$$.fragment,e),Ee=!0)},o(e){p(v.$$.fragment,e),p($.$$.fragment,e),p(y.$$.fragment,e),p(w.$$.fragment,e),p(_.$$.fragment,e),p(P.$$.fragment,e),p(H.$$.fragment,e),p(S.$$.fragment,e),p(q.$$.fragment,e),p(A.$$.fragment,e),p(B.$$.fragment,e),p(X.$$.fragment,e),p(K.$$.fragment,e),p(W.$$.fragment,e),p(Z.$$.fragment,e),Ee=!1},d(e){e&&(n(ee),n(J),n(te),n(ne),n(ie),n(ae),n(x),n(oe),n(se),n(C),n(re),n(b),n(le),n(T),n(me),n(N),n(he),n(fe),n(L),n(ce),n(k),n(pe),n(M),n(ue),n(ge),n(z),n(de),n(ve),n(R),n($e),n(ye),n(I),n(xe),n(we),n(E),n(Ce),n(be),n(U),n(Te),n(D),n(Ne),n(_e),n(V),n(Le),n(F),n(ke),n(Me),n(j),n(Pe),n(O),n(ze),n(He),n(G),n(Re),n(Se),n(Y),n(Ie),n(qe),n(Q)),n(d),u(v,e),u($,e),u(y,e),u(w,e),u(_,e),u(P,e),u(H,e),u(S,e),u(q,e),u(A,e),u(B,e),u(X,e),u(K,e),u(W,e),u(Z,e)}}}const vt='{"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 $t(Ae){return mt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class bt extends ht{constructor(d){super(),ft(this,d,$t,dt,rt,{})}}export{bt as component};
