import{S as El,i as Tl,s as ql,e as o,k as d,w as C,t,M as Cl,c as r,d as a,m as g,a as n,x,h as l,b as _,N as xl,G as s,g as i,y as A,q as P,o as D,B as z,v as Al,L as rt}from"../../chunks/vendor-hf-doc-builder.js";import{T as ot}from"../../chunks/Tip-hf-doc-builder.js";import{Y as Pl}from"../../chunks/Youtube-hf-doc-builder.js";import{I as da}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as se}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Dl}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";import{F as ga,M as _a}from"../../chunks/Markdown-hf-doc-builder.js";function zl(ae){let m,c,p,v,k,j,E,S,F,V,O,M,L,I,q,J,Q,X,U,R,H,f,T,W,ce,Z,K,$e,ie,fe,ee,be,b,N,ve,re,ye,ne,te,$,Y,pe,Ie,le,ke,je,ue,Te,he,Ve,de,Be,Le,Pe,es,De,ge,Ce,_e,Fe,Me,ss,Re,xe,Ne,me,w,B,ze,Ue,Se,oe,Ye,Ge,Oe,rs,Ke,Ae,We,Xe,we,as;return{c(){m=t(`The task illustrated in this tutorial is supported by the following model architectures:

`),c=o("p"),p=o("a"),v=t("BEiT"),k=t(", "),j=o("a"),E=t("BiT"),S=t(", "),F=o("a"),V=t("ConvNeXT"),O=t(", "),M=o("a"),L=t("ConvNeXTV2"),I=t(", "),q=o("a"),J=t("CvT"),Q=t(", "),X=o("a"),U=t("Data2VecVision"),R=t(", "),H=o("a"),f=t("DeiT"),T=t(", "),W=o("a"),ce=t("DiNAT"),Z=t(", "),K=o("a"),$e=t("EfficientFormer"),ie=t(", "),fe=o("a"),ee=t("EfficientNet"),be=t(", "),b=o("a"),N=t("ImageGPT"),ve=t(", "),re=o("a"),ye=t("LeViT"),ne=t(", "),te=o("a"),$=t("MobileNetV1"),Y=t(", "),pe=o("a"),Ie=t("MobileNetV2"),le=t(", "),ke=o("a"),je=t("MobileViT"),ue=t(", "),Te=o("a"),he=t("NAT"),Ve=t(", "),de=o("a"),Be=t("Perceiver"),Le=t(", "),Pe=o("a"),es=t("PoolFormer"),De=t(", "),ge=o("a"),Ce=t("RegNet"),_e=t(", "),Fe=o("a"),Me=t("ResNet"),ss=t(", "),Re=o("a"),xe=t("SegFormer"),Ne=t(", "),me=o("a"),w=t("Swin Transformer"),B=t(", "),ze=o("a"),Ue=t("Swin Transformer V2"),Se=t(", "),oe=o("a"),Ye=t("VAN"),Ge=t(", "),Oe=o("a"),rs=t("ViT"),Ke=t(", "),Ae=o("a"),We=t("ViT Hybrid"),Xe=t(", "),we=o("a"),as=t("ViTMSN"),this.h()},l(Ee){m=l(Ee,`The task illustrated in this tutorial is supported by the following model architectures:

`),c=r(Ee,"P",{});var y=n(c);p=r(y,"A",{href:!0});var Je=n(p);v=l(Je,"BEiT"),Je.forEach(a),k=l(y,", "),j=r(y,"A",{href:!0});var ts=n(j);E=l(ts,"BiT"),ts.forEach(a),S=l(y,", "),F=r(y,"A",{href:!0});var ms=n(F);V=l(ms,"ConvNeXT"),ms.forEach(a),O=l(y,", "),M=r(y,"A",{href:!0});var us=n(M);L=l(us,"ConvNeXTV2"),us.forEach(a),I=l(y,", "),q=r(y,"A",{href:!0});var Ze=n(q);J=l(Ze,"CvT"),Ze.forEach(a),Q=l(y,", "),X=r(y,"A",{href:!0});var js=n(X);U=l(js,"Data2VecVision"),js.forEach(a),R=l(y,", "),H=r(y,"A",{href:!0});var ns=n(H);f=l(ns,"DeiT"),ns.forEach(a),T=l(y,", "),W=r(y,"A",{href:!0});var is=n(W);ce=l(is,"DiNAT"),is.forEach(a),Z=l(y,", "),K=r(y,"A",{href:!0});var ds=n(K);$e=l(ds,"EfficientFormer"),ds.forEach(a),ie=l(y,", "),fe=r(y,"A",{href:!0});var h=n(fe);ee=l(h,"EfficientNet"),h.forEach(a),be=l(y,", "),b=r(y,"A",{href:!0});var G=n(b);N=l(G,"ImageGPT"),G.forEach(a),ve=l(y,", "),re=r(y,"A",{href:!0});var qe=n(re);ye=l(qe,"LeViT"),qe.forEach(a),ne=l(y,", "),te=r(y,"A",{href:!0});var cs=n(te);$=l(cs,"MobileNetV1"),cs.forEach(a),Y=l(y,", "),pe=r(y,"A",{href:!0});var Es=n(pe);Ie=l(Es,"MobileNetV2"),Es.forEach(a),le=l(y,", "),ke=r(y,"A",{href:!0});var Qe=n(ke);je=l(Qe,"MobileViT"),Qe.forEach(a),ue=l(y,", "),Te=r(y,"A",{href:!0});var ps=n(Te);he=l(ps,"NAT"),ps.forEach(a),Ve=l(y,", "),de=r(y,"A",{href:!0});var ys=n(de);Be=l(ys,"Perceiver"),ys.forEach(a),Le=l(y,", "),Pe=r(y,"A",{href:!0});var ls=n(Pe);es=l(ls,"PoolFormer"),ls.forEach(a),De=l(y,", "),ge=r(y,"A",{href:!0});var xs=n(ge);Ce=l(xs,"RegNet"),xs.forEach(a),_e=l(y,", "),Fe=r(y,"A",{href:!0});var ws=n(Fe);Me=l(ws,"ResNet"),ws.forEach(a),ss=l(y,", "),Re=r(y,"A",{href:!0});var As=n(Re);xe=l(As,"SegFormer"),As.forEach(a),Ne=l(y,", "),me=r(y,"A",{href:!0});var gs=n(me);w=l(gs,"Swin Transformer"),gs.forEach(a),B=l(y,", "),ze=r(y,"A",{href:!0});var _s=n(ze);Ue=l(_s,"Swin Transformer V2"),_s.forEach(a),Se=l(y,", "),oe=r(y,"A",{href:!0});var os=n(oe);Ye=l(os,"VAN"),os.forEach(a),Ge=l(y,", "),Oe=r(y,"A",{href:!0});var Ts=n(Oe);rs=l(Ts,"ViT"),Ts.forEach(a),Ke=l(y,", "),Ae=r(y,"A",{href:!0});var fs=n(Ae);We=l(fs,"ViT Hybrid"),fs.forEach(a),Xe=l(y,", "),we=r(y,"A",{href:!0});var qs=n(we);as=l(qs,"ViTMSN"),qs.forEach(a),y.forEach(a),this.h()},h(){_(p,"href","../model_doc/beit"),_(j,"href","../model_doc/bit"),_(F,"href","../model_doc/convnext"),_(M,"href","../model_doc/convnextv2"),_(q,"href","../model_doc/cvt"),_(X,"href","../model_doc/data2vec-vision"),_(H,"href","../model_doc/deit"),_(W,"href","../model_doc/dinat"),_(K,"href","../model_doc/efficientformer"),_(fe,"href","../model_doc/efficientnet"),_(b,"href","../model_doc/imagegpt"),_(re,"href","../model_doc/levit"),_(te,"href","../model_doc/mobilenet_v1"),_(pe,"href","../model_doc/mobilenet_v2"),_(ke,"href","../model_doc/mobilevit"),_(Te,"href","../model_doc/nat"),_(de,"href","../model_doc/perceiver"),_(Pe,"href","../model_doc/poolformer"),_(ge,"href","../model_doc/regnet"),_(Fe,"href","../model_doc/resnet"),_(Re,"href","../model_doc/segformer"),_(me,"href","../model_doc/swin"),_(ze,"href","../model_doc/swinv2"),_(oe,"href","../model_doc/van"),_(Oe,"href","../model_doc/vit"),_(Ae,"href","../model_doc/vit_hybrid"),_(we,"href","../model_doc/vit_msn")},m(Ee,y){i(Ee,m,y),i(Ee,c,y),s(c,p),s(p,v),s(c,k),s(c,j),s(j,E),s(c,S),s(c,F),s(F,V),s(c,O),s(c,M),s(M,L),s(c,I),s(c,q),s(q,J),s(c,Q),s(c,X),s(X,U),s(c,R),s(c,H),s(H,f),s(c,T),s(c,W),s(W,ce),s(c,Z),s(c,K),s(K,$e),s(c,ie),s(c,fe),s(fe,ee),s(c,be),s(c,b),s(b,N),s(c,ve),s(c,re),s(re,ye),s(c,ne),s(c,te),s(te,$),s(c,Y),s(c,pe),s(pe,Ie),s(c,le),s(c,ke),s(ke,je),s(c,ue),s(c,Te),s(Te,he),s(c,Ve),s(c,de),s(de,Be),s(c,Le),s(c,Pe),s(Pe,es),s(c,De),s(c,ge),s(ge,Ce),s(c,_e),s(c,Fe),s(Fe,Me),s(c,ss),s(c,Re),s(Re,xe),s(c,Ne),s(c,me),s(me,w),s(c,B),s(c,ze),s(ze,Ue),s(c,Se),s(c,oe),s(oe,Ye),s(c,Ge),s(c,Oe),s(Oe,rs),s(c,Ke),s(c,Ae),s(Ae,We),s(c,Xe),s(c,we),s(we,as)},d(Ee){Ee&&a(m),Ee&&a(c)}}}function Il(ae){let m,c,p,v,k,j,E,S,F,V,O,M,L,I,q,J,Q,X,U,R,H,f,T,W,ce,Z,K,$e,ie,fe,ee,be,b,N,ve,re,ye,ne,te;return O=new se({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])`}}),U=new se({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`}}),K=new se({props:{code:"food = food.with_transform(transforms)",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>food = food.with_transform(transforms)'}}),ne=new se({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()`}}),{c(){m=o("p"),c=t("Apply some image transformations to the images to make the model more robust against overfitting. Here you\u2019ll use torchvision\u2019s "),p=o("a"),v=o("code"),k=t("transforms"),j=t(" module, but you can also use any image library you like."),E=d(),S=o("p"),F=t("Crop a random part of the image, resize it, and normalize it with the image mean and standard deviation:"),V=d(),C(O.$$.fragment),M=d(),L=o("p"),I=t("Then create a preprocessing function to apply the transforms and return the "),q=o("code"),J=t("pixel_values"),Q=t(" - the inputs to the model - of the image:"),X=d(),C(U.$$.fragment),R=d(),H=o("p"),f=t("To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),T=o("code"),W=t("with_transform"),ce=t(" method. The transforms are applied on the fly when you load an element of the dataset:"),Z=d(),C(K.$$.fragment),$e=d(),ie=o("p"),fe=t("Now create a batch of examples using "),ee=o("a"),be=t("DefaultDataCollator"),b=t(". Unlike other data collators in \u{1F917} Transformers, the "),N=o("code"),ve=t("DefaultDataCollator"),re=t(" does not apply additional preprocessing such as padding."),ye=d(),C(ne.$$.fragment),this.h()},l($){m=r($,"P",{});var Y=n(m);c=l(Y,"Apply some image transformations to the images to make the model more robust against overfitting. Here you\u2019ll use torchvision\u2019s "),p=r(Y,"A",{href:!0,rel:!0});var pe=n(p);v=r(pe,"CODE",{});var Ie=n(v);k=l(Ie,"transforms"),Ie.forEach(a),pe.forEach(a),j=l(Y," module, but you can also use any image library you like."),Y.forEach(a),E=g($),S=r($,"P",{});var le=n(S);F=l(le,"Crop a random part of the image, resize it, and normalize it with the image mean and standard deviation:"),le.forEach(a),V=g($),x(O.$$.fragment,$),M=g($),L=r($,"P",{});var ke=n(L);I=l(ke,"Then create a preprocessing function to apply the transforms and return the "),q=r(ke,"CODE",{});var je=n(q);J=l(je,"pixel_values"),je.forEach(a),Q=l(ke," - the inputs to the model - of the image:"),ke.forEach(a),X=g($),x(U.$$.fragment,$),R=g($),H=r($,"P",{});var ue=n(H);f=l(ue,"To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),T=r(ue,"CODE",{});var Te=n(T);W=l(Te,"with_transform"),Te.forEach(a),ce=l(ue," method. The transforms are applied on the fly when you load an element of the dataset:"),ue.forEach(a),Z=g($),x(K.$$.fragment,$),$e=g($),ie=r($,"P",{});var he=n(ie);fe=l(he,"Now create a batch of examples using "),ee=r(he,"A",{href:!0});var Ve=n(ee);be=l(Ve,"DefaultDataCollator"),Ve.forEach(a),b=l(he,". Unlike other data collators in \u{1F917} Transformers, the "),N=r(he,"CODE",{});var de=n(N);ve=l(de,"DefaultDataCollator"),de.forEach(a),re=l(he," does not apply additional preprocessing such as padding."),he.forEach(a),ye=g($),x(ne.$$.fragment,$),this.h()},h(){_(p,"href","https://pytorch.org/vision/stable/transforms.html"),_(p,"rel","nofollow"),_(ee,"href","/docs/transformers/v4.27.1/en/main_classes/data_collator#transformers.DefaultDataCollator")},m($,Y){i($,m,Y),s(m,c),s(m,p),s(p,v),s(v,k),s(m,j),i($,E,Y),i($,S,Y),s(S,F),i($,V,Y),A(O,$,Y),i($,M,Y),i($,L,Y),s(L,I),s(L,q),s(q,J),s(L,Q),i($,X,Y),A(U,$,Y),i($,R,Y),i($,H,Y),s(H,f),s(H,T),s(T,W),s(H,ce),i($,Z,Y),A(K,$,Y),i($,$e,Y),i($,ie,Y),s(ie,fe),s(ie,ee),s(ee,be),s(ie,b),s(ie,N),s(N,ve),s(ie,re),i($,ye,Y),A(ne,$,Y),te=!0},p:rt,i($){te||(P(O.$$.fragment,$),P(U.$$.fragment,$),P(K.$$.fragment,$),P(ne.$$.fragment,$),te=!0)},o($){D(O.$$.fragment,$),D(U.$$.fragment,$),D(K.$$.fragment,$),D(ne.$$.fragment,$),te=!1},d($){$&&a(m),$&&a(E),$&&a(S),$&&a(V),z(O,$),$&&a(M),$&&a(L),$&&a(X),z(U,$),$&&a(R),$&&a(H),$&&a(Z),z(K,$),$&&a($e),$&&a(ie),$&&a(ye),z(ne,$)}}}function Fl(ae){let m,c;return m=new _a({props:{$$slots:{default:[Il]},$$scope:{ctx:ae}}}),{c(){C(m.$$.fragment)},l(p){x(m.$$.fragment,p)},m(p,v){A(m,p,v),c=!0},p(p,v){const k={};v&2&&(k.$$scope={dirty:v,ctx:p}),m.$set(k)},i(p){c||(P(m.$$.fragment,p),c=!0)},o(p){D(m.$$.fragment,p),c=!1},d(p){z(m,p)}}}function Ml(ae){let m,c,p,v,k,j,E,S,F,V,O,M,L,I,q,J,Q,X,U,R,H,f,T,W,ce,Z,K,$e,ie,fe,ee,be;return E=new se({props:{code:`from tensorflow import keras
from tensorflow.keras import layers

size = (image_processor.size["height"], image_processor.size["width"])

train_data_augmentation = keras.Sequential(
    [
        layers.RandomCrop(size[0], size[1]),
        layers.Rescaling(scale=1.0 / 127.5, offset=-1),
        layers.RandomFlip("horizontal"),
        layers.RandomRotation(factor=0.02),
        layers.RandomZoom(height_factor=0.2, width_factor=0.2),
    ],
    name="train_data_augmentation",
)

val_data_augmentation = keras.Sequential(
    [
        layers.CenterCrop(size[0], size[1]),
        layers.Rescaling(scale=1.0 / 127.5, offset=-1),
    ],
    name="val_data_augmentation",
)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tensorflow <span class="hljs-keyword">import</span> keras
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tensorflow.keras <span class="hljs-keyword">import</span> layers

<span class="hljs-meta">&gt;&gt;&gt; </span>size = (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">&gt;&gt;&gt; </span>train_data_augmentation = keras.Sequential(
<span class="hljs-meta">... </span>    [
<span class="hljs-meta">... </span>        layers.RandomCrop(size[<span class="hljs-number">0</span>], size[<span class="hljs-number">1</span>]),
<span class="hljs-meta">... </span>        layers.Rescaling(scale=<span class="hljs-number">1.0</span> / <span class="hljs-number">127.5</span>, offset=-<span class="hljs-number">1</span>),
<span class="hljs-meta">... </span>        layers.RandomFlip(<span class="hljs-string">&quot;horizontal&quot;</span>),
<span class="hljs-meta">... </span>        layers.RandomRotation(factor=<span class="hljs-number">0.02</span>),
<span class="hljs-meta">... </span>        layers.RandomZoom(height_factor=<span class="hljs-number">0.2</span>, width_factor=<span class="hljs-number">0.2</span>),
<span class="hljs-meta">... </span>    ],
<span class="hljs-meta">... </span>    name=<span class="hljs-string">&quot;train_data_augmentation&quot;</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>val_data_augmentation = keras.Sequential(
<span class="hljs-meta">... </span>    [
<span class="hljs-meta">... </span>        layers.CenterCrop(size[<span class="hljs-number">0</span>], size[<span class="hljs-number">1</span>]),
<span class="hljs-meta">... </span>        layers.Rescaling(scale=<span class="hljs-number">1.0</span> / <span class="hljs-number">127.5</span>, offset=-<span class="hljs-number">1</span>),
<span class="hljs-meta">... </span>    ],
<span class="hljs-meta">... </span>    name=<span class="hljs-string">&quot;val_data_augmentation&quot;</span>,
<span class="hljs-meta">... </span>)`}}),M=new se({props:{code:`import numpy as np
import tensorflow as tf
from PIL import Image


def convert_to_tf_tensor(image: Image):
    np_image = np.array(image)
    tf_image = tf.convert_to_tensor(np_image)
    # \`expand_dims()\` is used to add a batch dimension since
    # the TF augmentation layers operates on batched inputs.
    return tf.expand_dims(tf_image, 0)


def preprocess_train(example_batch):
    """Apply train_transforms across a batch."""
    images = [
        train_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"]
    ]
    example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images]
    return example_batch


def preprocess_val(example_batch):
    """Apply val_transforms across a batch."""
    images = [
        val_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"]
    ]
    example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images]
    return example_batch`,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">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">convert_to_tf_tensor</span>(<span class="hljs-params">image: Image</span>):
<span class="hljs-meta">... </span>    np_image = np.array(image)
<span class="hljs-meta">... </span>    tf_image = tf.convert_to_tensor(np_image)
<span class="hljs-meta">... </span>    <span class="hljs-comment"># \`expand_dims()\` is used to add a batch dimension since</span>
<span class="hljs-meta">... </span>    <span class="hljs-comment"># the TF augmentation layers operates on batched inputs.</span>
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> tf.expand_dims(tf_image, <span class="hljs-number">0</span>)


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_train</span>(<span class="hljs-params">example_batch</span>):
<span class="hljs-meta">... </span>    <span class="hljs-string">&quot;&quot;&quot;Apply train_transforms across a batch.&quot;&quot;&quot;</span>
<span class="hljs-meta">... </span>    images = [
<span class="hljs-meta">... </span>        train_data_augmentation(convert_to_tf_tensor(image.convert(<span class="hljs-string">&quot;RGB&quot;</span>))) <span class="hljs-keyword">for</span> image <span class="hljs-keyword">in</span> example_batch[<span class="hljs-string">&quot;image&quot;</span>]
<span class="hljs-meta">... </span>    ]
<span class="hljs-meta">... </span>    example_batch[<span class="hljs-string">&quot;pixel_values&quot;</span>] = [tf.transpose(tf.squeeze(image)) <span class="hljs-keyword">for</span> image <span class="hljs-keyword">in</span> images]
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> example_batch


<span class="hljs-meta">... </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess_val</span>(<span class="hljs-params">example_batch</span>):
<span class="hljs-meta">... </span>    <span class="hljs-string">&quot;&quot;&quot;Apply val_transforms across a batch.&quot;&quot;&quot;</span>
<span class="hljs-meta">... </span>    images = [
<span class="hljs-meta">... </span>        val_data_augmentation(convert_to_tf_tensor(image.convert(<span class="hljs-string">&quot;RGB&quot;</span>))) <span class="hljs-keyword">for</span> image <span class="hljs-keyword">in</span> example_batch[<span class="hljs-string">&quot;image&quot;</span>]
<span class="hljs-meta">... </span>    ]
<span class="hljs-meta">... </span>    example_batch[<span class="hljs-string">&quot;pixel_values&quot;</span>] = [tf.transpose(tf.squeeze(image)) <span class="hljs-keyword">for</span> image <span class="hljs-keyword">in</span> images]
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> example_batch`}}),R=new se({props:{code:`food["train"].set_transform(preprocess_train)
food["test"].set_transform(preprocess_val)`,highlighted:`food[<span class="hljs-string">&quot;train&quot;</span>].set_transform(preprocess_train)
food[<span class="hljs-string">&quot;test&quot;</span>].set_transform(preprocess_val)`}}),ee=new se({props:{code:`from transformers import DefaultDataCollator

data_collator = DefaultDataCollator(return_tensors="tf")`,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(return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`}}),{c(){m=o("p"),c=t(`To avoid overfitting and to make the model more robust, add some data augmentation to the training part of the dataset.
Here we use Keras preprocessing layers to define the transformations for the training data (includes data augmentation),
and transformations for the validation data (only center cropping, resizing and normalizing). You can use `),p=o("code"),v=t("tf.image"),k=t(`or
any other library you prefer.`),j=d(),C(E.$$.fragment),S=d(),F=o("p"),V=t("Next, create functions to apply appropriate transformations to a batch of images, instead of one image at a time."),O=d(),C(M.$$.fragment),L=d(),I=o("p"),q=t("Use \u{1F917} Datasets "),J=o("code"),Q=t("set_transform"),X=t(" to apply the transformations on the fly:"),U=d(),C(R.$$.fragment),H=d(),f=o("p"),T=t("As a final preprocessing step, create a batch of examples using "),W=o("code"),ce=t("DefaultDataCollator"),Z=t(`. Unlike other data collators in \u{1F917} Transformers, the
`),K=o("code"),$e=t("DefaultDataCollator"),ie=t(" does not apply additional preprocessing, such as padding."),fe=d(),C(ee.$$.fragment)},l(b){m=r(b,"P",{});var N=n(m);c=l(N,`To avoid overfitting and to make the model more robust, add some data augmentation to the training part of the dataset.
Here we use Keras preprocessing layers to define the transformations for the training data (includes data augmentation),
and transformations for the validation data (only center cropping, resizing and normalizing). You can use `),p=r(N,"CODE",{});var ve=n(p);v=l(ve,"tf.image"),ve.forEach(a),k=l(N,`or
any other library you prefer.`),N.forEach(a),j=g(b),x(E.$$.fragment,b),S=g(b),F=r(b,"P",{});var re=n(F);V=l(re,"Next, create functions to apply appropriate transformations to a batch of images, instead of one image at a time."),re.forEach(a),O=g(b),x(M.$$.fragment,b),L=g(b),I=r(b,"P",{});var ye=n(I);q=l(ye,"Use \u{1F917} Datasets "),J=r(ye,"CODE",{});var ne=n(J);Q=l(ne,"set_transform"),ne.forEach(a),X=l(ye," to apply the transformations on the fly:"),ye.forEach(a),U=g(b),x(R.$$.fragment,b),H=g(b),f=r(b,"P",{});var te=n(f);T=l(te,"As a final preprocessing step, create a batch of examples using "),W=r(te,"CODE",{});var $=n(W);ce=l($,"DefaultDataCollator"),$.forEach(a),Z=l(te,`. Unlike other data collators in \u{1F917} Transformers, the
`),K=r(te,"CODE",{});var Y=n(K);$e=l(Y,"DefaultDataCollator"),Y.forEach(a),ie=l(te," does not apply additional preprocessing, such as padding."),te.forEach(a),fe=g(b),x(ee.$$.fragment,b)},m(b,N){i(b,m,N),s(m,c),s(m,p),s(p,v),s(m,k),i(b,j,N),A(E,b,N),i(b,S,N),i(b,F,N),s(F,V),i(b,O,N),A(M,b,N),i(b,L,N),i(b,I,N),s(I,q),s(I,J),s(J,Q),s(I,X),i(b,U,N),A(R,b,N),i(b,H,N),i(b,f,N),s(f,T),s(f,W),s(W,ce),s(f,Z),s(f,K),s(K,$e),s(f,ie),i(b,fe,N),A(ee,b,N),be=!0},p:rt,i(b){be||(P(E.$$.fragment,b),P(M.$$.fragment,b),P(R.$$.fragment,b),P(ee.$$.fragment,b),be=!0)},o(b){D(E.$$.fragment,b),D(M.$$.fragment,b),D(R.$$.fragment,b),D(ee.$$.fragment,b),be=!1},d(b){b&&a(m),b&&a(j),z(E,b),b&&a(S),b&&a(F),b&&a(O),z(M,b),b&&a(L),b&&a(I),b&&a(U),z(R,b),b&&a(H),b&&a(f),b&&a(fe),z(ee,b)}}}function Nl(ae){let m,c;return m=new _a({props:{$$slots:{default:[Ml]},$$scope:{ctx:ae}}}),{c(){C(m.$$.fragment)},l(p){x(m.$$.fragment,p)},m(p,v){A(m,p,v),c=!0},p(p,v){const k={};v&2&&(k.$$scope={dirty:v,ctx:p}),m.$set(k)},i(p){c||(P(m.$$.fragment,p),c=!0)},o(p){D(m.$$.fragment,p),c=!1},d(p){z(m,p)}}}function Sl(ae){let m,c,p,v,k,j,E,S;return{c(){m=o("p"),c=t("If you aren\u2019t familiar with finetuning a model with the "),p=o("a"),v=t("Trainer"),k=t(", take a look at the basic tutorial "),j=o("a"),E=t("here"),S=t("!"),this.h()},l(F){m=r(F,"P",{});var V=n(m);c=l(V,"If you aren\u2019t familiar with finetuning a model with the "),p=r(V,"A",{href:!0});var O=n(p);v=l(O,"Trainer"),O.forEach(a),k=l(V,", take a look at the basic tutorial "),j=r(V,"A",{href:!0});var M=n(j);E=l(M,"here"),M.forEach(a),S=l(V,"!"),V.forEach(a),this.h()},h(){_(p,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Trainer"),_(j,"href","../training#train-with-pytorch-trainer")},m(F,V){i(F,m,V),s(m,c),s(m,p),s(p,v),s(m,k),s(m,j),s(j,E),s(m,S)},d(F){F&&a(m)}}}function Ol(ae){let m,c,p,v,k,j,E,S,F,V,O,M,L,I,q,J,Q,X,U,R,H,f,T,W,ce,Z,K,$e,ie,fe,ee,be,b,N,ve,re,ye,ne,te,$,Y,pe,Ie,le,ke,je,ue,Te,he,Ve,de,Be,Le,Pe,es,De,ge,Ce,_e,Fe,Me,ss,Re,xe,Ne,me;return m=new ot({props:{$$slots:{default:[Sl]},$$scope:{ctx:ae}}}),F=new se({props:{code:`from transformers import AutoModelForImageClassification, TrainingArguments, Trainer

model = AutoModelForImageClassification.from_pretrained(
    checkpoint,
    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>    checkpoint,
<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>)`}}),ge=new se({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()`}}),Ne=new se({props:{code:"trainer.push_to_hub()",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.push_to_hub()'}}),{c(){C(m.$$.fragment),c=d(),p=o("p"),v=t("You\u2019re ready to start training your model now! Load ViT with "),k=o("a"),j=t("AutoModelForImageClassification"),E=t(". Specify the number of labels along with the number of expected labels, and the label mappings:"),S=d(),C(F.$$.fragment),V=d(),O=o("p"),M=t("At this point, only three steps remain:"),L=d(),I=o("ol"),q=o("li"),J=t("Define your training hyperparameters in "),Q=o("a"),X=t("TrainingArguments"),U=t(". It is important you don\u2019t remove unused columns because this\u2019ll drop the "),R=o("code"),H=t("image"),f=t(" column. Without the "),T=o("code"),W=t("image"),ce=t(" column, you can\u2019t create "),Z=o("code"),K=t("pixel_values"),$e=t(". Set "),ie=o("code"),fe=t("remove_unused_columns=False"),ee=t(" to prevent this behavior! The only other required parameter is "),be=o("code"),b=t("output_dir"),N=t(" which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),ve=o("code"),re=t("push_to_hub=True"),ye=t(" (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the "),ne=o("a"),te=t("Trainer"),$=t(" will evaluate the accuracy and save the training checkpoint."),Y=d(),pe=o("li"),Ie=t("Pass the training arguments to "),le=o("a"),ke=t("Trainer"),je=t(" along with the model, dataset, tokenizer, data collator, and "),ue=o("code"),Te=t("compute_metrics"),he=t(" function."),Ve=d(),de=o("li"),Be=t("Call "),Le=o("a"),Pe=t("train()"),es=t(" to finetune your model."),De=d(),C(ge.$$.fragment),Ce=d(),_e=o("p"),Fe=t("Once training is completed, share your model to the Hub with the "),Me=o("a"),ss=t("push_to_hub()"),Re=t(" method so everyone can use your model:"),xe=d(),C(Ne.$$.fragment),this.h()},l(w){x(m.$$.fragment,w),c=g(w),p=r(w,"P",{});var B=n(p);v=l(B,"You\u2019re ready to start training your model now! Load ViT with "),k=r(B,"A",{href:!0});var ze=n(k);j=l(ze,"AutoModelForImageClassification"),ze.forEach(a),E=l(B,". Specify the number of labels along with the number of expected labels, and the label mappings:"),B.forEach(a),S=g(w),x(F.$$.fragment,w),V=g(w),O=r(w,"P",{});var Ue=n(O);M=l(Ue,"At this point, only three steps remain:"),Ue.forEach(a),L=g(w),I=r(w,"OL",{});var Se=n(I);q=r(Se,"LI",{});var oe=n(q);J=l(oe,"Define your training hyperparameters in "),Q=r(oe,"A",{href:!0});var Ye=n(Q);X=l(Ye,"TrainingArguments"),Ye.forEach(a),U=l(oe,". It is important you don\u2019t remove unused columns because this\u2019ll drop the "),R=r(oe,"CODE",{});var Ge=n(R);H=l(Ge,"image"),Ge.forEach(a),f=l(oe," column. Without the "),T=r(oe,"CODE",{});var Oe=n(T);W=l(Oe,"image"),Oe.forEach(a),ce=l(oe," column, you can\u2019t create "),Z=r(oe,"CODE",{});var rs=n(Z);K=l(rs,"pixel_values"),rs.forEach(a),$e=l(oe,". Set "),ie=r(oe,"CODE",{});var Ke=n(ie);fe=l(Ke,"remove_unused_columns=False"),Ke.forEach(a),ee=l(oe," to prevent this behavior! The only other required parameter is "),be=r(oe,"CODE",{});var Ae=n(be);b=l(Ae,"output_dir"),Ae.forEach(a),N=l(oe," which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),ve=r(oe,"CODE",{});var We=n(ve);re=l(We,"push_to_hub=True"),We.forEach(a),ye=l(oe," (you need to be signed in to Hugging Face to upload your model). At the end of each epoch, the "),ne=r(oe,"A",{href:!0});var Xe=n(ne);te=l(Xe,"Trainer"),Xe.forEach(a),$=l(oe," will evaluate the accuracy and save the training checkpoint."),oe.forEach(a),Y=g(Se),pe=r(Se,"LI",{});var we=n(pe);Ie=l(we,"Pass the training arguments to "),le=r(we,"A",{href:!0});var as=n(le);ke=l(as,"Trainer"),as.forEach(a),je=l(we," along with the model, dataset, tokenizer, data collator, and "),ue=r(we,"CODE",{});var Ee=n(ue);Te=l(Ee,"compute_metrics"),Ee.forEach(a),he=l(we," function."),we.forEach(a),Ve=g(Se),de=r(Se,"LI",{});var y=n(de);Be=l(y,"Call "),Le=r(y,"A",{href:!0});var Je=n(Le);Pe=l(Je,"train()"),Je.forEach(a),es=l(y," to finetune your model."),y.forEach(a),Se.forEach(a),De=g(w),x(ge.$$.fragment,w),Ce=g(w),_e=r(w,"P",{});var ts=n(_e);Fe=l(ts,"Once training is completed, share your model to the Hub with the "),Me=r(ts,"A",{href:!0});var ms=n(Me);ss=l(ms,"push_to_hub()"),ms.forEach(a),Re=l(ts," method so everyone can use your model:"),ts.forEach(a),xe=g(w),x(Ne.$$.fragment,w),this.h()},h(){_(k,"href","/docs/transformers/v4.27.1/en/model_doc/auto#transformers.AutoModelForImageClassification"),_(Q,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.TrainingArguments"),_(ne,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Trainer"),_(le,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Trainer"),_(Le,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Trainer.train"),_(Me,"href","/docs/transformers/v4.27.1/en/main_classes/trainer#transformers.Trainer.push_to_hub")},m(w,B){A(m,w,B),i(w,c,B),i(w,p,B),s(p,v),s(p,k),s(k,j),s(p,E),i(w,S,B),A(F,w,B),i(w,V,B),i(w,O,B),s(O,M),i(w,L,B),i(w,I,B),s(I,q),s(q,J),s(q,Q),s(Q,X),s(q,U),s(q,R),s(R,H),s(q,f),s(q,T),s(T,W),s(q,ce),s(q,Z),s(Z,K),s(q,$e),s(q,ie),s(ie,fe),s(q,ee),s(q,be),s(be,b),s(q,N),s(q,ve),s(ve,re),s(q,ye),s(q,ne),s(ne,te),s(q,$),s(I,Y),s(I,pe),s(pe,Ie),s(pe,le),s(le,ke),s(pe,je),s(pe,ue),s(ue,Te),s(pe,he),s(I,Ve),s(I,de),s(de,Be),s(de,Le),s(Le,Pe),s(de,es),i(w,De,B),A(ge,w,B),i(w,Ce,B),i(w,_e,B),s(_e,Fe),s(_e,Me),s(Me,ss),s(_e,Re),i(w,xe,B),A(Ne,w,B),me=!0},p(w,B){const ze={};B&2&&(ze.$$scope={dirty:B,ctx:w}),m.$set(ze)},i(w){me||(P(m.$$.fragment,w),P(F.$$.fragment,w),P(ge.$$.fragment,w),P(Ne.$$.fragment,w),me=!0)},o(w){D(m.$$.fragment,w),D(F.$$.fragment,w),D(ge.$$.fragment,w),D(Ne.$$.fragment,w),me=!1},d(w){z(m,w),w&&a(c),w&&a(p),w&&a(S),z(F,w),w&&a(V),w&&a(O),w&&a(L),w&&a(I),w&&a(De),z(ge,w),w&&a(Ce),w&&a(_e),w&&a(xe),z(Ne,w)}}}function Ll(ae){let m,c;return m=new _a({props:{$$slots:{default:[Ol]},$$scope:{ctx:ae}}}),{c(){C(m.$$.fragment)},l(p){x(m.$$.fragment,p)},m(p,v){A(m,p,v),c=!0},p(p,v){const k={};v&2&&(k.$$scope={dirty:v,ctx:p}),m.$set(k)},i(p){c||(P(m.$$.fragment,p),c=!0)},o(p){D(m.$$.fragment,p),c=!1},d(p){z(m,p)}}}function Rl(ae){let m,c,p,v,k;return{c(){m=o("p"),c=t("If you are unfamiliar with fine-tuning a model with Keras, check out the "),p=o("a"),v=t("basic tutorial"),k=t(" first!"),this.h()},l(j){m=r(j,"P",{});var E=n(m);c=l(E,"If you are unfamiliar with fine-tuning a model with Keras, check out the "),p=r(E,"A",{href:!0});var S=n(p);v=l(S,"basic tutorial"),S.forEach(a),k=l(E," first!"),E.forEach(a),this.h()},h(){_(p,"href","./training#train-a-tensorflow-model-with-keras")},m(j,E){i(j,m,E),s(m,c),s(m,p),s(p,v),s(m,k)},d(j){j&&a(m)}}}function Hl(ae){let m,c,p,v,k,j,E,S,F,V,O,M,L,I,q,J,Q,X,U,R,H,f,T,W,ce,Z,K,$e,ie,fe,ee,be,b,N,ve,re,ye,ne,te,$,Y,pe,Ie,le,ke,je,ue,Te,he,Ve,de,Be,Le,Pe,es,De,ge,Ce,_e,Fe,Me,ss,Re,xe,Ne,me,w,B,ze,Ue,Se,oe,Ye,Ge,Oe,rs,Ke,Ae,We,Xe,we,as,Ee,y,Je,ts,ms,us,Ze,js,ns,is,ds;return m=new ot({props:{$$slots:{default:[Rl]},$$scope:{ctx:ae}}}),N=new se({props:{code:`from transformers import create_optimizer

batch_size = 16
num_epochs = 5
num_train_steps = len(food["train"]) * num_epochs
learning_rate = 3e-5
weight_decay_rate = 0.01

optimizer, lr_schedule = create_optimizer(
    init_lr=learning_rate,
    num_train_steps=num_train_steps,
    weight_decay_rate=weight_decay_rate,
    num_warmup_steps=0,
)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> create_optimizer

<span class="hljs-meta">&gt;&gt;&gt; </span>batch_size = <span class="hljs-number">16</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>num_epochs = <span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>num_train_steps = <span class="hljs-built_in">len</span>(food[<span class="hljs-string">&quot;train&quot;</span>]) * num_epochs
<span class="hljs-meta">&gt;&gt;&gt; </span>learning_rate = <span class="hljs-number">3e-5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>weight_decay_rate = <span class="hljs-number">0.01</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>optimizer, lr_schedule = create_optimizer(
<span class="hljs-meta">... </span>    init_lr=learning_rate,
<span class="hljs-meta">... </span>    num_train_steps=num_train_steps,
<span class="hljs-meta">... </span>    weight_decay_rate=weight_decay_rate,
<span class="hljs-meta">... </span>    num_warmup_steps=<span class="hljs-number">0</span>,
<span class="hljs-meta">... </span>)`}}),pe=new se({props:{code:`from transformers import TFAutoModelForImageClassification

model = TFAutoModelForImageClassification.from_pretrained(
    checkpoint,
    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> TFAutoModelForImageClassification

<span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForImageClassification.from_pretrained(
<span class="hljs-meta">... </span>    checkpoint,
<span class="hljs-meta">... </span>    id2label=id2label,
<span class="hljs-meta">... </span>    label2id=label2id,
<span class="hljs-meta">... </span>)`}}),De=new se({props:{code:`# converting our train dataset to tf.data.Dataset
tf_train_dataset = food["train"].to_tf_dataset(
    columns=["pixel_values"], label_cols=["label"], shuffle=True, batch_size=batch_size, collate_fn=data_collator
)

# converting our test dataset to tf.data.Dataset
tf_eval_dataset = food["test"].to_tf_dataset(
    columns=["pixel_values"], label_cols=["label"], shuffle=True, batch_size=batch_size, collate_fn=data_collator
)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># converting our train dataset to tf.data.Dataset</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>tf_train_dataset = food[<span class="hljs-string">&quot;train&quot;</span>].to_tf_dataset(
<span class="hljs-meta">... </span>    columns=[<span class="hljs-string">&quot;pixel_values&quot;</span>], label_cols=[<span class="hljs-string">&quot;label&quot;</span>], shuffle=<span class="hljs-literal">True</span>, batch_size=batch_size, collate_fn=data_collator
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># converting our test dataset to tf.data.Dataset</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>tf_eval_dataset = food[<span class="hljs-string">&quot;test&quot;</span>].to_tf_dataset(
<span class="hljs-meta">... </span>    columns=[<span class="hljs-string">&quot;pixel_values&quot;</span>], label_cols=[<span class="hljs-string">&quot;label&quot;</span>], shuffle=<span class="hljs-literal">True</span>, batch_size=batch_size, collate_fn=data_collator
<span class="hljs-meta">... </span>)`}}),xe=new se({props:{code:`from tensorflow.keras.losses import SparseCategoricalCrossentropy

loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer=optimizer, loss=loss)`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> tensorflow.keras.losses <span class="hljs-keyword">import</span> SparseCategoricalCrossentropy

<span class="hljs-meta">&gt;&gt;&gt; </span>loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=<span class="hljs-literal">True</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model.<span class="hljs-built_in">compile</span>(optimizer=optimizer, loss=loss)`}}),we=new se({props:{code:`from transformers.keras_callbacks import KerasMetricCallback, PushToHubCallback

metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_eval_dataset)
push_to_hub_callback = PushToHubCallback(
    output_dir="food_classifier",
    tokenizer=image_processor,
    save_strategy="no",
)
callbacks = [metric_callback, push_to_hub_callback]`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers.keras_callbacks <span class="hljs-keyword">import</span> KerasMetricCallback, PushToHubCallback

<span class="hljs-meta">&gt;&gt;&gt; </span>metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_eval_dataset)
<span class="hljs-meta">&gt;&gt;&gt; </span>push_to_hub_callback = PushToHubCallback(
<span class="hljs-meta">... </span>    output_dir=<span class="hljs-string">&quot;food_classifier&quot;</span>,
<span class="hljs-meta">... </span>    tokenizer=image_processor,
<span class="hljs-meta">... </span>    save_strategy=<span class="hljs-string">&quot;no&quot;</span>,
<span class="hljs-meta">... </span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>callbacks = [metric_callback, push_to_hub_callback]`}}),Ze=new se({props:{code:"model.fit(tf_train_dataset, validation_data=tf_eval_dataset, epochs=num_epochs, callbacks=callbacks)",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>model.fit(tf_train_dataset, validation_data=tf_eval_dataset, epochs=num_epochs, callbacks=callbacks)
Epoch <span class="hljs-number">1</span>/<span class="hljs-number">5</span>
<span class="hljs-number">250</span>/<span class="hljs-number">250</span> [==============================] - 313s 1s/step - loss: <span class="hljs-number">2.5623</span> - val_loss: <span class="hljs-number">1.4161</span> - accuracy: <span class="hljs-number">0.9290</span>
Epoch <span class="hljs-number">2</span>/<span class="hljs-number">5</span>
<span class="hljs-number">250</span>/<span class="hljs-number">250</span> [==============================] - 265s 1s/step - loss: <span class="hljs-number">0.9181</span> - val_loss: <span class="hljs-number">0.6808</span> - accuracy: <span class="hljs-number">0.9690</span>
Epoch <span class="hljs-number">3</span>/<span class="hljs-number">5</span>
<span class="hljs-number">250</span>/<span class="hljs-number">250</span> [==============================] - 252s 1s/step - loss: <span class="hljs-number">0.3910</span> - val_loss: <span class="hljs-number">0.4303</span> - accuracy: <span class="hljs-number">0.9820</span>
Epoch <span class="hljs-number">4</span>/<span class="hljs-number">5</span>
<span class="hljs-number">250</span>/<span class="hljs-number">250</span> [==============================] - 251s 1s/step - loss: <span class="hljs-number">0.2028</span> - val_loss: <span class="hljs-number">0.3191</span> - accuracy: <span class="hljs-number">0.9900</span>
Epoch <span class="hljs-number">5</span>/<span class="hljs-number">5</span>
<span class="hljs-number">250</span>/<span class="hljs-number">250</span> [==============================] - 238s 949ms/step - loss: <span class="hljs-number">0.1232</span> - val_loss: <span class="hljs-number">0.3259</span> - accuracy: <span class="hljs-number">0.9890</span>`}}),{c(){C(m.$$.fragment),c=d(),p=o("p"),v=t("To fine-tune a model in TensorFlow, follow these steps:"),k=d(),j=o("ol"),E=o("li"),S=t("Define the training hyperparameters, and set up an optimizer and a learning rate schedule."),F=d(),V=o("li"),O=t("Instantiate a pre-treined model."),M=d(),L=o("li"),I=t("Convert a \u{1F917} Dataset to a "),q=o("code"),J=t("tf.data.Dataset"),Q=t("."),X=d(),U=o("li"),R=t("Compile your model."),H=d(),f=o("li"),T=t("Add callbacks and use the "),W=o("code"),ce=t("fit()"),Z=t(" method to run the training."),K=d(),$e=o("li"),ie=t("Upload your model to \u{1F917} Hub to share with the community."),fe=d(),ee=o("p"),be=t("Start by defining the hyperparameters, optimizer and learning rate schedule:"),b=d(),C(N.$$.fragment),ve=d(),re=o("p"),ye=t("Then, load ViT with "),ne=o("a"),te=t("TFAutoModelForImageClassification"),$=t(" along with the label mappings:"),Y=d(),C(pe.$$.fragment),Ie=d(),le=o("p"),ke=t("Convert your datasets to the "),je=o("code"),ue=t("tf.data.Dataset"),Te=t(" format using the "),he=o("code"),Ve=t("to_tf_dataset"),de=t(" and your "),Be=o("code"),Le=t("data_collator"),Pe=t(":"),es=d(),C(De.$$.fragment),ge=d(),Ce=o("p"),_e=t("Configure the model for training with "),Fe=o("code"),Me=t("compile()"),ss=t(":"),Re=d(),C(xe.$$.fragment),Ne=d(),me=o("p"),w=t("To compute the accuracy from the predictions and push your model to the \u{1F917} Hub, use "),B=o("a"),ze=t("Keras callbacks"),Ue=t(`.
Pass your `),Se=o("code"),oe=t("compute_metrics"),Ye=t(" function to "),Ge=o("a"),Oe=t("KerasMetricCallback"),rs=t(`,
and use the `),Ke=o("a"),Ae=t("PushToHubCallback"),We=t(" to upload the model:"),Xe=d(),C(we.$$.fragment),as=d(),Ee=o("p"),y=t("Finally, you are ready to train your model! Call "),Je=o("code"),ts=t("fit()"),ms=t(` with your training and validation datasets, the number of epochs,
and your callbacks to fine-tune the model:`),us=d(),C(Ze.$$.fragment),js=d(),ns=o("p"),is=t("Congratulations! You have fine-tuned your model and shared it on the \u{1F917} Hub. You can now use it for inference!"),this.h()},l(h){x(m.$$.fragment,h),c=g(h),p=r(h,"P",{});var G=n(p);v=l(G,"To fine-tune a model in TensorFlow, follow these steps:"),G.forEach(a),k=g(h),j=r(h,"OL",{});var qe=n(j);E=r(qe,"LI",{});var cs=n(E);S=l(cs,"Define the training hyperparameters, and set up an optimizer and a learning rate schedule."),cs.forEach(a),F=g(qe),V=r(qe,"LI",{});var Es=n(V);O=l(Es,"Instantiate a pre-treined model."),Es.forEach(a),M=g(qe),L=r(qe,"LI",{});var Qe=n(L);I=l(Qe,"Convert a \u{1F917} Dataset to a "),q=r(Qe,"CODE",{});var ps=n(q);J=l(ps,"tf.data.Dataset"),ps.forEach(a),Q=l(Qe,"."),Qe.forEach(a),X=g(qe),U=r(qe,"LI",{});var ys=n(U);R=l(ys,"Compile your model."),ys.forEach(a),H=g(qe),f=r(qe,"LI",{});var ls=n(f);T=l(ls,"Add callbacks and use the "),W=r(ls,"CODE",{});var xs=n(W);ce=l(xs,"fit()"),xs.forEach(a),Z=l(ls," method to run the training."),ls.forEach(a),K=g(qe),$e=r(qe,"LI",{});var ws=n($e);ie=l(ws,"Upload your model to \u{1F917} Hub to share with the community."),ws.forEach(a),qe.forEach(a),fe=g(h),ee=r(h,"P",{});var As=n(ee);be=l(As,"Start by defining the hyperparameters, optimizer and learning rate schedule:"),As.forEach(a),b=g(h),x(N.$$.fragment,h),ve=g(h),re=r(h,"P",{});var gs=n(re);ye=l(gs,"Then, load ViT with "),ne=r(gs,"A",{href:!0});var _s=n(ne);te=l(_s,"TFAutoModelForImageClassification"),_s.forEach(a),$=l(gs," along with the label mappings:"),gs.forEach(a),Y=g(h),x(pe.$$.fragment,h),Ie=g(h),le=r(h,"P",{});var os=n(le);ke=l(os,"Convert your datasets to the "),je=r(os,"CODE",{});var Ts=n(je);ue=l(Ts,"tf.data.Dataset"),Ts.forEach(a),Te=l(os," format using the "),he=r(os,"CODE",{});var fs=n(he);Ve=l(fs,"to_tf_dataset"),fs.forEach(a),de=l(os," and your "),Be=r(os,"CODE",{});var qs=n(Be);Le=l(qs,"data_collator"),qs.forEach(a),Pe=l(os,":"),os.forEach(a),es=g(h),x(De.$$.fragment,h),ge=g(h),Ce=r(h,"P",{});var hs=n(Ce);_e=l(hs,"Configure the model for training with "),Fe=r(hs,"CODE",{});var Bs=n(Fe);Me=l(Bs,"compile()"),Bs.forEach(a),ss=l(hs,":"),hs.forEach(a),Re=g(h),x(xe.$$.fragment,h),Ne=g(h),me=r(h,"P",{});var He=n(me);w=l(He,"To compute the accuracy from the predictions and push your model to the \u{1F917} Hub, use "),B=r(He,"A",{href:!0});var Us=n(B);ze=l(Us,"Keras callbacks"),Us.forEach(a),Ue=l(He,`.
Pass your `),Se=r(He,"CODE",{});var $s=n(Se);oe=l($s,"compute_metrics"),$s.forEach(a),Ye=l(He," function to "),Ge=r(He,"A",{href:!0});var ks=n(Ge);Oe=l(ks,"KerasMetricCallback"),ks.forEach(a),rs=l(He,`,
and use the `),Ke=r(He,"A",{href:!0});var Is=n(Ke);Ae=l(Is,"PushToHubCallback"),Is.forEach(a),We=l(He," to upload the model:"),He.forEach(a),Xe=g(h),x(we.$$.fragment,h),as=g(h),Ee=r(h,"P",{});var bs=n(Ee);y=l(bs,"Finally, you are ready to train your model! Call "),Je=r(bs,"CODE",{});var ia=n(Je);ts=l(ia,"fit()"),ia.forEach(a),ms=l(bs,` with your training and validation datasets, the number of epochs,
and your callbacks to fine-tune the model:`),bs.forEach(a),us=g(h),x(Ze.$$.fragment,h),js=g(h),ns=r(h,"P",{});var Fs=n(ns);is=l(Fs,"Congratulations! You have fine-tuned your model and shared it on the \u{1F917} Hub. You can now use it for inference!"),Fs.forEach(a),this.h()},h(){_(ne,"href","/docs/transformers/v4.27.1/en/model_doc/auto#transformers.TFAutoModelForImageClassification"),_(B,"href","./main_classes/keras_callbacks"),_(Ge,"href","./main_classes/keras_callbacks#transformers.KerasMetricCallback"),_(Ke,"href","./main_classes/keras_callbacks#transformers.PushToHubCallback")},m(h,G){A(m,h,G),i(h,c,G),i(h,p,G),s(p,v),i(h,k,G),i(h,j,G),s(j,E),s(E,S),s(j,F),s(j,V),s(V,O),s(j,M),s(j,L),s(L,I),s(L,q),s(q,J),s(L,Q),s(j,X),s(j,U),s(U,R),s(j,H),s(j,f),s(f,T),s(f,W),s(W,ce),s(f,Z),s(j,K),s(j,$e),s($e,ie),i(h,fe,G),i(h,ee,G),s(ee,be),i(h,b,G),A(N,h,G),i(h,ve,G),i(h,re,G),s(re,ye),s(re,ne),s(ne,te),s(re,$),i(h,Y,G),A(pe,h,G),i(h,Ie,G),i(h,le,G),s(le,ke),s(le,je),s(je,ue),s(le,Te),s(le,he),s(he,Ve),s(le,de),s(le,Be),s(Be,Le),s(le,Pe),i(h,es,G),A(De,h,G),i(h,ge,G),i(h,Ce,G),s(Ce,_e),s(Ce,Fe),s(Fe,Me),s(Ce,ss),i(h,Re,G),A(xe,h,G),i(h,Ne,G),i(h,me,G),s(me,w),s(me,B),s(B,ze),s(me,Ue),s(me,Se),s(Se,oe),s(me,Ye),s(me,Ge),s(Ge,Oe),s(me,rs),s(me,Ke),s(Ke,Ae),s(me,We),i(h,Xe,G),A(we,h,G),i(h,as,G),i(h,Ee,G),s(Ee,y),s(Ee,Je),s(Je,ts),s(Ee,ms),i(h,us,G),A(Ze,h,G),i(h,js,G),i(h,ns,G),s(ns,is),ds=!0},p(h,G){const qe={};G&2&&(qe.$$scope={dirty:G,ctx:h}),m.$set(qe)},i(h){ds||(P(m.$$.fragment,h),P(N.$$.fragment,h),P(pe.$$.fragment,h),P(De.$$.fragment,h),P(xe.$$.fragment,h),P(we.$$.fragment,h),P(Ze.$$.fragment,h),ds=!0)},o(h){D(m.$$.fragment,h),D(N.$$.fragment,h),D(pe.$$.fragment,h),D(De.$$.fragment,h),D(xe.$$.fragment,h),D(we.$$.fragment,h),D(Ze.$$.fragment,h),ds=!1},d(h){z(m,h),h&&a(c),h&&a(p),h&&a(k),h&&a(j),h&&a(fe),h&&a(ee),h&&a(b),z(N,h),h&&a(ve),h&&a(re),h&&a(Y),z(pe,h),h&&a(Ie),h&&a(le),h&&a(es),z(De,h),h&&a(ge),h&&a(Ce),h&&a(Re),z(xe,h),h&&a(Ne),h&&a(me),h&&a(Xe),z(we,h),h&&a(as),h&&a(Ee),h&&a(us),z(Ze,h),h&&a(js),h&&a(ns)}}}function Vl(ae){let m,c;return m=new _a({props:{$$slots:{default:[Hl]},$$scope:{ctx:ae}}}),{c(){C(m.$$.fragment)},l(p){x(m.$$.fragment,p)},m(p,v){A(m,p,v),c=!0},p(p,v){const k={};v&2&&(k.$$scope={dirty:v,ctx:p}),m.$set(k)},i(p){c||(P(m.$$.fragment,p),c=!0)},o(p){D(m.$$.fragment,p),c=!1},d(p){z(m,p)}}}function Yl(ae){let m,c,p,v,k;return{c(){m=o("p"),c=t("For a more in-depth example of how to finetune a model for image classification, take a look at the corresponding "),p=o("a"),v=t("PyTorch notebook"),k=t("."),this.h()},l(j){m=r(j,"P",{});var E=n(m);c=l(E,"For a more in-depth example of how to finetune a model for image classification, take a look at the corresponding "),p=r(E,"A",{href:!0,rel:!0});var S=n(p);v=l(S,"PyTorch notebook"),S.forEach(a),k=l(E,"."),E.forEach(a),this.h()},h(){_(p,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb"),_(p,"rel","nofollow")},m(j,E){i(j,m,E),s(m,c),s(m,p),s(p,v),s(m,k)},d(j){j&&a(m)}}}function Gl(ae){let m,c,p,v,k,j,E,S,F,V,O,M,L,I,q,J,Q,X,U,R,H;return E=new se({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>)`}}),M=new se({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`}}),R=new se({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=o("p"),c=t("Load an image processor to preprocess the image and return the "),p=o("code"),v=t("input"),k=t(" as PyTorch tensors:"),j=d(),C(E.$$.fragment),S=d(),F=o("p"),V=t("Pass your inputs to the model and return the logits:"),O=d(),C(M.$$.fragment),L=d(),I=o("p"),q=t("Get the predicted label with the highest probability, and use the model\u2019s "),J=o("code"),Q=t("id2label"),X=t(" mapping to convert it to a label:"),U=d(),C(R.$$.fragment)},l(f){m=r(f,"P",{});var T=n(m);c=l(T,"Load an image processor to preprocess the image and return the "),p=r(T,"CODE",{});var W=n(p);v=l(W,"input"),W.forEach(a),k=l(T," as PyTorch tensors:"),T.forEach(a),j=g(f),x(E.$$.fragment,f),S=g(f),F=r(f,"P",{});var ce=n(F);V=l(ce,"Pass your inputs to the model and return the logits:"),ce.forEach(a),O=g(f),x(M.$$.fragment,f),L=g(f),I=r(f,"P",{});var Z=n(I);q=l(Z,"Get the predicted label with the highest probability, and use the model\u2019s "),J=r(Z,"CODE",{});var K=n(J);Q=l(K,"id2label"),K.forEach(a),X=l(Z," mapping to convert it to a label:"),Z.forEach(a),U=g(f),x(R.$$.fragment,f)},m(f,T){i(f,m,T),s(m,c),s(m,p),s(p,v),s(m,k),i(f,j,T),A(E,f,T),i(f,S,T),i(f,F,T),s(F,V),i(f,O,T),A(M,f,T),i(f,L,T),i(f,I,T),s(I,q),s(I,J),s(J,Q),s(I,X),i(f,U,T),A(R,f,T),H=!0},p:rt,i(f){H||(P(E.$$.fragment,f),P(M.$$.fragment,f),P(R.$$.fragment,f),H=!0)},o(f){D(E.$$.fragment,f),D(M.$$.fragment,f),D(R.$$.fragment,f),H=!1},d(f){f&&a(m),f&&a(j),z(E,f),f&&a(S),f&&a(F),f&&a(O),z(M,f),f&&a(L),f&&a(I),f&&a(U),z(R,f)}}}function Kl(ae){let m,c;return m=new _a({props:{$$slots:{default:[Gl]},$$scope:{ctx:ae}}}),{c(){C(m.$$.fragment)},l(p){x(m.$$.fragment,p)},m(p,v){A(m,p,v),c=!0},p(p,v){const k={};v&2&&(k.$$scope={dirty:v,ctx:p}),m.$set(k)},i(p){c||(P(m.$$.fragment,p),c=!0)},o(p){D(m.$$.fragment,p),c=!1},d(p){z(m,p)}}}function Bl(ae){let m,c,p,v,k,j,E,S,F,V,O,M,L,I,q,J,Q,X,U,R,H;return E=new se({props:{code:`from transformers import AutoImageProcessor

image_processor = AutoImageProcessor.from_pretrained("MariaK/food_classifier")
inputs = image_processor(image, return_tensors="tf")`,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;MariaK/food_classifier&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = image_processor(image, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`}}),M=new se({props:{code:`from transformers import TFAutoModelForImageClassification

model = TFAutoModelForImageClassification.from_pretrained("MariaK/food_classifier")
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> TFAutoModelForImageClassification

<span class="hljs-meta">&gt;&gt;&gt; </span>model = TFAutoModelForImageClassification.from_pretrained(<span class="hljs-string">&quot;MariaK/food_classifier&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>logits = model(**inputs).logits`}}),R=new se({props:{code:`predicted_class_id = int(tf.math.argmax(logits, axis=-1)[0])
model.config.id2label[predicted_class_id]`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>predicted_class_id = <span class="hljs-built_in">int</span>(tf.math.argmax(logits, axis=-<span class="hljs-number">1</span>)[<span class="hljs-number">0</span>])
<span class="hljs-meta">&gt;&gt;&gt; </span>model.config.id2label[predicted_class_id]
<span class="hljs-string">&#x27;beignets&#x27;</span>`}}),{c(){m=o("p"),c=t("Load an image processor to preprocess the image and return the "),p=o("code"),v=t("input"),k=t(" as TensorFlow tensors:"),j=d(),C(E.$$.fragment),S=d(),F=o("p"),V=t("Pass your inputs to the model and return the logits:"),O=d(),C(M.$$.fragment),L=d(),I=o("p"),q=t("Get the predicted label with the highest probability, and use the model\u2019s "),J=o("code"),Q=t("id2label"),X=t(" mapping to convert it to a label:"),U=d(),C(R.$$.fragment)},l(f){m=r(f,"P",{});var T=n(m);c=l(T,"Load an image processor to preprocess the image and return the "),p=r(T,"CODE",{});var W=n(p);v=l(W,"input"),W.forEach(a),k=l(T," as TensorFlow tensors:"),T.forEach(a),j=g(f),x(E.$$.fragment,f),S=g(f),F=r(f,"P",{});var ce=n(F);V=l(ce,"Pass your inputs to the model and return the logits:"),ce.forEach(a),O=g(f),x(M.$$.fragment,f),L=g(f),I=r(f,"P",{});var Z=n(I);q=l(Z,"Get the predicted label with the highest probability, and use the model\u2019s "),J=r(Z,"CODE",{});var K=n(J);Q=l(K,"id2label"),K.forEach(a),X=l(Z," mapping to convert it to a label:"),Z.forEach(a),U=g(f),x(R.$$.fragment,f)},m(f,T){i(f,m,T),s(m,c),s(m,p),s(p,v),s(m,k),i(f,j,T),A(E,f,T),i(f,S,T),i(f,F,T),s(F,V),i(f,O,T),A(M,f,T),i(f,L,T),i(f,I,T),s(I,q),s(I,J),s(J,Q),s(I,X),i(f,U,T),A(R,f,T),H=!0},p:rt,i(f){H||(P(E.$$.fragment,f),P(M.$$.fragment,f),P(R.$$.fragment,f),H=!0)},o(f){D(E.$$.fragment,f),D(M.$$.fragment,f),D(R.$$.fragment,f),H=!1},d(f){f&&a(m),f&&a(j),z(E,f),f&&a(S),f&&a(F),f&&a(O),z(M,f),f&&a(L),f&&a(I),f&&a(U),z(R,f)}}}function Ul(ae){let m,c;return m=new _a({props:{$$slots:{default:[Bl]},$$scope:{ctx:ae}}}),{c(){C(m.$$.fragment)},l(p){x(m.$$.fragment,p)},m(p,v){A(m,p,v),c=!0},p(p,v){const k={};v&2&&(k.$$scope={dirty:v,ctx:p}),m.$set(k)},i(p){c||(P(m.$$.fragment,p),c=!0)},o(p){D(m.$$.fragment,p),c=!1},d(p){z(m,p)}}}function Wl(ae){let m,c,p,v,k,j,E,S,F,V,O,M,L,I,q,J,Q,X,U,R,H,f,T,W,ce,Z,K,$e,ie,fe,ee,be,b,N,ve,re,ye,ne,te,$,Y,pe,Ie,le,ke,je,ue,Te,he,Ve,de,Be,Le,Pe,es,De,ge,Ce,_e,Fe,Me,ss,Re,xe,Ne,me,w,B,ze,Ue,Se,oe,Ye,Ge,Oe,rs,Ke,Ae,We,Xe,we,as,Ee,y,Je,ts,ms,us,Ze,js,ns,is,ds,h,G,qe,cs,Es,Qe,ps,ys,ls,xs,ws,As,gs,_s,os,Ts,fs,qs,hs,Bs,He,Us,$s,ks,Is,bs,ia,Fs,nt,Aa,vs,it,Ws,pt,mt,Xs,ct,ft,Js,ht,ut,Pa,Zs,Da,Ms,dt,Qs,gt,_t,za,ea,Ia,Ns,$t,$a,bt,vt,Fa,Ps,Ss,ba,sa,jt,va,yt,Ma,Os,Na,Ls,Sa,Rs,Oa,Ds,Hs,ja,aa,wt,ya,kt,La,pa,Et,Ra,ma,Tt,Ha,ta,Va,la,ca,Nt,Ya,Cs,qt,fa,Ct,xt,wa,At,Pt,Ga,oa,Ka,Vs,Dt,ka,zt,It,Ba,Ys,Ua,Gs,Wa;return j=new da({}),O=new Dl({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"}]}}),L=new Pl({props:{id:"tjAIM7BOYhw"}}),N=new ot({props:{$$slots:{default:[zl]},$$scope:{ctx:ae}}}),te=new se({props:{code:"pip install transformers datasets evaluate",highlighted:"pip install transformers datasets evaluate"}}),le=new se({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()`}}),he=new da({}),ge=new se({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>)`}}),B=new se({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>)'}}),Ye=new se({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>}`}}),is=new se({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`}}),cs=new se({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>`}}),ls=new da({}),fs=new se({props:{code:`from transformers import AutoImageProcessor

checkpoint = "google/vit-base-patch16-224-in21k"
image_processor = AutoImageProcessor.from_pretrained(checkpoint)`,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>checkpoint = <span class="hljs-string">&quot;google/vit-base-patch16-224-in21k&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>image_processor = AutoImageProcessor.from_pretrained(checkpoint)`}}),hs=new ga({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[Fl]},$$scope:{ctx:ae}}}),He=new ga({props:{pytorch:!1,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Nl]},$$scope:{ctx:ae}}}),bs=new da({}),Zs=new se({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>)`}}),ea=new se({props:{code:`import numpy as np


def compute_metrics(eval_pred):
    predictions, labels = eval_pred
    predictions = np.argmax(predictions, axis=1)
    return accuracy.compute(predictions=predictions, references=labels)`,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, labels = eval_pred
<span class="hljs-meta">... </span>    predictions = np.argmax(predictions, axis=<span class="hljs-number">1</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> accuracy.compute(predictions=predictions, references=labels)`}}),sa=new da({}),Os=new ga({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[Ll]},$$scope:{ctx:ae}}}),Ls=new ga({props:{pytorch:!1,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Vl]},$$scope:{ctx:ae}}}),Rs=new ot({props:{$$slots:{default:[Yl]},$$scope:{ctx:ae}}}),aa=new da({}),ta=new se({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>]`}}),oa=new se({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.31856709718704224</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.015232225880026817</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.01519392803311348</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.013022331520915031</span>, <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;pork_chop&#x27;</span>},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.012728818692266941</span>, <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;prime_rib&#x27;</span>}]`}}),Ys=new ga({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[Kl]},$$scope:{ctx:ae}}}),Gs=new ga({props:{pytorch:!1,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Ul]},$$scope:{ctx:ae}}}),{c(){m=o("meta"),c=d(),p=o("h1"),v=o("a"),k=o("span"),C(j.$$.fragment),E=d(),S=o("span"),F=t("Image classification"),V=d(),C(O.$$.fragment),M=d(),C(L.$$.fragment),I=d(),q=o("p"),J=t(`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.`),Q=d(),X=o("p"),U=t("This guide illustrates how to:"),R=d(),H=o("ol"),f=o("li"),T=t("Fine-tune "),W=o("a"),ce=t("ViT"),Z=t(" on the "),K=o("a"),$e=t("Food-101"),ie=t(" dataset to classify a food item in an image."),fe=d(),ee=o("li"),be=t("Use your fine-tuned model for inference."),b=d(),C(N.$$.fragment),ve=d(),re=o("p"),ye=t("Before you begin, make sure you have all the necessary libraries installed:"),ne=d(),C(te.$$.fragment),$=d(),Y=o("p"),pe=t("We encourage you to log in to your Hugging Face account to upload and share your model with the community. When prompted, enter your token to log in:"),Ie=d(),C(le.$$.fragment),ke=d(),je=o("h2"),ue=o("a"),Te=o("span"),C(he.$$.fragment),Ve=d(),de=o("span"),Be=t("Load Food-101 dataset"),Le=d(),Pe=o("p"),es=t(`Start by loading a smaller subset of the Food-101 dataset from the \u{1F917} Datasets library. This will give you a chance to
experiment and make sure everything works before spending more time training on the full dataset.`),De=d(),C(ge.$$.fragment),Ce=d(),_e=o("p"),Fe=t("Split the dataset\u2019s "),Me=o("code"),ss=t("train"),Re=t(" split into a train and test set with the "),xe=o("code"),Ne=t("train_test_split"),me=t(" method:"),w=d(),C(B.$$.fragment),ze=d(),Ue=o("p"),Se=t("Then take a look at an example:"),oe=d(),C(Ye.$$.fragment),Ge=d(),Oe=o("p"),rs=t("Each example in the dataset has two fields:"),Ke=d(),Ae=o("ul"),We=o("li"),Xe=o("code"),we=t("image"),as=t(": a PIL image of the food item"),Ee=d(),y=o("li"),Je=o("code"),ts=t("label"),ms=t(": the label class of the food item"),us=d(),Ze=o("p"),js=t(`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:`),ns=d(),C(is.$$.fragment),ds=d(),h=o("p"),G=t("Now you can convert the label id to a label name:"),qe=d(),C(cs.$$.fragment),Es=d(),Qe=o("h2"),ps=o("a"),ys=o("span"),C(ls.$$.fragment),xs=d(),ws=o("span"),As=t("Preprocess"),gs=d(),_s=o("p"),os=t("The next step is to load a ViT image processor to process the image into a tensor:"),Ts=d(),C(fs.$$.fragment),qs=d(),C(hs.$$.fragment),Bs=d(),C(He.$$.fragment),Us=d(),$s=o("h2"),ks=o("a"),Is=o("span"),C(bs.$$.fragment),ia=d(),Fs=o("span"),nt=t("Evaluate"),Aa=d(),vs=o("p"),it=t(`Including a metric during training is often helpful for evaluating your model\u2019s performance. You can quickly load an
evaluation method with the \u{1F917} `),Ws=o("a"),pt=t("Evaluate"),mt=t(` library. For this task, load
the `),Xs=o("a"),ct=t("accuracy"),ft=t(" metric (see the \u{1F917} Evaluate "),Js=o("a"),ht=t("quick tour"),ut=t(" to learn more about how to load and compute a metric):"),Pa=d(),C(Zs.$$.fragment),Da=d(),Ms=o("p"),dt=t("Then create a function that passes your predictions and labels to "),Qs=o("a"),gt=t("compute"),_t=t(" to calculate the accuracy:"),za=d(),C(ea.$$.fragment),Ia=d(),Ns=o("p"),$t=t("Your "),$a=o("code"),bt=t("compute_metrics"),vt=t(" function is ready to go now, and you\u2019ll return to it when you set up your training."),Fa=d(),Ps=o("h2"),Ss=o("a"),ba=o("span"),C(sa.$$.fragment),jt=d(),va=o("span"),yt=t("Train"),Ma=d(),C(Os.$$.fragment),Na=d(),C(Ls.$$.fragment),Sa=d(),C(Rs.$$.fragment),Oa=d(),Ds=o("h2"),Hs=o("a"),ja=o("span"),C(aa.$$.fragment),wt=d(),ya=o("span"),kt=t("Inference"),La=d(),pa=o("p"),Et=t("Great, now that you\u2019ve fine-tuned a model, you can use it for inference!"),Ra=d(),ma=o("p"),Tt=t("Load an image you\u2019d like to run inference on:"),Ha=d(),C(ta.$$.fragment),Va=d(),la=o("div"),ca=o("img"),Ya=d(),Cs=o("p"),qt=t("The simplest way to try out your finetuned model for inference is to use it in a "),fa=o("a"),Ct=t("pipeline()"),xt=t(". Instantiate a "),wa=o("code"),At=t("pipeline"),Pt=t(" for image classification with your model, and pass your image to it:"),Ga=d(),C(oa.$$.fragment),Ka=d(),Vs=o("p"),Dt=t("You can also manually replicate the results of the "),ka=o("code"),zt=t("pipeline"),It=t(" if you\u2019d like:"),Ba=d(),C(Ys.$$.fragment),Ua=d(),C(Gs.$$.fragment),this.h()},l(e){const u=Cl('[data-svelte="svelte-1phssyn"]',document.head);m=r(u,"META",{name:!0,content:!0}),u.forEach(a),c=g(e),p=r(e,"H1",{class:!0});var ra=n(p);v=r(ra,"A",{id:!0,class:!0,href:!0});var Ea=n(v);k=r(Ea,"SPAN",{});var Ta=n(k);x(j.$$.fragment,Ta),Ta.forEach(a),Ea.forEach(a),E=g(ra),S=r(ra,"SPAN",{});var qa=n(S);F=l(qa,"Image classification"),qa.forEach(a),ra.forEach(a),V=g(e),x(O.$$.fragment,e),M=g(e),x(L.$$.fragment,e),I=g(e),q=r(e,"P",{});var Ca=n(q);J=l(Ca,`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.`),Ca.forEach(a),Q=g(e),X=r(e,"P",{});var xa=n(X);U=l(xa,"This guide illustrates how to:"),xa.forEach(a),R=g(e),H=r(e,"OL",{});var na=n(H);f=r(na,"LI",{});var zs=n(f);T=l(zs,"Fine-tune "),W=r(zs,"A",{href:!0});var St=n(W);ce=l(St,"ViT"),St.forEach(a),Z=l(zs," on the "),K=r(zs,"A",{href:!0,rel:!0});var Ot=n(K);$e=l(Ot,"Food-101"),Ot.forEach(a),ie=l(zs," dataset to classify a food item in an image."),zs.forEach(a),fe=g(na),ee=r(na,"LI",{});var Lt=n(ee);be=l(Lt,"Use your fine-tuned model for inference."),Lt.forEach(a),na.forEach(a),b=g(e),x(N.$$.fragment,e),ve=g(e),re=r(e,"P",{});var Rt=n(re);ye=l(Rt,"Before you begin, make sure you have all the necessary libraries installed:"),Rt.forEach(a),ne=g(e),x(te.$$.fragment,e),$=g(e),Y=r(e,"P",{});var Ht=n(Y);pe=l(Ht,"We encourage you to log in to your Hugging Face account to upload and share your model with the community. When prompted, enter your token to log in:"),Ht.forEach(a),Ie=g(e),x(le.$$.fragment,e),ke=g(e),je=r(e,"H2",{class:!0});var Xa=n(je);ue=r(Xa,"A",{id:!0,class:!0,href:!0});var Vt=n(ue);Te=r(Vt,"SPAN",{});var Yt=n(Te);x(he.$$.fragment,Yt),Yt.forEach(a),Vt.forEach(a),Ve=g(Xa),de=r(Xa,"SPAN",{});var Gt=n(de);Be=l(Gt,"Load Food-101 dataset"),Gt.forEach(a),Xa.forEach(a),Le=g(e),Pe=r(e,"P",{});var Kt=n(Pe);es=l(Kt,`Start by loading a smaller subset of the Food-101 dataset from the \u{1F917} Datasets library. This will give you a chance to
experiment and make sure everything works before spending more time training on the full dataset.`),Kt.forEach(a),De=g(e),x(ge.$$.fragment,e),Ce=g(e),_e=r(e,"P",{});var ha=n(_e);Fe=l(ha,"Split the dataset\u2019s "),Me=r(ha,"CODE",{});var Bt=n(Me);ss=l(Bt,"train"),Bt.forEach(a),Re=l(ha," split into a train and test set with the "),xe=r(ha,"CODE",{});var Ut=n(xe);Ne=l(Ut,"train_test_split"),Ut.forEach(a),me=l(ha," method:"),ha.forEach(a),w=g(e),x(B.$$.fragment,e),ze=g(e),Ue=r(e,"P",{});var Wt=n(Ue);Se=l(Wt,"Then take a look at an example:"),Wt.forEach(a),oe=g(e),x(Ye.$$.fragment,e),Ge=g(e),Oe=r(e,"P",{});var Xt=n(Oe);rs=l(Xt,"Each example in the dataset has two fields:"),Xt.forEach(a),Ke=g(e),Ae=r(e,"UL",{});var Ja=n(Ae);We=r(Ja,"LI",{});var Ft=n(We);Xe=r(Ft,"CODE",{});var Jt=n(Xe);we=l(Jt,"image"),Jt.forEach(a),as=l(Ft,": a PIL image of the food item"),Ft.forEach(a),Ee=g(Ja),y=r(Ja,"LI",{});var Mt=n(y);Je=r(Mt,"CODE",{});var Zt=n(Je);ts=l(Zt,"label"),Zt.forEach(a),ms=l(Mt,": the label class of the food item"),Mt.forEach(a),Ja.forEach(a),us=g(e),Ze=r(e,"P",{});var Qt=n(Ze);js=l(Qt,`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:`),Qt.forEach(a),ns=g(e),x(is.$$.fragment,e),ds=g(e),h=r(e,"P",{});var el=n(h);G=l(el,"Now you can convert the label id to a label name:"),el.forEach(a),qe=g(e),x(cs.$$.fragment,e),Es=g(e),Qe=r(e,"H2",{class:!0});var Za=n(Qe);ps=r(Za,"A",{id:!0,class:!0,href:!0});var sl=n(ps);ys=r(sl,"SPAN",{});var al=n(ys);x(ls.$$.fragment,al),al.forEach(a),sl.forEach(a),xs=g(Za),ws=r(Za,"SPAN",{});var tl=n(ws);As=l(tl,"Preprocess"),tl.forEach(a),Za.forEach(a),gs=g(e),_s=r(e,"P",{});var ll=n(_s);os=l(ll,"The next step is to load a ViT image processor to process the image into a tensor:"),ll.forEach(a),Ts=g(e),x(fs.$$.fragment,e),qs=g(e),x(hs.$$.fragment,e),Bs=g(e),x(He.$$.fragment,e),Us=g(e),$s=r(e,"H2",{class:!0});var Qa=n($s);ks=r(Qa,"A",{id:!0,class:!0,href:!0});var ol=n(ks);Is=r(ol,"SPAN",{});var rl=n(Is);x(bs.$$.fragment,rl),rl.forEach(a),ol.forEach(a),ia=g(Qa),Fs=r(Qa,"SPAN",{});var nl=n(Fs);nt=l(nl,"Evaluate"),nl.forEach(a),Qa.forEach(a),Aa=g(e),vs=r(e,"P",{});var Ks=n(vs);it=l(Ks,`Including a metric during training is often helpful for evaluating your model\u2019s performance. You can quickly load an
evaluation method with the \u{1F917} `),Ws=r(Ks,"A",{href:!0,rel:!0});var il=n(Ws);pt=l(il,"Evaluate"),il.forEach(a),mt=l(Ks,` library. For this task, load
the `),Xs=r(Ks,"A",{href:!0,rel:!0});var pl=n(Xs);ct=l(pl,"accuracy"),pl.forEach(a),ft=l(Ks," metric (see the \u{1F917} Evaluate "),Js=r(Ks,"A",{href:!0,rel:!0});var ml=n(Js);ht=l(ml,"quick tour"),ml.forEach(a),ut=l(Ks," to learn more about how to load and compute a metric):"),Ks.forEach(a),Pa=g(e),x(Zs.$$.fragment,e),Da=g(e),Ms=r(e,"P",{});var et=n(Ms);dt=l(et,"Then create a function that passes your predictions and labels to "),Qs=r(et,"A",{href:!0,rel:!0});var cl=n(Qs);gt=l(cl,"compute"),cl.forEach(a),_t=l(et," to calculate the accuracy:"),et.forEach(a),za=g(e),x(ea.$$.fragment,e),Ia=g(e),Ns=r(e,"P",{});var st=n(Ns);$t=l(st,"Your "),$a=r(st,"CODE",{});var fl=n($a);bt=l(fl,"compute_metrics"),fl.forEach(a),vt=l(st," function is ready to go now, and you\u2019ll return to it when you set up your training."),st.forEach(a),Fa=g(e),Ps=r(e,"H2",{class:!0});var at=n(Ps);Ss=r(at,"A",{id:!0,class:!0,href:!0});var hl=n(Ss);ba=r(hl,"SPAN",{});var ul=n(ba);x(sa.$$.fragment,ul),ul.forEach(a),hl.forEach(a),jt=g(at),va=r(at,"SPAN",{});var dl=n(va);yt=l(dl,"Train"),dl.forEach(a),at.forEach(a),Ma=g(e),x(Os.$$.fragment,e),Na=g(e),x(Ls.$$.fragment,e),Sa=g(e),x(Rs.$$.fragment,e),Oa=g(e),Ds=r(e,"H2",{class:!0});var tt=n(Ds);Hs=r(tt,"A",{id:!0,class:!0,href:!0});var gl=n(Hs);ja=r(gl,"SPAN",{});var _l=n(ja);x(aa.$$.fragment,_l),_l.forEach(a),gl.forEach(a),wt=g(tt),ya=r(tt,"SPAN",{});var $l=n(ya);kt=l($l,"Inference"),$l.forEach(a),tt.forEach(a),La=g(e),pa=r(e,"P",{});var bl=n(pa);Et=l(bl,"Great, now that you\u2019ve fine-tuned a model, you can use it for inference!"),bl.forEach(a),Ra=g(e),ma=r(e,"P",{});var vl=n(ma);Tt=l(vl,"Load an image you\u2019d like to run inference on:"),vl.forEach(a),Ha=g(e),x(ta.$$.fragment,e),Va=g(e),la=r(e,"DIV",{class:!0});var jl=n(la);ca=r(jl,"IMG",{src:!0,alt:!0}),jl.forEach(a),Ya=g(e),Cs=r(e,"P",{});var ua=n(Cs);qt=l(ua,"The simplest way to try out your finetuned model for inference is to use it in a "),fa=r(ua,"A",{href:!0});var yl=n(fa);Ct=l(yl,"pipeline()"),yl.forEach(a),xt=l(ua,". Instantiate a "),wa=r(ua,"CODE",{});var wl=n(wa);At=l(wl,"pipeline"),wl.forEach(a),Pt=l(ua," for image classification with your model, and pass your image to it:"),ua.forEach(a),Ga=g(e),x(oa.$$.fragment,e),Ka=g(e),Vs=r(e,"P",{});var lt=n(Vs);Dt=l(lt,"You can also manually replicate the results of the "),ka=r(lt,"CODE",{});var kl=n(ka);zt=l(kl,"pipeline"),kl.forEach(a),It=l(lt," if you\u2019d like:"),lt.forEach(a),Ba=g(e),x(Ys.$$.fragment,e),Ua=g(e),x(Gs.$$.fragment,e),this.h()},h(){_(m,"name","hf:doc:metadata"),_(m,"content",JSON.stringify(Xl)),_(v,"id","image-classification"),_(v,"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"),_(v,"href","#image-classification"),_(p,"class","relative group"),_(W,"href","model_doc/vit"),_(K,"href","https://huggingface.co/datasets/food101"),_(K,"rel","nofollow"),_(ue,"id","load-food101-dataset"),_(ue,"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"),_(ue,"href","#load-food101-dataset"),_(je,"class","relative group"),_(ps,"id","preprocess"),_(ps,"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"),_(ps,"href","#preprocess"),_(Qe,"class","relative group"),_(ks,"id","evaluate"),_(ks,"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"),_(ks,"href","#evaluate"),_($s,"class","relative group"),_(Ws,"href","https://huggingface.co/docs/evaluate/index"),_(Ws,"rel","nofollow"),_(Xs,"href","https://huggingface.co/spaces/evaluate-metric/accuracy"),_(Xs,"rel","nofollow"),_(Js,"href","https://huggingface.co/docs/evaluate/a_quick_tour"),_(Js,"rel","nofollow"),_(Qs,"href","https://huggingface.co/docs/evaluate/v0.4.0/en/package_reference/main_classes#evaluate.EvaluationModule.compute"),_(Qs,"rel","nofollow"),_(Ss,"id","train"),_(Ss,"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"),_(Ss,"href","#train"),_(Ps,"class","relative group"),_(Hs,"id","inference"),_(Hs,"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"),_(Hs,"href","#inference"),_(Ds,"class","relative group"),xl(ca.src,Nt="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png")||_(ca,"src",Nt),_(ca,"alt","image of beignets"),_(la,"class","flex justify-center"),_(fa,"href","/docs/transformers/v4.27.1/en/main_classes/pipelines#transformers.pipeline")},m(e,u){s(document.head,m),i(e,c,u),i(e,p,u),s(p,v),s(v,k),A(j,k,null),s(p,E),s(p,S),s(S,F),i(e,V,u),A(O,e,u),i(e,M,u),A(L,e,u),i(e,I,u),i(e,q,u),s(q,J),i(e,Q,u),i(e,X,u),s(X,U),i(e,R,u),i(e,H,u),s(H,f),s(f,T),s(f,W),s(W,ce),s(f,Z),s(f,K),s(K,$e),s(f,ie),s(H,fe),s(H,ee),s(ee,be),i(e,b,u),A(N,e,u),i(e,ve,u),i(e,re,u),s(re,ye),i(e,ne,u),A(te,e,u),i(e,$,u),i(e,Y,u),s(Y,pe),i(e,Ie,u),A(le,e,u),i(e,ke,u),i(e,je,u),s(je,ue),s(ue,Te),A(he,Te,null),s(je,Ve),s(je,de),s(de,Be),i(e,Le,u),i(e,Pe,u),s(Pe,es),i(e,De,u),A(ge,e,u),i(e,Ce,u),i(e,_e,u),s(_e,Fe),s(_e,Me),s(Me,ss),s(_e,Re),s(_e,xe),s(xe,Ne),s(_e,me),i(e,w,u),A(B,e,u),i(e,ze,u),i(e,Ue,u),s(Ue,Se),i(e,oe,u),A(Ye,e,u),i(e,Ge,u),i(e,Oe,u),s(Oe,rs),i(e,Ke,u),i(e,Ae,u),s(Ae,We),s(We,Xe),s(Xe,we),s(We,as),s(Ae,Ee),s(Ae,y),s(y,Je),s(Je,ts),s(y,ms),i(e,us,u),i(e,Ze,u),s(Ze,js),i(e,ns,u),A(is,e,u),i(e,ds,u),i(e,h,u),s(h,G),i(e,qe,u),A(cs,e,u),i(e,Es,u),i(e,Qe,u),s(Qe,ps),s(ps,ys),A(ls,ys,null),s(Qe,xs),s(Qe,ws),s(ws,As),i(e,gs,u),i(e,_s,u),s(_s,os),i(e,Ts,u),A(fs,e,u),i(e,qs,u),A(hs,e,u),i(e,Bs,u),A(He,e,u),i(e,Us,u),i(e,$s,u),s($s,ks),s(ks,Is),A(bs,Is,null),s($s,ia),s($s,Fs),s(Fs,nt),i(e,Aa,u),i(e,vs,u),s(vs,it),s(vs,Ws),s(Ws,pt),s(vs,mt),s(vs,Xs),s(Xs,ct),s(vs,ft),s(vs,Js),s(Js,ht),s(vs,ut),i(e,Pa,u),A(Zs,e,u),i(e,Da,u),i(e,Ms,u),s(Ms,dt),s(Ms,Qs),s(Qs,gt),s(Ms,_t),i(e,za,u),A(ea,e,u),i(e,Ia,u),i(e,Ns,u),s(Ns,$t),s(Ns,$a),s($a,bt),s(Ns,vt),i(e,Fa,u),i(e,Ps,u),s(Ps,Ss),s(Ss,ba),A(sa,ba,null),s(Ps,jt),s(Ps,va),s(va,yt),i(e,Ma,u),A(Os,e,u),i(e,Na,u),A(Ls,e,u),i(e,Sa,u),A(Rs,e,u),i(e,Oa,u),i(e,Ds,u),s(Ds,Hs),s(Hs,ja),A(aa,ja,null),s(Ds,wt),s(Ds,ya),s(ya,kt),i(e,La,u),i(e,pa,u),s(pa,Et),i(e,Ra,u),i(e,ma,u),s(ma,Tt),i(e,Ha,u),A(ta,e,u),i(e,Va,u),i(e,la,u),s(la,ca),i(e,Ya,u),i(e,Cs,u),s(Cs,qt),s(Cs,fa),s(fa,Ct),s(Cs,xt),s(Cs,wa),s(wa,At),s(Cs,Pt),i(e,Ga,u),A(oa,e,u),i(e,Ka,u),i(e,Vs,u),s(Vs,Dt),s(Vs,ka),s(ka,zt),s(Vs,It),i(e,Ba,u),A(Ys,e,u),i(e,Ua,u),A(Gs,e,u),Wa=!0},p(e,[u]){const ra={};u&2&&(ra.$$scope={dirty:u,ctx:e}),N.$set(ra);const Ea={};u&2&&(Ea.$$scope={dirty:u,ctx:e}),hs.$set(Ea);const Ta={};u&2&&(Ta.$$scope={dirty:u,ctx:e}),He.$set(Ta);const qa={};u&2&&(qa.$$scope={dirty:u,ctx:e}),Os.$set(qa);const Ca={};u&2&&(Ca.$$scope={dirty:u,ctx:e}),Ls.$set(Ca);const xa={};u&2&&(xa.$$scope={dirty:u,ctx:e}),Rs.$set(xa);const na={};u&2&&(na.$$scope={dirty:u,ctx:e}),Ys.$set(na);const zs={};u&2&&(zs.$$scope={dirty:u,ctx:e}),Gs.$set(zs)},i(e){Wa||(P(j.$$.fragment,e),P(O.$$.fragment,e),P(L.$$.fragment,e),P(N.$$.fragment,e),P(te.$$.fragment,e),P(le.$$.fragment,e),P(he.$$.fragment,e),P(ge.$$.fragment,e),P(B.$$.fragment,e),P(Ye.$$.fragment,e),P(is.$$.fragment,e),P(cs.$$.fragment,e),P(ls.$$.fragment,e),P(fs.$$.fragment,e),P(hs.$$.fragment,e),P(He.$$.fragment,e),P(bs.$$.fragment,e),P(Zs.$$.fragment,e),P(ea.$$.fragment,e),P(sa.$$.fragment,e),P(Os.$$.fragment,e),P(Ls.$$.fragment,e),P(Rs.$$.fragment,e),P(aa.$$.fragment,e),P(ta.$$.fragment,e),P(oa.$$.fragment,e),P(Ys.$$.fragment,e),P(Gs.$$.fragment,e),Wa=!0)},o(e){D(j.$$.fragment,e),D(O.$$.fragment,e),D(L.$$.fragment,e),D(N.$$.fragment,e),D(te.$$.fragment,e),D(le.$$.fragment,e),D(he.$$.fragment,e),D(ge.$$.fragment,e),D(B.$$.fragment,e),D(Ye.$$.fragment,e),D(is.$$.fragment,e),D(cs.$$.fragment,e),D(ls.$$.fragment,e),D(fs.$$.fragment,e),D(hs.$$.fragment,e),D(He.$$.fragment,e),D(bs.$$.fragment,e),D(Zs.$$.fragment,e),D(ea.$$.fragment,e),D(sa.$$.fragment,e),D(Os.$$.fragment,e),D(Ls.$$.fragment,e),D(Rs.$$.fragment,e),D(aa.$$.fragment,e),D(ta.$$.fragment,e),D(oa.$$.fragment,e),D(Ys.$$.fragment,e),D(Gs.$$.fragment,e),Wa=!1},d(e){a(m),e&&a(c),e&&a(p),z(j),e&&a(V),z(O,e),e&&a(M),z(L,e),e&&a(I),e&&a(q),e&&a(Q),e&&a(X),e&&a(R),e&&a(H),e&&a(b),z(N,e),e&&a(ve),e&&a(re),e&&a(ne),z(te,e),e&&a($),e&&a(Y),e&&a(Ie),z(le,e),e&&a(ke),e&&a(je),z(he),e&&a(Le),e&&a(Pe),e&&a(De),z(ge,e),e&&a(Ce),e&&a(_e),e&&a(w),z(B,e),e&&a(ze),e&&a(Ue),e&&a(oe),z(Ye,e),e&&a(Ge),e&&a(Oe),e&&a(Ke),e&&a(Ae),e&&a(us),e&&a(Ze),e&&a(ns),z(is,e),e&&a(ds),e&&a(h),e&&a(qe),z(cs,e),e&&a(Es),e&&a(Qe),z(ls),e&&a(gs),e&&a(_s),e&&a(Ts),z(fs,e),e&&a(qs),z(hs,e),e&&a(Bs),z(He,e),e&&a(Us),e&&a($s),z(bs),e&&a(Aa),e&&a(vs),e&&a(Pa),z(Zs,e),e&&a(Da),e&&a(Ms),e&&a(za),z(ea,e),e&&a(Ia),e&&a(Ns),e&&a(Fa),e&&a(Ps),z(sa),e&&a(Ma),z(Os,e),e&&a(Na),z(Ls,e),e&&a(Sa),z(Rs,e),e&&a(Oa),e&&a(Ds),z(aa),e&&a(La),e&&a(pa),e&&a(Ra),e&&a(ma),e&&a(Ha),z(ta,e),e&&a(Va),e&&a(la),e&&a(Ya),e&&a(Cs),e&&a(Ga),z(oa,e),e&&a(Ka),e&&a(Vs),e&&a(Ba),z(Ys,e),e&&a(Ua),z(Gs,e)}}}const Xl={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 Jl(ae){return Al(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class oo extends El{constructor(m){super();Tl(this,m,Jl,Wl,ql,{})}}export{oo as default,Xl as metadata};
