import{S as El,i as Tl,s as ql,e as o,k as d,w as x,t,M as xl,c as r,d as a,m as g,a as n,x as A,h as l,b as _,N as Al,G as s,g as i,y as C,q as P,o as D,B as z,v as Cl,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,f,p,j,k,v,E,S,F,V,L,M,O,I,q,W,Q,X,U,R,H,c,T,J,fe,Z,K,$e,ie,he,ee,be,b,N,je,re,ye,ne,te,$,Y,pe,ze,le,we,ve,ue,Ee,me,He,de,Be,Le,Ae,Ze,Ce,ge,xe,_e,Ie,Fe,ss,Oe,ke,Me,ce,y,B,Pe,Ue,Ne,oe,Ve,Ye,Se,ls;return{c(){m=t(`The task illustrated in this tutorial is supported by the following model architectures:

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

`),f=r(Te,"P",{});var w=n(f);p=r(w,"A",{href:!0});var Qe=n(p);j=l(Qe,"BEiT"),Qe.forEach(a),k=l(w,", "),v=r(w,"A",{href:!0});var es=n(v);E=l(es,"BiT"),es.forEach(a),S=l(w,", "),F=r(w,"A",{href:!0});var De=n(F);V=l(De,"ConvNeXT"),De.forEach(a),L=l(w,", "),M=r(w,"A",{href:!0});var os=n(M);O=l(os,"CvT"),os.forEach(a),I=l(w,", "),q=r(w,"A",{href:!0});var Ge=n(q);W=l(Ge,"Data2VecVision"),Ge.forEach(a),Q=l(w,", "),X=r(w,"A",{href:!0});var Ke=n(X);U=l(Ke,"DeiT"),Ke.forEach(a),R=l(w,", "),H=r(w,"A",{href:!0});var We=n(H);c=l(We,"DiNAT"),We.forEach(a),T=l(w,", "),J=r(w,"A",{href:!0});var as=n(J);fe=l(as,"EfficientFormer"),as.forEach(a),Z=l(w,", "),K=r(w,"A",{href:!0});var ps=n(K);$e=l(ps,"ImageGPT"),ps.forEach(a),ie=l(w,", "),he=r(w,"A",{href:!0});var fs=n(he);ee=l(fs,"LeViT"),fs.forEach(a),be=l(w,", "),b=r(w,"A",{href:!0});var Je=n(b);N=l(Je,"MobileNetV1"),Je.forEach(a),je=l(w,", "),re=r(w,"A",{href:!0});var $s=n(re);ye=l($s,"MobileNetV2"),$s.forEach(a),ne=l(w,", "),te=r(w,"A",{href:!0});var rs=n(te);$=l(rs,"MobileViT"),rs.forEach(a),Y=l(w,", "),pe=r(w,"A",{href:!0});var ns=n(pe);ze=l(ns,"NAT"),ns.forEach(a),le=l(w,", "),we=r(w,"A",{href:!0});var hs=n(we);ve=l(hs,"Perceiver"),hs.forEach(a),ue=l(w,", "),Ee=r(w,"A",{href:!0});var h=n(Ee);me=l(h,"PoolFormer"),h.forEach(a),He=l(w,", "),de=r(w,"A",{href:!0});var G=n(de);Be=l(G,"RegNet"),G.forEach(a),Le=l(w,", "),Ae=r(w,"A",{href:!0});var qe=n(Ae);Ze=l(qe,"ResNet"),qe.forEach(a),Ce=l(w,", "),ge=r(w,"A",{href:!0});var ms=n(ge);xe=l(ms,"SegFormer"),ms.forEach(a),_e=l(w,", "),Ie=r(w,"A",{href:!0});var vs=n(Ie);Fe=l(vs,"Swin Transformer"),vs.forEach(a),ss=l(w,", "),Oe=r(w,"A",{href:!0});var Xe=n(Oe);ke=l(Xe,"Swin Transformer V2"),Xe.forEach(a),Me=l(w,", "),ce=r(w,"A",{href:!0});var is=n(ce);y=l(is,"VAN"),is.forEach(a),B=l(w,", "),Pe=r(w,"A",{href:!0});var bs=n(Pe);Ue=l(bs,"ViT"),bs.forEach(a),Ne=l(w,", "),oe=r(w,"A",{href:!0});var ts=n(oe);Ve=l(ts,"ViT Hybrid"),ts.forEach(a),Ye=l(w,", "),Se=r(w,"A",{href:!0});var ks=n(Se);ls=l(ks,"ViTMSN"),ks.forEach(a),w.forEach(a),this.h()},h(){_(p,"href","../model_doc/beit"),_(v,"href","../model_doc/bit"),_(F,"href","../model_doc/convnext"),_(M,"href","../model_doc/cvt"),_(q,"href","../model_doc/data2vec-vision"),_(X,"href","../model_doc/deit"),_(H,"href","../model_doc/dinat"),_(J,"href","../model_doc/efficientformer"),_(K,"href","../model_doc/imagegpt"),_(he,"href","../model_doc/levit"),_(b,"href","../model_doc/mobilenet_v1"),_(re,"href","../model_doc/mobilenet_v2"),_(te,"href","../model_doc/mobilevit"),_(pe,"href","../model_doc/nat"),_(we,"href","../model_doc/perceiver"),_(Ee,"href","../model_doc/poolformer"),_(de,"href","../model_doc/regnet"),_(Ae,"href","../model_doc/resnet"),_(ge,"href","../model_doc/segformer"),_(Ie,"href","../model_doc/swin"),_(Oe,"href","../model_doc/swinv2"),_(ce,"href","../model_doc/van"),_(Pe,"href","../model_doc/vit"),_(oe,"href","../model_doc/vit_hybrid"),_(Se,"href","../model_doc/vit_msn")},m(Te,w){i(Te,m,w),i(Te,f,w),s(f,p),s(p,j),s(f,k),s(f,v),s(v,E),s(f,S),s(f,F),s(F,V),s(f,L),s(f,M),s(M,O),s(f,I),s(f,q),s(q,W),s(f,Q),s(f,X),s(X,U),s(f,R),s(f,H),s(H,c),s(f,T),s(f,J),s(J,fe),s(f,Z),s(f,K),s(K,$e),s(f,ie),s(f,he),s(he,ee),s(f,be),s(f,b),s(b,N),s(f,je),s(f,re),s(re,ye),s(f,ne),s(f,te),s(te,$),s(f,Y),s(f,pe),s(pe,ze),s(f,le),s(f,we),s(we,ve),s(f,ue),s(f,Ee),s(Ee,me),s(f,He),s(f,de),s(de,Be),s(f,Le),s(f,Ae),s(Ae,Ze),s(f,Ce),s(f,ge),s(ge,xe),s(f,_e),s(f,Ie),s(Ie,Fe),s(f,ss),s(f,Oe),s(Oe,ke),s(f,Me),s(f,ce),s(ce,y),s(f,B),s(f,Pe),s(Pe,Ue),s(f,Ne),s(f,oe),s(oe,Ve),s(f,Ye),s(f,Se),s(Se,ls)},d(Te){Te&&a(m),Te&&a(f)}}}function Il(ae){let m,f,p,j,k,v,E,S,F,V,L,M,O,I,q,W,Q,X,U,R,H,c,T,J,fe,Z,K,$e,ie,he,ee,be,b,N,je,re,ye,ne,te;return L=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"),f=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"),j=o("code"),k=t("transforms"),v=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(),x(L.$$.fragment),M=d(),O=o("p"),I=t("Then create a preprocessing function to apply the transforms and return the "),q=o("code"),W=t("pixel_values"),Q=t(" - the inputs to the model - of the image:"),X=d(),x(U.$$.fragment),R=d(),H=o("p"),c=t("To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),T=o("a"),J=t("with_transform"),fe=t(" method. The transforms are applied on the fly when you load an element of the dataset:"),Z=d(),x(K.$$.fragment),$e=d(),ie=o("p"),he=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"),je=t("DefaultDataCollator"),re=t(" does not apply additional preprocessing such as padding."),ye=d(),x(ne.$$.fragment),this.h()},l($){m=r($,"P",{});var Y=n(m);f=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);j=r(pe,"CODE",{});var ze=n(j);k=l(ze,"transforms"),ze.forEach(a),pe.forEach(a),v=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($),A(L.$$.fragment,$),M=g($),O=r($,"P",{});var we=n(O);I=l(we,"Then create a preprocessing function to apply the transforms and return the "),q=r(we,"CODE",{});var ve=n(q);W=l(ve,"pixel_values"),ve.forEach(a),Q=l(we," - the inputs to the model - of the image:"),we.forEach(a),X=g($),A(U.$$.fragment,$),R=g($),H=r($,"P",{});var ue=n(H);c=l(ue,"To apply the preprocessing function over the entire dataset, use \u{1F917} Datasets "),T=r(ue,"A",{href:!0,rel:!0});var Ee=n(T);J=l(Ee,"with_transform"),Ee.forEach(a),fe=l(ue," method. The transforms are applied on the fly when you load an element of the dataset:"),ue.forEach(a),Z=g($),A(K.$$.fragment,$),$e=g($),ie=r($,"P",{});var me=n(ie);he=l(me,"Now create a batch of examples using "),ee=r(me,"A",{href:!0});var He=n(ee);be=l(He,"DefaultDataCollator"),He.forEach(a),b=l(me,". Unlike other data collators in \u{1F917} Transformers, the "),N=r(me,"CODE",{});var de=n(N);je=l(de,"DefaultDataCollator"),de.forEach(a),re=l(me," does not apply additional preprocessing such as padding."),me.forEach(a),ye=g($),A(ne.$$.fragment,$),this.h()},h(){_(p,"href","https://pytorch.org/vision/stable/transforms.html"),_(p,"rel","nofollow"),_(T,"href","https://huggingface.co/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset.with_transform"),_(T,"rel","nofollow"),_(ee,"href","/docs/transformers/main/en/main_classes/data_collator#transformers.DefaultDataCollator")},m($,Y){i($,m,Y),s(m,f),s(m,p),s(p,j),s(j,k),s(m,v),i($,E,Y),i($,S,Y),s(S,F),i($,V,Y),C(L,$,Y),i($,M,Y),i($,O,Y),s(O,I),s(O,q),s(q,W),s(O,Q),i($,X,Y),C(U,$,Y),i($,R,Y),i($,H,Y),s(H,c),s(H,T),s(T,J),s(H,fe),i($,Z,Y),C(K,$,Y),i($,$e,Y),i($,ie,Y),s(ie,he),s(ie,ee),s(ee,be),s(ie,b),s(ie,N),s(N,je),s(ie,re),i($,ye,Y),C(ne,$,Y),te=!0},p:rt,i($){te||(P(L.$$.fragment,$),P(U.$$.fragment,$),P(K.$$.fragment,$),P(ne.$$.fragment,$),te=!0)},o($){D(L.$$.fragment,$),D(U.$$.fragment,$),D(K.$$.fragment,$),D(ne.$$.fragment,$),te=!1},d($){$&&a(m),$&&a(E),$&&a(S),$&&a(V),z(L,$),$&&a(M),$&&a(O),$&&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,f;return m=new _a({props:{$$slots:{default:[Il]},$$scope:{ctx:ae}}}),{c(){x(m.$$.fragment)},l(p){A(m.$$.fragment,p)},m(p,j){C(m,p,j),f=!0},p(p,j){const k={};j&2&&(k.$$scope={dirty:j,ctx:p}),m.$set(k)},i(p){f||(P(m.$$.fragment,p),f=!0)},o(p){D(m.$$.fragment,p),f=!1},d(p){z(m,p)}}}function Ml(ae){let m,f,p,j,k,v,E,S,F,V,L,M,O,I,q,W,Q,X,U,R,H,c,T,J,fe,Z,K,$e,ie,he,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"),f=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"),j=t("tf.image"),k=t(`or
any other library you prefer.`),v=d(),x(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."),L=d(),x(M.$$.fragment),O=d(),I=o("p"),q=t("Use \u{1F917} Datasets "),W=o("a"),Q=t("set_transform"),X=t(" to apply the transformations on the fly:"),U=d(),x(R.$$.fragment),H=d(),c=o("p"),T=t("As a final preprocessing step, create a batch of examples using "),J=o("code"),fe=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."),he=d(),x(ee.$$.fragment),this.h()},l(b){m=r(b,"P",{});var N=n(m);f=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 je=n(p);j=l(je,"tf.image"),je.forEach(a),k=l(N,`or
any other library you prefer.`),N.forEach(a),v=g(b),A(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),L=g(b),A(M.$$.fragment,b),O=g(b),I=r(b,"P",{});var ye=n(I);q=l(ye,"Use \u{1F917} Datasets "),W=r(ye,"A",{href:!0,rel:!0});var ne=n(W);Q=l(ne,"set_transform"),ne.forEach(a),X=l(ye," to apply the transformations on the fly:"),ye.forEach(a),U=g(b),A(R.$$.fragment,b),H=g(b),c=r(b,"P",{});var te=n(c);T=l(te,"As a final preprocessing step, create a batch of examples using "),J=r(te,"CODE",{});var $=n(J);fe=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),he=g(b),A(ee.$$.fragment,b),this.h()},h(){_(W,"href","https://huggingface.co/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset.set_transform"),_(W,"rel","nofollow")},m(b,N){i(b,m,N),s(m,f),s(m,p),s(p,j),s(m,k),i(b,v,N),C(E,b,N),i(b,S,N),i(b,F,N),s(F,V),i(b,L,N),C(M,b,N),i(b,O,N),i(b,I,N),s(I,q),s(I,W),s(W,Q),s(I,X),i(b,U,N),C(R,b,N),i(b,H,N),i(b,c,N),s(c,T),s(c,J),s(J,fe),s(c,Z),s(c,K),s(K,$e),s(c,ie),i(b,he,N),C(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(v),z(E,b),b&&a(S),b&&a(F),b&&a(L),z(M,b),b&&a(O),b&&a(I),b&&a(U),z(R,b),b&&a(H),b&&a(c),b&&a(he),z(ee,b)}}}function Nl(ae){let m,f;return m=new _a({props:{$$slots:{default:[Ml]},$$scope:{ctx:ae}}}),{c(){x(m.$$.fragment)},l(p){A(m.$$.fragment,p)},m(p,j){C(m,p,j),f=!0},p(p,j){const k={};j&2&&(k.$$scope={dirty:j,ctx:p}),m.$set(k)},i(p){f||(P(m.$$.fragment,p),f=!0)},o(p){D(m.$$.fragment,p),f=!1},d(p){z(m,p)}}}function Sl(ae){let m,f,p,j,k,v,E,S;return{c(){m=o("p"),f=t("If you aren\u2019t familiar with finetuning a model with the "),p=o("a"),j=t("Trainer"),k=t(", take a look at the basic tutorial "),v=o("a"),E=t("here"),S=t("!"),this.h()},l(F){m=r(F,"P",{});var V=n(m);f=l(V,"If you aren\u2019t familiar with finetuning a model with the "),p=r(V,"A",{href:!0});var L=n(p);j=l(L,"Trainer"),L.forEach(a),k=l(V,", take a look at the basic tutorial "),v=r(V,"A",{href:!0});var M=n(v);E=l(M,"here"),M.forEach(a),S=l(V,"!"),V.forEach(a),this.h()},h(){_(p,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer"),_(v,"href","../training#train-with-pytorch-trainer")},m(F,V){i(F,m,V),s(m,f),s(m,p),s(p,j),s(m,k),s(m,v),s(v,E),s(m,S)},d(F){F&&a(m)}}}function Ll(ae){let m,f,p,j,k,v,E,S,F,V,L,M,O,I,q,W,Q,X,U,R,H,c,T,J,fe,Z,K,$e,ie,he,ee,be,b,N,je,re,ye,ne,te,$,Y,pe,ze,le,we,ve,ue,Ee,me,He,de,Be,Le,Ae,Ze,Ce,ge,xe,_e,Ie,Fe,ss,Oe,ke,Me,ce;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()`}}),Me=new se({props:{code:"trainer.push_to_hub()",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>trainer.push_to_hub()'}}),{c(){x(m.$$.fragment),f=d(),p=o("p"),j=t("You\u2019re ready to start training your model now! Load ViT with "),k=o("a"),v=t("AutoModelForImageClassification"),E=t(". Specify the number of labels along with the number of expected labels, and the label mappings:"),S=d(),x(F.$$.fragment),V=d(),L=o("p"),M=t("At this point, only three steps remain:"),O=d(),I=o("ol"),q=o("li"),W=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"),c=t(" column. Without the "),T=o("code"),J=t("image"),fe=t(" column, you can\u2019t create "),Z=o("code"),K=t("pixel_values"),$e=t(". Set "),ie=o("code"),he=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 "),je=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"),ze=t("Pass the training arguments to "),le=o("a"),we=t("Trainer"),ve=t(" along with the model, dataset, tokenizer, data collator, and "),ue=o("code"),Ee=t("compute_metrics"),me=t(" function."),He=d(),de=o("li"),Be=t("Call "),Le=o("a"),Ae=t("train()"),Ze=t(" to finetune your model."),Ce=d(),x(ge.$$.fragment),xe=d(),_e=o("p"),Ie=t("Once training is completed, share your model to the Hub with the "),Fe=o("a"),ss=t("push_to_hub()"),Oe=t(" method so everyone can use your model:"),ke=d(),x(Me.$$.fragment),this.h()},l(y){A(m.$$.fragment,y),f=g(y),p=r(y,"P",{});var B=n(p);j=l(B,"You\u2019re ready to start training your model now! Load ViT with "),k=r(B,"A",{href:!0});var Pe=n(k);v=l(Pe,"AutoModelForImageClassification"),Pe.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(y),A(F.$$.fragment,y),V=g(y),L=r(y,"P",{});var Ue=n(L);M=l(Ue,"At this point, only three steps remain:"),Ue.forEach(a),O=g(y),I=r(y,"OL",{});var Ne=n(I);q=r(Ne,"LI",{});var oe=n(q);W=l(oe,"Define your training hyperparameters in "),Q=r(oe,"A",{href:!0});var Ve=n(Q);X=l(Ve,"TrainingArguments"),Ve.forEach(a),U=l(oe,". It is important you don\u2019t remove unused columns because this\u2019ll drop the "),R=r(oe,"CODE",{});var Ye=n(R);H=l(Ye,"image"),Ye.forEach(a),c=l(oe," column. Without the "),T=r(oe,"CODE",{});var Se=n(T);J=l(Se,"image"),Se.forEach(a),fe=l(oe," column, you can\u2019t create "),Z=r(oe,"CODE",{});var ls=n(Z);K=l(ls,"pixel_values"),ls.forEach(a),$e=l(oe,". Set "),ie=r(oe,"CODE",{});var Te=n(ie);he=l(Te,"remove_unused_columns=False"),Te.forEach(a),ee=l(oe," to prevent this behavior! The only other required parameter is "),be=r(oe,"CODE",{});var w=n(be);b=l(w,"output_dir"),w.forEach(a),N=l(oe," which specifies where to save your model. You\u2019ll push this model to the Hub by setting "),je=r(oe,"CODE",{});var Qe=n(je);re=l(Qe,"push_to_hub=True"),Qe.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 es=n(ne);te=l(es,"Trainer"),es.forEach(a),$=l(oe," will evaluate the accuracy and save the training checkpoint."),oe.forEach(a),Y=g(Ne),pe=r(Ne,"LI",{});var De=n(pe);ze=l(De,"Pass the training arguments to "),le=r(De,"A",{href:!0});var os=n(le);we=l(os,"Trainer"),os.forEach(a),ve=l(De," along with the model, dataset, tokenizer, data collator, and "),ue=r(De,"CODE",{});var Ge=n(ue);Ee=l(Ge,"compute_metrics"),Ge.forEach(a),me=l(De," function."),De.forEach(a),He=g(Ne),de=r(Ne,"LI",{});var Ke=n(de);Be=l(Ke,"Call "),Le=r(Ke,"A",{href:!0});var We=n(Le);Ae=l(We,"train()"),We.forEach(a),Ze=l(Ke," to finetune your model."),Ke.forEach(a),Ne.forEach(a),Ce=g(y),A(ge.$$.fragment,y),xe=g(y),_e=r(y,"P",{});var as=n(_e);Ie=l(as,"Once training is completed, share your model to the Hub with the "),Fe=r(as,"A",{href:!0});var ps=n(Fe);ss=l(ps,"push_to_hub()"),ps.forEach(a),Oe=l(as," method so everyone can use your model:"),as.forEach(a),ke=g(y),A(Me.$$.fragment,y),this.h()},h(){_(k,"href","/docs/transformers/main/en/model_doc/auto#transformers.AutoModelForImageClassification"),_(Q,"href","/docs/transformers/main/en/main_classes/trainer#transformers.TrainingArguments"),_(ne,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer"),_(le,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer"),_(Le,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer.train"),_(Fe,"href","/docs/transformers/main/en/main_classes/trainer#transformers.Trainer.push_to_hub")},m(y,B){C(m,y,B),i(y,f,B),i(y,p,B),s(p,j),s(p,k),s(k,v),s(p,E),i(y,S,B),C(F,y,B),i(y,V,B),i(y,L,B),s(L,M),i(y,O,B),i(y,I,B),s(I,q),s(q,W),s(q,Q),s(Q,X),s(q,U),s(q,R),s(R,H),s(q,c),s(q,T),s(T,J),s(q,fe),s(q,Z),s(Z,K),s(q,$e),s(q,ie),s(ie,he),s(q,ee),s(q,be),s(be,b),s(q,N),s(q,je),s(je,re),s(q,ye),s(q,ne),s(ne,te),s(q,$),s(I,Y),s(I,pe),s(pe,ze),s(pe,le),s(le,we),s(pe,ve),s(pe,ue),s(ue,Ee),s(pe,me),s(I,He),s(I,de),s(de,Be),s(de,Le),s(Le,Ae),s(de,Ze),i(y,Ce,B),C(ge,y,B),i(y,xe,B),i(y,_e,B),s(_e,Ie),s(_e,Fe),s(Fe,ss),s(_e,Oe),i(y,ke,B),C(Me,y,B),ce=!0},p(y,B){const Pe={};B&2&&(Pe.$$scope={dirty:B,ctx:y}),m.$set(Pe)},i(y){ce||(P(m.$$.fragment,y),P(F.$$.fragment,y),P(ge.$$.fragment,y),P(Me.$$.fragment,y),ce=!0)},o(y){D(m.$$.fragment,y),D(F.$$.fragment,y),D(ge.$$.fragment,y),D(Me.$$.fragment,y),ce=!1},d(y){z(m,y),y&&a(f),y&&a(p),y&&a(S),z(F,y),y&&a(V),y&&a(L),y&&a(O),y&&a(I),y&&a(Ce),z(ge,y),y&&a(xe),y&&a(_e),y&&a(ke),z(Me,y)}}}function Ol(ae){let m,f;return m=new _a({props:{$$slots:{default:[Ll]},$$scope:{ctx:ae}}}),{c(){x(m.$$.fragment)},l(p){A(m.$$.fragment,p)},m(p,j){C(m,p,j),f=!0},p(p,j){const k={};j&2&&(k.$$scope={dirty:j,ctx:p}),m.$set(k)},i(p){f||(P(m.$$.fragment,p),f=!0)},o(p){D(m.$$.fragment,p),f=!1},d(p){z(m,p)}}}function Rl(ae){let m,f,p,j,k;return{c(){m=o("p"),f=t("If you are unfamiliar with fine-tuning a model with Keras, check out the "),p=o("a"),j=t("basic tutorial"),k=t(" first!"),this.h()},l(v){m=r(v,"P",{});var E=n(m);f=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);j=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(v,E){i(v,m,E),s(m,f),s(m,p),s(p,j),s(m,k)},d(v){v&&a(m)}}}function Hl(ae){let m,f,p,j,k,v,E,S,F,V,L,M,O,I,q,W,Q,X,U,R,H,c,T,J,fe,Z,K,$e,ie,he,ee,be,b,N,je,re,ye,ne,te,$,Y,pe,ze,le,we,ve,ue,Ee,me,He,de,Be,Le,Ae,Ze,Ce,ge,xe,_e,Ie,Fe,ss,Oe,ke,Me,ce,y,B,Pe,Ue,Ne,oe,Ve,Ye,Se,ls,Te,w,Qe,es,De,os,Ge,Ke,We,as,ps,fs,Je,$s,rs,ns,hs;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>)`}}),Ce=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>)`}}),ke=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)`}}),De=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]`}}),Je=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(){x(m.$$.fragment),f=d(),p=o("p"),j=t("To fine-tune a model in TensorFlow, follow these steps:"),k=d(),v=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"),L=t("Instantiate a pre-treined model."),M=d(),O=o("li"),I=t("Convert a \u{1F917} Dataset to a "),q=o("code"),W=t("tf.data.Dataset"),Q=t("."),X=d(),U=o("li"),R=t("Compile your model."),H=d(),c=o("li"),T=t("Add callbacks and use the "),J=o("code"),fe=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."),he=d(),ee=o("p"),be=t("Start by defining the hyperparameters, optimizer and learning rate schedule:"),b=d(),x(N.$$.fragment),je=d(),re=o("p"),ye=t("Then, load ViT with "),ne=o("a"),te=t("TFAutoModelForImageClassification"),$=t(" along with the label mappings:"),Y=d(),x(pe.$$.fragment),ze=d(),le=o("p"),we=t("Convert your datasets to the "),ve=o("code"),ue=t("tf.data.Dataset"),Ee=t(" format using the "),me=o("a"),He=t("to_tf_dataset"),de=t(" and your "),Be=o("code"),Le=t("data_collator"),Ae=t(":"),Ze=d(),x(Ce.$$.fragment),ge=d(),xe=o("p"),_e=t("Configure the model for training with "),Ie=o("code"),Fe=t("compile()"),ss=t(":"),Oe=d(),x(ke.$$.fragment),Me=d(),ce=o("p"),y=t("To compute the accuracy from the predictions and push your model to the \u{1F917} Hub, use "),B=o("a"),Pe=t("Keras callbacks"),Ue=t(`.
Pass your `),Ne=o("code"),oe=t("compute_metrics"),Ve=t(" function to "),Ye=o("a"),Se=t("KerasMetricCallback"),ls=t(`,
and use the `),Te=o("a"),w=t("PushToHubCallback"),Qe=t(" to upload the model:"),es=d(),x(De.$$.fragment),os=d(),Ge=o("p"),Ke=t("Finally, you are ready to train your model! Call "),We=o("code"),as=t("fit()"),ps=t(` with your training and validation datasets, the number of epochs,
and your callbacks to fine-tune the model:`),fs=d(),x(Je.$$.fragment),$s=d(),rs=o("p"),ns=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){A(m.$$.fragment,h),f=g(h),p=r(h,"P",{});var G=n(p);j=l(G,"To fine-tune a model in TensorFlow, follow these steps:"),G.forEach(a),k=g(h),v=r(h,"OL",{});var qe=n(v);E=r(qe,"LI",{});var ms=n(E);S=l(ms,"Define the training hyperparameters, and set up an optimizer and a learning rate schedule."),ms.forEach(a),F=g(qe),V=r(qe,"LI",{});var vs=n(V);L=l(vs,"Instantiate a pre-treined model."),vs.forEach(a),M=g(qe),O=r(qe,"LI",{});var Xe=n(O);I=l(Xe,"Convert a \u{1F917} Dataset to a "),q=r(Xe,"CODE",{});var is=n(q);W=l(is,"tf.data.Dataset"),is.forEach(a),Q=l(Xe,"."),Xe.forEach(a),X=g(qe),U=r(qe,"LI",{});var bs=n(U);R=l(bs,"Compile your model."),bs.forEach(a),H=g(qe),c=r(qe,"LI",{});var ts=n(c);T=l(ts,"Add callbacks and use the "),J=r(ts,"CODE",{});var ks=n(J);fe=l(ks,"fit()"),ks.forEach(a),Z=l(ts," method to run the training."),ts.forEach(a),K=g(qe),$e=r(qe,"LI",{});var Cs=n($e);ie=l(Cs,"Upload your model to \u{1F917} Hub to share with the community."),Cs.forEach(a),qe.forEach(a),he=g(h),ee=r(h,"P",{});var na=n(ee);be=l(na,"Start by defining the hyperparameters, optimizer and learning rate schedule:"),na.forEach(a),b=g(h),A(N.$$.fragment,h),je=g(h),re=r(h,"P",{});var Es=n(re);ye=l(Es,"Then, load ViT with "),ne=r(Es,"A",{href:!0});var Ts=n(ne);te=l(Ts,"TFAutoModelForImageClassification"),Ts.forEach(a),$=l(Es," along with the label mappings:"),Es.forEach(a),Y=g(h),A(pe.$$.fragment,h),ze=g(h),le=r(h,"P",{});var us=n(le);we=l(us,"Convert your datasets to the "),ve=r(us,"CODE",{});var Ys=n(ve);ue=l(Ys,"tf.data.Dataset"),Ys.forEach(a),Ee=l(us," format using the "),me=r(us,"A",{href:!0,rel:!0});var ys=n(me);He=l(ys,"to_tf_dataset"),ys.forEach(a),de=l(us," and your "),Be=r(us,"CODE",{});var Gs=n(Be);Le=l(Gs,"data_collator"),Gs.forEach(a),Ae=l(us,":"),us.forEach(a),Ze=g(h),A(Ce.$$.fragment,h),ge=g(h),xe=r(h,"P",{});var cs=n(xe);_e=l(cs,"Configure the model for training with "),Ie=r(cs,"CODE",{});var Ks=n(Ie);Fe=l(Ks,"compile()"),Ks.forEach(a),ss=l(cs,":"),cs.forEach(a),Oe=g(h),A(ke.$$.fragment,h),Me=g(h),ce=r(h,"P",{});var Re=n(ce);y=l(Re,"To compute the accuracy from the predictions and push your model to the \u{1F917} Hub, use "),B=r(Re,"A",{href:!0});var Bs=n(B);Pe=l(Bs,"Keras callbacks"),Bs.forEach(a),Ue=l(Re,`.
Pass your `),Ne=r(Re,"CODE",{});var ds=n(Ne);oe=l(ds,"compute_metrics"),ds.forEach(a),Ve=l(Re," function to "),Ye=r(Re,"A",{href:!0});var js=n(Ye);Se=l(js,"KerasMetricCallback"),js.forEach(a),ls=l(Re,`,
and use the `),Te=r(Re,"A",{href:!0});var Ps=n(Te);w=l(Ps,"PushToHubCallback"),Ps.forEach(a),Qe=l(Re," to upload the model:"),Re.forEach(a),es=g(h),A(De.$$.fragment,h),os=g(h),Ge=r(h,"P",{});var gs=n(Ge);Ke=l(gs,"Finally, you are ready to train your model! Call "),We=r(gs,"CODE",{});var ia=n(We);as=l(ia,"fit()"),ia.forEach(a),ps=l(gs,` with your training and validation datasets, the number of epochs,
and your callbacks to fine-tune the model:`),gs.forEach(a),fs=g(h),A(Je.$$.fragment,h),$s=g(h),rs=r(h,"P",{});var Ds=n(rs);ns=l(Ds,"Congratulations! You have fine-tuned your model and shared it on the \u{1F917} Hub. You can now use it for inference!"),Ds.forEach(a),this.h()},h(){_(ne,"href","/docs/transformers/main/en/model_doc/auto#transformers.TFAutoModelForImageClassification"),_(me,"href","https://huggingface.co/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset.to_tf_dataset"),_(me,"rel","nofollow"),_(B,"href","./main_classes/keras_callbacks"),_(Ye,"href","./main_classes/keras_callbacks#transformers.KerasMetricCallback"),_(Te,"href","./main_classes/keras_callbacks#transformers.PushToHubCallback")},m(h,G){C(m,h,G),i(h,f,G),i(h,p,G),s(p,j),i(h,k,G),i(h,v,G),s(v,E),s(E,S),s(v,F),s(v,V),s(V,L),s(v,M),s(v,O),s(O,I),s(O,q),s(q,W),s(O,Q),s(v,X),s(v,U),s(U,R),s(v,H),s(v,c),s(c,T),s(c,J),s(J,fe),s(c,Z),s(v,K),s(v,$e),s($e,ie),i(h,he,G),i(h,ee,G),s(ee,be),i(h,b,G),C(N,h,G),i(h,je,G),i(h,re,G),s(re,ye),s(re,ne),s(ne,te),s(re,$),i(h,Y,G),C(pe,h,G),i(h,ze,G),i(h,le,G),s(le,we),s(le,ve),s(ve,ue),s(le,Ee),s(le,me),s(me,He),s(le,de),s(le,Be),s(Be,Le),s(le,Ae),i(h,Ze,G),C(Ce,h,G),i(h,ge,G),i(h,xe,G),s(xe,_e),s(xe,Ie),s(Ie,Fe),s(xe,ss),i(h,Oe,G),C(ke,h,G),i(h,Me,G),i(h,ce,G),s(ce,y),s(ce,B),s(B,Pe),s(ce,Ue),s(ce,Ne),s(Ne,oe),s(ce,Ve),s(ce,Ye),s(Ye,Se),s(ce,ls),s(ce,Te),s(Te,w),s(ce,Qe),i(h,es,G),C(De,h,G),i(h,os,G),i(h,Ge,G),s(Ge,Ke),s(Ge,We),s(We,as),s(Ge,ps),i(h,fs,G),C(Je,h,G),i(h,$s,G),i(h,rs,G),s(rs,ns),hs=!0},p(h,G){const qe={};G&2&&(qe.$$scope={dirty:G,ctx:h}),m.$set(qe)},i(h){hs||(P(m.$$.fragment,h),P(N.$$.fragment,h),P(pe.$$.fragment,h),P(Ce.$$.fragment,h),P(ke.$$.fragment,h),P(De.$$.fragment,h),P(Je.$$.fragment,h),hs=!0)},o(h){D(m.$$.fragment,h),D(N.$$.fragment,h),D(pe.$$.fragment,h),D(Ce.$$.fragment,h),D(ke.$$.fragment,h),D(De.$$.fragment,h),D(Je.$$.fragment,h),hs=!1},d(h){z(m,h),h&&a(f),h&&a(p),h&&a(k),h&&a(v),h&&a(he),h&&a(ee),h&&a(b),z(N,h),h&&a(je),h&&a(re),h&&a(Y),z(pe,h),h&&a(ze),h&&a(le),h&&a(Ze),z(Ce,h),h&&a(ge),h&&a(xe),h&&a(Oe),z(ke,h),h&&a(Me),h&&a(ce),h&&a(es),z(De,h),h&&a(os),h&&a(Ge),h&&a(fs),z(Je,h),h&&a($s),h&&a(rs)}}}function Vl(ae){let m,f;return m=new _a({props:{$$slots:{default:[Hl]},$$scope:{ctx:ae}}}),{c(){x(m.$$.fragment)},l(p){A(m.$$.fragment,p)},m(p,j){C(m,p,j),f=!0},p(p,j){const k={};j&2&&(k.$$scope={dirty:j,ctx:p}),m.$set(k)},i(p){f||(P(m.$$.fragment,p),f=!0)},o(p){D(m.$$.fragment,p),f=!1},d(p){z(m,p)}}}function Yl(ae){let m,f,p,j,k;return{c(){m=o("p"),f=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"),j=t("PyTorch notebook"),k=t("."),this.h()},l(v){m=r(v,"P",{});var E=n(m);f=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);j=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(v,E){i(v,m,E),s(m,f),s(m,p),s(p,j),s(m,k)},d(v){v&&a(m)}}}function Gl(ae){let m,f,p,j,k,v,E,S,F,V,L,M,O,I,q,W,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"),f=t("Load an image processor to preprocess the image and return the "),p=o("code"),j=t("input"),k=t(" as PyTorch tensors:"),v=d(),x(E.$$.fragment),S=d(),F=o("p"),V=t("Pass your inputs to the model and return the logits:"),L=d(),x(M.$$.fragment),O=d(),I=o("p"),q=t("Get the predicted label with the highest probability, and use the model\u2019s "),W=o("code"),Q=t("id2label"),X=t(" mapping to convert it to a label:"),U=d(),x(R.$$.fragment)},l(c){m=r(c,"P",{});var T=n(m);f=l(T,"Load an image processor to preprocess the image and return the "),p=r(T,"CODE",{});var J=n(p);j=l(J,"input"),J.forEach(a),k=l(T," as PyTorch tensors:"),T.forEach(a),v=g(c),A(E.$$.fragment,c),S=g(c),F=r(c,"P",{});var fe=n(F);V=l(fe,"Pass your inputs to the model and return the logits:"),fe.forEach(a),L=g(c),A(M.$$.fragment,c),O=g(c),I=r(c,"P",{});var Z=n(I);q=l(Z,"Get the predicted label with the highest probability, and use the model\u2019s "),W=r(Z,"CODE",{});var K=n(W);Q=l(K,"id2label"),K.forEach(a),X=l(Z," mapping to convert it to a label:"),Z.forEach(a),U=g(c),A(R.$$.fragment,c)},m(c,T){i(c,m,T),s(m,f),s(m,p),s(p,j),s(m,k),i(c,v,T),C(E,c,T),i(c,S,T),i(c,F,T),s(F,V),i(c,L,T),C(M,c,T),i(c,O,T),i(c,I,T),s(I,q),s(I,W),s(W,Q),s(I,X),i(c,U,T),C(R,c,T),H=!0},p:rt,i(c){H||(P(E.$$.fragment,c),P(M.$$.fragment,c),P(R.$$.fragment,c),H=!0)},o(c){D(E.$$.fragment,c),D(M.$$.fragment,c),D(R.$$.fragment,c),H=!1},d(c){c&&a(m),c&&a(v),z(E,c),c&&a(S),c&&a(F),c&&a(L),z(M,c),c&&a(O),c&&a(I),c&&a(U),z(R,c)}}}function Kl(ae){let m,f;return m=new _a({props:{$$slots:{default:[Gl]},$$scope:{ctx:ae}}}),{c(){x(m.$$.fragment)},l(p){A(m.$$.fragment,p)},m(p,j){C(m,p,j),f=!0},p(p,j){const k={};j&2&&(k.$$scope={dirty:j,ctx:p}),m.$set(k)},i(p){f||(P(m.$$.fragment,p),f=!0)},o(p){D(m.$$.fragment,p),f=!1},d(p){z(m,p)}}}function Bl(ae){let m,f,p,j,k,v,E,S,F,V,L,M,O,I,q,W,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"),f=t("Load an image processor to preprocess the image and return the "),p=o("code"),j=t("input"),k=t(" as TensorFlow tensors:"),v=d(),x(E.$$.fragment),S=d(),F=o("p"),V=t("Pass your inputs to the model and return the logits:"),L=d(),x(M.$$.fragment),O=d(),I=o("p"),q=t("Get the predicted label with the highest probability, and use the model\u2019s "),W=o("code"),Q=t("id2label"),X=t(" mapping to convert it to a label:"),U=d(),x(R.$$.fragment)},l(c){m=r(c,"P",{});var T=n(m);f=l(T,"Load an image processor to preprocess the image and return the "),p=r(T,"CODE",{});var J=n(p);j=l(J,"input"),J.forEach(a),k=l(T," as TensorFlow tensors:"),T.forEach(a),v=g(c),A(E.$$.fragment,c),S=g(c),F=r(c,"P",{});var fe=n(F);V=l(fe,"Pass your inputs to the model and return the logits:"),fe.forEach(a),L=g(c),A(M.$$.fragment,c),O=g(c),I=r(c,"P",{});var Z=n(I);q=l(Z,"Get the predicted label with the highest probability, and use the model\u2019s "),W=r(Z,"CODE",{});var K=n(W);Q=l(K,"id2label"),K.forEach(a),X=l(Z," mapping to convert it to a label:"),Z.forEach(a),U=g(c),A(R.$$.fragment,c)},m(c,T){i(c,m,T),s(m,f),s(m,p),s(p,j),s(m,k),i(c,v,T),C(E,c,T),i(c,S,T),i(c,F,T),s(F,V),i(c,L,T),C(M,c,T),i(c,O,T),i(c,I,T),s(I,q),s(I,W),s(W,Q),s(I,X),i(c,U,T),C(R,c,T),H=!0},p:rt,i(c){H||(P(E.$$.fragment,c),P(M.$$.fragment,c),P(R.$$.fragment,c),H=!0)},o(c){D(E.$$.fragment,c),D(M.$$.fragment,c),D(R.$$.fragment,c),H=!1},d(c){c&&a(m),c&&a(v),z(E,c),c&&a(S),c&&a(F),c&&a(L),z(M,c),c&&a(O),c&&a(I),c&&a(U),z(R,c)}}}function Ul(ae){let m,f;return m=new _a({props:{$$slots:{default:[Bl]},$$scope:{ctx:ae}}}),{c(){x(m.$$.fragment)},l(p){A(m.$$.fragment,p)},m(p,j){C(m,p,j),f=!0},p(p,j){const k={};j&2&&(k.$$scope={dirty:j,ctx:p}),m.$set(k)},i(p){f||(P(m.$$.fragment,p),f=!0)},o(p){D(m.$$.fragment,p),f=!1},d(p){z(m,p)}}}function Wl(ae){let m,f,p,j,k,v,E,S,F,V,L,M,O,I,q,W,Q,X,U,R,H,c,T,J,fe,Z,K,$e,ie,he,ee,be,b,N,je,re,ye,ne,te,$,Y,pe,ze,le,we,ve,ue,Ee,me,He,de,Be,Le,Ae,Ze,Ce,ge,xe,_e,Ie,Fe,ss,Oe,ke,Me,ce,y,B,Pe,Ue,Ne,oe,Ve,Ye,Se,ls,Te,w,Qe,es,De,os,Ge,Ke,We,as,ps,fs,Je,$s,rs,ns,hs,h,G,qe,ms,vs,Xe,is,bs,ts,ks,Cs,na,Es,Ts,us,Ys,ys,Gs,cs,Ks,Re,Bs,ds,js,Ps,gs,ia,Ds,nt,Ca,_s,it,Us,pt,mt,Ws,ct,ft,Js,ht,ut,Pa,Xs,Da,zs,dt,Zs,gt,_t,za,Qs,Ia,Is,$t,$a,bt,jt,Fa,qs,Fs,ba,ea,vt,ja,yt,Ma,Ms,Na,Ns,Sa,Ss,La,xs,Ls,va,sa,wt,ya,kt,Oa,pa,Et,Ra,ma,Tt,Ha,aa,Va,ta,ca,Nt,Ya,ws,qt,fa,xt,At,wa,Ct,Pt,Ga,la,Ka,Os,Dt,ka,zt,It,Ba,Rs,Ua,Hs,Wa;return v=new da({}),L=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"}]}}),O=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()`}}),me=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>)'}}),Ve=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>}`}}),ns=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`}}),ms=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>`}}),ts=new da({}),ys=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)`}}),cs=new ga({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[Fl]},$$scope:{ctx:ae}}}),Re=new ga({props:{pytorch:!1,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Nl]},$$scope:{ctx:ae}}}),gs=new da({}),Xs=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>)`}}),Qs=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)`}}),ea=new da({}),Ms=new ga({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[Ol]},$$scope:{ctx:ae}}}),Ns=new ga({props:{pytorch:!1,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Vl]},$$scope:{ctx:ae}}}),Ss=new ot({props:{$$slots:{default:[Yl]},$$scope:{ctx:ae}}}),sa=new da({}),aa=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>]`}}),la=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>}]`}}),Rs=new ga({props:{pytorch:!0,tensorflow:!1,jax:!1,$$slots:{pytorch:[Kl]},$$scope:{ctx:ae}}}),Hs=new ga({props:{pytorch:!1,tensorflow:!0,jax:!1,$$slots:{tensorflow:[Ul]},$$scope:{ctx:ae}}}),{c(){m=o("meta"),f=d(),p=o("h1"),j=o("a"),k=o("span"),x(v.$$.fragment),E=d(),S=o("span"),F=t("Image classification"),V=d(),x(L.$$.fragment),M=d(),x(O.$$.fragment),I=d(),q=o("p"),W=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"),c=o("li"),T=t("Fine-tune "),J=o("a"),fe=t("ViT"),Z=t(" on the "),K=o("a"),$e=t("Food-101"),ie=t(" dataset to classify a food item in an image."),he=d(),ee=o("li"),be=t("Use your fine-tuned model for inference."),b=d(),x(N.$$.fragment),je=d(),re=o("p"),ye=t("Before you begin, make sure you have all the necessary libraries installed:"),ne=d(),x(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:"),ze=d(),x(le.$$.fragment),we=d(),ve=o("h2"),ue=o("a"),Ee=o("span"),x(me.$$.fragment),He=d(),de=o("span"),Be=t("Load Food-101 dataset"),Le=d(),Ae=o("p"),Ze=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.`),Ce=d(),x(ge.$$.fragment),xe=d(),_e=o("p"),Ie=t("Split the dataset\u2019s "),Fe=o("code"),ss=t("train"),Oe=t(" split into a train and test set with the "),ke=o("a"),Me=t("train_test_split"),ce=t(" method:"),y=d(),x(B.$$.fragment),Pe=d(),Ue=o("p"),Ne=t("Then take a look at an example:"),oe=d(),x(Ve.$$.fragment),Ye=d(),Se=o("p"),ls=t("Each example in the dataset has two fields:"),Te=d(),w=o("ul"),Qe=o("li"),es=o("code"),De=t("image"),os=t(": a PIL image of the food item"),Ge=d(),Ke=o("li"),We=o("code"),as=t("label"),ps=t(": the label class of the food item"),fs=d(),Je=o("p"),$s=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:`),rs=d(),x(ns.$$.fragment),hs=d(),h=o("p"),G=t("Now you can convert the label id to a label name:"),qe=d(),x(ms.$$.fragment),vs=d(),Xe=o("h2"),is=o("a"),bs=o("span"),x(ts.$$.fragment),ks=d(),Cs=o("span"),na=t("Preprocess"),Es=d(),Ts=o("p"),us=t("The next step is to load a ViT image processor to process the image into a tensor:"),Ys=d(),x(ys.$$.fragment),Gs=d(),x(cs.$$.fragment),Ks=d(),x(Re.$$.fragment),Bs=d(),ds=o("h2"),js=o("a"),Ps=o("span"),x(gs.$$.fragment),ia=d(),Ds=o("span"),nt=t("Evaluate"),Ca=d(),_s=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} `),Us=o("a"),pt=t("Evaluate"),mt=t(` library. For this task, load
the `),Ws=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(),x(Xs.$$.fragment),Da=d(),zs=o("p"),dt=t("Then create a function that passes your predictions and labels to "),Zs=o("a"),gt=t("compute"),_t=t(" to calculate the accuracy:"),za=d(),x(Qs.$$.fragment),Ia=d(),Is=o("p"),$t=t("Your "),$a=o("code"),bt=t("compute_metrics"),jt=t(" function is ready to go now, and you\u2019ll return to it when you set up your training."),Fa=d(),qs=o("h2"),Fs=o("a"),ba=o("span"),x(ea.$$.fragment),vt=d(),ja=o("span"),yt=t("Train"),Ma=d(),x(Ms.$$.fragment),Na=d(),x(Ns.$$.fragment),Sa=d(),x(Ss.$$.fragment),La=d(),xs=o("h2"),Ls=o("a"),va=o("span"),x(sa.$$.fragment),wt=d(),ya=o("span"),kt=t("Inference"),Oa=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(),x(aa.$$.fragment),Va=d(),ta=o("div"),ca=o("img"),Ya=d(),ws=o("p"),qt=t("The simplest way to try out your finetuned model for inference is to use it in a "),fa=o("a"),xt=t("pipeline()"),At=t(". Instantiate a "),wa=o("code"),Ct=t("pipeline"),Pt=t(" for image classification with your model, and pass your image to it:"),Ga=d(),x(la.$$.fragment),Ka=d(),Os=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(),x(Rs.$$.fragment),Ua=d(),x(Hs.$$.fragment),this.h()},l(e){const u=xl('[data-svelte="svelte-1phssyn"]',document.head);m=r(u,"META",{name:!0,content:!0}),u.forEach(a),f=g(e),p=r(e,"H1",{class:!0});var oa=n(p);j=r(oa,"A",{id:!0,class:!0,href:!0});var Ea=n(j);k=r(Ea,"SPAN",{});var Ta=n(k);A(v.$$.fragment,Ta),Ta.forEach(a),Ea.forEach(a),E=g(oa),S=r(oa,"SPAN",{});var qa=n(S);F=l(qa,"Image classification"),qa.forEach(a),oa.forEach(a),V=g(e),A(L.$$.fragment,e),M=g(e),A(O.$$.fragment,e),I=g(e),q=r(e,"P",{});var xa=n(q);W=l(xa,`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.`),xa.forEach(a),Q=g(e),X=r(e,"P",{});var Aa=n(X);U=l(Aa,"This guide illustrates how to:"),Aa.forEach(a),R=g(e),H=r(e,"OL",{});var ra=n(H);c=r(ra,"LI",{});var As=n(c);T=l(As,"Fine-tune "),J=r(As,"A",{href:!0});var St=n(J);fe=l(St,"ViT"),St.forEach(a),Z=l(As," on the "),K=r(As,"A",{href:!0,rel:!0});var Lt=n(K);$e=l(Lt,"Food-101"),Lt.forEach(a),ie=l(As," dataset to classify a food item in an image."),As.forEach(a),he=g(ra),ee=r(ra,"LI",{});var Ot=n(ee);be=l(Ot,"Use your fine-tuned model for inference."),Ot.forEach(a),ra.forEach(a),b=g(e),A(N.$$.fragment,e),je=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),A(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),ze=g(e),A(le.$$.fragment,e),we=g(e),ve=r(e,"H2",{class:!0});var Ja=n(ve);ue=r(Ja,"A",{id:!0,class:!0,href:!0});var Vt=n(ue);Ee=r(Vt,"SPAN",{});var Yt=n(Ee);A(me.$$.fragment,Yt),Yt.forEach(a),Vt.forEach(a),He=g(Ja),de=r(Ja,"SPAN",{});var Gt=n(de);Be=l(Gt,"Load Food-101 dataset"),Gt.forEach(a),Ja.forEach(a),Le=g(e),Ae=r(e,"P",{});var Kt=n(Ae);Ze=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),Ce=g(e),A(ge.$$.fragment,e),xe=g(e),_e=r(e,"P",{});var ha=n(_e);Ie=l(ha,"Split the dataset\u2019s "),Fe=r(ha,"CODE",{});var Bt=n(Fe);ss=l(Bt,"train"),Bt.forEach(a),Oe=l(ha," split into a train and test set with the "),ke=r(ha,"A",{href:!0,rel:!0});var Ut=n(ke);Me=l(Ut,"train_test_split"),Ut.forEach(a),ce=l(ha," method:"),ha.forEach(a),y=g(e),A(B.$$.fragment,e),Pe=g(e),Ue=r(e,"P",{});var Wt=n(Ue);Ne=l(Wt,"Then take a look at an example:"),Wt.forEach(a),oe=g(e),A(Ve.$$.fragment,e),Ye=g(e),Se=r(e,"P",{});var Jt=n(Se);ls=l(Jt,"Each example in the dataset has two fields:"),Jt.forEach(a),Te=g(e),w=r(e,"UL",{});var Xa=n(w);Qe=r(Xa,"LI",{});var Ft=n(Qe);es=r(Ft,"CODE",{});var Xt=n(es);De=l(Xt,"image"),Xt.forEach(a),os=l(Ft,": a PIL image of the food item"),Ft.forEach(a),Ge=g(Xa),Ke=r(Xa,"LI",{});var Mt=n(Ke);We=r(Mt,"CODE",{});var Zt=n(We);as=l(Zt,"label"),Zt.forEach(a),ps=l(Mt,": the label class of the food item"),Mt.forEach(a),Xa.forEach(a),fs=g(e),Je=r(e,"P",{});var Qt=n(Je);$s=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),rs=g(e),A(ns.$$.fragment,e),hs=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),A(ms.$$.fragment,e),vs=g(e),Xe=r(e,"H2",{class:!0});var Za=n(Xe);is=r(Za,"A",{id:!0,class:!0,href:!0});var sl=n(is);bs=r(sl,"SPAN",{});var al=n(bs);A(ts.$$.fragment,al),al.forEach(a),sl.forEach(a),ks=g(Za),Cs=r(Za,"SPAN",{});var tl=n(Cs);na=l(tl,"Preprocess"),tl.forEach(a),Za.forEach(a),Es=g(e),Ts=r(e,"P",{});var ll=n(Ts);us=l(ll,"The next step is to load a ViT image processor to process the image into a tensor:"),ll.forEach(a),Ys=g(e),A(ys.$$.fragment,e),Gs=g(e),A(cs.$$.fragment,e),Ks=g(e),A(Re.$$.fragment,e),Bs=g(e),ds=r(e,"H2",{class:!0});var Qa=n(ds);js=r(Qa,"A",{id:!0,class:!0,href:!0});var ol=n(js);Ps=r(ol,"SPAN",{});var rl=n(Ps);A(gs.$$.fragment,rl),rl.forEach(a),ol.forEach(a),ia=g(Qa),Ds=r(Qa,"SPAN",{});var nl=n(Ds);nt=l(nl,"Evaluate"),nl.forEach(a),Qa.forEach(a),Ca=g(e),_s=r(e,"P",{});var Vs=n(_s);it=l(Vs,`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} `),Us=r(Vs,"A",{href:!0,rel:!0});var il=n(Us);pt=l(il,"Evaluate"),il.forEach(a),mt=l(Vs,` library. For this task, load
the `),Ws=r(Vs,"A",{href:!0,rel:!0});var pl=n(Ws);ct=l(pl,"accuracy"),pl.forEach(a),ft=l(Vs," metric (see the \u{1F917} Evaluate "),Js=r(Vs,"A",{href:!0,rel:!0});var ml=n(Js);ht=l(ml,"quick tour"),ml.forEach(a),ut=l(Vs," to learn more about how to load and compute a metric):"),Vs.forEach(a),Pa=g(e),A(Xs.$$.fragment,e),Da=g(e),zs=r(e,"P",{});var et=n(zs);dt=l(et,"Then create a function that passes your predictions and labels to "),Zs=r(et,"A",{href:!0,rel:!0});var cl=n(Zs);gt=l(cl,"compute"),cl.forEach(a),_t=l(et," to calculate the accuracy:"),et.forEach(a),za=g(e),A(Qs.$$.fragment,e),Ia=g(e),Is=r(e,"P",{});var st=n(Is);$t=l(st,"Your "),$a=r(st,"CODE",{});var fl=n($a);bt=l(fl,"compute_metrics"),fl.forEach(a),jt=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),qs=r(e,"H2",{class:!0});var at=n(qs);Fs=r(at,"A",{id:!0,class:!0,href:!0});var hl=n(Fs);ba=r(hl,"SPAN",{});var ul=n(ba);A(ea.$$.fragment,ul),ul.forEach(a),hl.forEach(a),vt=g(at),ja=r(at,"SPAN",{});var dl=n(ja);yt=l(dl,"Train"),dl.forEach(a),at.forEach(a),Ma=g(e),A(Ms.$$.fragment,e),Na=g(e),A(Ns.$$.fragment,e),Sa=g(e),A(Ss.$$.fragment,e),La=g(e),xs=r(e,"H2",{class:!0});var tt=n(xs);Ls=r(tt,"A",{id:!0,class:!0,href:!0});var gl=n(Ls);va=r(gl,"SPAN",{});var _l=n(va);A(sa.$$.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),Oa=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 jl=n(ma);Tt=l(jl,"Load an image you\u2019d like to run inference on:"),jl.forEach(a),Ha=g(e),A(aa.$$.fragment,e),Va=g(e),ta=r(e,"DIV",{class:!0});var vl=n(ta);ca=r(vl,"IMG",{src:!0,alt:!0}),vl.forEach(a),Ya=g(e),ws=r(e,"P",{});var ua=n(ws);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);xt=l(yl,"pipeline()"),yl.forEach(a),At=l(ua,". Instantiate a "),wa=r(ua,"CODE",{});var wl=n(wa);Ct=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),A(la.$$.fragment,e),Ka=g(e),Os=r(e,"P",{});var lt=n(Os);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),A(Rs.$$.fragment,e),Ua=g(e),A(Hs.$$.fragment,e),this.h()},h(){_(m,"name","hf:doc:metadata"),_(m,"content",JSON.stringify(Jl)),_(j,"id","image-classification"),_(j,"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"),_(j,"href","#image-classification"),_(p,"class","relative group"),_(J,"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"),_(ve,"class","relative group"),_(ke,"href","https://huggingface.co/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset.train_test_split"),_(ke,"rel","nofollow"),_(is,"id","preprocess"),_(is,"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"),_(is,"href","#preprocess"),_(Xe,"class","relative group"),_(js,"id","evaluate"),_(js,"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"),_(js,"href","#evaluate"),_(ds,"class","relative group"),_(Us,"href","https://huggingface.co/docs/evaluate/index"),_(Us,"rel","nofollow"),_(Ws,"href","https://huggingface.co/spaces/evaluate-metric/accuracy"),_(Ws,"rel","nofollow"),_(Js,"href","https://huggingface.co/docs/evaluate/a_quick_tour"),_(Js,"rel","nofollow"),_(Zs,"href","https://huggingface.co/docs/evaluate/main/en/package_reference/main_classes#evaluate.EvaluationModule.compute"),_(Zs,"rel","nofollow"),_(Fs,"id","train"),_(Fs,"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"),_(Fs,"href","#train"),_(qs,"class","relative group"),_(Ls,"id","inference"),_(Ls,"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"),_(Ls,"href","#inference"),_(xs,"class","relative group"),Al(ca.src,Nt="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png")||_(ca,"src",Nt),_(ca,"alt","image of beignets"),_(ta,"class","flex justify-center"),_(fa,"href","/docs/transformers/main/en/main_classes/pipelines#transformers.pipeline")},m(e,u){s(document.head,m),i(e,f,u),i(e,p,u),s(p,j),s(j,k),C(v,k,null),s(p,E),s(p,S),s(S,F),i(e,V,u),C(L,e,u),i(e,M,u),C(O,e,u),i(e,I,u),i(e,q,u),s(q,W),i(e,Q,u),i(e,X,u),s(X,U),i(e,R,u),i(e,H,u),s(H,c),s(c,T),s(c,J),s(J,fe),s(c,Z),s(c,K),s(K,$e),s(c,ie),s(H,he),s(H,ee),s(ee,be),i(e,b,u),C(N,e,u),i(e,je,u),i(e,re,u),s(re,ye),i(e,ne,u),C(te,e,u),i(e,$,u),i(e,Y,u),s(Y,pe),i(e,ze,u),C(le,e,u),i(e,we,u),i(e,ve,u),s(ve,ue),s(ue,Ee),C(me,Ee,null),s(ve,He),s(ve,de),s(de,Be),i(e,Le,u),i(e,Ae,u),s(Ae,Ze),i(e,Ce,u),C(ge,e,u),i(e,xe,u),i(e,_e,u),s(_e,Ie),s(_e,Fe),s(Fe,ss),s(_e,Oe),s(_e,ke),s(ke,Me),s(_e,ce),i(e,y,u),C(B,e,u),i(e,Pe,u),i(e,Ue,u),s(Ue,Ne),i(e,oe,u),C(Ve,e,u),i(e,Ye,u),i(e,Se,u),s(Se,ls),i(e,Te,u),i(e,w,u),s(w,Qe),s(Qe,es),s(es,De),s(Qe,os),s(w,Ge),s(w,Ke),s(Ke,We),s(We,as),s(Ke,ps),i(e,fs,u),i(e,Je,u),s(Je,$s),i(e,rs,u),C(ns,e,u),i(e,hs,u),i(e,h,u),s(h,G),i(e,qe,u),C(ms,e,u),i(e,vs,u),i(e,Xe,u),s(Xe,is),s(is,bs),C(ts,bs,null),s(Xe,ks),s(Xe,Cs),s(Cs,na),i(e,Es,u),i(e,Ts,u),s(Ts,us),i(e,Ys,u),C(ys,e,u),i(e,Gs,u),C(cs,e,u),i(e,Ks,u),C(Re,e,u),i(e,Bs,u),i(e,ds,u),s(ds,js),s(js,Ps),C(gs,Ps,null),s(ds,ia),s(ds,Ds),s(Ds,nt),i(e,Ca,u),i(e,_s,u),s(_s,it),s(_s,Us),s(Us,pt),s(_s,mt),s(_s,Ws),s(Ws,ct),s(_s,ft),s(_s,Js),s(Js,ht),s(_s,ut),i(e,Pa,u),C(Xs,e,u),i(e,Da,u),i(e,zs,u),s(zs,dt),s(zs,Zs),s(Zs,gt),s(zs,_t),i(e,za,u),C(Qs,e,u),i(e,Ia,u),i(e,Is,u),s(Is,$t),s(Is,$a),s($a,bt),s(Is,jt),i(e,Fa,u),i(e,qs,u),s(qs,Fs),s(Fs,ba),C(ea,ba,null),s(qs,vt),s(qs,ja),s(ja,yt),i(e,Ma,u),C(Ms,e,u),i(e,Na,u),C(Ns,e,u),i(e,Sa,u),C(Ss,e,u),i(e,La,u),i(e,xs,u),s(xs,Ls),s(Ls,va),C(sa,va,null),s(xs,wt),s(xs,ya),s(ya,kt),i(e,Oa,u),i(e,pa,u),s(pa,Et),i(e,Ra,u),i(e,ma,u),s(ma,Tt),i(e,Ha,u),C(aa,e,u),i(e,Va,u),i(e,ta,u),s(ta,ca),i(e,Ya,u),i(e,ws,u),s(ws,qt),s(ws,fa),s(fa,xt),s(ws,At),s(ws,wa),s(wa,Ct),s(ws,Pt),i(e,Ga,u),C(la,e,u),i(e,Ka,u),i(e,Os,u),s(Os,Dt),s(Os,ka),s(ka,zt),s(Os,It),i(e,Ba,u),C(Rs,e,u),i(e,Ua,u),C(Hs,e,u),Wa=!0},p(e,[u]){const oa={};u&2&&(oa.$$scope={dirty:u,ctx:e}),N.$set(oa);const Ea={};u&2&&(Ea.$$scope={dirty:u,ctx:e}),cs.$set(Ea);const Ta={};u&2&&(Ta.$$scope={dirty:u,ctx:e}),Re.$set(Ta);const qa={};u&2&&(qa.$$scope={dirty:u,ctx:e}),Ms.$set(qa);const xa={};u&2&&(xa.$$scope={dirty:u,ctx:e}),Ns.$set(xa);const Aa={};u&2&&(Aa.$$scope={dirty:u,ctx:e}),Ss.$set(Aa);const ra={};u&2&&(ra.$$scope={dirty:u,ctx:e}),Rs.$set(ra);const As={};u&2&&(As.$$scope={dirty:u,ctx:e}),Hs.$set(As)},i(e){Wa||(P(v.$$.fragment,e),P(L.$$.fragment,e),P(O.$$.fragment,e),P(N.$$.fragment,e),P(te.$$.fragment,e),P(le.$$.fragment,e),P(me.$$.fragment,e),P(ge.$$.fragment,e),P(B.$$.fragment,e),P(Ve.$$.fragment,e),P(ns.$$.fragment,e),P(ms.$$.fragment,e),P(ts.$$.fragment,e),P(ys.$$.fragment,e),P(cs.$$.fragment,e),P(Re.$$.fragment,e),P(gs.$$.fragment,e),P(Xs.$$.fragment,e),P(Qs.$$.fragment,e),P(ea.$$.fragment,e),P(Ms.$$.fragment,e),P(Ns.$$.fragment,e),P(Ss.$$.fragment,e),P(sa.$$.fragment,e),P(aa.$$.fragment,e),P(la.$$.fragment,e),P(Rs.$$.fragment,e),P(Hs.$$.fragment,e),Wa=!0)},o(e){D(v.$$.fragment,e),D(L.$$.fragment,e),D(O.$$.fragment,e),D(N.$$.fragment,e),D(te.$$.fragment,e),D(le.$$.fragment,e),D(me.$$.fragment,e),D(ge.$$.fragment,e),D(B.$$.fragment,e),D(Ve.$$.fragment,e),D(ns.$$.fragment,e),D(ms.$$.fragment,e),D(ts.$$.fragment,e),D(ys.$$.fragment,e),D(cs.$$.fragment,e),D(Re.$$.fragment,e),D(gs.$$.fragment,e),D(Xs.$$.fragment,e),D(Qs.$$.fragment,e),D(ea.$$.fragment,e),D(Ms.$$.fragment,e),D(Ns.$$.fragment,e),D(Ss.$$.fragment,e),D(sa.$$.fragment,e),D(aa.$$.fragment,e),D(la.$$.fragment,e),D(Rs.$$.fragment,e),D(Hs.$$.fragment,e),Wa=!1},d(e){a(m),e&&a(f),e&&a(p),z(v),e&&a(V),z(L,e),e&&a(M),z(O,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(je),e&&a(re),e&&a(ne),z(te,e),e&&a($),e&&a(Y),e&&a(ze),z(le,e),e&&a(we),e&&a(ve),z(me),e&&a(Le),e&&a(Ae),e&&a(Ce),z(ge,e),e&&a(xe),e&&a(_e),e&&a(y),z(B,e),e&&a(Pe),e&&a(Ue),e&&a(oe),z(Ve,e),e&&a(Ye),e&&a(Se),e&&a(Te),e&&a(w),e&&a(fs),e&&a(Je),e&&a(rs),z(ns,e),e&&a(hs),e&&a(h),e&&a(qe),z(ms,e),e&&a(vs),e&&a(Xe),z(ts),e&&a(Es),e&&a(Ts),e&&a(Ys),z(ys,e),e&&a(Gs),z(cs,e),e&&a(Ks),z(Re,e),e&&a(Bs),e&&a(ds),z(gs),e&&a(Ca),e&&a(_s),e&&a(Pa),z(Xs,e),e&&a(Da),e&&a(zs),e&&a(za),z(Qs,e),e&&a(Ia),e&&a(Is),e&&a(Fa),e&&a(qs),z(ea),e&&a(Ma),z(Ms,e),e&&a(Na),z(Ns,e),e&&a(Sa),z(Ss,e),e&&a(La),e&&a(xs),z(sa),e&&a(Oa),e&&a(pa),e&&a(Ra),e&&a(ma),e&&a(Ha),z(aa,e),e&&a(Va),e&&a(ta),e&&a(Ya),e&&a(ws),e&&a(Ga),z(la,e),e&&a(Ka),e&&a(Os),e&&a(Ba),z(Rs,e),e&&a(Ua),z(Hs,e)}}}const Jl={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 Xl(ae){return Cl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class oo extends El{constructor(m){super();Tl(this,m,Xl,Wl,ql,{})}}export{oo as default,Jl as metadata};
