import{S as hl,i as ul,s as dl,e as n,k as f,w as v,t as o,M as gl,c as i,d as s,m as c,a as p,x as b,h as l,b as u,N as _l,G as a,g as r,y,q as w,o as j,B as k,v as $l,L as vl}from"../../chunks/vendor-hf-doc-builder.js";import{T as co}from"../../chunks/Tip-hf-doc-builder.js";import{Y as bl}from"../../chunks/Youtube-hf-doc-builder.js";import{I as Ks}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as z}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as yl}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";import{F as fl,M as cl}from"../../chunks/Markdown-hf-doc-builder.js";function wl(H){let m,E,h,_,T;return{c(){m=n("p"),E=o("See the image classification "),h=n("a"),_=o("task page"),T=o(" for more information about its associated models, datasets, and metrics."),this.h()},l(x){m=i(x,"P",{});var A=p(m);E=l(A,"See the image classification "),h=i(A,"A",{href:!0,rel:!0});var P=p(h);_=l(P,"task page"),P.forEach(s),T=l(A," for more information about its associated models, datasets, and metrics."),A.forEach(s),this.h()},h(){u(h,"href","https://huggingface.co/tasks/image-classification"),u(h,"rel","nofollow")},m(x,A){r(x,m,A),a(m,E),a(m,h),a(h,_),a(m,T)},d(x){x&&s(m)}}}function jl(H){let m,E,h,_,T,x,A,P;return{c(){m=n("p"),E=o("If you aren\u2019t familiar with finetuning a model with the "),h=n("a"),_=o("Trainer"),T=o(", take a look at the basic tutorial "),x=n("a"),A=o("here"),P=o("!"),this.h()},l($){m=i($,"P",{});var F=p(m);E=l(F,"If you aren\u2019t familiar with finetuning a model with the "),h=i(F,"A",{href:!0});var D=p(h);_=l(D,"Trainer"),D.forEach(s),T=l(F,", take a look at the basic tutorial "),x=i(F,"A",{href:!0});var S=p(x);A=l(S,"here"),S.forEach(s),P=l(F,"!"),F.forEach(s),this.h()},h(){u(h,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer"),u(x,"href","../training#train-with-pytorch-trainer")},m($,F){r($,m,F),a(m,E),a(m,h),a(h,_),a(m,T),a(m,x),a(x,A),a(m,P)},d($){$&&s(m)}}}function kl(H){let m,E,h,_,T,x,A,P,$,F,D,S,Y,L,W,X,B,U,oe,N,M,g,q,V,ue,J,K,ss,ye,as,we,de,Ie,le,De,R,ts,pe,me,ze,re,os,Fe,fe,ne,Z,G,je,ce,Se,ee,Le,se,ge,_e,Ne,he,ke,O,Oe;return m=new co({props:{$$slots:{default:[jl]},$$scope:{ctx:H}}}),h=new z({props:{code:`from transformers import AutoModelForImageClassification, TrainingArguments, Trainer

model = AutoModelForImageClassification.from_pretrained(
    "google/vit-base-patch16-224-in21k",
    num_labels=len(labels),
    id2label=id2label,
    label2id=label2id,
)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForImageClassification, TrainingArguments, Trainer

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForImageClassification.from_pretrained(
<span class="hljs-meta">... </span>    <span class="hljs-string">&quot;google/vit-base-patch16-224-in21k&quot;</span>,
<span class="hljs-meta">... </span>    num_labels=<span class="hljs-built_in">len</span>(labels),
<span class="hljs-meta">... </span>    id2label=id2label,
<span class="hljs-meta">... </span>    label2id=label2id,
<span class="hljs-meta">... </span>)`}}),ee=new z({props:{code:`training_args = TrainingArguments(
    output_dir="my_awesome_food_model",
    remove_unused_columns=False,
    evaluation_strategy="epoch",
    save_strategy="epoch",
    learning_rate=5e-5,
    per_device_train_batch_size=16,
    gradient_accumulation_steps=4,
    per_device_eval_batch_size=16,
    num_train_epochs=3,
    warmup_ratio=0.1,
    logging_steps=10,
    load_best_model_at_end=True,
    metric_for_best_model="accuracy",
    push_to_hub=True,
)

trainer = Trainer(
    model=model,
    args=training_args,
    data_collator=data_collator,
    train_dataset=food["train"],
    eval_dataset=food["test"],
    tokenizer=image_processor,
    compute_metrics=compute_metrics,
)

