import{S as zs,i as Fs,s as Gs,e as n,k as m,w as C,t as o,M as Ys,c as i,d as s,m as f,a as r,x as q,h as p,b as h,N as $s,G as e,g as l,y as D,q as L,o as T,B as N,v as Hs}from"../chunks/vendor-hf-doc-builder.js";import{T as Js}from"../chunks/Tip-hf-doc-builder.js";import{I as Vs}from"../chunks/IconCopyLink-hf-doc-builder.js";import{C as ta}from"../chunks/CodeBlock-hf-doc-builder.js";function Ks(ca){let c,j,u,g,$;return{c(){c=n("p"),j=o(`Now that you know how to process a dataset for image classification, learn
`),u=n("a"),g=o("how to train an image classification model"),$=o(`
and use it for inference.`),this.h()},l(d){c=i(d,"P",{});var _=r(c);j=p(_,`Now that you know how to process a dataset for image classification, learn
`),u=i(_,"A",{href:!0,rel:!0});var w=r(u);g=p(w,"how to train an image classification model"),w.forEach(s),$=p(_,`
and use it for inference.`),_.forEach(s),this.h()},h(){h(u,"href","https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification.ipynb"),h(u,"rel","nofollow")},m(d,_){l(d,c,_),e(c,j),e(c,u),e(u,g),e(c,$)},d(d){d&&s(c)}}}function Qs(ca){let c,j,u,g,$,d,_,w,Ra,ha,G,Sa,ua,y,Ua,la,za,Fa,na,Ga,Ya,da,B,ga,k,Ha,A,Ja,Va,_a,Y,Ka,ya,O,va,H,Qa,$a,v,J,ia,Wa,Xa,Za,V,oa,as,ss,es,K,pa,ts,ls,ba,Q,ns,ja,M,ra,bs,wa,x,is,ma,os,ps,ka,R,xa,W,rs,Ea,S,Ia,E,ms,X,fs,cs,Pa,U,Ca,I,hs,fa,us,ds,qa,z,Da,b,Z,js,gs,aa,ws,La,P,Ta;return d=new Vs({}),B=new ta({props:{code:"pip install -U albumentations opencv-python",highlighted:"pip install -U albumentations opencv-python"}}),O=new ta({props:{code:`from datasets import load_dataset

dataset = load_dataset("beans")
dataset["train"][10]`,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>dataset = load_dataset(<span class="hljs-string">&quot;beans&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>dataset[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">10</span>]
{<span class="hljs-string">&#x27;image&#x27;</span>: &lt;PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x500 at <span class="hljs-number">0x7F8D2F4D7A10</span>&gt;,
 <span class="hljs-string">&#x27;image_file_path&#x27;</span>: <span class="hljs-string">&#x27;/root/.cache/huggingface/datasets/downloads/extracted/b0a21163f78769a2cf11f58dfc767fb458fc7cea5c05dccc0144a2c0f0bc1292/train/angular_leaf_spot/angular_leaf_spot_train.204.jpg&#x27;</span>,
 <span class="hljs-string">&#x27;labels&#x27;</span>: <span class="hljs-number">0</span>}`}}),R=new ta({props:{code:`import cv2
import albumentations
import numpy as np

transform = albumentations.Compose([
    albumentations.RandomCrop(width=256, height=256),
    albumentations.HorizontalFlip(p=0.5),
    albumentations.RandomBrightnessContrast(p=0.2),
])`,highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> cv2
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> albumentations
<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>transform = albumentations.Compose([
<span class="hljs-meta">... </span>    albumentations.RandomCrop(width=<span class="hljs-number">256</span>, height=<span class="hljs-number">256</span>),
<span class="hljs-meta">... </span>    albumentations.HorizontalFlip(p=<span class="hljs-number">0.5</span>),
<span class="hljs-meta">... </span>    albumentations.RandomBrightnessContrast(p=<span class="hljs-number">0.2</span>),
<span class="hljs-meta">... </span>])`}}),S=new ta({props:{code:`def transforms(examples):
    examples["pixel_values"] = [
        transform(image=np.array(image))["image"] for image in 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>] = [
<span class="hljs-meta">... </span>        transform(image=np.array(image))[<span class="hljs-string">&quot;image&quot;</span>] <span class="hljs-keyword">for</span> image <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;image&quot;</span>]
<span class="hljs-meta">... </span>    ]
<span class="hljs-meta">... </span>
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> examples`}}),U=new ta({props:{code:"dataset.set_transform(transforms)",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>dataset.set_transform(transforms)'}}),z=new ta({props:{code:`import numpy as np
import matplotlib.pyplot as plt

img = dataset["train"][0]["pixel_values"]
plt.imshow(img)`,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> matplotlib.pyplot <span class="hljs-keyword">as</span> plt

<span class="hljs-meta">&gt;&gt;&gt; </span>img = dataset[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">0</span>][<span class="hljs-string">&quot;pixel_values&quot;</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>plt.imshow(img)`}}),P=new Js({props:{$$slots:{default:[Ks]},$$scope:{ctx:ca}}}),{c(){c=n("meta"),j=m(),u=n("h1"),g=n("a"),$=n("span"),C(d.$$.fragment),_=m(),w=n("span"),Ra=o("Image classification"),ha=m(),G=n("p"),Sa=o("Image classification datasets are used to train a model to classify an entire image. There are a wide variety of applications enabled by these datasets such as identifying endangered wildlife species or screening for disease in medical images. This guide will show you how to apply transformations to an image classification dataset."),ua=m(),y=n("p"),Ua=o("Before you start, make sure you have up-to-date versions of "),la=n("code"),za=o("albumentations"),Fa=o(" and "),na=n("code"),Ga=o("cv2"),Ya=o(" installed:"),da=m(),C(B.$$.fragment),ga=m(),k=n("p"),Ha=o("This guide uses the "),A=n("a"),Ja=o("Beans"),Va=o(" dataset for identifying the type of bean plant disease based on an image of its leaf."),_a=m(),Y=n("p"),Ka=o("Load the dataset and take a look at an example:"),ya=m(),C(O.$$.fragment),va=m(),H=n("p"),Qa=o("The dataset has three fields:"),$a=m(),v=n("ul"),J=n("li"),ia=n("code"),Wa=o("image"),Xa=o(": a PIL image object."),Za=m(),V=n("li"),oa=n("code"),as=o("image_file_path"),ss=o(": the path to the image file."),es=m(),K=n("li"),pa=n("code"),ts=o("labels"),ls=o(": the label or category of the image."),ba=m(),Q=n("p"),ns=o("Next, check out an image:"),ja=m(),M=n("div"),ra=n("img"),wa=m(),x=n("p"),is=o("Now apply some augmentations with "),ma=n("code"),os=o("albumentations"),ps=o(". You\u2019ll randomly crop the image, flip it horizontally, and adjust its brightness."),ka=m(),C(R.$$.fragment),xa=m(),W=n("p"),rs=o("Create a function to apply the transformation to the images:"),Ea=m(),C(S.$$.fragment),Ia=m(),E=n("p"),ms=o("Use the "),X=n("a"),fs=o("set_transform()"),cs=o(" function to apply the transformation on-the-fly to batches of the dataset to consume less disk space:"),Pa=m(),C(U.$$.fragment),Ca=m(),I=n("p"),hs=o("You can verify the transformation worked by indexing into the "),fa=n("code"),us=o("pixel_values"),ds=o(" of the first example:"),qa=m(),C(z.$$.fragment),Da=m(),b=n("div"),Z=n("img"),gs=m(),aa=n("img"),La=m(),C(P.$$.fragment),this.h()},l(a){const t=Ys('[data-svelte="svelte-1phssyn"]',document.head);c=i(t,"META",{name:!0,content:!0}),t.forEach(s),j=f(a),u=i(a,"H1",{class:!0});var F=r(u);g=i(F,"A",{id:!0,class:!0,href:!0});var ks=r(g);$=i(ks,"SPAN",{});var xs=r($);q(d.$$.fragment,xs),xs.forEach(s),ks.forEach(s),_=f(F),w=i(F,"SPAN",{});var Es=r(w);Ra=p(Es,"Image classification"),Es.forEach(s),F.forEach(s),ha=f(a),G=i(a,"P",{});var Is=r(G);Sa=p(Is,"Image classification datasets are used to train a model to classify an entire image. There are a wide variety of applications enabled by these datasets such as identifying endangered wildlife species or screening for disease in medical images. This guide will show you how to apply transformations to an image classification dataset."),Is.forEach(s),ua=f(a),y=i(a,"P",{});var sa=r(y);Ua=p(sa,"Before you start, make sure you have up-to-date versions of "),la=i(sa,"CODE",{});var Ps=r(la);za=p(Ps,"albumentations"),Ps.forEach(s),Fa=p(sa," and "),na=i(sa,"CODE",{});var Cs=r(na);Ga=p(Cs,"cv2"),Cs.forEach(s),Ya=p(sa," installed:"),sa.forEach(s),da=f(a),q(B.$$.fragment,a),ga=f(a),k=i(a,"P",{});var Na=r(k);Ha=p(Na,"This guide uses the "),A=i(Na,"A",{href:!0,rel:!0});var qs=r(A);Ja=p(qs,"Beans"),qs.forEach(s),Va=p(Na," dataset for identifying the type of bean plant disease based on an image of its leaf."),Na.forEach(s),_a=f(a),Y=i(a,"P",{});var Ds=r(Y);Ka=p(Ds,"Load the dataset and take a look at an example:"),Ds.forEach(s),ya=f(a),q(O.$$.fragment,a),va=f(a),H=i(a,"P",{});var Ls=r(H);Qa=p(Ls,"The dataset has three fields:"),Ls.forEach(s),$a=f(a),v=i(a,"UL",{});var ea=r(v);J=i(ea,"LI",{});var _s=r(J);ia=i(_s,"CODE",{});var Ts=r(ia);Wa=p(Ts,"image"),Ts.forEach(s),Xa=p(_s,": a PIL image object."),_s.forEach(s),Za=f(ea),V=i(ea,"LI",{});var ys=r(V);oa=i(ys,"CODE",{});var Ns=r(oa);as=p(Ns,"image_file_path"),Ns.forEach(s),ss=p(ys,": the path to the image file."),ys.forEach(s),es=f(ea),K=i(ea,"LI",{});var vs=r(K);pa=i(vs,"CODE",{});var Bs=r(pa);ts=p(Bs,"labels"),Bs.forEach(s),ls=p(vs,": the label or category of the image."),vs.forEach(s),ea.forEach(s),ba=f(a),Q=i(a,"P",{});var As=r(Q);ns=p(As,"Next, check out an image:"),As.forEach(s),ja=f(a),M=i(a,"DIV",{class:!0});var Os=r(M);ra=i(Os,"IMG",{src:!0}),Os.forEach(s),wa=f(a),x=i(a,"P",{});var Ba=r(x);is=p(Ba,"Now apply some augmentations with "),ma=i(Ba,"CODE",{});var Ms=r(ma);os=p(Ms,"albumentations"),Ms.forEach(s),ps=p(Ba,". You\u2019ll randomly crop the image, flip it horizontally, and adjust its brightness."),Ba.forEach(s),ka=f(a),q(R.$$.fragment,a),xa=f(a),W=i(a,"P",{});var Rs=r(W);rs=p(Rs,"Create a function to apply the transformation to the images:"),Rs.forEach(s),Ea=f(a),q(S.$$.fragment,a),Ia=f(a),E=i(a,"P",{});var Aa=r(E);ms=p(Aa,"Use the "),X=i(Aa,"A",{href:!0});var Ss=r(X);fs=p(Ss,"set_transform()"),Ss.forEach(s),cs=p(Aa," function to apply the transformation on-the-fly to batches of the dataset to consume less disk space:"),Aa.forEach(s),Pa=f(a),q(U.$$.fragment,a),Ca=f(a),I=i(a,"P",{});var Oa=r(I);hs=p(Oa,"You can verify the transformation worked by indexing into the "),fa=i(Oa,"CODE",{});var Us=r(fa);us=p(Us,"pixel_values"),Us.forEach(s),ds=p(Oa," of the first example:"),Oa.forEach(s),qa=f(a),q(z.$$.fragment,a),Da=f(a),b=i(a,"DIV",{class:!0});var Ma=r(b);Z=i(Ma,"IMG",{class:!0,src:!0}),gs=f(Ma),aa=i(Ma,"IMG",{class:!0,src:!0}),Ma.forEach(s),La=f(a),q(P.$$.fragment,a),this.h()},h(){h(c,"name","hf:doc:metadata"),h(c,"content",JSON.stringify(Ws)),h(g,"id","image-classification"),h(g,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(g,"href","#image-classification"),h(u,"class","relative group"),h(A,"href","https://huggingface.co/datasets/beans"),h(A,"rel","nofollow"),$s(ra.src,bs="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/datasets/img_clf.png")||h(ra,"src",bs),h(M,"class","flex justify-center"),h(X,"href","/docs/datasets/v2.11.0/en/package_reference/main_classes#datasets.Dataset.set_transform"),h(Z,"class","block dark:hidden"),$s(Z.src,js="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/datasets/img_clf_aug.png")||h(Z,"src",js),h(aa,"class","hidden dark:block"),$s(aa.src,ws="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/datasets/img_clf_aug.png")||h(aa,"src",ws),h(b,"class","flex justify-center")},m(a,t){e(document.head,c),l(a,j,t),l(a,u,t),e(u,g),e(g,$),D(d,$,null),e(u,_),e(u,w),e(w,Ra),l(a,ha,t),l(a,G,t),e(G,Sa),l(a,ua,t),l(a,y,t),e(y,Ua),e(y,la),e(la,za),e(y,Fa),e(y,na),e(na,Ga),e(y,Ya),l(a,da,t),D(B,a,t),l(a,ga,t),l(a,k,t),e(k,Ha),e(k,A),e(A,Ja),e(k,Va),l(a,_a,t),l(a,Y,t),e(Y,Ka),l(a,ya,t),D(O,a,t),l(a,va,t),l(a,H,t),e(H,Qa),l(a,$a,t),l(a,v,t),e(v,J),e(J,ia),e(ia,Wa),e(J,Xa),e(v,Za),e(v,V),e(V,oa),e(oa,as),e(V,ss),e(v,es),e(v,K),e(K,pa),e(pa,ts),e(K,ls),l(a,ba,t),l(a,Q,t),e(Q,ns),l(a,ja,t),l(a,M,t),e(M,ra),l(a,wa,t),l(a,x,t),e(x,is),e(x,ma),e(ma,os),e(x,ps),l(a,ka,t),D(R,a,t),l(a,xa,t),l(a,W,t),e(W,rs),l(a,Ea,t),D(S,a,t),l(a,Ia,t),l(a,E,t),e(E,ms),e(E,X),e(X,fs),e(E,cs),l(a,Pa,t),D(U,a,t),l(a,Ca,t),l(a,I,t),e(I,hs),e(I,fa),e(fa,us),e(I,ds),l(a,qa,t),D(z,a,t),l(a,Da,t),l(a,b,t),e(b,Z),e(b,gs),e(b,aa),l(a,La,t),D(P,a,t),Ta=!0},p(a,[t]){const F={};t&2&&(F.$$scope={dirty:t,ctx:a}),P.$set(F)},i(a){Ta||(L(d.$$.fragment,a),L(B.$$.fragment,a),L(O.$$.fragment,a),L(R.$$.fragment,a),L(S.$$.fragment,a),L(U.$$.fragment,a),L(z.$$.fragment,a),L(P.$$.fragment,a),Ta=!0)},o(a){T(d.$$.fragment,a),T(B.$$.fragment,a),T(O.$$.fragment,a),T(R.$$.fragment,a),T(S.$$.fragment,a),T(U.$$.fragment,a),T(z.$$.fragment,a),T(P.$$.fragment,a),Ta=!1},d(a){s(c),a&&s(j),a&&s(u),N(d),a&&s(ha),a&&s(G),a&&s(ua),a&&s(y),a&&s(da),N(B,a),a&&s(ga),a&&s(k),a&&s(_a),a&&s(Y),a&&s(ya),N(O,a),a&&s(va),a&&s(H),a&&s($a),a&&s(v),a&&s(ba),a&&s(Q),a&&s(ja),a&&s(M),a&&s(wa),a&&s(x),a&&s(ka),N(R,a),a&&s(xa),a&&s(W),a&&s(Ea),N(S,a),a&&s(Ia),a&&s(E),a&&s(Pa),N(U,a),a&&s(Ca),a&&s(I),a&&s(qa),N(z,a),a&&s(Da),a&&s(b),a&&s(La),N(P,a)}}}const Ws={local:"image-classification",title:"Image classification"};function Xs(ca){return Hs(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class te extends zs{constructor(c){super();Fs(this,c,Xs,Qs,Gs,{})}}export{te as default,Ws as metadata};
