{"id":36609,"date":"2024-11-01T09:49:58","date_gmt":"2024-11-01T09:49:58","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36609"},"modified":"2024-11-01T11:52:31","modified_gmt":"2024-11-01T11:52:31","slug":"deep-learning-pytorch-course-fully-convolutional-network","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36609\/","title":{"rendered":"Deep Learning PyTorch Course, Fully Convolutional Network"},"content":{"rendered":"<p><body><\/p>\n<p>A Fully Convolutional Network (FCN) is a neural network architecture that is mainly suitable for the problem of image segmentation, which involves separating specific objects within an image at the pixel level. Traditional Convolutional Neural Networks (CNNs) are primarily used for classification tasks and produce fixed-size outputs. However, FCNs are structured to generate transformed outputs while maintaining visual information, allowing each pixel in the image to carry meaning.<\/p>\n<h2>1. Basic Structure of Fully Convolutional Networks<\/h2>\n<p>FCNs essentially inherit the architecture of CNNs. However, an important point is that the fully connected layers are removed from the last part of the CNN, and instead, convolutional layers and upsampling layers are used to achieve the desired output size.<\/p>\n<p>The main components of FCNs are as follows:<\/p>\n<ul>\n<li><strong>Convolutional Layer:<\/strong> A layer that extracts features from the input image.<\/li>\n<li><strong>Non-linear Activation Function:<\/strong> Mainly, the ReLU (Rectified Linear Unit) function is used.<\/li>\n<li><strong>Upsampling:<\/strong> Restores downsampled data to the size of the original image.<\/li>\n<li><strong>Skip Connection:<\/strong> Used to integrate while maintaining the characteristics of the original resolution.<\/li>\n<\/ul>\n<h2>2. Implementing FCN with PyTorch<\/h2>\n<p>Now, let&#8217;s implement FCN using PyTorch. Below is a simple Python code example of an FCN.<\/p>\n<div class=\"example\">\n<pre><code>import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass FCN(nn.Module):\n    def __init__(self, num_classes):\n        super(FCN, self).__init__()\n        self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)\n        self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1)\n        self.conv3 = nn.Conv2d(128, 256, kernel_size=3, padding=1)\n        self.conv4 = nn.Conv2d(256, 256, kernel_size=3, padding=1)\n        \n        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)\n        \n        self.upconv1 = nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2)\n        self.upconv2 = nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2)\n        \n        self.final_conv = nn.Conv2d(64, num_classes, kernel_size=1)\n\n    def forward(self, x):\n        x1 = F.relu(self.conv1(x))\n        x2 = self.pool(x1)\n        x2 = F.relu(self.conv2(x2))\n        x3 = self.pool(x2)\n        x3 = F.relu(self.conv3(x3))\n        x4 = self.pool(x3)\n        x4 = F.relu(self.conv4(x4))\n\n        x = self.upconv1(x4)\n        x = self.upconv2(x)\n        x = self.final_conv(x)\n        \n        return x<\/code><\/pre>\n<\/div>\n<h3>2.1 Model Description<\/h3>\n<p>In the code above, our FCN model undergoes the following steps:<\/p>\n<ol>\n<li>Takes a 3-channel (typical RGB image) input and passes through the first convolutional layer that generates 64 feature maps.<\/li>\n<li>Moves through the next two convolutional layers, gradually generating more feature maps and reducing the image size by half through max pooling.<\/li>\n<li>Upsamples the image size back to the original size.<\/li>\n<li>The final output passes through a convolutional layer with a number of channels equal to the number of classes.<\/li>\n<\/ol>\n<h2>3. Preparing the Dataset<\/h2>\n<p>To train the FCN model, an appropriate dataset is needed. Commonly used datasets for image segmentation include Pascal VOC, COCO, etc., and here we will use a simple example of an image and a mask.<\/p>\n<h3>3.1 Generating Example Dataset<\/h3>\n<div class=\"example\">\n<pre><code>import numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\n\ndef generate_example_data():\n    h, w = 128, 128\n    image = np.random.randint(0, 255, (h, w, 3), dtype=np.uint8)\n    mask = np.zeros((h, w), dtype=np.uint8)\n    mask[30:70, 30:70] = 1  # Rectangular object\n\n    return image, mask\n\nimage, mask = generate_example_data()\n\n# Visualizing the image and mask\nplt.subplot(1, 2, 1)\nplt.title('Image')\nplt.imshow(image)\nplt.subplot(1, 2, 2)\nplt.title('Mask')\nplt.imshow(mask, cmap='gray')\nplt.show()<\/code><\/pre>\n<\/div>\n<h2>4. Training the Model<\/h2>\n<p>Once the dataset is created, we are ready to train the FCN model. During the training process, we need to set the loss function and optimizer of PyTorch.<\/p>\n<div class=\"example\">\n<pre><code>import torch.optim as optim\n\n# Initialize the model, loss function, and optimizer\nnum_classes = 2  # Object and background\nmodel = FCN(num_classes)\ncriterion = nn.CrossEntropyLoss()  # Cross-entropy loss function\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Dummy training loop\nfor epoch in range(5):  # Train for 5 epochs\n    model.train()\n    optimizer.zero_grad()\n    \n    # Forward pass\n    inputs = torch.Tensor(image).permute(2, 0, 1).unsqueeze(0)  # (1, 3, 128, 128)\n    targets = torch.Tensor(mask).long().unsqueeze(0)  # (1, 128, 128)\n    \n    outputs = model(inputs)\n    loss = criterion(outputs, targets)\n    \n    # Backward pass\n    loss.backward()\n    optimizer.step()\n    \n    print(f'Epoch [{epoch+1}\/5], Loss: {loss.item():.4f}')<\/code><\/pre>\n<\/div>\n<h2>5. Conclusion<\/h2>\n<p>In this article, we examined the basic concepts of Fully Convolutional Networks (FCN), the implementation process of a simple FCN model using PyTorch, dataset preparation, and training methods. FCNs are highly useful models for image segmentation and can be used in various application fields.<\/p>\n<p>With further research on more advanced FCN models and additional datasets, we can aim for better performance. If you are curious about the applications of FCNs, I recommend exploring more content!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Fully Convolutional Network (FCN) is a neural network architecture that is mainly suitable for the problem of image segmentation, which involves separating specific objects within an image at the pixel level. Traditional Convolutional Neural Networks (CNNs) are primarily used for classification tasks and produce fixed-size outputs. However, FCNs are structured to generate transformed outputs &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36609\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, Fully Convolutional Network&#8221;<\/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":[149],"tags":[],"class_list":["post-36609","post","type-post","status-publish","format-standard","hentry","category-pytorch-study"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Deep Learning PyTorch Course, Fully Convolutional Network - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/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\/36609\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deep Learning PyTorch Course, Fully Convolutional Network - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"A Fully Convolutional Network (FCN) is a neural network architecture that is mainly suitable for the problem of image segmentation, which involves separating specific objects within an image at the pixel level. Traditional Convolutional Neural Networks (CNNs) are primarily used for classification tasks and produce fixed-size outputs. However, FCNs are structured to generate transformed outputs &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, Fully Convolutional Network&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36609\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:49:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:52:31+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\/36609\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36609\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, Fully Convolutional Network\",\"datePublished\":\"2024-11-01T09:49:58+00:00\",\"dateModified\":\"2024-11-01T11:52:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36609\/\"},\"wordCount\":427,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36609\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36609\/\",\"name\":\"Deep Learning PyTorch Course, Fully Convolutional Network - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:49:58+00:00\",\"dateModified\":\"2024-11-01T11:52:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36609\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36609\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36609\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, Fully Convolutional Network\"}]},{\"@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":"Deep Learning PyTorch Course, Fully Convolutional Network - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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\/36609\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, Fully Convolutional Network - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"A Fully Convolutional Network (FCN) is a neural network architecture that is mainly suitable for the problem of image segmentation, which involves separating specific objects within an image at the pixel level. Traditional Convolutional Neural Networks (CNNs) are primarily used for classification tasks and produce fixed-size outputs. However, FCNs are structured to generate transformed outputs &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, Fully Convolutional Network\"","og_url":"https:\/\/atmokpo.com\/w\/36609\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:49:58+00:00","article_modified_time":"2024-11-01T11:52:31+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\/36609\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36609\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, Fully Convolutional Network","datePublished":"2024-11-01T09:49:58+00:00","dateModified":"2024-11-01T11:52:31+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36609\/"},"wordCount":427,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36609\/","url":"https:\/\/atmokpo.com\/w\/36609\/","name":"Deep Learning PyTorch Course, Fully Convolutional Network - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:49:58+00:00","dateModified":"2024-11-01T11:52:31+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36609\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36609\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36609\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, Fully Convolutional Network"}]},{"@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\/36609","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=36609"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36609\/revisions"}],"predecessor-version":[{"id":36610,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36609\/revisions\/36610"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}