trainer.train()`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>training_args = TrainingArguments(
<span class="hljs-meta">... </span>    output_dir=<span class="hljs-string">&quot;my_awesome_food_model&quot;</span>,
<span class="hljs-meta">... </span>    remove_unused_columns=<span class="hljs-literal">False</span>,
<span class="hljs-meta">... </span>    evaluation_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
<span class="hljs-meta">... </span>    save_strategy=<span class="hljs-string">&quot;epoch&quot;</span>,
<span class="hljs-meta">... </span>    learning_rate=<span class="hljs-number">5e-5</span>,
<span class="hljs-meta">... </span>    per_device_train_batch_size=<span class="hljs-number">16</span>,
<span class="hljs-meta">... </span>    gradient_accumulation_steps=<span class="hljs-number">4</span>,
<span class="hljs-meta">... </span>    per_device_eval_batch_size=<span class="hljs-number">16</span>,
<span class="hljs-meta">... </span>    num_train_epochs=<span class="hljs-number">3</span>,
<span class="hljs-meta">... </span>    warmup_ratio=<span class="hljs-number">0.1</span>,
<span class="hljs-meta">... </span>    logging_steps=<span class="hljs-number">10</span>,
<span class="hljs-meta">... </span>    load_best_model_at_end=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>    metric_for_best_model=<span class="hljs-string">&quot;accuracy&quot;</span>,
<span class="hljs-meta">... </span>    push_to_hub=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>trainer = Trainer(
<span class="hljs-meta">... </span>    model=model,
<span class="hljs-meta">... </span>    args=training_args,
<span class="hljs-meta">... </span>    data_collator=data_collator,
<span class="hljs-meta">... </span>    train_dataset=food[<span class="hljs-string">&quot;train&quot;</span>],
<span class="hljs-meta">... </span>    eval_dataset=food[<span class="hljs-string">&quot;test&quot;</span>],
<span class="hljs-meta">... </span>    tokenizer=image_processor,
<span class="hljs-meta">... </span>    compute_metrics=compute_metrics,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.train()`}}),O=new z({props:{code:"trainer.push_to_hub()",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.push_to_hub()'}}),{c(){v(m.$$.fragment),E=o(`
You're ready to start training your model now! Load ViT with [AutoModelForImageClassification](/docs/transformers/v4.25.1/en/model_doc/auto#transformers.AutoModelForImageClassification). Specify the number of labels along with the number of expected labels, and the label mappings:

	`),v(h.$$.fragment),_=f(),T=n("p"),x=o("At this point, only three steps remain:"),A=f(),P=n("ol"),$=n("li"),F=o("Define your training hyperparameters in "),D=n("a"),S=o("TrainingArguments"),Y=o(". It is important you don\u2019t remove unused columns because this\u2019ll drop the "),L=n("code"),W=o("image"),X=o(" column. Without the "),B=n("code"),U=o("image"),oe=o(" column, you can\u2019t create "),N=n("code"),M=o("pixel_values"),g=o(". Set "),q=n("code"),V=o("remove_unused_columns=False"),ue=o(" to prevent this behavior! The only other required parameter is "),J=n("code"),K=o("output_dir"),ss=o(" which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),ye=n("code"),as=o("push_to_hub=True"),we=o(" (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the "),de=n("a"),Ie=o("Trainer"),le=o(" will evaluate the accuracy and save the training checkpoint."),De=f(),R=n("li"),ts=o("Pass the training arguments to "),pe=n("a"),me=o("Trainer"),ze=o(" along with the model, dataset, tokenizer, data collator, and "),re=n("code"),os=o("compute_metrics"),Fe=o(" function."),fe=f(),ne=n("li"),Z=o("Call "),G=n("a"),je=o("train()"),ce=o(" to finetune your model."),Se=f(),v(ee.$$.fragment),Le=f(),se=n("p"),ge=o("Once training is completed, share your model to the Hub with the "),_e=n("a"),Ne=o("push_to_hub()"),he=o(" method so everyone can use your model:"),ke=f(),v(O.$$.fragment),this.h()},l(d){b(m.$$.fragment,d),E=l(d,`
You're ready to start training your model now! Load ViT with [AutoModelForImageClassification](/docs/transformers/v4.25.1/en/model_doc/auto#transformers.AutoModelForImageClassification). Specify the number of labels along with the number of expected labels, and the label mappings:

	`),b(h.$$.fragment,d),_=c(d),T=i(d,"P",{});var I=p(T);x=l(I,"At this point, only three steps remain:"),I.forEach(s),A=c(d),P=i(d,"OL",{});var ae=p(P);$=i(ae,"LI",{});var C=p($);F=l(C,"Define your training hyperparameters in "),D=i(C,"A",{href:!0});var Ds=p(D);S=l(Ds,"TrainingArguments"),Ds.forEach(s),Y=l(C,". It is important you don\u2019t remove unused columns because this\u2019ll drop the "),L=i(C,"CODE",{});var zs=p(L);W=l(zs,"image"),zs.forEach(s),X=l(C," column. Without the "),B=i(C,"CODE",{});var ls=p(B);U=l(ls,"image"),ls.forEach(s),oe=l(C," column, you can\u2019t create "),N=i(C,"CODE",{});var $e=p(N);M=l($e,"pixel_values"),$e.forEach(s),g=l(C,". Set "),q=i(C,"CODE",{});var rs=p(q);V=l(rs,"remove_unused_columns=False"),rs.forEach(s),ue=l(C," to prevent this behavior! The only other required parameter is "),J=i(C,"CODE",{});var Ee=p(J);K=l(Ee,"output_dir"),Ee.forEach(s),ss=l(C," which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),ye=i(C,"CODE",{});var Fs=p(ye);as=l(Fs,"push_to_hub=True"),Fs.forEach(s),we=l(C," (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the "),de=i(C,"A",{href:!0});var ns=p(de);Ie=l(ns,"Trainer"),ns.forEach(s),le=l(C," will evaluate the accuracy and save the training checkpoint."),C.forEach(s),De=c(ae),R=i(ae,"LI",{});var Q=p(R);ts=l(Q,"Pass the training arguments to "),pe=i(Q,"A",{href:!0});var is=p(pe);me=l(is,"Trainer"),is.forEach(s),ze=l(Q," along with the model, dataset, tokenizer, data collator, and "),re=i(Q,"CODE",{});var xe=p(re);os=l(xe,"compute_metrics"),xe.forEach(s),Fe=l(Q," function."),Q.forEach(s),fe=c(ae),ne=i(ae,"LI",{});var Me=p(ne);Z=l(Me,"Call "),G=i(Me,"A",{href:!0});var ps=p(G);je=l(ps,"train()"),ps.forEach(s),ce=l(Me," to finetune your model."),Me.forEach(s),ae.forEach(s),Se=c(d),b(ee.$$.fragment,d),Le=c(d),se=i(d,"P",{});var te=p(se);ge=l(te,"Once training is completed, share your model to the Hub with the "),_e=i(te,"A",{href:!0});var Te=p(_e);Ne=l(Te,"push_to_hub()"),Te.forEach(s),he=l(te," method so everyone can use your model:"),te.forEach(s),ke=c(d),b(O.$$.fragment,d),this.h()},h(){u(D,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.TrainingArguments"),u(de,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer"),u(pe,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer"),u(G,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer.train"),u(_e,"href","/docs/transformers/v4.25.1/en/main_classes/trainer#transformers.Trainer.push_to_hub")},m(d,I){y(m,d,I),r(d,E,I),y(h,d,I),r(d,_,I),r(d,T,I),a(T,x),r(d,A,I),r(d,P,I),a(P,$),a($,F),a($,D),a(D,S),a($,Y),a($,L),a(L,W),a($,X),a($,B),a(B,U),a($,oe),a($,N),a(N,M),a($,g),a($,q),a(q,V),a($,ue),a($,J),a(J,K),a($,ss),a($,ye),a(ye,as),a($,we),a($,de),a(de,Ie),a($,le),a(P,De),a(P,R),a(R,ts),a(R,pe),a(pe,me),a(R,ze),a(R,re),a(re,os),a(R,Fe),a(P,fe),a(P,ne),a(ne,Z),a(ne,G),a(G,je),a(ne,ce),r(d,Se,I),y(ee,d,I),r(d,Le,I),r(d,se,I),a(se,ge),a(se,_e),a(_e,Ne),a(se,he),r(d,ke,I),y(O,d,I),Oe=!0},p(d,I){const ae={};I&2&&(ae.$$scope={dirty:I,ctx:d}),m.$set(ae)},i(d){Oe||(w(m.$$.fragment,d),w(h.$$.fragment,d),w(ee.$$.fragment,d),w(O.$$.fragment,d),Oe=!0)},o(d){j(m.$$.fragment,d),j(h.$$.fragment,d),j(ee.$$.fragment,d),j(O.$$.fragment,d),Oe=!1},d(d){k(m,d),d&&s(E),k(h,d),d&&s(_),d&&s(T),d&&s(A),d&&s(P),d&&s(Se),k(ee,d),d&&s(Le),d&&s(se),d&&s(ke),k(O,d)}}}function El(H){let m,E;return m=new cl({props:{$$slots:{default:[kl]},$$scope:{ctx:H}}}),{c(){v(m.$$.fragment)},l(h){b(m.$$.fragment,h)},m(h,_){y(m,h,_),E=!0},p(h,_){const T={};_&2&&(T.$$scope={dirty:_,ctx:h}),m.$set(T)},i(h){E||(w(m.$$.fragment,h),E=!0)},o(h){j(m.$$.fragment,h),E=!1},d(h){k(m,h)}}}function xl(H){let m,E,h,_,T;return{c(){m=n("p"),E=o("For a more in-depth example of how to finetune a model for image classification, take a look at the corresponding "),h=n("a"),_=o("PyTorch notebook"),T=o("."),this.h()},l(x){m=i(x,"P",{});var A=p(m);E=l(A,"For a more in-depth example of how to finetune a model for image classification, take a look at the corresponding "),h=i(A,"A",{href:!0,rel:!0});var P=p(h);_=l(P,"PyTorch notebook"),P.forEach(s),T=l(A,"."),A.forEach(s),this.h()},h(){u(h,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb"),u(h,"rel","nofollow")},m(x,A){r(x,m,A),a(m,E),a(m,h),a(h,_),a(m,T)},d(x){x&&s(m)}}}function Tl(H){let m,E,h,_,T,x,A,P,$,F,D,S,Y,L,W,X,B,U,oe,N,M;return A=new z({props:{code:`from transformers import AutoImageProcessor
import torch

image_processor = AutoImageProcessor.from_pretrained("my_awesome_food_model")
inputs = image_processor(image, return_tensors="pt")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoImageProcessor
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch

<span class="hljs-meta">&gt;&gt;&gt; </span>image_processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">&quot;my_awesome_food_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = image_processor(image, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)`}}),S=new z({props:{code:`from transformers import AutoModelForImageClassification

model = AutoModelForImageClassification.from_pretrained("my_awesome_food_model")
with torch.no_grad():
    logits = model(**inputs).logits`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForImageClassification

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForImageClassification.from_pretrained(<span class="hljs-string">&quot;my_awesome_food_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">with</span> torch.no_grad():
<span class="hljs-meta">... </span>    logits = model(**inputs).logits`}}),N=new z({props:{code:`predicted_label = logits.argmax(-1).item()
model.config.id2label[predicted_label]`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>predicted_label = logits.argmax(-<span class="hljs-number">1</span>).item()
<span class="hljs-meta">&gt;&gt;&gt; </span>model.config.id2label[predicted_label]
<span class="hljs-string">&#x27;beignets&#x27;</span>`}}),{c(){m=n("p"),E=o("Load an image processor to preprocess the image and return the "),h=n("code"),_=o("input"),T=o(" as PyTorch tensors:"),x=f(),v(A.$$.fragment),P=f(),$=n("p"),F=o("Pass your inputs to the model and return the logits:"),D=f(),v(S.$$.fragment),Y=f(),L=n("p"),W=o("Get the predicted label with the highest probability, and use the model\u2019s "),X=n("code"),B=o("id2label"),U=o(" mapping to convert it to a label:"),oe=f(),v(N.$$.fragment)},l(g){m=i(g,"P",{});var q=p(m);E=l(q,"Load an image processor to preprocess the image and return the "),h=i(q,"CODE",{});var V=p(h);_=l(V,"input"),V.forEach(s),T=l(q," as PyTorch tensors:"),q.forEach(s),x=c(g),b(A.$$.fragment,g),P=c(g),$=i(g,"P",{});var ue=p($);F=l(ue,"Pass your inputs to the model and return the logits:"),ue.forEach(s),D=c(g),b(S.$$.fragment,g),Y=c(g),L=i(g,"P",{});var J=p(L);W=l(J,"Get the predicted label with the highest probability, and use the model\u2019s "),X=i(J,"CODE",{});var K=p(X);B=l(K,"id2label"),K.forEach(s),U=l(J," mapping to convert it to a label:"),J.forEach(s),oe=c(g),b(N.$$.fragment,g)},m(g,q){r(g,m,q),a(m,E),a(m,h),a(h,_),a(m,T),r(g,x,q),y(A,g,q),r(g,P,q),r(g,$,q),a($,F),r(g,D,q),y(S,g,q),r(g,Y,q),r(g,L,q),a(L,W),a(L,X),a(X,B),a(L,U),r(g,oe,q),y(N,g,q),M=!0},p:vl,i(g){M||(w(A.$$.fragment,g),w(S.$$.fragment,g),w(N.$$.fragment,g),M=!0)},o(g){j(A.$$.fragment,g),j(S.$$.fragment,g),j(N.$$.fragment,g),M=!1},d(g){g&&s(m),g&&s(x),k(A,g),g&&s(P),g&&s($),g&&s(D),k(S,g),g&&s(Y),g&&s(L),g&&s(oe),k(N,g)}}}function Al(H){let m,E;return m=new cl({props:{$$slots:{default:[Tl]},$$scope:{ctx:H}}}),{c(){v(m.$$.fragment)},l(h){b(m.$$.fragment,h)},m(h,_){y(m,h,_),E=!0},p(h,_){const T={};_&2&&(T.$$scope={dirty:_,ctx:h}),m.$set(T)},i(h){E||(w(m.$$.fragment,h),E=!0)},o(h){j(m.$$.fragment,h),E=!1},d(h){k(m,h)}}}function Pl(H){let m,E,h,_,T,x,A,P,$,F,D,S,Y,L,W,X,B,U,oe,N,M,g,q,V,ue,J,K,ss,ye,as,we,de,Ie,le,De,R,ts,pe,me,ze,re,os,Fe,fe,ne,Z,G,je,ce,Se,ee,Le,se,ge,_e,Ne,he,ke,O,Oe,d,I,ae,C,Ds,zs,ls,$e,rs,Ee,Fs,ns,Q,is,xe,Me,ps,te,Te,Qs,mt,ft,ct,Ss,Xs,ht,ut,ga,Ls,dt,_a,ms,$a,Ns,gt,va,fs,ba,Ae,He,Zs,cs,_t,ea,$t,ya,Os,vt,wa,hs,ja,Ye,bt,us,sa,yt,wt,ka,Ms,jt,Ea,ds,xa,Re,kt,aa,Et,xt,Ta,gs,Aa,Ge,Tt,_s,At,Pt,Pa,$s,qa,ve,qt,Hs,Ct,It,ta,Dt,zt,Ca,vs,Ia,Pe,We,oa,bs,Ft,la,St,Da,ie,Lt,ys,Nt,Ot,ws,Mt,Ht,js,Yt,Rt,za,ks,Fa,Be,Gt,Es,Wt,Bt,Sa,xs,La,Ue,Ut,ra,Vt,Jt,Na,qe,Ve,na,Ts,Kt,ia,Qt,Oa,Je,Ma,Ke,Ha,Ce,Qe,pa,As,Xt,ma,Zt,Ya,Ys,eo,Ra,Rs,so,Ga,Ps,Wa,qs,Gs,ho,Ba,be,ao,Ws,to,oo,fa,lo,ro,Ua,Cs,Va,Xe,no,ca,io,po,Ja,Ze,Ka;return x=new Ks({}),D=new yl({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/image_classification.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/image_classification.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/image_classification.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/image_classification.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/image_classification.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/image_classification.ipynb"}]}}),Y=new bl({props:{id:"tjAIM7BOYhw"}}),le=new co({props:{$$slots:{default:[wl]},$$scope:{ctx:H}}}),me=new z({props:{code:"pip install transformers datasets evaluate",highlighted:"pip install transformers datasets evaluate"}}),fe=new z({props:{code:`from huggingface_hub import notebook_login

notebook_login()`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

<span class="hljs-meta">&gt;&gt;&gt; </span>notebook_login()`}}),ce=new Ks({}),he=new z({props:{code:`from datasets import load_dataset

food = load_dataset("food101", split="train[:5000]")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

<span class="hljs-meta">&gt;&gt;&gt; </span>food = load_dataset(<span class="hljs-string">&quot;food101&quot;</span>, split=<span class="hljs-string">&quot;train[:5000]&quot;</span>)`}}),$e=new z({props:{code:"food = food.train_test_split(test_size=0.2)",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>food = food.train_test_split(test_size=<span class="hljs-number">0.2</span>)'}}),Q=new z({props:{code:'food["train"][0]',highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>food[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>]
{<span class="hljs-string">&#x27;image&#x27;</span>: &lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=512x512 at <span class="hljs-number">0x7F52AFC8AC50</span>&gt;,
 <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-number">79</span>}`}}),ms=new z({props:{code:`labels = food["train"].features["label"].names
label2id, id2label = dict(), dict()
for i, label in enumerate(labels):
    label2id[label] = str(i)
    id2label[str(i)] = label`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>labels = food[<span class="hljs-string">&quot;train&quot;</span>].features[<span class="hljs-string">&quot;label&quot;</span>].names
<span class="hljs-meta">&gt;&gt;&gt; </span>label2id, id2label = <span class="hljs-built_in">dict</span>(), <span class="hljs-built_in">dict</span>()
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(labels):
<span class="hljs-meta">... </span>    label2id[label] = <span class="hljs-built_in">str</span>(i)
<span class="hljs-meta">... </span>    id2label[<span class="hljs-built_in">str</span>(i)] = label`}}),fs=new z({props:{code:"id2label[str(79)]",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>id2label[<span class="hljs-built_in">str</span>(<span class="hljs-number">79</span>)]
<span class="hljs-string">&#x27;prime_rib&#x27;</span>`}}),cs=new Ks({}),hs=new z({props:{code:`from transformers import AutoImageProcessor

image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoImageProcessor

<span class="hljs-meta">&gt;&gt;&gt; </span>image_processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">&quot;google/vit-base-patch16-224-in21k&quot;</span>)`}}),ds=new z({props:{code:`from torchvision.transforms import RandomResizedCrop, Compose, Normalize, ToTensor

normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std)
size = (
    image_processor.size["shortest_edge"]
    if "shortest_edge" in image_processor.size
    else (image_processor.size["height"], image_processor.size["width"])
)
_transforms = Compose([RandomResizedCrop(size), ToTensor(), normalize])`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> torchvision.transforms <span class="hljs-keyword">import</span> RandomResizedCrop, Compose, Normalize, ToTensor

<span class="hljs-meta">&gt;&gt;&gt; </span>normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std)
<span class="hljs-meta">&gt;&gt;&gt; </span>size = (
<span class="hljs-meta">... </span>    image_processor.size[<span class="hljs-string">&quot;shortest_edge&quot;</span>]
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> <span class="hljs-string">&quot;shortest_edge&quot;</span> <span class="hljs-keyword">in</span> image_processor.size
<span class="hljs-meta">... </span>    <span class="hljs-keyword">else</span> (image_processor.size[<span class="hljs-string">&quot;height&quot;</span>], image_processor.size[<span class="hljs-string">&quot;width&quot;</span>])
<span class="hljs-meta">... </span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>_transforms = Compose([RandomResizedCrop(size), ToTensor(), normalize])`}}),gs=new z({props:{code:`def transforms(examples):
    examples["pixel_values"] = [_transforms(img.convert("RGB")) for img in examples["image"]]
    del examples["image"]
    return examples`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">transforms</span>(<span class="hljs-params">examples</span>):
<span class="hljs-meta">... </span>    examples[<span class="hljs-string">&quot;pixel_values&quot;</span>] = [_transforms(img.convert(<span class="hljs-string">&quot;RGB&quot;</span>)) <span class="hljs-keyword">for</span> img <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;image&quot;</span>]]
<span class="hljs-meta">... </span>    <span class="hljs-keyword">del</span> examples[<span class="hljs-string">&quot;image&quot;</span>]
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> examples`}}),$s=new z({props:{code:"food = food.with_transform(transforms)",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>food = food.with_transform(transforms)'}}),vs=new z({props:{code:`from transformers import DefaultDataCollator

data_collator = DefaultDataCollator()`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DefaultDataCollator

<span class="hljs-meta">&gt;&gt;&gt; </span>data_collator = DefaultDataCollator()`}}),bs=new Ks({}),ks=new z({props:{code:`import evaluate

accuracy = evaluate.load("accuracy")`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> evaluate

<span class="hljs-meta">&gt;&gt;&gt; </span>accuracy = evaluate.load(<span class="hljs-string">&quot;accuracy&quot;</span>)`}}),xs=new z({props:{code:`import numpy as np


def compute_metrics(eval_pred):
    predictions = np.argmax(eval_pred.predictions, axis=1)
    return accuracy.compute(predictions=predictions, references=eval_pred.label_ids)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_metrics</span>(<span class="hljs-params">eval_pred</span>):
<span class="hljs-meta">... </span>    predictions = np.argmax(eval_pred.predictions, axis=<span class="hljs-number">1</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> accuracy.compute(predictions=predictions, references=eval_pred.label_ids)`}}),Ts=new Ks({}),Je=new fl({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[El]},$$scope:{ctx:H}}}),Ke=new co({props:{$$slots:{default:[xl]},$$scope:{ctx:H}}}),As=new Ks({}),Ps=new z({props:{code:`ds = load_dataset("food101", split="validation[:10]")
image = ds["image"][0]`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>ds = load_dataset(<span class="hljs-string">&quot;food101&quot;</span>, split=<span class="hljs-string">&quot;validation[:10]&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>image = ds[<span class="hljs-string">&quot;image&quot;</span>][<span class="hljs-number">0</span>]`}}),Cs=new z({props:{code:`from transformers import pipeline

classifier = pipeline("image-classification", model="my_awesome_food_model")
classifier(image)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

<span class="hljs-meta">&gt;&gt;&gt; </span>classifier = pipeline(<span class="hljs-string">&quot;image-classification&quot;</span>, model=<span class="hljs-string">&quot;my_awesome_food_model&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>classifier(image)
[{<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.35574808716773987</span>, <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;beignets&#x27;</span>},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.018057454377412796</span>, <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;chicken_wings&#x27;</span>},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.017733804881572723</span>, <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;prime_rib&#x27;</span>},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.016335085034370422</span>, <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;bruschetta&#x27;</span>},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.0160061065107584</span>, <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;ramen&#x27;</span>}]`}}),Ze=new fl({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[Al]},$$scope:{ctx:H}}}),{c(){m=n("meta"),E=f(),h=n("h1"),_=n("a"),T=n("span"),v(x.$$.fragment),A=f(),P=n("span"),$=o("Image classification"),F=f(),v(D.$$.fragment),S=f(),v(Y.$$.fragment),L=f(),W=n("p"),X=o("Image classification assigns a label or class to an image. Unlike text or audio classification, the inputs are the pixel values that comprise an image. There are many applications for image classification such as detecting damage after a natural disaster, monitoring crop health, or helping screen medical images for signs of disease."),B=f(),U=n("p"),oe=o("This guide will show you how to:"),N=f(),M=n("ol"),g=n("li"),q=o("Finetune "),V=n("a"),ue=o("ViT"),J=o(" on the "),K=n("a"),ss=o("Food-101"),ye=o(" dataset to classify a food item in an image."),as=f(),we=n("li"),de=o("Use your finetuned model for inference."),Ie=f(),v(le.$$.fragment),De=f(),R=n("p"),ts=o("Before you begin, make sure you have all the necessary libraries installed:"),pe=f(),v(me.$$.fragment),ze=f(),re=n("p"),os=o("We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login:"),Fe=f(),v(fe.$$.fragment),ne=f(),Z=n("h2"),G=n("a"),je=n("span"),v(ce.$$.fragment),Se=f(),ee=n("span"),Le=o("Load Food-101 dataset"),se=f(),ge=n("p"),_e=o("Start by loading a smaller subset of the Food-101 dataset from the \u{1F917} Datasets library. This\u2019ll give you a chance to experiment and make sure everythings works before spending more time training on the full dataset."),Ne=f(),v(he.$$.fragment),ke=f(),O=n("p"),Oe=o("Split the dataset\u2019s "),d=n("code"),I=o("train"),ae=o(" split into a train and test set with the "),C=n("a"),Ds=o("train_test_split"),zs=o(" method:"),ls=f(),v($e.$$.fragment),rs=f(),Ee=n("p"),Fs=o("Then take a look at an example:"),ns=f(),v(Q.$$.fragment),is=f(),xe=n("p"),Me=o("There are two fields:"),ps=f(),te=n("ul"),Te=n("li"),Qs=n("code"),mt=o("image"),ft=o(": a PIL image of the food item."),ct=f(),Ss=n("li"),Xs=n("code"),ht=o("label"),ut=o(": the label class of the food item."),ga=f(),Ls=n("p"),dt=o("To make it easier for the model to get the label name from the label id, create a dictionary that maps the label name to an integer and vice versa:"),_a=f(),v(ms.$$.fragment),$a=f(),Ns=n("p"),gt=o("Now you can convert the label id to a label name:"),va=f(),v(fs.$$.fragment),ba=f(),Ae=n("h2"),He=n("a"),Zs=n("span"),v(cs.$$.fragment),_t=f(),ea=n("span"),$t=o("Preprocess"),ya=f(),Os=n("p"),vt=o("The next step is to load a ViT image processor to process the image into a tensor:"),wa=f(),v(hs.$$.fragment),ja=f(),Ye=n("p"),bt=o("Apply some image transformations to the images to make the model more robust against overfitting. Here you\u2019ll use torchvision\u2019s "),us=n("a"),sa=n("code"),yt=o("transforms"),wt=o(" module, but you can also use any image library you like."),ka=f(),Ms=n("p"),jt=o("Crop a random part of the image, resize it, and normalize it with the image mean and standard deviation:"),Ea=f(),v(ds.$$.fragment),xa=f(),Re=n("p"),kt=o("Then create a preprocessing function to apply the transforms and return the "),aa=n("code"),Et=o("pixel_values"),xt=o(" - the inputs to the model - of the image:"),Ta=f(),v(gs.$$.fragment),Aa=f(),Ge=n("p"),Tt=o("To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),_s=n("a"),At=o("with_transform"),Pt=o(" method. The transforms are applied on the fly when you load an element of the dataset:"),Pa=f(),v($s.$$.fragment),qa=f(),ve=n("p"),qt=o("Now create a batch of examples using "),Hs=n("a"),Ct=o("DataCollatorWithPadding"),It=o(". Unlike other data collators in \u{1F917} Transformers, the "),ta=n("code"),Dt=o("DefaultDataCollator"),zt=o(" does not apply additional preprocessing such as padding."),Ca=f(),v(vs.$$.fragment),Ia=f(),Pe=n("h2"),We=n("a"),oa=n("span"),v(bs.$$.fragment),Ft=f(),la=n("span"),St=o("Evaluate"),Da=f(),ie=n("p"),Lt=o("Including a metric during training is often helpful for evaluating your model\u2019s performance. You can quickly load a evaluation method with the \u{1F917} "),ys=n("a"),Nt=o("Evaluate"),Ot=o(" library. For this task, load the "),ws=n("a"),Mt=o("accuracy"),Ht=o(" metric (see the \u{1F917} Evaluate "),js=n("a"),Yt=o("quick tour"),Rt=o(" to learn more about how to load and compute a metric):"),za=f(),v(ks.$$.fragment),Fa=f(),Be=n("p"),Gt=o("Then create a function that passes your predictions and labels to "),Es=n("a"),Wt=o("compute"),Bt=o(" to calculate the accuracy:"),Sa=f(),v(xs.$$.fragment),La=f(),Ue=n("p"),Ut=o("Your "),ra=n("code"),Vt=o("compute_metrics"),Jt=o(" function is ready to go now, and you\u2019ll return to it when you setup your training."),Na=f(),qe=n("h2"),Ve=n("a"),na=n("span"),v(Ts.$$.fragment),Kt=f(),ia=n("span"),Qt=o("Train"),Oa=f(),v(Je.$$.fragment),Ma=f(),v(Ke.$$.fragment),Ha=f(),Ce=n("h2"),Qe=n("a"),pa=n("span"),v(As.$$.fragment),Xt=f(),ma=n("span"),Zt=o("Inference"),Ya=f(),Ys=n("p"),eo=o("Great, now that you\u2019ve finetuned a model, you can use it for inference!"),Ra=f(),Rs=n("p"),so=o("Load an image you\u2019d like to run inference on:"),Ga=f(),v(Ps.$$.fragment),Wa=f(),qs=n("div"),Gs=n("img"),Ba=f(),be=n("p"),ao=o("The simplest way to try out your finetuned model for inference is to use it in a "),Ws=n("a"),to=o("pipeline()"),oo=o(". Instantiate a "),fa=n("code"),lo=o("pipeline"),ro=o(" for image classification with your model, and pass your image to it:"),Ua=f(),v(Cs.$$.fragment),Va=f(),Xe=n("p"),no=o("You can also manually replicate the results of the "),ca=n("code"),io=o("pipeline"),po=o(" if you\u2019d like:"),Ja=f(),v(Ze.$$.fragment),this.h()},l(e){const t=gl('[data-svelte="svelte-1phssyn"]',document.head);m=i(t,"META",{name:!0,content:!0}),t.forEach(s),E=c(e),h=i(e,"H1",{class:!0});var Is=p(h);_=i(Is,"A",{id:!0,class:!0,href:!0});var ha=p(_);T=i(ha,"SPAN",{});var ua=p(T);b(x.$$.fragment,ua),ua.forEach(s),ha.forEach(s),A=c(Is),P=i(Is,"SPAN",{});var da=p(P);$=l(da,"Image classification"),da.forEach(s),Is.forEach(s),F=c(e),b(D.$$.fragment,e),S=c(e),b(Y.$$.fragment,e),L=c(e),W=i(e,"P",{});var uo=p(W);X=l(uo,"Image classification assigns a label or class to an image. Unlike text or audio classification, the inputs are the pixel values that comprise an image. There are many applications for image classification such as detecting damage after a natural disaster, monitoring crop health, or helping screen medical images for signs of disease."),uo.forEach(s),B=c(e),U=i(e,"P",{});var go=p(U);oe=l(go,"This guide will show you how to:"),go.forEach(s),N=c(e),M=i(e,"OL",{});var Qa=p(M);g=i(Qa,"LI",{});var Bs=p(g);q=l(Bs,"Finetune "),V=i(Bs,"A",{href:!0,rel:!0});var _o=p(V);ue=l(_o,"ViT"),_o.forEach(s),J=l(Bs," on the "),K=i(Bs,"A",{href:!0,rel:!0});var $o=p(K);ss=l($o,"Food-101"),$o.forEach(s),ye=l(Bs," dataset to classify a food item in an image."),Bs.forEach(s),as=c(Qa),we=i(Qa,"LI",{});var vo=p(we);de=l(vo,"Use your finetuned model for inference."),vo.forEach(s),Qa.forEach(s),Ie=c(e),b(le.$$.fragment,e),De=c(e),R=i(e,"P",{});var bo=p(R);ts=l(bo,"Before you begin, make sure you have all the necessary libraries installed:"),bo.forEach(s),pe=c(e),b(me.$$.fragment,e),ze=c(e),re=i(e,"P",{});var yo=p(re);os=l(yo,"We encourage you to login to your Hugging Face account so you can upload and share your model with the community. When prompted, enter your token to login:"),yo.forEach(s),Fe=c(e),b(fe.$$.fragment,e),ne=c(e),Z=i(e,"H2",{class:!0});var Xa=p(Z);G=i(Xa,"A",{id:!0,class:!0,href:!0});var wo=p(G);je=i(wo,"SPAN",{});var jo=p(je);b(ce.$$.fragment,jo),jo.forEach(s),wo.forEach(s),Se=c(Xa),ee=i(Xa,"SPAN",{});var ko=p(ee);Le=l(ko,"Load Food-101 dataset"),ko.forEach(s),Xa.forEach(s),se=c(e),ge=i(e,"P",{});var Eo=p(ge);_e=l(Eo,"Start by loading a smaller subset of the Food-101 dataset from the \u{1F917} Datasets library. This\u2019ll give you a chance to experiment and make sure everythings works before spending more time training on the full dataset."),Eo.forEach(s),Ne=c(e),b(he.$$.fragment,e),ke=c(e),O=i(e,"P",{});var Us=p(O);Oe=l(Us,"Split the dataset\u2019s "),d=i(Us,"CODE",{});var xo=p(d);I=l(xo,"train"),xo.forEach(s),ae=l(Us," split into a train and test set with the "),C=i(Us,"A",{href:!0,rel:!0});var To=p(C);Ds=l(To,"train_test_split"),To.forEach(s),zs=l(Us," method:"),Us.forEach(s),ls=c(e),b($e.$$.fragment,e),rs=c(e),Ee=i(e,"P",{});var Ao=p(Ee);Fs=l(Ao,"Then take a look at an example:"),Ao.forEach(s),ns=c(e),b(Q.$$.fragment,e),is=c(e),xe=i(e,"P",{});var Po=p(xe);Me=l(Po,"There are two fields:"),Po.forEach(s),ps=c(e),te=i(e,"UL",{});var Za=p(te);Te=i(Za,"LI",{});var mo=p(Te);Qs=i(mo,"CODE",{});var qo=p(Qs);mt=l(qo,"image"),qo.forEach(s),ft=l(mo,": a PIL image of the food item."),mo.forEach(s),ct=c(Za),Ss=i(Za,"LI",{});var fo=p(Ss);Xs=i(fo,"CODE",{});var Co=p(Xs);ht=l(Co,"label"),Co.forEach(s),ut=l(fo,": the label class of the food item."),fo.forEach(s),Za.forEach(s),ga=c(e),Ls=i(e,"P",{});var Io=p(Ls);dt=l(Io,"To make it easier for the model to get the label name from the label id, create a dictionary that maps the label name to an integer and vice versa:"),Io.forEach(s),_a=c(e),b(ms.$$.fragment,e),$a=c(e),Ns=i(e,"P",{});var Do=p(Ns);gt=l(Do,"Now you can convert the label id to a label name:"),Do.forEach(s),va=c(e),b(fs.$$.fragment,e),ba=c(e),Ae=i(e,"H2",{class:!0});var et=p(Ae);He=i(et,"A",{id:!0,class:!0,href:!0});var zo=p(He);Zs=i(zo,"SPAN",{});var Fo=p(Zs);b(cs.$$.fragment,Fo),Fo.forEach(s),zo.forEach(s),_t=c(et),ea=i(et,"SPAN",{});var So=p(ea);$t=l(So,"Preprocess"),So.forEach(s),et.forEach(s),ya=c(e),Os=i(e,"P",{});var Lo=p(Os);vt=l(Lo,"The next step is to load a ViT image processor to process the image into a tensor:"),Lo.forEach(s),wa=c(e),b(hs.$$.fragment,e),ja=c(e),Ye=i(e,"P",{});var st=p(Ye);bt=l(st,"Apply some image transformations to the images to make the model more robust against overfitting. Here you\u2019ll use torchvision\u2019s "),us=i(st,"A",{href:!0,rel:!0});var No=p(us);sa=i(No,"CODE",{});var Oo=p(sa);yt=l(Oo,"transforms"),Oo.forEach(s),No.forEach(s),wt=l(st," module, but you can also use any image library you like."),st.forEach(s),ka=c(e),Ms=i(e,"P",{});var Mo=p(Ms);jt=l(Mo,"Crop a random part of the image, resize it, and normalize it with the image mean and standard deviation:"),Mo.forEach(s),Ea=c(e),b(ds.$$.fragment,e),xa=c(e),Re=i(e,"P",{});var at=p(Re);kt=l(at,"Then create a preprocessing function to apply the transforms and return the "),aa=i(at,"CODE",{});var Ho=p(aa);Et=l(Ho,"pixel_values"),Ho.forEach(s),xt=l(at," - the inputs to the model - of the image:"),at.forEach(s),Ta=c(e),b(gs.$$.fragment,e),Aa=c(e),Ge=i(e,"P",{});var tt=p(Ge);Tt=l(tt,"To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),_s=i(tt,"A",{href:!0,rel:!0});var Yo=p(_s);At=l(Yo,"with_transform"),Yo.forEach(s),Pt=l(tt," method. The transforms are applied on the fly when you load an element of the dataset:"),tt.forEach(s),Pa=c(e),b($s.$$.fragment,e),qa=c(e),ve=i(e,"P",{});var Vs=p(ve);qt=l(Vs,"Now create a batch of examples using "),Hs=i(Vs,"A",{href:!0});var Ro=p(Hs);Ct=l(Ro,"DataCollatorWithPadding"),Ro.forEach(s),It=l(Vs,". Unlike other data collators in \u{1F917} Transformers, the "),ta=i(Vs,"CODE",{});var Go=p(ta);Dt=l(Go,"DefaultDataCollator"),Go.forEach(s),zt=l(Vs," does not apply additional preprocessing such as padding."),Vs.forEach(s),Ca=c(e),b(vs.$$.fragment,e),Ia=c(e),Pe=i(e,"H2",{class:!0});var ot=p(Pe);We=i(ot,"A",{id:!0,class:!0,href:!0});var Wo=p(We);oa=i(Wo,"SPAN",{});var Bo=p(oa);b(bs.$$.fragment,Bo),Bo.forEach(s),Wo.forEach(s),Ft=c(ot),la=i(ot,"SPAN",{});var Uo=p(la);St=l(Uo,"Evaluate"),Uo.forEach(s),ot.forEach(s),Da=c(e),ie=i(e,"P",{});var es=p(ie);Lt=l(es,"Including a metric during training is often helpful for evaluating your model\u2019s performance. You can quickly load a evaluation method with the \u{1F917} "),ys=i(es,"A",{href:!0,rel:!0});var Vo=p(ys);Nt=l(Vo,"Evaluate"),Vo.forEach(s),Ot=l(es," library. For this task, load the "),ws=i(es,"A",{href:!0,rel:!0});var Jo=p(ws);Mt=l(Jo,"accuracy"),Jo.forEach(s),Ht=l(es," metric (see the \u{1F917} Evaluate "),js=i(es,"A",{href:!0,rel:!0});var Ko=p(js);Yt=l(Ko,"quick tour"),Ko.forEach(s),Rt=l(es," to learn more about how to load and compute a metric):"),es.forEach(s),za=c(e),b(ks.$$.fragment,e),Fa=c(e),Be=i(e,"P",{});var lt=p(Be);Gt=l(lt,"Then create a function that passes your predictions and labels to "),Es=i(lt,"A",{href:!0,rel:!0});var Qo=p(Es);Wt=l(Qo,"compute"),Qo.forEach(s),Bt=l(lt," to calculate the accuracy:"),lt.forEach(s),Sa=c(e),b(xs.$$.fragment,e),La=c(e),Ue=i(e,"P",{});var rt=p(Ue);Ut=l(rt,"Your "),ra=i(rt,"CODE",{});var Xo=p(ra);Vt=l(Xo,"compute_metrics"),Xo.forEach(s),Jt=l(rt," function is ready to go now, and you\u2019ll return to it when you setup your training."),rt.forEach(s),Na=c(e),qe=i(e,"H2",{class:!0});var nt=p(qe);Ve=i(nt,"A",{id:!0,class:!0,href:!0});var Zo=p(Ve);na=i(Zo,"SPAN",{});var el=p(na);b(Ts.$$.fragment,el),el.forEach(s),Zo.forEach(s),Kt=c(nt),ia=i(nt,"SPAN",{});var sl=p(ia);Qt=l(sl,"Train"),sl.forEach(s),nt.forEach(s),Oa=c(e),b(Je.$$.fragment,e),Ma=c(e),b(Ke.$$.fragment,e),Ha=c(e),Ce=i(e,"H2",{class:!0});var it=p(Ce);Qe=i(it,"A",{id:!0,class:!0,href:!0});var al=p(Qe);pa=i(al,"SPAN",{});var tl=p(pa);b(As.$$.fragment,tl),tl.forEach(s),al.forEach(s),Xt=c(it),ma=i(it,"SPAN",{});var ol=p(ma);Zt=l(ol,"Inference"),ol.forEach(s),it.forEach(s),Ya=c(e),Ys=i(e,"P",{});var ll=p(Ys);eo=l(ll,"Great, now that you\u2019ve finetuned a model, you can use it for inference!"),ll.forEach(s),Ra=c(e),Rs=i(e,"P",{});var rl=p(Rs);so=l(rl,"Load an image you\u2019d like to run inference on:"),rl.forEach(s),Ga=c(e),b(Ps.$$.fragment,e),Wa=c(e),qs=i(e,"DIV",{class:!0});var nl=p(qs);Gs=i(nl,"IMG",{src:!0,alt:!0}),nl.forEach(s),Ba=c(e),be=i(e,"P",{});var Js=p(be);ao=l(Js,"The simplest way to try out your finetuned model for inference is to use it in a "),Ws=i(Js,"A",{href:!0});var il=p(Ws);to=l(il,"pipeline()"),il.forEach(s),oo=l(Js,". Instantiate a "),fa=i(Js,"CODE",{});var pl=p(fa);lo=l(pl,"pipeline"),pl.forEach(s),ro=l(Js," for image classification with your model, and pass your image to it:"),Js.forEach(s),Ua=c(e),b(Cs.$$.fragment,e),Va=c(e),Xe=i(e,"P",{});var pt=p(Xe);no=l(pt,"You can also manually replicate the results of the "),ca=i(pt,"CODE",{});var ml=p(ca);io=l(ml,"pipeline"),ml.forEach(s),po=l(pt," if you\u2019d like:"),pt.forEach(s),Ja=c(e),b(Ze.$$.fragment,e),this.h()},h(){u(m,"name","hf:doc:metadata"),u(m,"content",JSON.stringify(ql)),u(_,"id","image-classification"),u(_,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(_,"href","#image-classification"),u(h,"class","relative group"),u(V,"href","https://huggingface.co/docs/transformers/v4.16.2/en/model_doc/vit"),u(V,"rel","nofollow"),u(K,"href","https://huggingface.co/datasets/food101"),u(K,"rel","nofollow"),u(G,"id","load-food101-dataset"),u(G,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(G,"href","#load-food101-dataset"),u(Z,"class","relative group"),u(C,"href","https://huggingface.co/docs/datasets/v2.7.1/en/package_reference/main_classes#datasets.Dataset.train_test_split"),u(C,"rel","nofollow"),u(He,"id","preprocess"),u(He,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(He,"href","#preprocess"),u(Ae,"class","relative group"),u(us,"href","https://pytorch.org/vision/stable/transforms.html"),u(us,"rel","nofollow"),u(_s,"href","https://huggingface.co/docs/datasets/v2.7.1/en/package_reference/main_classes#datasets.Dataset.with_transform"),u(_s,"rel","nofollow"),u(Hs,"href","/docs/transformers/v4.25.1/en/main_classes/data_collator#transformers.DataCollatorWithPadding"),u(We,"id","evaluate"),u(We,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(We,"href","#evaluate"),u(Pe,"class","relative group"),u(ys,"href","https://huggingface.co/docs/evaluate/index"),u(ys,"rel","nofollow"),u(ws,"href","https://huggingface.co/spaces/evaluate-metric/accuracy"),u(ws,"rel","nofollow"),u(js,"href","https://huggingface.co/docs/evaluate/a_quick_tour"),u(js,"rel","nofollow"),u(Es,"href","https://huggingface.co/docs/evaluate/v0.3.0/en/package_reference/main_classes#evaluate.EvaluationModule.compute"),u(Es,"rel","nofollow"),u(Ve,"id","train"),u(Ve,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(Ve,"href","#train"),u(qe,"class","relative group"),u(Qe,"id","inference"),u(Qe,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),u(Qe,"href","#inference"),u(Ce,"class","relative group"),_l(Gs.src,ho="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png")||u(Gs,"src",ho),u(Gs,"alt","image of beignets"),u(qs,"class","flex justify-center"),u(Ws,"href","/docs/transformers/v4.25.1/en/main_classes/pipelines#transformers.pipeline")},m(e,t){a(document.head,m),r(e,E,t),r(e,h,t),a(h,_),a(_,T),y(x,T,null),a(h,A),a(h,P),a(P,$),r(e,F,t),y(D,e,t),r(e,S,t),y(Y,e,t),r(e,L,t),r(e,W,t),a(W,X),r(e,B,t),r(e,U,t),a(U,oe),r(e,N,t),r(e,M,t),a(M,g),a(g,q),a(g,V),a(V,ue),a(g,J),a(g,K),a(K,ss),a(g,ye),a(M,as),a(M,we),a(we,de),r(e,Ie,t),y(le,e,t),r(e,De,t),r(e,R,t),a(R,ts),r(e,pe,t),y(me,e,t),r(e,ze,t),r(e,re,t),a(re,os),r(e,Fe,t),y(fe,e,t),r(e,ne,t),r(e,Z,t),a(Z,G),a(G,je),y(ce,je,null),a(Z,Se),a(Z,ee),a(ee,Le),r(e,se,t),r(e,ge,t),a(ge,_e),r(e,Ne,t),y(he,e,t),r(e,ke,t),r(e,O,t),a(O,Oe),a(O,d),a(d,I),a(O,ae),a(O,C),a(C,Ds),a(O,zs),r(e,ls,t),y($e,e,t),r(e,rs,t),r(e,Ee,t),a(Ee,Fs),r(e,ns,t),y(Q,e,t),r(e,is,t),r(e,xe,t),a(xe,Me),r(e,ps,t),r(e,te,t),a(te,Te),a(Te,Qs),a(Qs,mt),a(Te,ft),a(te,ct),a(te,Ss),a(Ss,Xs),a(Xs,ht),a(Ss,ut),r(e,ga,t),r(e,Ls,t),a(Ls,dt),r(e,_a,t),y(ms,e,t),r(e,$a,t),r(e,Ns,t),a(Ns,gt),r(e,va,t),y(fs,e,t),r(e,ba,t),r(e,Ae,t),a(Ae,He),a(He,Zs),y(cs,Zs,null),a(Ae,_t),a(Ae,ea),a(ea,$t),r(e,ya,t),r(e,Os,t),a(Os,vt),r(e,wa,t),y(hs,e,t),r(e,ja,t),r(e,Ye,t),a(Ye,bt),a(Ye,us),a(us,sa),a(sa,yt),a(Ye,wt),r(e,ka,t),r(e,Ms,t),a(Ms,jt),r(e,Ea,t),y(ds,e,t),r(e,xa,t),r(e,Re,t),a(Re,kt),a(Re,aa),a(aa,Et),a(Re,xt),r(e,Ta,t),y(gs,e,t),r(e,Aa,t),r(e,Ge,t),a(Ge,Tt),a(Ge,_s),a(_s,At),a(Ge,Pt),r(e,Pa,t),y($s,e,t),r(e,qa,t),r(e,ve,t),a(ve,qt),a(ve,Hs),a(Hs,Ct),a(ve,It),a(ve,ta),a(ta,Dt),a(ve,zt),r(e,Ca,t),y(vs,e,t),r(e,Ia,t),r(e,Pe,t),a(Pe,We),a(We,oa),y(bs,oa,null),a(Pe,Ft),a(Pe,la),a(la,St),r(e,Da,t),r(e,ie,t),a(ie,Lt),a(ie,ys),a(ys,Nt),a(ie,Ot),a(ie,ws),a(ws,Mt),a(ie,Ht),a(ie,js),a(js,Yt),a(ie,Rt),r(e,za,t),y(ks,e,t),r(e,Fa,t),r(e,Be,t),a(Be,Gt),a(Be,Es),a(Es,Wt),a(Be,Bt),r(e,Sa,t),y(xs,e,t),r(e,La,t),r(e,Ue,t),a(Ue,Ut),a(Ue,ra),a(ra,Vt),a(Ue,Jt),r(e,Na,t),r(e,qe,t),a(qe,Ve),a(Ve,na),y(Ts,na,null),a(qe,Kt),a(qe,ia),a(ia,Qt),r(e,Oa,t),y(Je,e,t),r(e,Ma,t),y(Ke,e,t),r(e,Ha,t),r(e,Ce,t),a(Ce,Qe),a(Qe,pa),y(As,pa,null),a(Ce,Xt),a(Ce,ma),a(ma,Zt),r(e,Ya,t),r(e,Ys,t),a(Ys,eo),r(e,Ra,t),r(e,Rs,t),a(Rs,so),r(e,Ga,t),y(Ps,e,t),r(e,Wa,t),r(e,qs,t),a(qs,Gs),r(e,Ba,t),r(e,be,t),a(be,ao),a(be,Ws),a(Ws,to),a(be,oo),a(be,fa),a(fa,lo),a(be,ro),r(e,Ua,t),y(Cs,e,t),r(e,Va,t),r(e,Xe,t),a(Xe,no),a(Xe,ca),a(ca,io),a(Xe,po),r(e,Ja,t),y(Ze,e,t),Ka=!0},p(e,[t]){const Is={};t&2&&(Is.$$scope={dirty:t,ctx:e}),le.$set(Is);const ha={};t&2&&(ha.$$scope={dirty:t,ctx:e}),Je.$set(ha);const ua={};t&2&&(ua.$$scope={dirty:t,ctx:e}),Ke.$set(ua);const da={};t&2&&(da.$$scope={dirty:t,ctx:e}),Ze.$set(da)},i(e){Ka||(w(x.$$.fragment,e),w(D.$$.fragment,e),w(Y.$$.fragment,e),w(le.$$.fragment,e),w(me.$$.fragment,e),w(fe.$$.fragment,e),w(ce.$$.fragment,e),w(he.$$.fragment,e),w($e.$$.fragment,e),w(Q.$$.fragment,e),w(ms.$$.fragment,e),w(fs.$$.fragment,e),w(cs.$$.fragment,e),w(hs.$$.fragment,e),w(ds.$$.fragment,e),w(gs.$$.fragment,e),w($s.$$.fragment,e),w(vs.$$.fragment,e),w(bs.$$.fragment,e),w(ks.$$.fragment,e),w(xs.$$.fragment,e),w(Ts.$$.fragment,e),w(Je.$$.fragment,e),w(Ke.$$.fragment,e),w(As.$$.fragment,e),w(Ps.$$.fragment,e),w(Cs.$$.fragment,e),w(Ze.$$.fragment,e),Ka=!0)},o(e){j(x.$$.fragment,e),j(D.$$.fragment,e),j(Y.$$.fragment,e),j(le.$$.fragment,e),j(me.$$.fragment,e),j(fe.$$.fragment,e),j(ce.$$.fragment,e),j(he.$$.fragment,e),j($e.$$.fragment,e),j(Q.$$.fragment,e),j(ms.$$.fragment,e),j(fs.$$.fragment,e),j(cs.$$.fragment,e),j(hs.$$.fragment,e),j(ds.$$.fragment,e),j(gs.$$.fragment,e),j($s.$$.fragment,e),j(vs.$$.fragment,e),j(bs.$$.fragment,e),j(ks.$$.fragment,e),j(xs.$$.fragment,e),j(Ts.$$.fragment,e),j(Je.$$.fragment,e),j(Ke.$$.fragment,e),j(As.$$.fragment,e),j(Ps.$$.fragment,e),j(Cs.$$.fragment,e),j(Ze.$$.fragment,e),Ka=!1},d(e){s(m),e&&s(E),e&&s(h),k(x),e&&s(F),k(D,e),e&&s(S),k(Y,e),e&&s(L),e&&s(W),e&&s(B),e&&s(U),e&&s(N),e&&s(M),e&&s(Ie),k(le,e),e&&s(De),e&&s(R),e&&s(pe),k(me,e),e&&s(ze),e&&s(re),e&&s(Fe),k(fe,e),e&&s(ne),e&&s(Z),k(ce),e&&s(se),e&&s(ge),e&&s(Ne),k(he,e),e&&s(ke),e&&s(O),e&&s(ls),k($e,e),e&&s(rs),e&&s(Ee),e&&s(ns),k(Q,e),e&&s(is),e&&s(xe),e&&s(ps),e&&s(te),e&&s(ga),e&&s(Ls),e&&s(_a),k(ms,e),e&&s($a),e&&s(Ns),e&&s(va),k(fs,e),e&&s(ba),e&&s(Ae),k(cs),e&&s(ya),e&&s(Os),e&&s(wa),k(hs,e),e&&s(ja),e&&s(Ye),e&&s(ka),e&&s(Ms),e&&s(Ea),k(ds,e),e&&s(xa),e&&s(Re),e&&s(Ta),k(gs,e),e&&s(Aa),e&&s(Ge),e&&s(Pa),k($s,e),e&&s(qa),e&&s(ve),e&&s(Ca),k(vs,e),e&&s(Ia),e&&s(Pe),k(bs),e&&s(Da),e&&s(ie),e&&s(za),k(ks,e),e&&s(Fa),e&&s(Be),e&&s(Sa),k(xs,e),e&&s(La),e&&s(Ue),e&&s(Na),e&&s(qe),k(Ts),e&&s(Oa),k(Je,e),e&&s(Ma),k(Ke,e),e&&s(Ha),e&&s(Ce),k(As),e&&s(Ya),e&&s(Ys),e&&s(Ra),e&&s(Rs),e&&s(Ga),k(Ps,e),e&&s(Wa),e&&s(qs),e&&s(Ba),e&&s(be),e&&s(Ua),k(Cs,e),e&&s(Va),e&&s(Xe),e&&s(Ja),k(Ze,e)}}}const ql={local:"image-classification",sections:[{local:"load-food101-dataset",title:"Load Food-101 dataset"},{local:"preprocess",title:"Preprocess"},{local:"evaluate",title:"Evaluate"},{local:"train",title:"Train"},{local:"inference",title:"Inference"}],title:"Image classification"};function Cl(H){return $l(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ol extends hl{constructor(m){super();ul(this,m,Cl,Pl,dl,{})}}export{Ol as default,ql as metadata};
