{"id":36385,"date":"2024-11-01T09:48:04","date_gmt":"2024-11-01T09:48:04","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36385"},"modified":"2024-11-01T11:00:08","modified_gmt":"2024-11-01T11:00:08","slug":"divadvancements-in-image-generation-field-using-gan-deep-learning-with-pytorch","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36385\/","title":{"rendered":"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch<\/div"},"content":{"rendered":"<p><body><\/p>\n<p>With the advancement of deep learning, a framework called GAN (Generative Adversarial Network) has brought about innovative changes in the fields of image generation, transformation, and editing. GAN consists of two neural networks, the Generator and the Discriminator, which compete and learn from each other. In this article, we will delve into the basic concepts of GAN, its operating principles, an implementation example using PyTorch, and the advancements in image generation using GAN.<\/p>\n<h2>1. Basic Concepts of GAN<\/h2>\n<p>GAN is a model proposed by Ian Goodfellow and others in 2014, where two neural networks learn through an adversarial relationship. The Generator generates fake images, while the Discriminator&#8217;s role is to distinguish between real and fake images. This process proceeds as follows:<\/p>\n<h3>1.1 Generator and Discriminator<\/h3>\n<p>The fundamental components of GAN are the Generator and the Discriminator, which perform the following roles:<\/p>\n<ul>\n<li><strong>Generator<\/strong>: Takes a random noise vector as input and generates images that resemble real ones.<\/li>\n<li><strong>Discriminator<\/strong>: Performs the task of determining whether the input image is real or fake.<\/li>\n<\/ul>\n<h3>1.2 Loss Function<\/h3>\n<p>The loss function of GAN is defined as follows:<\/p>\n<p>The loss function of the Discriminator aims to maximize the predictions for real and fake images.<\/p>\n<pre>\nD_loss = -E[log(D(x))] - E[log(1 - D(G(z)))]\n<\/pre>\n<p>The loss function of the Generator learns to make the Discriminator incorrectly classify fake images as real.<\/p>\n<pre>\nG_loss = -E[log(D(G(z)))]\n<\/pre>\n<h2>2. Implementing GAN using PyTorch<\/h2>\n<p>Now we will implement a simple GAN using PyTorch. This will allow us to practice the workings of GAN and visually understand the process of generating images.<\/p>\n<h3>2.1 Installing Required Libraries<\/h3>\n<p>We will install PyTorch and torchvision. These are necessary for building neural networks and loading datasets.<\/p>\n<pre>\npip install torch torchvision\n<\/pre>\n<h3>2.2 Preparing the Dataset<\/h3>\n<p>We will use the MNIST dataset to generate images of digits.<\/p>\n<pre>\nimport torch\nimport torchvision\nimport torchvision.transforms as transforms\n\n# Load the MNIST dataset\ntransform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])\ntrainset = torchvision.datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\ntrainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)\n<\/pre>\n<h3>2.3 Defining the Generator and Discriminator Models<\/h3>\n<pre>\nimport torch.nn as nn\n\n# Define the Generator\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.fc = nn.Sequential(\n            nn.Linear(100, 256),\n            nn.ReLU(),\n            nn.Linear(256, 512),\n            nn.ReLU(),\n            nn.Linear(512, 1024),\n            nn.ReLU(),\n            nn.Linear(1024, 28 * 28),\n            nn.Tanh()\n        )\n\n    def forward(self, z):\n        return self.fc(z).view(-1, 1, 28, 28)\n\n# Define the Discriminator\nclass Discriminator(nn.Module):\n    def __init__(self):\n        super(Discriminator, self).__init__()\n        self.fc = nn.Sequential(\n            nn.Linear(28 * 28, 512),\n            nn.LeakyReLU(0.2),\n            nn.Linear(512, 256),\n            nn.LeakyReLU(0.2),\n            nn.Linear(256, 1),\n            nn.Sigmoid()\n        )\n\n    def forward(self, x):\n        return self.fc(x.view(-1, 28 * 28))\n<\/pre>\n<h3>2.4 Setting Loss Function and Optimizer<\/h3>\n<pre>\nimport torch.optim as optim\n\n# Create model instances\nG = Generator()\nD = Discriminator()\n\n# Set loss function and optimizer\ncriterion = nn.BCELoss()\noptimizer_G = optim.Adam(G.parameters(), lr=0.0002, betas=(0.5, 0.999))\noptimizer_D = optim.Adam(D.parameters(), lr=0.0002, betas=(0.5, 0.999))\n<\/pre>\n<h3>2.5 GAN Training Loop<\/h3>\n<p>Now it&#8217;s time to train the GAN. We will update the Generator and Discriminator alternately through the training loop.<\/p>\n<pre>\nnum_epochs = 200\nfor epoch in range(num_epochs):\n    for i, (real_images, _) in enumerate(trainloader):\n        # Create labels for real and fake\n        real_labels = torch.ones(real_images.size(0), 1)\n        fake_labels = torch.zeros(real_images.size(0), 1)\n\n        # Train the Discriminator\n        optimizer_D.zero_grad()\n        outputs = D(real_images)\n        d_loss_real = criterion(outputs, real_labels)\n\n        z = torch.randn(real_images.size(0), 100)\n        fake_images = G(z)\n        outputs = D(fake_images.detach())\n        d_loss_fake = criterion(outputs, fake_labels)\n\n        d_loss = d_loss_real + d_loss_fake\n        d_loss.backward()\n        optimizer_D.step()\n\n        # Train the Generator\n        optimizer_G.zero_grad()\n        outputs = D(fake_images)\n        g_loss = criterion(outputs, real_labels)\n        g_loss.backward()\n        optimizer_G.step()\n\n    if (epoch+1) % 10 == 0:\n        print(f'Epoch [{epoch+1}\/{num_epochs}], d_loss: {d_loss.item():.4f}, g_loss: {g_loss.item():.4f}')\n<\/pre>\n<h3>2.6 Visualizing Image Generation<\/h3>\n<p>After training is completed, we can use the Generator to create and visualize images.<\/p>\n<pre>\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nz = torch.randn(64, 100)\nfake_images = G(z)\n\n# Visualize the generated images\ngrid = torchvision.utils.make_grid(fake_images, nrow=8, normalize=True)\nplt.imshow(np.transpose(grid.detach().numpy(), (1, 2, 0)))\nplt.axis('off')\nplt.show()\n<\/pre>\n<h2>3. Advancements and Applications of GAN<\/h2>\n<p>Beyond generating images, GANs are utilized in various fields. For example:<\/p>\n<ul>\n<li><strong>Style Transfer<\/strong>: Styles of images can be transformed into different styles.<\/li>\n<li><strong>Image Inpainting<\/strong>: Missing parts of images can be generated to restore complete images.<\/li>\n<li><strong>Super Resolution<\/strong>: GANs can be used to convert low-resolution images to high-resolution.<\/li>\n<\/ul>\n<h3>3.1 Recent Trends in GAN Research<\/h3>\n<p>Recent studies have proposed various approaches to stabilize the training of GANs. For instance, Wasserstein GAN (WGAN) can improve the stability of the loss function to prevent mode collapse.<\/p>\n<h2>4. Conclusion<\/h2>\n<p>GANs play a significant role in image generation and transformation, and they can be easily implemented through frameworks such as PyTorch. GANs are expected to continue evolving in various fields, contributing to the expansion of deep learning&#8217;s boundaries.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the advancement of deep learning, a framework called GAN (Generative Adversarial Network) has brought about innovative changes in the fields of image generation, transformation, and editing. GAN consists of two neural networks, the Generator and the Discriminator, which compete and learn from each other. In this article, we will delve into the basic concepts &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36385\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch<\/div\"<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[113],"tags":[],"class_list":["post-36385","post","type-post","status-publish","format-standard","hentry","category-gan-deep-learning-course"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>div&gt;Advancements in Image Generation Field using GAN Deep Learning with PyTorch<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/36385\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"div&gt;Advancements in Image Generation Field using GAN Deep Learning with PyTorch\" \/>\n<meta property=\"og:description\" content=\"With the advancement of deep learning, a framework called GAN (Generative Adversarial Network) has brought about innovative changes in the fields of image generation, transformation, and editing. GAN consists of two neural networks, the Generator and the Discriminator, which compete and learn from each other. In this article, we will delve into the basic concepts &hellip; \ub354 \ubcf4\uae30 &quot;div&gt;Advancements in Image Generation Field using GAN Deep Learning with PyTorch\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36385\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:48:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:00:08+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36385\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36385\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch\",\"datePublished\":\"2024-11-01T09:48:04+00:00\",\"dateModified\":\"2024-11-01T11:00:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36385\/\"},\"wordCount\":12,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"GAN deep learning course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36385\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36385\/\",\"name\":\"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:48:04+00:00\",\"dateModified\":\"2024-11-01T11:00:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36385\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36385\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36385\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/36385\/","og_locale":"ko_KR","og_type":"article","og_title":"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch","og_description":"With the advancement of deep learning, a framework called GAN (Generative Adversarial Network) has brought about innovative changes in the fields of image generation, transformation, and editing. GAN consists of two neural networks, the Generator and the Discriminator, which compete and learn from each other. In this article, we will delve into the basic concepts &hellip; \ub354 \ubcf4\uae30 \"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch","og_url":"https:\/\/atmokpo.com\/w\/36385\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:48:04+00:00","article_modified_time":"2024-11-01T11:00:08+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36385\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36385\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch","datePublished":"2024-11-01T09:48:04+00:00","dateModified":"2024-11-01T11:00:08+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36385\/"},"wordCount":12,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["GAN deep learning course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36385\/","url":"https:\/\/atmokpo.com\/w\/36385\/","name":"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:48:04+00:00","dateModified":"2024-11-01T11:00:08+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36385\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36385\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36385\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"div>Advancements in Image Generation Field using GAN Deep Learning with PyTorch"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=36385"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36385\/revisions"}],"predecessor-version":[{"id":36386,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36385\/revisions\/36386"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}