import{s as Ne,n as Me,o as ke}from"../chunks/scheduler.35aab934.js";import{S as He,i as Pe,e as o,s as i,c as P,h as Ie,a as l,d as n,b as s,f as Le,g as I,j as r,k as Ge,l as ze,m as a,n as z,t as S,o as q,p as E}from"../chunks/index.de17b971.js";import{C as Se}from"../chunks/CopyLLMTxtMenu.48bc5cc2.js";import{H as ce,E as qe}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8fdece89.js";function Ee(me){let c,j,k,F,m,U,h,D,d,he='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.',W,p,de="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.",R,f,X,g,pe='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/unpaired_images.png" alt="paired and unpaired_images"/>',Y,u,fe="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.",B,v,O,y,ge="<strong>Dual GAN Structure</strong>:",Z,w,ue="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).",J,b,ve=`<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>`,K,C,ye="<strong>Cycle Consistency Loss</strong>:",Q,x,we="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.",V,T,be="Implementation Involves two stages of transformation.",ee,$,Ce=`<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.`,te,A,xe="<strong>Least-Square Loss</strong>:",ne,_,Te="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.",ae,L,$e="<strong>Indentity Loss</strong>:",ie,G,Ae=`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).`,se,N,_e=`<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.`,oe,M,le,H,re;return m=new Se({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),h=new ce({props:{title:"CycleGAN Introduction",local:"cyclegan-introduction",headingTag:"h1"}}),f=new ce({props:{title:"What Is Unpaired Image-to-Image Translation",local:"what-is-unpaired-image-to-image-translation",headingTag:"h2"}}),v=new ce({props:{title:"Main Components of CycleGAN",local:"main-components-of-cyclegan",headingTag:"h2"}}),M=new qe({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"),j=i(),k=o("p"),F=i(),P(m.$$.fragment),U=i(),P(h.$$.fragment),D=i(),d=o("p"),d.innerHTML=he,W=i(),p=o("p"),p.textContent=de,R=i(),P(f.$$.fragment),X=i(),g=o("p"),g.innerHTML=pe,Y=i(),u=o("p"),u.textContent=fe,B=i(),P(v.$$.fragment),O=i(),y=o("p"),y.innerHTML=ge,Z=i(),w=o("p"),w.textContent=ue,J=i(),b=o("ul"),b.innerHTML=ve,K=i(),C=o("p"),C.innerHTML=ye,Q=i(),x=o("p"),x.textContent=we,V=i(),T=o("p"),T.textContent=be,ee=i(),$=o("p"),$.innerHTML=Ce,te=i(),A=o("p"),A.innerHTML=xe,ne=i(),_=o("p"),_.innerHTML=Te,ae=i(),L=o("p"),L.innerHTML=$e,ie=i(),G=o("p"),G.textContent=Ae,se=i(),N=o("p"),N.innerHTML=_e,oe=i(),P(M.$$.fragment),le=i(),H=o("p"),this.h()},l(e){const t=Ie("svelte-u9bgzb",document.head);c=l(t,"META",{name:!0,content:!0}),t.forEach(n),j=s(e),k=l(e,"P",{}),Le(k).forEach(n),F=s(e),I(m.$$.fragment,e),U=s(e),I(h.$$.fragment,e),D=s(e),d=l(e,"P",{"data-svelte-h":!0}),r(d)!=="svelte-1vunqvr"&&(d.innerHTML=he),W=s(e),p=l(e,"P",{"data-svelte-h":!0}),r(p)!=="svelte-1wd5x5o"&&(p.textContent=de),R=s(e),I(f.$$.fragment,e),X=s(e),g=l(e,"P",{"data-svelte-h":!0}),r(g)!=="svelte-1ok4k6q"&&(g.innerHTML=pe),Y=s(e),u=l(e,"P",{"data-svelte-h":!0}),r(u)!=="svelte-aq342c"&&(u.textContent=fe),B=s(e),I(v.$$.fragment,e),O=s(e),y=l(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-1d6m7lo"&&(y.innerHTML=ge),Z=s(e),w=l(e,"P",{"data-svelte-h":!0}),r(w)!=="svelte-g7ojp9"&&(w.textContent=ue),J=s(e),b=l(e,"UL",{"data-svelte-h":!0}),r(b)!=="svelte-119u2tg"&&(b.innerHTML=ve),K=s(e),C=l(e,"P",{"data-svelte-h":!0}),r(C)!=="svelte-gg19do"&&(C.innerHTML=ye),Q=s(e),x=l(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-emhl4j"&&(x.textContent=we),V=s(e),T=l(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-pabc23"&&(T.textContent=be),ee=s(e),$=l(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-c2z849"&&($.innerHTML=Ce),te=s(e),A=l(e,"P",{"data-svelte-h":!0}),r(A)!=="svelte-1651i7l"&&(A.innerHTML=xe),ne=s(e),_=l(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-edylf9"&&(_.innerHTML=Te),ae=s(e),L=l(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-oz5lfc"&&(L.innerHTML=$e),ie=s(e),G=l(e,"P",{"data-svelte-h":!0}),r(G)!=="svelte-1ig7g33"&&(G.textContent=Ae),se=s(e),N=l(e,"P",{"data-svelte-h":!0}),r(N)!=="svelte-1bir4m6"&&(N.innerHTML=_e),oe=s(e),I(M.$$.fragment,e),le=s(e),H=l(e,"P",{}),Le(H).forEach(n),this.h()},h(){Ge(c,"name","hf:doc:metadata"),Ge(c,"content",je)},m(e,t){ze(document.head,c),a(e,j,t),a(e,k,t),a(e,F,t),z(m,e,t),a(e,U,t),z(h,e,t),a(e,D,t),a(e,d,t),a(e,W,t),a(e,p,t),a(e,R,t),z(f,e,t),a(e,X,t),a(e,g,t),a(e,Y,t),a(e,u,t),a(e,B,t),z(v,e,t),a(e,O,t),a(e,y,t),a(e,Z,t),a(e,w,t),a(e,J,t),a(e,b,t),a(e,K,t),a(e,C,t),a(e,Q,t),a(e,x,t),a(e,V,t),a(e,T,t),a(e,ee,t),a(e,$,t),a(e,te,t),a(e,A,t),a(e,ne,t),a(e,_,t),a(e,ae,t),a(e,L,t),a(e,ie,t),a(e,G,t),a(e,se,t),a(e,N,t),a(e,oe,t),z(M,e,t),a(e,le,t),a(e,H,t),re=!0},p:Me,i(e){re||(S(m.$$.fragment,e),S(h.$$.fragment,e),S(f.$$.fragment,e),S(v.$$.fragment,e),S(M.$$.fragment,e),re=!0)},o(e){q(m.$$.fragment,e),q(h.$$.fragment,e),q(f.$$.fragment,e),q(v.$$.fragment,e),q(M.$$.fragment,e),re=!1},d(e){e&&(n(j),n(k),n(F),n(U),n(D),n(d),n(W),n(p),n(R),n(X),n(g),n(Y),n(u),n(B),n(O),n(y),n(Z),n(w),n(J),n(b),n(K),n(C),n(Q),n(x),n(V),n(T),n(ee),n($),n(te),n(A),n(ne),n(_),n(ae),n(L),n(ie),n(G),n(se),n(N),n(oe),n(le),n(H)),n(c),E(m,e),E(h,e),E(f,e),E(v,e),E(M,e)}}}const je='{"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 Fe(me){return ke(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Xe extends He{constructor(c){super(),Pe(this,c,Fe,Ee,Ne,{})}}export{Xe as component};
