import{s as Ee,o as Le,n as Fe}from"../chunks/scheduler.d6170356.js";import{S as ze,i as Qe,g as o,s as a,r,A as Ye,h as m,f as l,c as n,j as We,u as p,x as g,k as Xe,y as Ne,a as s,v as c,d,t as M,w as h}from"../chunks/index.fcd4cc08.js";import{T as Ve}from"../chunks/Tip.b09c67cf.js";import{C as $e}from"../chunks/CodeBlock.7b16bdef.js";import{H as u,E as Ae}from"../chunks/EditOnGithub.da2b595c.js";function Se(S){let i,y="As of today (2024-03-15) mlx does not support <code>group</code> param for nn.Conv2d. Therefore, architectures such as <code>resnext</code>, <code>regnet</code> or <code>efficientnet</code> are not yet supported in <code>mlx-image</code>.";return{c(){i=o("p"),i.innerHTML=y},l(f){i=m(f,"P",{"data-svelte-h":!0}),g(i)!=="svelte-1ayt94f"&&(i.innerHTML=y)},m(f,V){s(f,i,V)},p:Fe,d(f){f&&l(i)}}}function qe(S){let i,y,f,V,T,q,J,je='<a href="https://github.com/riccardomusmeci/mlx-image" rel="nofollow"><code>mlx-image</code></a> is an image models library developed by <a href="https://github.com/riccardomusmeci" rel="nofollow">Riccardo Musmeci</a> built on Apple <a href="https://github.com/ml-explore/mlx" rel="nofollow">MLX</a>. It tries to replicate the great <a href="https://github.com/huggingface/pytorch-image-models" rel="nofollow">timm</a>, but for MLX models.',P,b,D,$,Ue=`You can find <code>mlx-image</code> models by filtering using the <code>mlx-image</code> library name, like in <a href="https://huggingface.co/models?library=mlx-image&amp;sort=trending" rel="nofollow">this query</a>.
There’s also an open <a href="https://huggingface.co/mlx-vision" rel="nofollow">mlx-vision</a> community for contributors converting and publishing weights for MLX format.`,K,j,O,U,ee,I,te,x,Ie='Model weights are available on the <a href="https://huggingface.co/mlx-vision" rel="nofollow"><code>mlx-vision</code></a> community on HuggingFace.',le,C,xe="To load a model with pre-trained weights:",se,v,ae,Z,Ce="To list all available models:",ne,G,ie,w,oe,k,me,_,ve='Go to <a href="https://github.com/riccardomusmeci/mlx-image/blob/main/results/results-imagenet-1k.csv" rel="nofollow">results-imagenet-1k.csv</a> to check every model converted to <code>mlx-image</code> and its performance on ImageNet-1K with different settings.',re,H,Ze="<p><strong>TL;DR</strong> performance is comparable to the original models from PyTorch implementations.</p>",pe,R,ce,B,Ge="<code>mlx-image</code> tries to be as close as possible to PyTorch:",de,W,ke="<li><p><code>DataLoader</code> -&gt; you can define your own <code>collate_fn</code> and also use <code>num_workers</code> to speed up data loading</p></li> <li><p><code>Dataset</code> -&gt; <code>mlx-image</code> already supports <code>LabelFolderDataset</code> (the good and old PyTorch <code>ImageFolder</code>) and <code>FolderDataset</code> (a generic folder with images in it)</p></li> <li><p><code>ModelCheckpoint</code> -&gt; keeps track of the best model and saves it to disk (similar to PyTorchLightning). It also suggests early stopping</p></li>",Me,X,he,E,_e="Training is similar to PyTorch. Here’s an example of how to train a model:",ge,L,fe,F,ue,z,He='<li><a href="https://github.com/riccardomusmeci/mlx-image" rel="nofollow">mlx-image repository</a></li> <li><a href="https://huggingface.co/mlx-vision" rel="nofollow">mlx-vision community</a></li>',we,Q,ye,Y,Re="If you have any questions, please email <code>riccardomusmeci92@gmail.com</code>.",Te,N,Je,A,be;return T=new u({props:{title:"Using mlx-image at Hugging Face",local:"using-mlx-image-at-hugging-face",headingTag:"h1"}}),b=new u({props:{title:"Exploring mlx-image on the Hub",local:"exploring-mlx-image-on-the-hub",headingTag:"h2"}}),j=new u({props:{title:"Installation",local:"installation",headingTag:"h2"}}),U=new $e({props:{code:"cGlwJTIwaW5zdGFsbCUyMG1seC1pbWFnZQ==",highlighted:"pip install mlx-image",wrap:!1}}),I=new u({props:{title:"Models",local:"models",headingTag:"h2"}}),v=new $e({props:{code:"ZnJvbSUyMG1seGltLm1vZGVsJTIwaW1wb3J0JTIwY3JlYXRlX21vZGVsJTBBJTBBJTIzJTIwbG9hZGluZyUyMHdlaWdodHMlMjBmcm9tJTIwSHVnZ2luZ0ZhY2UlMjAoaHR0cHMlM0ElMkYlMkZodWdnaW5nZmFjZS5jbyUyRm1seC12aXNpb24lMkZyZXNuZXQxOC1tbHhpbSklMEFtb2RlbCUyMCUzRCUyMGNyZWF0ZV9tb2RlbCglMjJyZXNuZXQxOCUyMiklMjAlMjMlMjBwcmV0cmFpbmVkJTIwd2VpZ2h0cyUyMGxvYWRlZCUyMGZyb20lMjBIRiUwQSUwQSUyMyUyMGxvYWRpbmclMjB3ZWlnaHRzJTIwZnJvbSUyMGxvY2FsJTIwZmlsZSUwQW1vZGVsJTIwJTNEJTIwY3JlYXRlX21vZGVsKCUyMnJlc25ldDE4JTIyJTJDJTIwd2VpZ2h0cyUzRCUyMnBhdGglMkZ0byUyRnJlc25ldDE4JTJGbW9kZWwuc2FmZXRlbnNvcnMlMjIp",highlighted:`<span class="hljs-keyword">from</span> mlxim.model <span class="hljs-keyword">import</span> create_model

<span class="hljs-comment"># loading weights from HuggingFace (https://huggingface.co/mlx-vision/resnet18-mlxim)</span>
model = create_model(<span class="hljs-string">&quot;resnet18&quot;</span>) <span class="hljs-comment"># pretrained weights loaded from HF</span>

<span class="hljs-comment"># loading weights from local file</span>
model = create_model(<span class="hljs-string">&quot;resnet18&quot;</span>, weights=<span class="hljs-string">&quot;path/to/resnet18/model.safetensors&quot;</span>)`,wrap:!1}}),G=new $e({props:{code:"ZnJvbSUyMG1seGltLm1vZGVsJTIwaW1wb3J0JTIwbGlzdF9tb2RlbHMlMEFsaXN0X21vZGVscygp",highlighted:`<span class="hljs-keyword">from</span> mlxim.model <span class="hljs-keyword">import</span> list_models
list_models()`,wrap:!1}}),w=new Ve({props:{warning:!0,$$slots:{default:[Se]},$$scope:{ctx:S}}}),k=new u({props:{title:"ImageNet-1K Results",local:"imagenet-1k-results",headingTag:"h2"}}),R=new u({props:{title:"Similarity to PyTorch and other familiar tools",local:"similarity-to-pytorch-and-other-familiar-tools",headingTag:"h2"}}),X=new u({props:{title:"Training",local:"training",headingTag:"h2"}}),L=new $e({props:{code:"aW1wb3J0JTIwbWx4Lm5uJTIwYXMlMjBubiUwQWltcG9ydCUyMG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bChpbnB1dHMpJTBBJTIwJTIwJTIwJTIwbG9zcyUyMCUzRCUyMG14Lm1lYW4obm4ubG9zc2VzLmNyb3NzX2VudHJvcHkobG9naXRzJTJDJTIwdGFyZ2V0KSklMEElMjAlMjAlMjAlMjByZXR1cm4lMjBsb3NzJTBBJTBBbW9kZWwudHJhaW4oKSUwQWZvciUyMGVwb2NoJTIwaW4lMjByYW5nZSgxMCklM0ElMEElMjAlMjAlMjAlMjBmb3IlMjBiYXRjaCUyMGluJTIwdHJhaW5fbG9hZGVyJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIweCUyQyUyMHRhcmdldCUyMCUzRCUyMGJhdGNoJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdHJhaW5fc3RlcF9mbiUyMCUzRCUyMG5uLnZhbHVlX2FuZF9ncmFkKG1vZGVsJTJDJTIwdHJhaW5fc3RlcCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb3NzJTJDJTIwZ3JhZHMlMjAlM0QlMjB0cmFpbl9zdGVwX2ZuKHglMkMlMjB0YXJnZXQpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwb3B0aW1pemVyLnVwZGF0ZShtb2RlbCUyQyUyMGdyYWRzKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG14LmV2YWwobW9kZWwuc3RhdGUlMkMlMjBvcHRpbWl6ZXIuc3RhdGUp",highlighted:`<span class="hljs-keyword">import</span> mlx.nn <span class="hljs-keyword">as</span> nn
<span class="hljs-keyword">import</span> mlx.optimizers <span class="hljs-keyword">as</span> optim
<span class="hljs-keyword">from</span> mlxim.model <span class="hljs-keyword">import</span> create_model
<span class="hljs-keyword">from</span> mlxim.data <span class="hljs-keyword">import</span> LabelFolderDataset, DataLoader

train_dataset = LabelFolderDataset(
    root_dir=<span class="hljs-string">&quot;path/to/train&quot;</span>,
    class_map={<span class="hljs-number">0</span>: <span class="hljs-string">&quot;class_0&quot;</span>, <span class="hljs-number">1</span>: <span class="hljs-string">&quot;class_1&quot;</span>, <span class="hljs-number">2</span>: [<span class="hljs-string">&quot;class_2&quot;</span>, <span class="hljs-string">&quot;class_3&quot;</span>]}
)
train_loader = DataLoader(
    dataset=train_dataset,
    batch_size=<span class="hljs-number">32</span>,
    shuffle=<span class="hljs-literal">True</span>,
    num_workers=<span class="hljs-number">4</span>
)
model = create_model(<span class="hljs-string">&quot;resnet18&quot;</span>) <span class="hljs-comment"># pretrained weights loaded from HF</span>
optimizer = optim.Adam(learning_rate=<span class="hljs-number">1e-3</span>)

<span class="hljs-keyword">def</span> <span class="hljs-title function_">train_step</span>(<span class="hljs-params">model, inputs, targets</span>):
    logits = model(inputs)
    loss = mx.mean(nn.losses.cross_entropy(logits, target))
    <span class="hljs-keyword">return</span> loss

model.train()
<span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>):
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_loader:
        x, target = batch
        train_step_fn = nn.value_and_grad(model, train_step)
        loss, grads = train_step_fn(x, target)
        optimizer.update(model, grads)
        mx.<span class="hljs-built_in">eval</span>(model.state, optimizer.state)`,wrap:!1}}),F=new u({props:{title:"Additional Resources",local:"additional-resources",headingTag:"h2"}}),Q=new u({props:{title:"Contact",local:"contact",headingTag:"h2"}}),N=new Ae({props:{source:"https://github.com/huggingface/hub-docs/blob/main/docs/hub/mlx-image.md"}}),{c(){i=o("meta"),y=a(),f=o("p"),V=a(),r(T.$$.fragment),q=a(),J=o("p"),J.innerHTML=je,P=a(),r(b.$$.fragment),D=a(),$=o("p"),$.innerHTML=Ue,K=a(),r(j.$$.fragment),O=a(),r(U.$$.fragment),ee=a(),r(I.$$.fragment),te=a(),x=o("p"),x.innerHTML=Ie,le=a(),C=o("p"),C.textContent=xe,se=a(),r(v.$$.fragment),ae=a(),Z=o("p"),Z.textContent=Ce,ne=a(),r(G.$$.fragment),ie=a(),r(w.$$.fragment),oe=a(),r(k.$$.fragment),me=a(),_=o("p"),_.innerHTML=ve,re=a(),H=o("blockquote"),H.innerHTML=Ze,pe=a(),r(R.$$.fragment),ce=a(),B=o("p"),B.innerHTML=Ge,de=a(),W=o("ul"),W.innerHTML=ke,Me=a(),r(X.$$.fragment),he=a(),E=o("p"),E.textContent=_e,ge=a(),r(L.$$.fragment),fe=a(),r(F.$$.fragment),ue=a(),z=o("ul"),z.innerHTML=He,we=a(),r(Q.$$.fragment),ye=a(),Y=o("p"),Y.innerHTML=Re,Te=a(),r(N.$$.fragment),Je=a(),A=o("p"),this.h()},l(e){const t=Ye("svelte-u9bgzb",document.head);i=m(t,"META",{name:!0,content:!0}),t.forEach(l),y=n(e),f=m(e,"P",{}),We(f).forEach(l),V=n(e),p(T.$$.fragment,e),q=n(e),J=m(e,"P",{"data-svelte-h":!0}),g(J)!=="svelte-1c0kah6"&&(J.innerHTML=je),P=n(e),p(b.$$.fragment,e),D=n(e),$=m(e,"P",{"data-svelte-h":!0}),g($)!=="svelte-15ir2pb"&&($.innerHTML=Ue),K=n(e),p(j.$$.fragment,e),O=n(e),p(U.$$.fragment,e),ee=n(e),p(I.$$.fragment,e),te=n(e),x=m(e,"P",{"data-svelte-h":!0}),g(x)!=="svelte-13m84xx"&&(x.innerHTML=Ie),le=n(e),C=m(e,"P",{"data-svelte-h":!0}),g(C)!=="svelte-rlp6qh"&&(C.textContent=xe),se=n(e),p(v.$$.fragment,e),ae=n(e),Z=m(e,"P",{"data-svelte-h":!0}),g(Z)!=="svelte-1xjx8g7"&&(Z.textContent=Ce),ne=n(e),p(G.$$.fragment,e),ie=n(e),p(w.$$.fragment,e),oe=n(e),p(k.$$.fragment,e),me=n(e),_=m(e,"P",{"data-svelte-h":!0}),g(_)!=="svelte-kcleo1"&&(_.innerHTML=ve),re=n(e),H=m(e,"BLOCKQUOTE",{"data-svelte-h":!0}),g(H)!=="svelte-1xz4blu"&&(H.innerHTML=Ze),pe=n(e),p(R.$$.fragment,e),ce=n(e),B=m(e,"P",{"data-svelte-h":!0}),g(B)!=="svelte-11sjtis"&&(B.innerHTML=Ge),de=n(e),W=m(e,"UL",{"data-svelte-h":!0}),g(W)!=="svelte-jcjia8"&&(W.innerHTML=ke),Me=n(e),p(X.$$.fragment,e),he=n(e),E=m(e,"P",{"data-svelte-h":!0}),g(E)!=="svelte-1cvxtzq"&&(E.textContent=_e),ge=n(e),p(L.$$.fragment,e),fe=n(e),p(F.$$.fragment,e),ue=n(e),z=m(e,"UL",{"data-svelte-h":!0}),g(z)!=="svelte-k2vhlo"&&(z.innerHTML=He),we=n(e),p(Q.$$.fragment,e),ye=n(e),Y=m(e,"P",{"data-svelte-h":!0}),g(Y)!=="svelte-12c9hfs"&&(Y.innerHTML=Re),Te=n(e),p(N.$$.fragment,e),Je=n(e),A=m(e,"P",{}),We(A).forEach(l),this.h()},h(){Xe(i,"name","hf:doc:metadata"),Xe(i,"content",Pe)},m(e,t){Ne(document.head,i),s(e,y,t),s(e,f,t),s(e,V,t),c(T,e,t),s(e,q,t),s(e,J,t),s(e,P,t),c(b,e,t),s(e,D,t),s(e,$,t),s(e,K,t),c(j,e,t),s(e,O,t),c(U,e,t),s(e,ee,t),c(I,e,t),s(e,te,t),s(e,x,t),s(e,le,t),s(e,C,t),s(e,se,t),c(v,e,t),s(e,ae,t),s(e,Z,t),s(e,ne,t),c(G,e,t),s(e,ie,t),c(w,e,t),s(e,oe,t),c(k,e,t),s(e,me,t),s(e,_,t),s(e,re,t),s(e,H,t),s(e,pe,t),c(R,e,t),s(e,ce,t),s(e,B,t),s(e,de,t),s(e,W,t),s(e,Me,t),c(X,e,t),s(e,he,t),s(e,E,t),s(e,ge,t),c(L,e,t),s(e,fe,t),c(F,e,t),s(e,ue,t),s(e,z,t),s(e,we,t),c(Q,e,t),s(e,ye,t),s(e,Y,t),s(e,Te,t),c(N,e,t),s(e,Je,t),s(e,A,t),be=!0},p(e,[t]){const Be={};t&2&&(Be.$$scope={dirty:t,ctx:e}),w.$set(Be)},i(e){be||(d(T.$$.fragment,e),d(b.$$.fragment,e),d(j.$$.fragment,e),d(U.$$.fragment,e),d(I.$$.fragment,e),d(v.$$.fragment,e),d(G.$$.fragment,e),d(w.$$.fragment,e),d(k.$$.fragment,e),d(R.$$.fragment,e),d(X.$$.fragment,e),d(L.$$.fragment,e),d(F.$$.fragment,e),d(Q.$$.fragment,e),d(N.$$.fragment,e),be=!0)},o(e){M(T.$$.fragment,e),M(b.$$.fragment,e),M(j.$$.fragment,e),M(U.$$.fragment,e),M(I.$$.fragment,e),M(v.$$.fragment,e),M(G.$$.fragment,e),M(w.$$.fragment,e),M(k.$$.fragment,e),M(R.$$.fragment,e),M(X.$$.fragment,e),M(L.$$.fragment,e),M(F.$$.fragment,e),M(Q.$$.fragment,e),M(N.$$.fragment,e),be=!1},d(e){e&&(l(y),l(f),l(V),l(q),l(J),l(P),l(D),l($),l(K),l(O),l(ee),l(te),l(x),l(le),l(C),l(se),l(ae),l(Z),l(ne),l(ie),l(oe),l(me),l(_),l(re),l(H),l(pe),l(ce),l(B),l(de),l(W),l(Me),l(he),l(E),l(ge),l(fe),l(ue),l(z),l(we),l(ye),l(Y),l(Te),l(Je),l(A)),l(i),h(T,e),h(b,e),h(j,e),h(U,e),h(I,e),h(v,e),h(G,e),h(w,e),h(k,e),h(R,e),h(X,e),h(L,e),h(F,e),h(Q,e),h(N,e)}}}const Pe='{"title":"Using mlx-image at Hugging Face","local":"using-mlx-image-at-hugging-face","sections":[{"title":"Exploring mlx-image on the Hub","local":"exploring-mlx-image-on-the-hub","sections":[],"depth":2},{"title":"Installation","local":"installation","sections":[],"depth":2},{"title":"Models","local":"models","sections":[],"depth":2},{"title":"ImageNet-1K Results","local":"imagenet-1k-results","sections":[],"depth":2},{"title":"Similarity to PyTorch and other familiar tools","local":"similarity-to-pytorch-and-other-familiar-tools","sections":[],"depth":2},{"title":"Training","local":"training","sections":[],"depth":2},{"title":"Additional Resources","local":"additional-resources","sections":[],"depth":2},{"title":"Contact","local":"contact","sections":[],"depth":2}],"depth":1}';function De(S){return Le(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class st extends ze{constructor(i){super(),Qe(this,i,De,qe,Ee,{})}}export{st as component};
