import{s as Pe,n as Ye,o as Ie}from"../chunks/scheduler.7bc62968.js";import{S as qe,i as Xe,g as i,s as l,r as m,A as Se,h as r,f as n,c as a,j as ze,u as d,x as o,k as Ee,y as Ae,a as s,v as c,d as u,t as h,w as f}from"../chunks/index.2f8492b0.js";import{C as Be}from"../chunks/CodeBlock.4e96d6f7.js";import{H as I,E as Le}from"../chunks/EditOnGithub.2a9ce03a.js";function Qe(Ce){let p,q,P,X,g,S,y,ve="Neural networks with more layers were assumed to be more effective because adding more layers improves the model performance.",A,b,xe="As the networks became deeper, the extracted features could be further enriched, such as seen with VGG16 and VGG19.",L,w,Me=`A question arose: “Is learning networks as easy as stacking more layers”?
An obstacle to answering this question, the gradient vanishing problem, was addressed by normalized initializations and intermediate normalization layers.`,Q,C,Re="However, a new issue emerged: the degradation problem. As the neural networks became deeper, accuracy saturated and degraded rapidly. An experiment comparing shallow and deep plain networks revealed that deeper models exhibited higher training and test errors, suggesting a fundamental challenge in training deeper architectures effectively. This degradation was not because of overfitting but because the training error increased when the network became deeper. The added layers did not approximate the identity function.",K,v,$e="ResNet’s residual connections unlocked the potential of the extreme depth, propelling the accuracy upwards compared to the previous architectures.",D,x,O,M,ke=`<li>A Residual Block. Source: ResNet Paper
<img src="https://huggingface.co/datasets/hf-vision/course-assets/blob/main/ResnetBlock.png" alt="residual"/></li>`,ee,R,Je="ResNet’s building blocks designed as identity functions, preserve input information while enabling learning. This approach ensures efficient weight optimization and prevents degradation as the network becomes deeper.",te,$,Ue="The building block of ResNet can be shown in the picture, source ResNet paper.",ne,k,Te='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/ResnetBlock.png" alt="resnet_building_block"/>',se,J,_e=`Shortcut connections perform identity mapping and their output is added to the output of the stacked layers. Identity shortcut connections add neither extra parameters nor
computational complexity, these connections bypass layers, creating direct paths for information flow, and they enable neural networks to learn the residual function (F).`,le,U,je="We can summarize ResNet Network ->  Plain Network + Shortcuts!",ae,T,Ne=`For operations  (F(x) + x), (F(x)) and (x) should have identical dimensions.
ResNet employs two techniques to achieve this:`,ie,_,Ze="<li>Zero-padding shortcuts that add channels with zero values, maintaining dimensions without introducing extra parameters to be learned.</li> <li>Projection shortcuts that use 1x1 convolutions to adjust dimensions when necessary, involving some additional learnable parameters.</li>",re,j,Fe="In deeper ResNet architectures like ResNet 50, 101, and 152, a specialized “bottleneck building block” is employed to manage parameter complexity and maintain efficiency while enabling even deeper learning.",oe,N,pe,Z,me,F,Ve="Below you can see how to load pre-trained ResNet with an image classification head using transformers library.",de,V,ce,G,Ge="All pre-trained models expect input images normalized similarly, i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be at least 224. The images have to be loaded into a range of [0, 1] and then normalized using mean = [0.485, 0.456, 0.406] and std = [0.229, 0.224, 0.225].",ue,H,He='Here’s a sample execution. This example is available in the <a href="https://huggingface.co/docs/transformers/v4.18.0/en/model_doc/resnet" rel="nofollow">Hugging Face documentation</a>.',he,W,fe,z,ge,E,We='<li><p><a href="https://pytorch.org/hub/pytorch_vision_resnet/" rel="nofollow">PyTorch docs</a></p></li> <li><p><a href="https://arxiv.org/abs/1512.03385" rel="nofollow">ResNet: Deep Residual Learning for Image Recognition</a></p></li> <li><p><a href="https://arxiv.org/abs/1512.03385" rel="nofollow">Resnet Architecture Source: ResNet Paper</a></p></li> <li><p><a href="https://huggingface.co/docs/transformers/en/model_doc/resnet" rel="nofollow">Hugging Face Documentation on ResNet</a></p></li>',ye,B,be,Y,we;return g=new I({props:{title:"ResNet (Residual Network)",local:"resnet-residual-network",headingTag:"h1"}}),x=new I({props:{title:"ResNet Architecture",local:"resnet-architecture",headingTag:"h2"}}),N=new I({props:{title:"ResNet Code",local:"resnet-code",headingTag:"h2"}}),Z=new I({props:{title:"Deep Residual Networks Pre-trained on ImageNet",local:"deep-residual-networks-pre-trained-on-imagenet",headingTag:"h3"}}),V=new Be({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFJlc05ldEZvckltYWdlQ2xhc3NpZmljYXRpb24lMEElMEFtb2RlbCUyMCUzRCUyMFJlc05ldEZvckltYWdlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUyMm1pY3Jvc29mdCUyRnJlc25ldC01MCUyMiklMEElMEFtb2RlbC5ldmFsKCk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ResNetForImageClassification

model = ResNetForImageClassification.from_pretrained(<span class="hljs-string">&quot;microsoft/resnet-50&quot;</span>)

model.<span class="hljs-built_in">eval</span>()`,wrap:!1}}),W=new Be({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9GZWF0dXJlRXh0cmFjdG9yJTJDJTIwUmVzTmV0Rm9ySW1hZ2VDbGFzc2lmaWNhdGlvbiUwQWltcG9ydCUyMHRvcmNoJTBBZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBJTBBZGF0YXNldCUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJodWdnaW5nZmFjZSUyRmNhdHMtaW1hZ2UlMjIpJTBBaW1hZ2UlMjAlM0QlMjBkYXRhc2V0JTVCJTIydGVzdCUyMiU1RCU1QiUyMmltYWdlJTIyJTVEJTVCMCU1RCUwQSUwQWZlYXR1cmVfZXh0cmFjdG9yJTIwJTNEJTIwQXV0b0ZlYXR1cmVFeHRyYWN0b3IuZnJvbV9wcmV0cmFpbmVkKCUyMm1pY3Jvc29mdCUyRnJlc25ldC01MCUyMiklMEFtb2RlbCUyMCUzRCUyMFJlc05ldEZvckltYWdlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKCUyMm1pY3Jvc29mdCUyRnJlc25ldC01MCUyMiklMEElMEFpbnB1dHMlMjAlM0QlMjBmZWF0dXJlX2V4dHJhY3RvcihpbWFnZSUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIycHQlMjIpJTBBJTBBd2l0aCUyMHRvcmNoLm5vX2dyYWQoKSUzQSUwQSUyMCUyMCUyMCUyMGxvZ2l0cyUyMCUzRCUyMG1vZGVsKCoqaW5wdXRzKS5sb2dpdHMlMEElMEElMjMlMjBtb2RlbCUyMHByZWRpY3RzJTIwb25lJTIwb2YlMjB0aGUlMjAxMDAwJTIwSW1hZ2VOZXQlMjBjbGFzc2VzJTBBcHJlZGljdGVkX2xhYmVsJTIwJTNEJTIwbG9naXRzLmFyZ21heCgtMSkuaXRlbSgpJTBBcHJpbnQobW9kZWwuY29uZmlnLmlkMmxhYmVsJTVCcHJlZGljdGVkX2xhYmVsJTVEKQ==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoFeatureExtractor, ResNetForImageClassification
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

dataset = load_dataset(<span class="hljs-string">&quot;huggingface/cats-image&quot;</span>)
image = dataset[<span class="hljs-string">&quot;test&quot;</span>][<span class="hljs-string">&quot;image&quot;</span>][<span class="hljs-number">0</span>]

feature_extractor = AutoFeatureExtractor.from_pretrained(<span class="hljs-string">&quot;microsoft/resnet-50&quot;</span>)
model = ResNetForImageClassification.from_pretrained(<span class="hljs-string">&quot;microsoft/resnet-50&quot;</span>)

inputs = feature_extractor(image, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)

<span class="hljs-keyword">with</span> torch.no_grad():
    logits = model(**inputs).logits

<span class="hljs-comment"># model predicts one of the 1000 ImageNet classes</span>
predicted_label = logits.argmax(-<span class="hljs-number">1</span>).item()
<span class="hljs-built_in">print</span>(model.config.id2label[predicted_label])`,wrap:!1}}),z=new I({props:{title:"References",local:"references",headingTag:"h2"}}),B=new Le({props:{source:"https://github.com/johko/computer-vision-course/blob/main/chapters/en/unit2/cnns/resnet.mdx"}}),{c(){p=i("meta"),q=l(),P=i("p"),X=l(),m(g.$$.fragment),S=l(),y=i("p"),y.textContent=ve,A=l(),b=i("p"),b.textContent=xe,L=l(),w=i("p"),w.textContent=Me,Q=l(),C=i("p"),C.textContent=Re,K=l(),v=i("p"),v.textContent=$e,D=l(),m(x.$$.fragment),O=l(),M=i("ul"),M.innerHTML=ke,ee=l(),R=i("p"),R.textContent=Je,te=l(),$=i("p"),$.textContent=Ue,ne=l(),k=i("p"),k.innerHTML=Te,se=l(),J=i("p"),J.textContent=_e,le=l(),U=i("p"),U.textContent=je,ae=l(),T=i("p"),T.textContent=Ne,ie=l(),_=i("ul"),_.innerHTML=Ze,re=l(),j=i("p"),j.textContent=Fe,oe=l(),m(N.$$.fragment),pe=l(),m(Z.$$.fragment),me=l(),F=i("p"),F.textContent=Ve,de=l(),m(V.$$.fragment),ce=l(),G=i("p"),G.textContent=Ge,ue=l(),H=i("p"),H.innerHTML=He,he=l(),m(W.$$.fragment),fe=l(),m(z.$$.fragment),ge=l(),E=i("ul"),E.innerHTML=We,ye=l(),m(B.$$.fragment),be=l(),Y=i("p"),this.h()},l(e){const t=Se("svelte-u9bgzb",document.head);p=r(t,"META",{name:!0,content:!0}),t.forEach(n),q=a(e),P=r(e,"P",{}),ze(P).forEach(n),X=a(e),d(g.$$.fragment,e),S=a(e),y=r(e,"P",{"data-svelte-h":!0}),o(y)!=="svelte-v6nqw1"&&(y.textContent=ve),A=a(e),b=r(e,"P",{"data-svelte-h":!0}),o(b)!=="svelte-tay29i"&&(b.textContent=xe),L=a(e),w=r(e,"P",{"data-svelte-h":!0}),o(w)!=="svelte-f2z8gv"&&(w.textContent=Me),Q=a(e),C=r(e,"P",{"data-svelte-h":!0}),o(C)!=="svelte-1xf9wlr"&&(C.textContent=Re),K=a(e),v=r(e,"P",{"data-svelte-h":!0}),o(v)!=="svelte-jv7kti"&&(v.textContent=$e),D=a(e),d(x.$$.fragment,e),O=a(e),M=r(e,"UL",{"data-svelte-h":!0}),o(M)!=="svelte-w6gay8"&&(M.innerHTML=ke),ee=a(e),R=r(e,"P",{"data-svelte-h":!0}),o(R)!=="svelte-bkjwk3"&&(R.textContent=Je),te=a(e),$=r(e,"P",{"data-svelte-h":!0}),o($)!=="svelte-12g5dwc"&&($.textContent=Ue),ne=a(e),k=r(e,"P",{"data-svelte-h":!0}),o(k)!=="svelte-stgboa"&&(k.innerHTML=Te),se=a(e),J=r(e,"P",{"data-svelte-h":!0}),o(J)!=="svelte-tsll05"&&(J.textContent=_e),le=a(e),U=r(e,"P",{"data-svelte-h":!0}),o(U)!=="svelte-12k99oq"&&(U.textContent=je),ae=a(e),T=r(e,"P",{"data-svelte-h":!0}),o(T)!=="svelte-v51ypa"&&(T.textContent=Ne),ie=a(e),_=r(e,"UL",{"data-svelte-h":!0}),o(_)!=="svelte-1rnct1s"&&(_.innerHTML=Ze),re=a(e),j=r(e,"P",{"data-svelte-h":!0}),o(j)!=="svelte-1r7ha0d"&&(j.textContent=Fe),oe=a(e),d(N.$$.fragment,e),pe=a(e),d(Z.$$.fragment,e),me=a(e),F=r(e,"P",{"data-svelte-h":!0}),o(F)!=="svelte-xgcjij"&&(F.textContent=Ve),de=a(e),d(V.$$.fragment,e),ce=a(e),G=r(e,"P",{"data-svelte-h":!0}),o(G)!=="svelte-kh8rtv"&&(G.textContent=Ge),ue=a(e),H=r(e,"P",{"data-svelte-h":!0}),o(H)!=="svelte-q6mi08"&&(H.innerHTML=He),he=a(e),d(W.$$.fragment,e),fe=a(e),d(z.$$.fragment,e),ge=a(e),E=r(e,"UL",{"data-svelte-h":!0}),o(E)!=="svelte-s3zcq2"&&(E.innerHTML=We),ye=a(e),d(B.$$.fragment,e),be=a(e),Y=r(e,"P",{}),ze(Y).forEach(n),this.h()},h(){Ee(p,"name","hf:doc:metadata"),Ee(p,"content",Ke)},m(e,t){Ae(document.head,p),s(e,q,t),s(e,P,t),s(e,X,t),c(g,e,t),s(e,S,t),s(e,y,t),s(e,A,t),s(e,b,t),s(e,L,t),s(e,w,t),s(e,Q,t),s(e,C,t),s(e,K,t),s(e,v,t),s(e,D,t),c(x,e,t),s(e,O,t),s(e,M,t),s(e,ee,t),s(e,R,t),s(e,te,t),s(e,$,t),s(e,ne,t),s(e,k,t),s(e,se,t),s(e,J,t),s(e,le,t),s(e,U,t),s(e,ae,t),s(e,T,t),s(e,ie,t),s(e,_,t),s(e,re,t),s(e,j,t),s(e,oe,t),c(N,e,t),s(e,pe,t),c(Z,e,t),s(e,me,t),s(e,F,t),s(e,de,t),c(V,e,t),s(e,ce,t),s(e,G,t),s(e,ue,t),s(e,H,t),s(e,he,t),c(W,e,t),s(e,fe,t),c(z,e,t),s(e,ge,t),s(e,E,t),s(e,ye,t),c(B,e,t),s(e,be,t),s(e,Y,t),we=!0},p:Ye,i(e){we||(u(g.$$.fragment,e),u(x.$$.fragment,e),u(N.$$.fragment,e),u(Z.$$.fragment,e),u(V.$$.fragment,e),u(W.$$.fragment,e),u(z.$$.fragment,e),u(B.$$.fragment,e),we=!0)},o(e){h(g.$$.fragment,e),h(x.$$.fragment,e),h(N.$$.fragment,e),h(Z.$$.fragment,e),h(V.$$.fragment,e),h(W.$$.fragment,e),h(z.$$.fragment,e),h(B.$$.fragment,e),we=!1},d(e){e&&(n(q),n(P),n(X),n(S),n(y),n(A),n(b),n(L),n(w),n(Q),n(C),n(K),n(v),n(D),n(O),n(M),n(ee),n(R),n(te),n($),n(ne),n(k),n(se),n(J),n(le),n(U),n(ae),n(T),n(ie),n(_),n(re),n(j),n(oe),n(pe),n(me),n(F),n(de),n(ce),n(G),n(ue),n(H),n(he),n(fe),n(ge),n(E),n(ye),n(be),n(Y)),n(p),f(g,e),f(x,e),f(N,e),f(Z,e),f(V,e),f(W,e),f(z,e),f(B,e)}}}const Ke='{"title":"ResNet (Residual Network)","local":"resnet-residual-network","sections":[{"title":"ResNet Architecture","local":"resnet-architecture","sections":[],"depth":2},{"title":"ResNet Code","local":"resnet-code","sections":[{"title":"Deep Residual Networks Pre-trained on ImageNet","local":"deep-residual-networks-pre-trained-on-imagenet","sections":[],"depth":3}],"depth":2},{"title":"References","local":"references","sections":[],"depth":2}],"depth":1}';function De(Ce){return Ie(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class st extends qe{constructor(p){super(),Xe(this,p,De,Qe,Pe,{})}}export{st as component};
