import{s as Le,n as Ge,o as Ne}from"../chunks/scheduler.7bc62968.js";import{S as ke,i as He,g as o,s as i,r as te,A as Me,h as l,f as a,c as s,j as _e,u as ae,x as r,k as $e,y as Pe,a as n,v as ne,d as ie,t as se,w as oe}from"../chunks/index.2f8492b0.js";import{H as le,E as Ie}from"../chunks/getInferenceSnippets.06ca42cd.js";function ze(re){let c,M,k,P,h,I,m,ce='This section introduces CycleGAN, short for <em>Cycle-Consistent Generative Adversarial Network</em>, which is a framework designed for image-to-image translation tasks where paired examples are not available. Introduced by Zhu et al. in a <a href="https://arxiv.org/abs/1703.10593" rel="nofollow">2017</a> paper, it represents a significant advancement in the field of computer vision and machine learning.',z,d,he="In many image-to-image translation tasks, the goal is to learn a mapping between an input image and an output image. Traditional approaches often rely on large datasets of paired examples (e.g., photos and corresponding sketches). However, obtaining such paired datasets can be extremely challenging or even infeasible in many scenarios. This is where CycleGAN comes into play, as it is designed to work with unpaired datasets.",q,p,S,g,me='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/unpaired_images.png" alt="paired and unpaired_images"/>',E,f,de="In many image translation scenarios, we encounter datasets lacking direct, one-to-one correspondence between image pairs. This scenario is where unpaired image-to-image translation comes into play. Here, instead of having matching pairs of images, you work with two distinct sets or “piles” of images, each representing a different style or domain, such as X and Y. For instance, one pile might consist of realistic photographs, while the other could contain artworks by Monet, Cezanne, or other artists. Alternatively, the piles could represent different seasons, with one showcasing winter landscapes and the other summer scenes. Another example could be a collection of horse images in one pile and zebra images in the other, without any direct pairing between the two. The objective in unpaired image-to-image translation is for the model to learn and extract the general stylistic elements from each pile and apply these learned styles to transform images from one domain to another. This transformation often works both ways, allowing images from domain X to be translated into the style of domain Y, and vice versa. This approach is particularly valuable when exact image pairs are unavailable or difficult to obtain.",j,u,F,v,pe="<strong>Dual GAN Structure</strong>:",U,y,ge="CycleGAN employs two GANs (Generative Adversarial Networks), one for translating from the first set to the second (e.g., zebra to horse) and another for the reverse process (horse to zebra). This dual structure ensures realism (via the adversarial process) and content preservation (via cycle consistency).",D,w,fe=`<li>PatchGAN Discriminators: The discriminators used in CycleGAN are based on <a href="https://arxiv.org/pdf/1611.07004.pdf" rel="nofollow">PatchGAN</a> architecture, which assesses patches of an image rather than the whole, contributing to more detailed and localized realism.</li> <li>Generator Architecture: The generators in CycleGAN draw from <a href="https://arxiv.org/abs/1505.04597" rel="nofollow">U-Net</a> and [DCGAN](<a href="https://arxiv.org/abs/1511.06434" rel="nofollow">https://arxiv.org/abs/1511.06434</a> architectures, involving downsampling (encoding), upsampling (decoding), and convolutional layers with batch normalization and ReLU.
These generators are enhanced with additional convolutional layers and skip connections, known as residual connections, which aid in learning identity functions and supporting deeper transformations.</li>`,W,b,ue="<strong>Cycle Consistency Loss</strong>:",R,C,ve="Ensures that the style of an image can be changed (e.g., sad face to hugging face) and then reverted back to its original form (hugging face back to sad face) with minimal loss of detail or content.",X,x,ye="Implementation Involves two stages of transformation.",Y,T,we=`<strong>First Stage</strong>: A sad face is transformed into a hugging face. <strong>Second Stage</strong>: This hugging face is then converted back into a sad face. The model aims for the final image (reverted sad face) to closely resemble the original sad face. This is achieved by minimizing the pixel difference between these two images, which is added to the model’s loss function. The cycle consistency is applied in both directions: sad face to hugging face to sad face, and hugging face to sad face to hugging face. The loss for each cycle is calculated by summing the pixel differences and is then incorporated into the overall generator loss.
Integration with Adversarial Loss: Cycle consistency loss is combined with adversarial loss, commonly used in GANs, to form a comprehensive loss function. This combined loss function is optimized simultaneously for both generators in CycleGAN.`,B,A,be="<strong>Least-Square Loss</strong>:",O,_,Ce="It’s a method that minimizes the sum of squared residuals. What that means is that it tries to find the best-fit line that has the smallest sum of squared distances between that line and all the points. In GANs, the <strong>line</strong> represents the label (real or fake), and the points represent the discriminator’s predictions. The discriminator’s loss function in Least Squares adversarial loss is formulated by calculating the squared distance between its predictions and the actual labels (real or fake) across multiple images. For the generator, the loss function is designed to make its fake outputs appear as real as possible, measured by how far these outputs deviate from the label of <strong>real</strong>. Having Least-Square Loss addresses vanishing gradients and mode collapse issues common in BCE loss.",Z,$,xe="<strong>Indentity Loss</strong>:",J,L,Te=`Introduced in CycleGAN, identity loss is an optional loss term aimed at enhancing color preservation in generated images. It works by ensuring that an image input into a generator (e.g., a horse image into a zebra-to-horse generator) should ideally output the same image, as it is already in the target style. The loss is calculated as the pixel distance between the real input and the output of the generator. A zero pixel distance (no change in the image) results in zero identity loss, which is the desired outcome.
For CycleGANs generators, this loss is added alongside adversarial and cycle consistency losses. It’s adjusted using a lambda term (weighting factor).`,K,G,Ae=`<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/cycleGAN1.jpg" alt="CycleGAN"/>
This figure shows the combined GAN architecture functionality for both <strong>GANs</strong>. These GANs are linked by cycle consistency, forming a cycle.
For real images, the classification matrix contains ones. For fake images, it contains zeros. In summary, CycleGAN intricately combines two GANs with various loss functions, including adversarial, cycle consistency, and optional identity loss,
to effectively transfer styles between two domains while preserving the essential characteristics of the input images.`,Q,N,V,H,ee;return h=new le({props:{title:"CycleGAN Introduction",local:"cyclegan-introduction",headingTag:"h1"}}),p=new le({props:{title:"What Is Unpaired Image-to-Image Translation",local:"what-is-unpaired-image-to-image-translation",headingTag:"h2"}}),u=new le({props:{title:"Main Components of CycleGAN",local:"main-components-of-cyclegan",headingTag:"h2"}}),N=new Ie({props:{source:"https://github.com/huggingface/computer-vision-course/blob/main/chapters/en/unit5/generative-models/practical-applications/cycle_gan.mdx"}}),{c(){c=o("meta"),M=i(),k=o("p"),P=i(),te(h.$$.fragment),I=i(),m=o("p"),m.innerHTML=ce,z=i(),d=o("p"),d.textContent=he,q=i(),te(p.$$.fragment),S=i(),g=o("p"),g.innerHTML=me,E=i(),f=o("p"),f.textContent=de,j=i(),te(u.$$.fragment),F=i(),v=o("p"),v.innerHTML=pe,U=i(),y=o("p"),y.textContent=ge,D=i(),w=o("ul"),w.innerHTML=fe,W=i(),b=o("p"),b.innerHTML=ue,R=i(),C=o("p"),C.textContent=ve,X=i(),x=o("p"),x.textContent=ye,Y=i(),T=o("p"),T.innerHTML=we,B=i(),A=o("p"),A.innerHTML=be,O=i(),_=o("p"),_.innerHTML=Ce,Z=i(),$=o("p"),$.innerHTML=xe,J=i(),L=o("p"),L.textContent=Te,K=i(),G=o("p"),G.innerHTML=Ae,Q=i(),te(N.$$.fragment),V=i(),H=o("p"),this.h()},l(e){const t=Me("svelte-u9bgzb",document.head);c=l(t,"META",{name:!0,content:!0}),t.forEach(a),M=s(e),k=l(e,"P",{}),_e(k).forEach(a),P=s(e),ae(h.$$.fragment,e),I=s(e),m=l(e,"P",{"data-svelte-h":!0}),r(m)!=="svelte-1vunqvr"&&(m.innerHTML=ce),z=s(e),d=l(e,"P",{"data-svelte-h":!0}),r(d)!=="svelte-1wd5x5o"&&(d.textContent=he),q=s(e),ae(p.$$.fragment,e),S=s(e),g=l(e,"P",{"data-svelte-h":!0}),r(g)!=="svelte-1ok4k6q"&&(g.innerHTML=me),E=s(e),f=l(e,"P",{"data-svelte-h":!0}),r(f)!=="svelte-aq342c"&&(f.textContent=de),j=s(e),ae(u.$$.fragment,e),F=s(e),v=l(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-1d6m7lo"&&(v.innerHTML=pe),U=s(e),y=l(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-g7ojp9"&&(y.textContent=ge),D=s(e),w=l(e,"UL",{"data-svelte-h":!0}),r(w)!=="svelte-119u2tg"&&(w.innerHTML=fe),W=s(e),b=l(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-gg19do"&&(b.innerHTML=ue),R=s(e),C=l(e,"P",{"data-svelte-h":!0}),r(C)!=="svelte-emhl4j"&&(C.textContent=ve),X=s(e),x=l(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-pabc23"&&(x.textContent=ye),Y=s(e),T=l(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-c2z849"&&(T.innerHTML=we),B=s(e),A=l(e,"P",{"data-svelte-h":!0}),r(A)!=="svelte-1651i7l"&&(A.innerHTML=be),O=s(e),_=l(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-edylf9"&&(_.innerHTML=Ce),Z=s(e),$=l(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-oz5lfc"&&($.innerHTML=xe),J=s(e),L=l(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-1ig7g33"&&(L.textContent=Te),K=s(e),G=l(e,"P",{"data-svelte-h":!0}),r(G)!=="svelte-1bir4m6"&&(G.innerHTML=Ae),Q=s(e),ae(N.$$.fragment,e),V=s(e),H=l(e,"P",{}),_e(H).forEach(a),this.h()},h(){$e(c,"name","hf:doc:metadata"),$e(c,"content",qe)},m(e,t){Pe(document.head,c),n(e,M,t),n(e,k,t),n(e,P,t),ne(h,e,t),n(e,I,t),n(e,m,t),n(e,z,t),n(e,d,t),n(e,q,t),ne(p,e,t),n(e,S,t),n(e,g,t),n(e,E,t),n(e,f,t),n(e,j,t),ne(u,e,t),n(e,F,t),n(e,v,t),n(e,U,t),n(e,y,t),n(e,D,t),n(e,w,t),n(e,W,t),n(e,b,t),n(e,R,t),n(e,C,t),n(e,X,t),n(e,x,t),n(e,Y,t),n(e,T,t),n(e,B,t),n(e,A,t),n(e,O,t),n(e,_,t),n(e,Z,t),n(e,$,t),n(e,J,t),n(e,L,t),n(e,K,t),n(e,G,t),n(e,Q,t),ne(N,e,t),n(e,V,t),n(e,H,t),ee=!0},p:Ge,i(e){ee||(ie(h.$$.fragment,e),ie(p.$$.fragment,e),ie(u.$$.fragment,e),ie(N.$$.fragment,e),ee=!0)},o(e){se(h.$$.fragment,e),se(p.$$.fragment,e),se(u.$$.fragment,e),se(N.$$.fragment,e),ee=!1},d(e){e&&(a(M),a(k),a(P),a(I),a(m),a(z),a(d),a(q),a(S),a(g),a(E),a(f),a(j),a(F),a(v),a(U),a(y),a(D),a(w),a(W),a(b),a(R),a(C),a(X),a(x),a(Y),a(T),a(B),a(A),a(O),a(_),a(Z),a($),a(J),a(L),a(K),a(G),a(Q),a(V),a(H)),a(c),oe(h,e),oe(p,e),oe(u,e),oe(N,e)}}}const qe='{"title":"CycleGAN Introduction","local":"cyclegan-introduction","sections":[{"title":"What Is Unpaired Image-to-Image Translation","local":"what-is-unpaired-image-to-image-translation","sections":[],"depth":2},{"title":"Main Components of CycleGAN","local":"main-components-of-cyclegan","sections":[],"depth":2}],"depth":1}';function Se(re){return Ne(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ue extends ke{constructor(c){super(),He(this,c,Se,ze,Le,{})}}export{Ue as component};
