<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mail&#39;Suesarn Wilainuch on Medium]]></title>
        <description><![CDATA[Stories by Mail&#39;Suesarn Wilainuch on Medium]]></description>
        <link>https://medium.com/@mailsuesarnwilainuch?source=rss-b546586d119b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*845_4_kfSjUZwQ5zmUKRmw.jpeg</url>
            <title>Stories by Mail&amp;#39;Suesarn Wilainuch on Medium</title>
            <link>https://medium.com/@mailsuesarnwilainuch?source=rss-b546586d119b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 04:25:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mailsuesarnwilainuch/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[On the Use of Attention Map for Land Cover Mapping]]></title>
            <link>https://medium.com/analytics-vidhya/on-the-use-of-attention-map-for-land-cover-mapping-4aebb59885cd?source=rss-b546586d119b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4aebb59885cd</guid>
            <category><![CDATA[expsuperai]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[attention-map]]></category>
            <category><![CDATA[land-cover-mapping]]></category>
            <category><![CDATA[super-ai-engineer]]></category>
            <dc:creator><![CDATA[Mail'Suesarn Wilainuch]]></dc:creator>
            <pubDate>Wed, 17 Feb 2021 02:51:36 GMT</pubDate>
            <atom:updated>2023-06-03T05:37:06.718Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t-bD31xsJVaYjOl9m0fuQw.png" /></figure><p>Today, I will review my work, which is my thesis, when studying for a Master’s degree. This work will be about implementing <strong>AI</strong> in the field of <strong>remote sensing</strong>. I will briefly describe what remote sensing is about and how it will be used in this work.</p><h3>Introduction</h3><p>Remote sensing is the acquisition of information about an object or phenomenon without direct physical contact with that object or phenomenon. Remote sensing is used in many fields, especially in the field of geography. Land surveys and earth sciences such as hydrology, ecology, oceanography Glacier science, and geology, including military Secret service, trade, economy, planning, and humanitarian use</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*5YnbtE-Zw3u5sK3fgwddhw.jpeg" /><figcaption>Remote sensing</figcaption></figure><p>At present, remote sensing generally refers to the use of air sensing technology to detect and classify objects on Earth, both on the surface of the Earth’s ocean and at the Earth’s atmosphere by the principle of distribution of waves such as electromagnetic waves. This may be categorized as active remote sensing when an aircraft or satellite is self-emitting. Or self-powered remote sensing (passive remote sensing) by receiving information from the reflection of other energy such as sunlight.</p><p>Therefore, the remote sensing that we are most familiar with and closest to us. Is a normal camera Because we get information about objects such as color, texture, characteristics. Without touching the object</p><p>Currently, satellite images are beneficial and essential in many fields. Weather forecast uses satellite images to analyze the changes in the Earth’s atmosphere. Disaster management uses satellite images to identify and assess the situation to develop a timely and suitable response. Governments can use satellite images for crop monitoring for a better policy in agriculture product price scheme. Therefore, we see that satellite images are beneficial because they can apply to many tasks. Currently, research in artificial intelligence in image or computer vision has received a lot of attention. Both hardware and software have been developed to speed up the image analysis tasks. One of the most influential and popular techniques in artificial intelligence is Deep Learning, which is used as the main structure for many tasks. With this technique, artificial intelligence can deal with problems better. Higher accuracy than previous techniques, Therefore, we used the <strong>Deep learning</strong> technique with satellite images in this work.</p><p>In this work, we design a convolution neural network training model to reduce resources, labeling data for training for <strong>segmentation, or satellite imagery, land cover mapping</strong> of objects on the Earth’s surface. This process is to assign a class to each pixel of the image. We will specify which areas of the image are what kinds of objects since the satellite images are usually huge and can collect much information. Since it is a collection of images from satellites that orbit all the time, even though it can store many data, labeling takes much time and requires experts to do it carefully. That takes much time to prepare data for training models. Moreover, sometimes the data with quality labels are hard to find. Therefore, we adjusted the model training. Typically, in a general model, <strong>U-Net uses pixel-level labels to specify classes for every pixel in the image</strong>. That takes a lot of time and effort. However, in our work, we have adapted to labeling by simply saying whether the image has a class that appears or not. That allows us to save time and expertise.</p><p>To achieve this goal, we used the <strong>Attention layer</strong> to separate each class to create an <strong>Attention map</strong> for land cover mapping. The Attention layer is included in the model structure. Make it learn along with the model while training. Finally, we will take the Attention map through the process and convert it into a segmented image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2-Q3vJJ2Crs2uGju-2ziTw.jpeg" /><figcaption>You will see that this is a task that requires a lot of time. To identify the class in every position. The larger the image volume and the higher the detail, the more time it takes</figcaption></figure><h3><strong>Objectives</strong></h3><p>From the introduction that has already been mentioned, Therefore, the primary purpose of this work can be identified as follow:</p><blockquote>To design a model using deep learning for land cover mapping of satellite imagery by reducing the labeling resources for model training.</blockquote><ol><li>To reduce the time of data labeling.</li><li>To segment the image Without needing to label every pixel.</li><li>To design a convolutional neural network architecture that gives the land cover mapping using the <strong>“Attention map.”</strong></li></ol><h3>Materials and Methods</h3><blockquote><strong><em>1. Dataset</em></strong></blockquote><p>Images from the UC Merced Land Use dataset (Yang &amp; Newsam, 2010) was used in our experiment. This dataset is divided from the large image from the USGS National Map. Images in this dataset contain 2100 images and 256 x 256 pixels with 0.3 meters resolution. All images are captured in the RGB color space, and in this dataset, 17 land cover classes composed of airplanes, buildings, cars, court, dock, mobile homes, pavement, ship, tanks, sea, water, bare soil, sand, chaparral, field, grass, and trees.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oOuMOKqWBBspwu3-T8KAqw.jpeg" /><figcaption>The dataset we used for this work is called the UC Merced land-use dataset, which is a 3-branch satellite image, RGB with a resolution of 30 cm.</figcaption></figure><blockquote><strong><em>2. Pre-processing data</em></strong></blockquote><p>2.1 Combine similar classes</p><p>We grouped them from 17 classes into 4 land cover classes: impervious, water, land, and vegetation due to the limited number of training images.</p><blockquote><strong><em>Impervious (Man-made)</em></strong><em> contains airplanes, buildings, cars, courts, dock, mobile homes, pavement, ships, and tanks.</em></blockquote><blockquote><strong><em>Water</em></strong><em> contains sea and water.</em></blockquote><blockquote><strong><em>Land</em></strong><em> contains bare soil and sand.</em></blockquote><blockquote><strong><em>Vegetation</em></strong><em> contains chaparral, field, grass, and trees.</em></blockquote><p>We combine classes because the data have too many classes compared to the amount of data in each class. That is why we have little data to train the model. So, we combine classes with similar meanings. Moreover, this also makes the model easier to learn because fewer classes reduce model confusion. Moreover, it can also solve the problem of class imbalance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MFGK5rkqnqZ17dNncI7-Lw.jpeg" /><figcaption>Combine similar classes together from 17 classes into 4 classes</figcaption></figure><blockquote><strong><em>3. Data normalization</em></strong></blockquote><p>The normalization That can be done in many ways in this work will scale the image’s pixel value by using standardization, which is computed from each channel of the red, green, blue image. computed by finding the mean and standard deviation of each channel of all data, with this method we found that the result is better than scale into 0-1 range and we assume that our training data is big enough to be a representation for testing data so we will scale testing data with the same mean and standard deviation that we compute from training data</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LJJh6SmGgdJWWgfb7J8_WQ.jpeg" /><figcaption>We found that this method is standardization by calculating separately for channels RGB gets better results</figcaption></figure><blockquote><strong><em>4. Split image</em></strong></blockquote><p>Our network must learn the corresponding between the observed data in an image and class label by itself. We must have a dataset with some training samples having one or two land cover classes. If all of the training data have all land cover classes present in all images, our network will not learn the differences between classes. To achieve this goal, we divide all images and resulting ground truth into a smaller size of 64 x 64 pixels resulting in a total of 33,600 images</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hnrKP_AgSNoPfKWcvMQjBA.jpeg" /><figcaption>Split the image from 256x256 into 64x64</figcaption></figure><p>Let’s look at an example explanation in the case of The classify between dog and cat if every image consists of dogs and cats all the time. And our label is image-level, that is, it just tells what image a dog or cat appears. And the model didn’t know dogs and cats before. That will confuse the model, unable to distinguish that What calls a dog or cat? But if we have images of a single dog, alone, and a cat alone, in the end, the model will learn to distinguish the characteristics of the dog and the cat.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JV1uI_cC_cPLBF9a3mQY-Q.png" /></figure><blockquote><strong><em>5. Label the image using the classes in the ground truth</em></strong></blockquote><p>Since our work does not require a pixel-wise label, we have to say which classes appear in the image. So, we can create a new label from the pixel-wise label by performing one-hot encoding</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Z8lxj7CWD93fOMgrWjysQ.jpeg" /><figcaption>Label the image using the classes in the ground truth</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JEYyOJIcKFUpLkFhQ4absQ.png" /></figure><p>So, if the image size is 64x64 pixels, if using a pixel-level label at 1 image, it will require 4096 labels while an image-level label is used only in 4 labels where we have 4 classes. Which can be seen that it varies with the number of classes Not the size of the image So, at 1 image, pixel-level more than 1024 times the labeling effort is required.</p><blockquote><strong><em>6. Proposed model architecture</em></strong></blockquote><p>In this work, we proposed a neural network to specify the interested class’s localization by segmenting an image-based, only the <strong>presence or absence</strong> of land cover classes. <strong>This process does not require the pixel-by-pixel labeling as in U-Net or bounding box labeling as in Faster R-CNN and YOLO</strong>. The network used in our paper. Here, we have included a concept model from ResNet and NIN (Network in Network) where skip convolution layers are used to avoid vanishing gradients with a filter of size 1 × 1. The Global Average Pooling (GAP) is also employed to include predictive features instead of a Fully Connected layer, preserving the feature map layer’s spatial information. GAP also acts as a regularizer to prevent the network from overfitting. The model does not reduce the image size much because we want the resize to maintain spatial resolution. Convolutional layers use a filter of size 3×3, and all convolutional layers are followed by batch normalization and ReLU activation function, respectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RpF5rm8pVmy1Gr8AVx0TOA.jpeg" /><figcaption>Proposed Network</figcaption></figure><p>Our model structure is deep learning. The model is divided into two parts for feature extraction or base network and attention mapping. In the find decision making, and attention map, and feature map, multiply element-by-element. The attention map uses the softmax activation function with the number of channels equal to the number of classes. (in our example is four for land, impervious, vegetation, and water) our model will assign each class to each different channel in the attention map after training. So the difference between an Attention layer and a feature layer is an <strong>Attention layer followed by a softmax activation function Will be estimated as a probability map Where each pixel value is between 0–1</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*-8dCzBgS0gmty7iO-JFctg.png" /><figcaption>The softmax activation function is used to map the values of the attention map in each channel to have a combination of values across the channel equal to 1</figcaption></figure><p>The softmax activation function is used to map the values of the attention map in each channel to have a combination of values across the channel equal to 1 because we want to separate each channel as each class, so each pixel value of each channel is the probability</p><p>In other words, on the Attention layer we use the softmax activation function to estimate the probability, which will be in the range 0–1, so at any position when the feature map is multiplied by the attention layer, if the probability is close to 0, then that feature is not passed. But if the probability value is close to 1 then it will pass</p><p>Using filtered features to make decisions, we found that using the softmax activation function at the spatial feature for converting to probability values gives the best results. Since each channel was mapped to a class output, that made the model learn to separate each channel map to the order of classes as we defined by labeling correctly. The order of classes is land, impervious, vegetation, and water, respectively. Moreover, if we used the sigmoid activation function at the final output, the model could correctly predict the apparent class, The Attention map will lose spatial data. Moreover, the model cannot separate each channel as we assign it appropriately.</p><p>Moreover, we use both Global Average Pooling and Global Max Pooling to summarize the feature and merge it by averaging. This method gives the best results significantly compared with only Global Average Pooling and Global Max Pooling alone. Global Average Pooling gives the summary feature, which may not indicate the unique feature that makes the model hard to classify the class. However, this can enforce attention layer be able to retain the spatial feature, and Global Max Pooling gives the unique feature that makes the model classify the class; however, this will make the attention layer ignore the spatial feature and focus on the only final output So, the right way is to combine it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wYQRbGKLMW0JWOdBzQQmLg.jpeg" /><figcaption>Compare the three methods and the method that gets the best results is the third method</figcaption></figure><p>Here, the attention map acts as a filter to enable or disable the feature extracted by the feature map passing through the decision-making process. Since the feature maps and Attention maps have the same size and the number of channels, and the multiplication layer is the element-wise multiplication between them. Each feature map has a corresponding attention map. Thus, if a pixel in one channel of the attention map associated with a land cover class (Say A) has a zero value, the feature extracted in this pixel from the corresponding channel in the feature map will be eliminated. Thus, the final decision regarding the presence of will not have any information regarding the feature from class A in this pixel. In contrast, if the same pixel and the same channel has a value of one, the final decision will be made from the information in this pixel with the feature of class A. From this idea, it is clear that the attention map will have a value of one in a given pixel if the feature of the underlying class is present in this pixel</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8SpH5JmAGSq-vgWssPMveA.jpeg" /><figcaption>The attention layer acts as a filter to enable or disable the feature extracted by the feature map passing through the decision-making process</figcaption></figure><blockquote><strong><em>7. Land cover mapping (Segmentation)</em></strong></blockquote><p>For land cover mapping from the attention map, the number of channels in the attention map is equal to the number of classes. We compare each class’s pixel value or each attention map and use the highest value to indicate the land cover class. Hence, every pixel is assigned as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*wVn1hBeQQVAL-FpfM4m3ow.png" /><figcaption>Compare the pixel values of each attention map and choose the highest value to indicate the class</figcaption></figure><blockquote><strong><em>where 𝑆(𝑥, 𝑦) is the class of the pixel (𝑥, 𝑦), and 𝐴𝑐 is the attention map of class 𝑐.</em></strong></blockquote><p>The steps to get a land cover map consists of the following main steps. The first step is to feed the input image into the model. To get output, our model will be able to provide two kinds of outputs. One is the probability of each class’s appearance in the image and the attention map of each class, which we will take the attention map through to a process to get the land cover map again. The second step compares each attention map’s pixel values and chooses the highest value to indicate the class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G2I33AIePQM-7Y4qeA3afA.png" /><figcaption>The process of land cover mapping begins by input images into the model. Which we will set the model to output as an Attention map.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G5QeFJjShxeMtzJn_DLDVw.png" /><figcaption>we compare the pixel probability values for each layer and select the maximum probability value to assign a class to that pixel. And this is the result We can show in the form of Color by mapping the color to each class.</figcaption></figure><h3>Results</h3><p>Our model and U-Net provide the segmented image or land cover mapping, and we will measure the performance of our model for both image-level and pixel-level accuracy. In contrast, the Kappa coefficient is only used for the pixel-level accuracy. Table. 1 and Table. 2 provide the image-level and pixel-level accuracies with the 107,520, respectively. It can be seen that our algorithm outperforms U-Net significantly since 107,520 labels in our algorithm correspond to 26,880 images. In contrast, U-Net uses only 27 images.</p><p>Table. 1 and Table. 2 provide the image-level and pixel-level accuracies with the 107,520, respectively. It can be seen that our algorithm outperforms U-Net significantly since 107,520 labels in our algorithm correspond to 26880 images. In contrast, U-Net uses only 27 images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RZEmC1UenNfhTcrJOBdgUw.png" /><figcaption><strong>Table 1<em> </em></strong>Image-Level Overall Accuracies at the same number of labels</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LSJwEmTsC4RTEYPz7S1SEg.png" /><figcaption><strong>Table 2<em> </em></strong>Pixel-Level Accuracies<em> </em>at the same number of labels</figcaption></figure><p>Next, we examined the performances of our algorithm and U-Net for the different number of training samples. From Table. 3, it can be easily seen that both algorithms perform better as the number of training samples increases. However, the U-Net algorithms require much more effort in the labeling processes. At 100% of training samples, the differences in overall accuracy between U-Net and our algorithm are less than 3%, where the U-Net requires about 1,000 times more labeling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*luHEHZYQnRAadiOMignfBg.png" /><figcaption><strong>Table 3 </strong>Pixel-Level Accuracies as a function of training sample size</figcaption></figure><p>In Table. 4, we investigate the class-level accuracy. We found that, at 100% training samples, the F-1 scores of U-Net is slightly higher than our proposed method. Note again that U-Net requires around 1,000 times more labeling process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23zEU8AlclpjNjuEKAfWYg.png" /><figcaption><strong>Table 4</strong><em> </em>Pixel-Level Accuracies in each class for the case of 100% training samples</figcaption></figure><p>We also investigated the effect of image size on accuracy. Since the image size is large, there are higher chances that the image will contain 2 or more classes. If all training images contain both land and vegetation together, our algorithm cannot find the distinct features between these two classes. To be more specific, It is obvious that land and vegetation have brown and green colors. Our algorithm sees both brown and green color and only knows that there are vegetation and land in the image, but it cannot make a brown link for land and green is for vegetation. Table. 5 demonstrates our hypothesis, as the image size more extensive, the accuracy of our algorithm decreases, whereas the performance of U-Net is roughly constant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WPbGJEyVZTWGa9MA_JWiCg.png" /><figcaption><strong>Table 5</strong><em> </em>Pixel-Level Accuracies as a function of image size for the case of 100% training samples</figcaption></figure><p>plots the required number of labels vs. the overall accuracy. <strong><em>It is clear that if only 80% OA is required, our algorithm can save roughly 1,000 times the number of labels that can save days or weeks preparing the training samples.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JNPSLWRrg6WN9_O5BNZCzw.png" /><figcaption><strong>It is clear from the graph that, if accuracy of 80% is required, our method can save approximately 1000 times of data labeling. Which can save resources both time and money</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iy-oY9ZeM3PcfT0SVZuYRA.png" /><figcaption>Define colors to represent each class</figcaption></figure><blockquote><strong>And this is the final result, land cover mapping or image segmentation at 100% number of the sample (our method uses less than 1,024 times the number of labels compare to U-net).</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JXS85BZUXVTVVwSUfacI_Q.png" /><figcaption>(a) attention map for land, (b) attention map for impervious, (c) attention map for vegetation, (d) attention map for water, (e) observed image, (f) ground truth, (g) proposed algorithm, (h) U-Net, (land: brown, impervious: gray, vegetation: green, water: light blue) (Results from the test set)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hd7rvihYrlP9et61_YasFQ.png" /><figcaption>(a) attention map for land, (b) attention map for impervious, (c) attention map for vegetation, (d) attention map for water, (e) observed image, (f) ground truth, (g) proposed algorithm, (h) U-Net, (land: brown, impervious: gray, vegetation: green, water: light blue) (Results from the test set)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wOARLP-x3-W9Gf2GWQzZlA.png" /><figcaption>(a) attention map for land, (b) attention map for impervious, (c) attention map for vegetation, (d) attention map for water, (e) observed image, (f) ground truth, (g) proposed algorithm, (h) U-Net, (land: brown, impervious: gray, vegetation: green, water: light blue) (Results from the test set)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lZ_CYMMDXML4ixYeoHD5_A.png" /><figcaption>(a) attention map for land, (b) attention map for impervious, (c) attention map for vegetation, (d) attention map for water, (e) observed image, (f) ground truth, (g) proposed algorithm, (h) U-Net, (land: brown, impervious: gray, vegetation: green, water: light blue) (Results from the test set)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wEqxJy5y-PM-TcHZylcdNw.png" /><figcaption>(a) attention map for land, (b) attention map for impervious, (c) attention map for vegetation, (d) attention map for water, (e) observed image, (f) ground truth, (g) proposed algorithm, (h) U-Net, (land: brown, impervious: gray, vegetation: green, water: light blue) (Results from the test set)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ghjAY6cksfRcNCKeo_O45A.png" /><figcaption>(a) attention map for land, (b) attention map for impervious, (c) attention map for vegetation, (d) attention map for water, (e) observed image, (f) ground truth, (g) proposed algorithm, (h) U-Net, (land: brown, impervious: gray, vegetation: green, water: light blue) (Results from the test set)</figcaption></figure><h3>Conclusion and Recommendation</h3><blockquote><strong><em>Conclusion</em></strong></blockquote><p>We proposed a new land cover mapping algorithm using the attention layer for remote sensing images. Our approach’s advantage is that a training set is required to label just four times per image by identifying presences and absences of land cover classes in the entire image rather than label every pixel in the image. This labeling process enables a large amount of training set to be developed quickly. To achieve this goal, we designed our network such that each channel of attention layer corresponding to one and only one land cover class. Thus, the attention layer acts as a switch to allow information from the feature extraction layer to pass through the final decision process. As a result, the only pixels with a given land cover class’s strong presence can pass through. We examined our algorithm’s performance against U-Net. <strong>We found that with the same number of labels, our algorithm can achieve an overall Pixel-Level accuracy of 82.86%, whereas U-Net can only obtain 59.71%.</strong></p><blockquote><strong><em>Recommendation</em></strong></blockquote><p>We found that our model will handle images with a small number of classes. Moreover, poorly handle spatial details and the class location because the label we use only tells that class it appears or not. Therefore, the information used to train the model is very little. Moreover, the model did not learn the class’s spatial position or each class’s density in the image.</p><p>Moreover, for that reason, although the overview of our model works satisfactorily. Moreover, get high accuracy at the same number of labels as U-Net, but that may come from because in the test dataset there are The images are up to 30% in a single class and while U-Net handles spatial resolution and location better So in order for our model to work better And can solve the problems mentioned above We may need to find other ways to increase the accuracy of the model. The potential approach is to <strong>Active learning</strong>, that is, if we need to do more labeling. How do we get smart and effective labeling? How do we choose</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*D3mtlyPboanyG1ZlNpN6Pg.jpeg" /><figcaption>Active learning</figcaption></figure><p>The way this might be done is instead of giving equal importance to every data and its labels. We may prioritize and label the most important data and will most likely help the model perform better. Because if we do not prioritize and assume that all information is equal, We will sometimes find that sometimes the results are useful when randomly selecting data to train. Moreover, sometimes wrong. Therefore, every data is different. However, it also depends on the problem we are facing. We want to find data that is an excellent representative to train the model in simple terms.</p><p>So, with our approach, we initially just said which classes were present. That allows us to use a small number of labels to train the model. And that gives us the baseline model. We will use this model to determine how important the data is. We then choose to label the data and train the new model with more new data. Then repeat this process loop. That will make the model development process more efficient. When we consider terms of the resources required for labeling data. With enhanced model performance</p><p>There are several ways to prioritize data to be labeled. For example, the so-called Least confidence method is where we use a model to predict unlabeled data. Then when we got the answer Select the maximum probability of each data that the model predicts to be that class. Then, sort this maximum probability value in ascending order, so we prioritize each data to be labeled. We will choose to label data with a low probability first because of Predictive models with low confidence. That means the model is still confused with this data.</p><p>Moreover, we are not learning well or have some features That make models hard to learn. So, if we label this data, It should help the model to learn better overall. When dealing with simple data, it should work just fine. Alternatively, when faced with complex data. It should work better because we can add additional labels in training.</p><p>Another issue that we encounter with the dataset is the class imbalance problem we have dealt with by combining classes. Moreover, perform weight loss function, but there are other ways that might help, such as oversampling and undersampling. However, it may have to lose some of the data but may make the model work better.</p><p>Moreover, another issue that may be tried is the adaptation of the base network(feature extraction part). We have done some experiments, but it is impossible to test the hypothesis enough with the limited time. To see the significance of this refinement but as far as I tried to think that there is a certain</p><p>Finally, thank you for everyone’s attention, hopefully, there will be more or fewer benefits to those who are interested. If there are any mistakes, I apologize here too.</p><h3>References</h3><p>[1] <a href="https://ieeexplore.ieee.org/document/9158220">https://ieeexplore.ieee.org/document/9158220</a></p><p>[2] <a href="https://github.com/MailSuesarn/On-the-Use-of-Attention-Map-for-Land-Cover-Mapping">https://github.com/MailSuesarn/On-the-Use-of-Attention-Map-for-Land-Cover-Mapping</a> (included dataset )</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4aebb59885cd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/on-the-use-of-attention-map-for-land-cover-mapping-4aebb59885cd">On the Use of Attention Map for Land Cover Mapping</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduced to the Convolutional Block Attention Module (CBAM)]]></title>
            <link>https://medium.com/super-ai-engineer/introduced-to-the-convolutional-block-attention-module-cbam-19f61a2c98b?source=rss-b546586d119b------2</link>
            <guid isPermaLink="false">https://medium.com/p/19f61a2c98b</guid>
            <category><![CDATA[super-ai-engineer]]></category>
            <category><![CDATA[cbam]]></category>
            <category><![CDATA[expsuperai]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[attention-mechanism]]></category>
            <dc:creator><![CDATA[Mail'Suesarn Wilainuch]]></dc:creator>
            <pubDate>Wed, 17 Feb 2021 02:49:07 GMT</pubDate>
            <atom:updated>2022-06-02T09:32:36.017Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vaz2L10WDhn3o_LnOde7aA.png" /></figure><p>สวัสดีครับ วันนี้ผมอยากจะมารีวิวแนะนำให้รู้จักกับ module ตัวนึงที่คิดว่าน่าสนใจที่สามารถนำไปต่อยอดในการพัฒนาโมเดล รวมถึงประยุกต์ใช้ในงานต่างๆ โดยที่โมดูลนี้ ใช้ Attention mechanism ในการเพิ่มความแม่นยำให้กับโมเดลซึ่งคุ้มค่าเมื่อเทียบกับจำนวน parameter ที่เพิ่มขึ้นเพียงเล็กน้อย แต่เมื่อพูดถึง Attention หลายๆคนคงได้ยินคำนี้บ่อยขึ้น จากชื่อเสียงของ paper Attention Is All You Need โดยที่คำนี้จริงๆมีมานานแล้วเพราะ คำนี้ เป็นเพียง concept ที่ไม่ได้มีรูปแบบตายตัวมีการ implement ที่หลากหลายขึ้นอยู่กับไอเดียและสมการเบื้องหลังว่ามีการออกแบบอย่างไร ดังนั้นถึงแม้จะพูดถึง Attention เหมือนกันแต่ก็ไม่ได้หมายความว่ารูปแบบโครงสร้างจะเหมือนกัน ซึ่งใน paper นี้เสนอสิ่งที่เขาเรียกว่า Convolutional Block Attention Module (CBAM) ซึ่งออกแบบมาให้ใช้กับ CNN 2D ซึ่งโดยปกติก็จะเป็นงานด้าน image โดยออกแบบให้เป็นโมดูลที่สามารถนำไป ปรับใช้กับ backbone CNN ต่างๆ จากผลการทดลองพบว่าโมดูลนี้สามารถเพิ่มความแม่นยำให้กับทุกๆโมเดลที่ทดสอบได้อย่างมีนัยสำคัญ</p><h3>Introduction</h3><p>ก่อนอื่นอยากจะพูดถึง Attention สักเล็กน้อย Attention mechanism เป็นกลไกที่เราออกแบบ (by design) โดยที่ตั้งใจให้โมเดลมีการ focus ไปยัง feature ที่มีความสำคัญในการเลือกคำตอบหรือตัดสินใจ โดยมีพื้นฐานเป็น math ที่ใช้หลักการถ่วงน้ำหนัก (weighted) และการเลือกผ่านเป็นหลัก (filter) กล่าวคือถ้า feature ใดที่มีความสำคัญและส่งผลต่อการตัดสินใจที่ถูกต้องมาก ก็จะได้รับการ weighted ที่สูง หรือ feature ใดที่มีความสำคัญเป็น feature เด่น ก็จะกรองให้ผ่านไปได้ และตัดสิ่งที่ไม่เกี่ยวออกไป ซึ่งกระบวกการดังกล่าวก็เป็นสิ่งที่มนุษย์หรือสิ่งมีชีวิตทำเป็นปกติอยู่แล้ว เนื่องจากโดยปกติเราจะมีการรับข้อมูลเข้ามาเป็นจำนวนมาก สมองไม่สามารถจัดการข้อมูลทั้งหมดได้ในเวลาเดียวกัน จึงต้องมีการเลือกที่จะมองหรือรับรู้ข้อมูลส่วนใดส่วนหนึ่ง เพื่อนำมาประมวลผลและนำไปสู่การตัดสินใจหรือการกระทำ ดังนั้นถ้าโมเดลสามารถเรียนรู้ที่จะโฟกัสข้อมูลที่รับเข้าได้ก็น่าจะทำให้เกิดความแม่นยำมากขึ้นได้</p><p>ตามที่บอกไปว่า Attention มีหลายวิธีแต่วิธีที่จะมาพูดถึงในวันนี้ก็คือ CBAM ซึ่ง paper นี้ ได้กล่าวถึงความสำเร็จของ CNN โมเดลต่างๆที่ผ่านมา ซึ่งจะเป็นในแง่ของ “Network engineering” ที่มีบทบาทสำคัญในการพัฒนา AI เนื่องจากการออกแบบที่ดีจะทำให ้AI มีความสามารถและประสิทธิภาพที่สูงขึ้น โดยโมเดลต่างๆที่ประสบความสำเร็จที่เคยเป็น SOTA (state of the art) และ breakthrough ทำให้ AI มีความแม่นยำที่สูงขึ้นมากและสามารถนำมาใช้ได้จริงมากขึ้นก็ อย่างเช่น ResNet, InceptionNet, XceptionNet, DenseNet และอื่นๆอีกมากมาย ซึ่งโมเดลเหล่านี้โฟกัสไปที่ Skip connection, depth และ width เป็นหลัก ดังนั้นใน paper นี้จึงโฟกัสไปที่ Attention ซึ่งเป็น ประเด็นที่น่าสนใจที่ คล้ายระบบการมองเห็นและรับรู้ของมนุษย์</p><p>ซึ่งใน paper ได้ทำการรีวิวงานก่อนหน้าที่พูดถึง attention ที่ใช้กับ CNN ซึ่งมีที่น่าสนใจหลายงาน หลักๆคือ <a href="https://arxiv.org/pdf/1704.06904.pdf">Residual Attention Network</a> ที่เป็นการทำ attention ที่เป็น residual (โครงสร้างแบบนี้ แทบจะกลายเป็นมาตราฐานไปแล้วสำหรับ deep learning model ในปัจจุบันที่ต้องมีการทำ skip connection) และใช้รูปแบบ encoder-decoder ในการทำ refining feature map ซึ่งจะใช้ sigmoid activation function เป็น key ซึ่งสามารถเรียกว่าเป็น Soft Mask ซึ่งพื้นฐานของมันก็เป็นเหมือนการทำ filter เพื่อกรอง feature ที่สำคัญ อย่างที่ทราบว่า sigmoid จะให้ output ออกมาเป็นช่วงการประมาณความน่าจะเป็น ในช่วง 0–1 ดังนั้น feature ใดที่คูณกับค่าใกล้ 1 ก็จะผ่านไปได้ ส่วน feature ใดที่คูณกับค่าใกล้ 0 ก็จะมีค่าลดลงหรือผ่านไปได้น้อย ด้วยกลไกแบบนี้จึงทำให้โมเดลเรียนรู้ที่จะปรับ weight และทำให้ filter กรองแต่ feature ที่ส่งผลให้การจำแนกทำได้แม่นยำขึ้น และไม่เพียงแค่นั้นยังช่วยในเรื่องของ ความ robust ต่อ noise ของ input อีกด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TcZ_XfI8zL11cxzqB01wNQ.png" /><figcaption><a href="https://arxiv.org/pdf/1704.06904.pdf">Residual Attention Network</a></figcaption></figure><p>และอีกงานที่เป็นพื้นฐานที่สำคัญในหลายๆโมเดลนั่นคือ <a href="https://arxiv.org/abs/1709.01507">Squeeze-and-Excitation module(SE)</a> ซึ่งถือเป็น Attention module ตัวหนึ่งที่ประสบความสำเร็จในแง่ของ ความแม่นยำ และยัง lightweight อีกด้วย เป็นการรีดความสามารถของโมเดลโดยไม่ทำให้เกิด redundancy โดยหนึ่งในโมเดลที่ใช้พื้นฐานไอเดียตัวนี้ คือ EfficientNet ที่เป็น SOTA อยู่นาน แม้ว่าปัจจุบันจะมีหลายโมเดลที่ออกมาเคลมใน paper ว่าสามารถทำความแม่นยำได้สูงกว่าและเร็วกว่าแล้ว แต่ EfficientNet ก็ถือว่าเป็นโมเดลที่น่าสนใจ ทั้งโครงสร้างและไอเดียการ scale ซึ่งโมเดลนี้ได้ใช้ reinforcement learning ในการ search แต่ก็ใช้มนุษย์ร่วมออกแบบด้วยในการ fine tune โครงสร้าง ดังนั้นถ้าเราค้นพบเทคนิควิธีใหม่ๆ ที่สามารถนำมาปรับใช้กับโมเดลต่างๆได ้ก็อาจทำให้โมเดลที่มีอยู่ดีขึ้นได้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jSSQ-OlKX19ofUFS12Ig1g.png" /><figcaption><a href="https://arxiv.org/abs/1709.01507">Squeeze-and-Excitation module(SE)</a></figcaption></figure><p>ซึ่ง Squeeze-and-Excitation นั้นใช้ global average-pooled ในการคำนวณและสร้างสิ่งที่เรียกว่า channel-wise attention แต่อย่างไรก็ดี paper นี้(CBAM)ได้เสนอให้มีการใช้ global max-pooled ร่วมด้วย และ Squeeze-and-Excitation ยังขาดสิ่งที่เรียกว่าการทำ spatial attention ซึ่งมีบทบาทสำคัญในการตัดสินใจว่าควร มอง “ตรงไหน” ซึ่งใน paper นี้ได้ทำการทดลอง ทั้งสองแบบ (channel and spatial attention) ซึ่งพบว่าการใช้ทั้งสองวิธีร่วมกันได้ผลดีกว่าใช้วิธีเดียว</p><h3>Proposed Method</h3><p>Convolutional Block Attention Module (CBAM) สามารถเขียนเป็น notation ได้ดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*sifN8yVYchIipRNG92ILGA.png" /></figure><p>ซึ่งคือ overall attention process ซึ่ง F’’ คือ output สุดท้ายของโมดูล โดยที่โมดูลนี้จะแบ่งเป็นสองส่วนหลักคือ Channel attention module(Mc) และ Spatial attention module(Ms) สามารถอธิบายได้ดังนี้ เริ่มจาก เครื่องหมายการคูณซึ่งเป็นแบบ element-wise multiplication ซึ่งก็คือการคูณกันระหว่าง tensor ตำแหน่งต่อตำแหน่ง โดยมี F เป็น input feature map ที่เข้ามา และมีการแบ่งเป็นโมดูล Mc และ Ms ซึ่งอธิบายได้ดังนี้</p><blockquote><strong><em>Channel attention module</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6aF2EjLtgVDijkHFy6lX0Q.png" /><figcaption><em>Channel attention module</em></figcaption></figure><p>คือการที่เราทำ refinement feature เพื่อเพิ่มการโฟกัสและลด feature ที่ไม่สำคัญโดยโฟกัสไปที่คุณลักษณะใด ที่เป็นลักษณะที่สำคัญของแต่ละคลาสในความสัมพันธ์ที่เป็นคำตอบของคลาสนั้นๆ เนื่องจากแต่ละ channel ของ feature map ก็คือการที่เราทำ feature extraction ของ input ที่เข้ามาดังนั้นก็เป็นเหมือนการที่พยายามเรียนรู้ว่าและโฟกัสว่า “อะไร” คือคุณลักษณะที่มีความหมายเชื่อมโยงกับ input หรือคลาสนั้นๆ โดยจะใช้วิธีการทำ average-pooling ซึ่งในที่นี้ จะเป็นการ average แต่ละ channel ของ feature map ซึ่งก็คือการทำ global average pooling นั่นเอง ซึ่งก็จะคล้ายกับ paper อื่นๆก่อนหน้า แต่ paper นี้ได้เสนอการทำ global max pooling ด้วยเพราะเชื่อว่าจะให้ข้อมูลที่เป็นเอกลักษณ์ที่สำคัญของแต่ละคลาสได้เพิ่มเติม โดยได้ทำการทดลองแล้วพบว่าได้ผลที่ดีกว่า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Oy5peznQqLAzRza1VZBMA.png" /></figure><p>โดยการทำ Channel attention module จะเริ่มจาก aggregate feature map ด้วย average-pooling และ max-pooling ซึ่งเราจะได้ spatial context descriptors สองตัวที่แตกต่างกัน ซึ่งจะมีมิติเป็น c x 1 x 1 เมื่อ c คือจำนวน channel ของ feature map จากนั้นทั้งสองจะถูกส่งไปสร้างเป็น multi-layer perceptron (MLP) ซึ่งจะเป็นแบบ shared weight ซึ่งก็คือการที่เราใช้ weight ชุดเดียวกันแต่ apply กับ input ทั้งสอง ข้อดีคือ ประหยัดทรัพยากรและเป็นการ aggregate information เข้าด้วยกัน เนื่องจาก output ที่ได้จากทั้งสอง input แม้จะแตกต่างกันเพราะ input ทั้งสองต่างกันแต่ก็มีความสัมพันธ์ร่วมกันเพราะใช้ learnable weight ร่วมกัน จากนั้นจะมี hidden layer 1 ชั้นโดยจะลดขนาดเพื่อลด parameter จึงทำให้ได้ c/r x 1 x 1 เมื่อ r คือ reduction ratio จากนั้นจะทำการ merge feature vectors ทั้งสองด้วย element-wise summation ซึ่งก็คือการบวกกันตำแหน่งต่อตำแหน่ง</p><p>ที่ซึ่ง σ ก็คือ sigmoid activation function เพื่อจะปรับให้ค่าที่ได้อยู่ในช่วง 0–1 และ W0, W1 ก็คือ MLP weights นั่นเอง(สังเกตในสมการ Favg และ Fmax จะใช้ weight ชุดเดียวกัน) ซึ่งเราจะเห็นว่า <strong>เราจะนำ Mc(F) ที่ได้ไปคูณกับ F ซึ่งก็คือ input feature map</strong> ซึ่งก็เป็นการทำ weighted feature map หรือเป็นการทำ refinement feature นั่นเอง ซึ่งตรงนี้จะเห็นว่า จะเป็นการทำ broadcasting กับ input ซึ่งจะทำให้ได้ shape เท่ากับ input feature</p><blockquote><strong><em>Spatial attention module</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_WuiAen-xm4-oIOcXBAmWg.png" /><figcaption><em>Spatial attention module</em></figcaption></figure><p>จะเป็นการโฟกัสไปที่ “ตำแหน่ง” แทนซึ่งแตกต่างจาก Channel attention ซึ่ง Spatial จะเป็นการดูว่าตำแหน่งของ feature ใดที่มีความสำคัญ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i0643TuMKnjL-916391HAA.png" /></figure><p>โดยการทำ Spatial attention จะเริ่มจาก apply average-pooling และ max-pooling ซึ่งจะกระทำ operation ทั้งสองใน channel axis ซึ่งจะทำให้ได้ output สองตัวที่มีอย่างละ 1 channel (สมมติมี 512 channels ที่ตำแหน่งหนึ่งๆ เราจะคำนวณค่า average ตลอด channel ที่ตำแหน่งนั้น ก็คือ 512 ค่า ส่วนหา max ก็แค่ ดึงค่า max จาก 512 ค่า) จากนั้น concatenate ทั้งสองเข้าด้วยกันเพื่อสร้าง feature descriptor จากนั้น apply convolution layer เพื่อ aggregate information เข้าด้วยกันจะได้เป็น spatial attention map Ms(F) โดยจะมี output เพียง 1 channel ซึ่งใน paper ใช้ filter ขนาด 7x7 จากนั้นก็นำไปผ่าน sigmoid activation function (σ) เพื่อจะปรับให้ค่าที่ได้อยู่ในช่วง 0–1 จากนั้นก็นำไปทำ element-wise multiplication กับ input อีกครั้งหนึ่งเหมือนกับ Channel attention ซึ่งตรงนี้ก็เป็นการทำ broadcasting ที่เป็นการ map 1 channel คูณกับทุกๆ channel ของ feature ซึ่งค่าของ Spatial attention หลังจากผ่าน sigmoid ที่อยู่ในช่วง 0–1 ก็จะเป็นการทำ refinement feature เพื่อเพิ่มการโฟกัสและลด feature ที่ไม่สำคัญ คล้ายกับ channel attention เพียงแต่อันนี้โฟกัสไปที่ตำแหน่ง นั่นเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nbxy1s-vuMnrvEQC5RChxw.png" /><figcaption>Residual block + Convolutional Block Attention Module (CBAM)</figcaption></figure><p>จากนั้นจะนำโมดูลทั้งสองมารวมเข้าด้วยกันโดยเมื่อมี input image เข้ามา attention module ทั้งสอง ซึ่งก็คือ channel และ spatial จะโฟกัสที่ “what” และ “where ” ตามลำดับ จากนั้นได้พิจารณาการจัดวางทั้งสองโมดูล ทั้งแบบ parallel และ sequential ซึ่งพบว่าการจัดแบบ sequential ได้ผลที่ดีกว่า และจากผลการทดลองยังพบว่า การจัดลำดับโดยให้ channel attention เป็น channel-first (ทางซ้ายของ sequential flow)ได้ผลที่ดีกว่า การจัดแบบ spatial-first เล็กน้อยดังแสดงในภาพ</p><p>ซึ่งจะเห็นว่าโมดูลนี้สามารถนำไป plugin ปรับใช้ได้ในหลายๆ backbone เช่น ใน ResNet block (ส่วนใหญ่เวลา paper มีไอเดียการปรับโครงสร้างหรือพัฒนาโมดูลใหม่ นึกอะไรไม่ออกก็จะนำมาทดลองกับ ResNet เป็นอันดับแรกๆ)</p><p>และนี้คือผลการทดลองส่วนหนึ่งจาก paper ที่แสดงถึงความแม่นยำที่เพิ่มขึ้น(Error rate ลดลง)และสังเกตว่าก็ต้องแลกกับ parameter ที่เพิ่มขึ้นเล็กน้อย ซึ่งตรงนี้ก็คงขึ้นอยู่กับหลายๆปัจจัยว่าจะมองว่าคุ้มค่าหรือไม่ ผลการทดลองได้ทดสอบกับ ImageNet-1K (1000 classes) ในงาน image classification และ MS COCO and VOC 2007 สำหรับงาน object detection</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kqv_Nre8msEOPMxUPVg8_w.png" /><figcaption>Image classification</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hi4IU-FjT94wZvwIiByYbA.png" /><figcaption>Object detection</figcaption></figure><p>นอกจากนั้น paper นี้ยังได้ทำการ visualization โมเดลด้วย <strong>CAM (Class Activation Map)</strong> ซึ่งพบว่าโมเดลที่ใช้โมดูล CBAM มีการโฟกัส discriminative region ที่ดีมากขึ้นครอบคลุมวัตถุและตำแหน่งที่สำคัญในภาพได้ดีขึ้น หากใครอ่านถึงตรงนี้ยังไม่รู้ว่า CAM คืออะไรลองไปอ่านได้ในบทความนี้ที่ผมอธิบายไว้และมีตัวอย่าง code ให้ลองไปศึกษาได้ครับ <a href="https://medium.com/super-ai-engineer/better-understand-why-ai-made-such-decisions-with-cam-class-activation-map-829c128f048f">Better understand why “AI” made such decisions with CAM (Class Activation Map)</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OtMiIt98gZKyDwGegZHpVQ.png" /><figcaption>visualization โมเดลด้วย CAM (Class Activation Map)</figcaption></figure><p>อันนี้เป็นตัวอย่างการ implement CBAM module โดยทำเป็น custom sub-classing layer เพื่อให้สะดวกในการใช้งานโดยประกาศเหมือน layer ทั่วๆไปใน tensorflow เผื่อใครสนใจจะนำไปต่อยอดหรือทดลองปรับปรุงโมเดลตัวเอง code นี้เขียนด้วย Tensorflow2 &amp; Keras</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/485d8c4d4ded4b699b97d5d6e0be49f4/href">https://medium.com/media/485d8c4d4ded4b699b97d5d6e0be49f4/href</a></iframe><h3>Conclusion</h3><p>จะเห็นว่า Attention mechanism นั้นมีหลากหลาย มีการพัฒนาให้เหมาะกับงานที่หลากหลาย ขึ้นอยู่กับการนำไปใช้ที่สำคัญต้องทดสอบและเลือกให้เหมาะสมเพราะไม่มีวิธีใดเหมาะสมกับทุกๆปัญหา และในความคิดของผม Attention mechanism เป็นแค่จุดเริ่มต้น อนาคตจะมีไอเดียหรือวิธีการที่จะสามารถพัฒนา AI ให้มีความสามารถที่เพิ่มขึ้นกว่านี้ได้แน่นอน</p><p>สุดท้ายหวังว่าบทความนี้จะมีประโยชน์ต่อผู้ที่สนใจไม่มากก็น้อย หากมีข้อผิดพลาดประการใดต้องขออภัยไว้ ณ ที่นี้ด้วยครับ</p><h3>References</h3><p>[1] <a href="https://arxiv.org/abs/1807.06521">https://arxiv.org/abs/1807.06521</a></p><p>[2] <a href="https://arxiv.org/abs/1704.06904">https://arxiv.org/abs/1704.06904</a></p><p>[3] <a href="https://arxiv.org/abs/1709.01507">https://arxiv.org/abs/1709.01507</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19f61a2c98b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/super-ai-engineer/introduced-to-the-convolutional-block-attention-module-cbam-19f61a2c98b">Introduced to the Convolutional Block Attention Module (CBAM)</a> was originally published in <a href="https://medium.com/super-ai-engineer">Super AI Engineer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Better understand why “AI” made such decisions with CAM (Class Activation Map)]]></title>
            <link>https://medium.com/super-ai-engineer/better-understand-why-ai-made-such-decisions-with-cam-class-activation-map-829c128f048f?source=rss-b546586d119b------2</link>
            <guid isPermaLink="false">https://medium.com/p/829c128f048f</guid>
            <category><![CDATA[class-activation-map]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[super-ai-engineer]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[expsuperai]]></category>
            <dc:creator><![CDATA[Mail'Suesarn Wilainuch]]></dc:creator>
            <pubDate>Mon, 01 Feb 2021 10:52:53 GMT</pubDate>
            <atom:updated>2021-02-05T15:59:31.767Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nyPmmK_cQInOsyEjB_OKJg.gif" /><figcaption>Super AI Engineer Heatmap Image</figcaption></figure><h3>Introduction</h3><p>สวัสดีครับ ก่อนอื่นผมอยากถามทุกท่านที่เปิดเข้ามาอ่านบทความนี้ ภาพที่ท่านเห็นด้านบน ท่านมองครั้งแรกจะมองจุดใดของภาพก่อนครับ ถ้าให้ผมเดาก็คงจะเป็นบริเวณกลางภาพเหมือนที่ได้ไฮไลท์ไว้ใช่ไหมครับ?</p><p>ภาพที่แสดงด้านบนโดยปกติเราจะเรียกว่า “Heatmap” ซึ่งก็คือการที่เราแสดงภาพด้วยสี (Colormaps)โดยส่วนมากจะแสดงร่วมกับภาพพื้นหลังซึ่งเป็นข้อมูลที่เราสนใจ โดยการตีความหมายนั้น บริเวณที่เป็นสีโทนร้อน (แดง) จะหมายถึงมีค่าสูงและบริเวณที่เป็นสีโทนเย็น (สีน้ำเงิน) จะหมายถึงมีค่าต่ำ แล้วสูงต่ำแค่ไหนวัดจากอะไร ก็วัดเทียมกับค่าสูงสุดและค่าต่ำสุดที่มีและไล่ระดับของค่าด้วยการไล่ระดับอุณหภูมิของสี ซึ่งค่าสูงต่ำก็สามารถนำไปตีความหมายได้หลายอย่างขึ้นกับว่า ตัวแปรที่เราสนใจเป็นอะไรเช่น เราอาจจะหมายถึง ค่าความหนาแน่นของบางสิ่ง, ค่าความถี่ของบางสิ่ง หรือค่าอะไรก็ตามที่มีระดับ</p><h3><strong>General part</strong></h3><p>ถึงตรงนี้หลายๆคนคงพอเข้าใจความหมายการแสดงภาพในลักษณะนี้กันแล้ว แต่บทความนี้เราจะพูดถึงสิ่งที่เรียกว่า “Class Activation Map” เรียกย่อๆว่า CAM ซึ่งเป็นคำในวงการ AI ซึ่งเป็นที่นิยมในการทำ Visualization (การทำให้มองเห็นเป็นภาพ) ซึ่งจริงๆแล้ว CAM ก็สามารถตีความได้ในลักษณะเดียวกันกับ Heatmap</p><p>ก่อนอื่นต้องบอกก่อนว่าบทความนี้มีจุดประสงค์คืออยากเล่าและแสดงให้เห็นว่า AI (Deep learning)ไม่ได้เป็นสิ่งที่เรียกว่า black box model 100% ทั้งหมด เราสามารถที่จะพอทราบและวิเคราะห์ได้ว่าทำไมมันถึงตัดสินใจแบบนั้น มันมองอะไร ใช้คุณลักษณะ (feature) หรือเกณฑ์ในการตัดสินใจเหมือนคนหรือไม่ (ในงานที่ง่ายที่เราอธิบายได้) และช่วงท้ายจะพูดถึงรายละเอียดของ CAM และสำหรับผู้ที่สนใจนำไปศึกษาต่อยอด ผมได้ให้ตัวอย่าง demo code สำหรับการทดลองไว้ด้วย</p><p>ในงานด้าน image ไม่ว่าจะเป็นในสายงานไหนที่มีการใช้ข้อมูล image ก็สามารถ นำ AI มาประยุกต์ใช้ได้ทั้งสิ้น ซึ่งจะว่าไปเราก็มีการนำ image มาใช้ประโยชน์อยู่แล้ว โดยสามารถนำ image มาคำนวณอะไรบางอย่าง (image processing) เพื่อให้ได้ข้อมูลที่มีประโยชน์นำไปใช้ต่อ ซึ่งการคำนวณเล่านี้ปกติจะเป็นการคำนวณที่เรารู้ถึงขั้นตอนต่างๆ เนื่องจากเป็นการคำนวณหรือ algorithm ที่เรากำหนดขึ้นมาทั้งหมด แต่มันก็มีขีดจำกัดในงานที่มีความซับซ้อนสูง(เราไม่รู้จะโปรแกรมให้คอมพิวเตอร์ทำงานอย่างไรดี) ยกตัวอย่างง่ายๆถ้าเราอยากจะแยกภาพแมวและหมาด้วยคอมพิวเตอร์เราจะเขียนโปรแกรมอย่างไรดีให้ครอบคลุมภาพแมวและหมาให้ได้มากๆ ทุกๆสายพันธุ์ ทุกๆมุมมองของภาพ แบบที่คนทำได้ ซึ่ง AI (Deep learning ในที่นี้)คือ algorithm ที่สามารถจัดการกับปัญหาแบบนี้ได้โดยให้ผลลัพท์ที่ดีกว่าวิธีก่อนๆมาก</p><p>โดยเราจะมาพูดถึงงาน image ที่เป็นที่นิยมและเป็นพื้นฐานที่สำคัญในการต่อยอดความสามารถไปเป็นงานอื่นๆ นั่นคือ “Image classification” หรือเราอาจเรียกว่า Image recognition ซึ่งก็คือการจดจำหรือการรู้จำภาพ แบบที่มนุษย์อย่างเราๆทำกันเป็นปกติ ซึ่งเมื่อเราจดจำภาพของสิ่งต่างๆได้ก็นำไปสู่การจำแนกภาพสิ่งต่างๆได้ ว่าภาพนี้ที่เราเห็นคือสิ่งที่เราเรียกว่า แมว, หมา, และอื่นๆอีกมากมายที่เรามีประสบการณ์เคยเห็นและรู้จัก ดังนั้นถ้าเราสามารถทำให้คอมพิวเตอร์มีความสามารถแบบนี้เหมือนคนได้ มันก็น่าจะสามารถนำไปต่อยอดในงานได้หลายๆอย่าง โดยเราสามารถมอง AI หรือ model เป็นกล่องหรือระบบที่เมื่อเราป้อน input เข้าไป จะมีการทำงานอะไรบางอย่างภายในและให้ output ออกมา ซึ่ง input ของเราในงานนี้ก็คือ image นั่นเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*nHGOgiEVJpMq4g0avN0hMw.jpeg" /><figcaption>มอง AI เป็นโมเดลหรือระบบที่มีการป้อน input เข้าไปและจะได้ output ออกมา</figcaption></figure><p>image คือข้อมูลที่มี 2 dimension หรือก็คือ 2 มิติ (กว้าง x ยาว) โดย image ที่เราพูดถึงในที่นี้คือ digital image ส่วน output ที่เราต้องการจากระบบก็คือคำตอบว่า image ที่ป้อนเข้ามาคือภาพอะไรหรือเป็นประเภทใด ซึ่งจะเห็นว่าตรงนี้จะเป็นสิ่งที่เรานิยามขึ้นมาเองเหมือนเป็นการตั้งชื่อ เช่นเราเรียกสัตว์ที่มีลักษณะแบบนี้ว่าแมว หรือแบบนี้เรียกว่าหมา โดยที่เราสอน AI ให้แยกสิ่งของที่มีความแตกต่างกัน (คุณลักษณะ)ดังนั้น output ที่ได้จะเป็นเป็นตัวเลขเช่นเดียวกับ input โดย ตัวเลขนี้จะเป็น 1 มิติ เพราะมันคือตัวเลขที่ represent คำตอบของเราโดยเราได้กำหนดเอาไว้ตอนที่สอน AI เช่น เลข 0 หมายถึง แมว เลข 1 หมายถึง หมา จากนั้นเราจึงนำตัวเลขที่ได้ไปแปลงกลับเป็นชื่อที่เรากำหนด โดยคำตอบที่ได้จะถูกประมาณเป็นค่าความน่าจะเป็นของแต่ละประเภท โดยเราก็จะเลือกคำตอบเป็นคลาส (ประเภท) ที่มีโอกาสหรือความน่าจะเป็นสูงสุด ซึ่งภาพรวมคร่าวๆก็จะประมาณนี้ ทีนี้เราจะพบว่า เมื่อเราป้อนภาพเป็น input เข้าไปในโมเดลเราก็จะได้คำตอบออกมาเป็นตัวเลขที่เชื่อมโยงกับคลาสที่เรากำหนด โดยที่เราไม่รู้ที่มาที่ไปว่าทำไมมันถึงตอบแบบนี้ ไม่รู้ว่ามันไปคูณ ไปบวกกับค่าอะไรบ้าง เนื่องจากโมเดลจะมีจำนวนค่าสัมประสิทธิ์ ที่เยอะมากเป็นแสน เป็นล้านค่า ดังนั้นเราไม่สามารถไปตามดูได้ทุกๆ step และโมเดลก็ออกแบบมาให้เรียนรู้จากข้อมูลโดยที่เราไม่ต้องโปรแกรมล่วงหน้า ดังนั้น</p><blockquote>จะเป็นไปได้ไหมที่เราจะพอบอกได้ว่าการที่โมเดลตัดสินใจตอบแบบนี้มันมองหรือให้ความสำคัญที่จุดไหนของภาพหรือใช้ คุณลักษณะใดในการตัดสินใจ</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/253/1*dL-etW5EJ-ifLjQ9azJe4A.jpeg" /><figcaption><a href="https://th.wikipedia.org/wiki/%E0%B9%80%E0%B8%AE%E0%B9%80%E0%B8%8B%E0%B8%A5%E0%B8%99%E0%B8%B1%E0%B8%95"><strong>เฮเซลนัต</strong> (hazelnut)</a></figcaption></figure><p>โดย CAM คือวิธีที่ทำให้เรารู้ว่าคุณลักษณะใดหรือจุดใดในภาพที่โมเดลใช้เป็นเกณฑ์ในการตัดสินใจมากที่สุด โดยเพื่อให้เห็นภาพผมจึงได้ทำการทดลองกับตัวอย่างชุดข้อมูลที่ง่าย เพื่อที่จะเห็นภาพชัดเจน โดยเป็นภาพ <a href="https://th.wikipedia.org/wiki/%E0%B9%80%E0%B8%AE%E0%B9%80%E0%B8%8B%E0%B8%A5%E0%B8%99%E0%B8%B1%E0%B8%95"><strong>เฮเซลนัต</strong> (hazelnut)</a> เป็นหนึ่งในชุดข้อมูลจาก <a href="https://www.mvtec.com/company/research/datasets">MVTec</a> ซึ่งมี dataset ภาพในอุตสาหกรรมที่หลากหลายพร้อม label ให้เราได้ไปทดลองสำหรับงานวิจัยได้ฟรี เพื่อนๆคนไหนกำลังมองหา dataset เพื่อนำไปทดสอบกับ algorithm หรือโมเดลตัวเอง สามารถเข้าไปโหลดได้เลยครับ แต่ถ้ามีการตีพิมพ์ต้องมีการ citation ตามที่ระบุไว้ในเว็บด้วยนะครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TKGO62-E4wgJxmCUdwP0YQ.png" /><figcaption>As part of MVTec’s commitment for advancing machine vision research, MVTec offers several <strong>datasets to download</strong>.</figcaption></figure><p>โดยผมมีภาพ hazelnut ทั้งหมด 170 ภาพ คำถามคือ ถ้าผมไม่บอกอะไรเพื่อนๆเลยบอกแค่ว่าช่วยแบ่งภาพทั้งหมดนี้ออกเป็น <strong><em>สองกลุ่ม </em></strong>ให้หน่อย สุดท้ายผมเชื่อว่าส่วนใหญ่ก็จะแบ่งภาพออกเป็นสองกลุ่มได้ และถ้าผมถามว่าใช้หลักเกณฑ์อะไรในการแบ่งดูจากอะไร เพื่อนๆส่วนใหญ่ก็จะตอบว่าดูที่ผิวของ hazelnut โดยจากที่สังเกตจะพบว่ามี hazelnut ที่มีผิวเรียบปกติไม่มีรอยตำหนิ และอีกแบบก็คือมีรอยตำหนิที่ผิวโดยอาจเป็นรอยขีด รอยแตก รอยประทับตัวอักษร ซึ่งเราจะพบว่า hazelnut สองกลุ่มนี้มีคุณลักษณะที่แตกต่างกัน สามารถแยกได้อย่างชัดเจน ดังนั้นผมจะสอนให้ AI จำแนก hazelnut ออกเป็นสองกลุ่ม โดยผมกำหนดให้กลุ่มหนึ่งเรียกว่า good หมายถึงสภาพดี และอีกกลุ่มหนึ่งเรียกว่า bad คือมีรอยตำหนิ และเรามีเฉลยสำหรับทุกๆภาพในการสอน AI ซึ่งสุดท้ายถ้า AI เรียนรู้จนสามารถจำแนกภาพได้ถูกต้องแม้จะเป็นภาพที่ไม่เคยเห็นมาก่อน แล้ว AI จะใช้หลักการมองแบบเดียวกับเราหรือไม่?</p><p>และนี้คือผลการทดลอง ผมได้พล็อต CAM ซ้อนกับ ภาพ input ซึ่งผมจะพล็อต ในทุกๆรอบการเรียนของ AI เมื่อเรียนภาพทั้งหมดที่มีจนครบทุกภาพเราจะนับเป็น 1 epoch และการเรียนจะวนซ้ำแล้วแต่เรากำหนดว่าจะให้เรียนทั้งหมดกี่รอบ (กี่ epoch) ซึ่งในภาพชุดแรกภาพซ้ายคือภาพที่ใช้เป็น input ในการสร้าง CAM ซึ่งเป็น test set ก็คือภาพที่อยู่นอก training set ที่ใช้สอน AI และภาพขวาคือ CAM ที่พล็อตซ้อนกับ ภาพซ้าย จะเห็นว่ารอบแรกๆของการเรียน AI จะยังตอบผิดอยู่แสดงด้วยตัว <strong>อักษรสีแดง (Predicted|Label)</strong> คำซ้ายคือคำตอบที่ได้จาก AI คำขวาคือเฉลย ดังนั้นสีแดงคือตอบผิดไม่ตรงกับเฉลย และถ้าสังเกตที่ CAM เราจะเห็นว่าในรอบแรกๆ การโฟกัสของคุณลักษณะจะยังไม่ถูกต้องจะเป็นเหมือนการมองภาพกว้างๆก่อน หลังจากเรียนรู้ไปซักพักก็จะรู้ว่าควรมองที่จุดใดเพื่อให้สามารถจำแนกประเภทได้ สังเกตว่ากรณีมีตำหนิ (bad) AI จะให้ความสนใจไปที่รอยของตำหนิ เช่น รอยขีด, รอยแตก ส่วนกรณีปกติก็จะมองไปที่ผิวบริเวณกลางๆของผล ซึ่งนี่สามารถทำให้เราบอกได้ว่า AI มีการเรียนรู้ในการจำแนกโดยใช้คุณลักษณะที่ถูกต้องในการจำแนก แบบเดียวกับที่คนใช้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*YCg7sgOEnGMp8kgzcLaHBA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*mG373xU76nwaxTFhX_Su5w.gif" /><figcaption><strong>Left:</strong> Input Image (from test set) <strong>Right:</strong> CAM Visualization with <strong>pre-trained</strong> EfficientNet model<strong> (Softmax)</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*YCg7sgOEnGMp8kgzcLaHBA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*0JuNFI-kpmGvAlDQF9afxQ.gif" /><figcaption><strong>Left:</strong> Input Image (from test set) <strong>Right: </strong>CAM Visualization with <strong>pre-trained</strong> EfficientNet model <strong>(Sigmoid)</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*YCg7sgOEnGMp8kgzcLaHBA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*rhgvQXI6hKK2dVBKr_8_YQ.gif" /><figcaption><strong>Left:</strong> Input Image (from test set) <strong>Right: </strong>CAM Visualization with training <strong>from scratch</strong> EfficientNet model <strong>(Softmax)</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*YCg7sgOEnGMp8kgzcLaHBA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*QTsNh5MT4qg6gTQ6mr81uA.gif" /><figcaption><strong>Left:</strong> Input Image (from test set) <strong>Right: </strong>CAM Visualization with training <strong>from scratch</strong> EfficientNet model <strong>(Sigmoid)</strong></figcaption></figure><h3>Technical part</h3><p>นอกจากนั้นจะเห็นว่าผมได้ทำการทดลองโดยแบ่งการสร้าง CAM ด้วย pre-trained model และ model ที่ train from scratch เรารู้ว่าการใช้ pre-trained model จะช่วยให้เราเทรนได้ไวขึ้น เนื่องจาก weight ของโมเดลได้มีการเรียนจากชุดข้อมูลอื่นมาก่อนแล้ว ทำให้เราสามารถเทรนโมเดลให้ลู่เข้าได้เร็วขึ้นมาก นอกจากนั้นยังถือว่าเป็น initialization weight ที่ดีเนื่องจากกระบวนการเทรน neural network เป็น stochastic และ gradient-based ดังนั้นในการเริ่มต้นที่ดีก็มีส่วนว่าโมเดลจะ converge ได้เร็วแค่ไหนจะติด local minima ไหม ดังนั้นถ้าเรา visualization ด้วย CAM เราจะเห็นความแตกต่างระหว่าง โมเดลที่เทรนด้วย pre-trained และ โมเดลที่เทรน from scratch หรือไม่</p><p>และเนื่องจากปัญหานี้เป็น binary classification ดังนั้น output สามารถกำหนดได้สองแบบ คือ กำหนดให้เป็น 1 node และใช้ sigmoid activation function และเลือกด้วยค่า threshold กับอีกวิธีคือกำหนดให้เป็น 2 nodes และใช้ softmax activation function และเลือกค่า Max</p><blockquote><em>ซึ่งจะพบว่ากรณี pre-trained ทั้ง softmax และ sigmoid โมเดลต่างก็ให้ผล CAM ที่ดีกว่ากรณีเทรน from scratch อย่างเห็นได้ชัดเจนด้วยจำนวนรอบการเทรนที่พอๆกัน ในการทดลองใช้ epoch ในการเทรน from scratch มากกว่า แต่ก็ยังไม่สามารถ focus ได้ดีเท่า pre-trained อาจหมายถึงโมเดลยังไม่สามารถ converge ไปถึงจุดที่สามารถไปได้</em></blockquote><p>และอีกประเด็นที่น่าใจสนคือ เราจะเห็นว่า กรณีของ softmax และ sigmoid นั้นจะได้ CAM ที่แตกต่างกัน คือ กรณีของ softmax ไม่ว่า hazelnut จะมีตำหนิหรือไม่มี จุดโฟกัสก็จะอยู่บริเวณกลางๆของผล แต่กรณีของ sigmoid ถ้าไม่พบจุดตำหนิหรือเป็นคลาส good CAM จะมีค่าสูงบริเวณรอบๆ ก่อนที่เราจะมาดูว่าทำไมเรามาทำความเข้าใจว่า CAM สร้างอย่างไรกันก่อนครับ</p><h3>How to create CAM</h3><p>ในงานด้าน image โครงสร้างโมเดลที่นิยมในการทำ feature extraction ก็คือ CNN (Convolutional Neural Network) ซึ่งโดยปกติเมื่อเราทำ Convolution ไปเรื่อยๆ จำนวน channel จะเพิ่มขึ้นและขนาด width, height จะลดลง ซึ่งการทำ Convolution จะทำให้ข้อมูลของเรายังเป็น 2D หรือก็คือมีความเป็น spatial feature อยู่ ดังนั้นถึงขนาดจะลดลงแต่ก็เป็นสัดส่วนกับขนาด input ถ้าเราสเกลกลับไปขนาดเท่าเดิม ตำแหน่งของ feature ที่เราสกัดได้ ก็จะตรงกับตำแหน่งของ raw feature นั้นๆ แต่เมื่อเราจะแปลง feature ที่เราสกัดได้ไปสู่การทำ classification เราจำเป็นต้องแปลงให้อยู่ในรูป 1D เพื่อทำ <strong>Fully connected layer</strong> กับ output node ซึ่งวิธีการที่ทำกันก็คือการเอา feature map มายืดออกทุกๆพิกเซลให้เป็น 1 มิติ ซึ่ง การทำแบบนี้จะทำให้มีจำนวน parameter(weight) สูงมากที่ชั้น FC (Fully connected layer) รวมถึงยังมีปัญหา overfit ซึ่งโมเดลอย่าง Network in Network (NIN), GoogLeNet ก็ได้พยายามแก้ปัญหานี้โดยใช้ <strong>GAP (Global Average Pooling)</strong> layer ในการแปลงจาก 2D เป็น 1D การทำ GAP คือการที่เราหาค่าเฉลี่ยของทั้ง channel รวมเป็นค่าเฉลี่ยค่าเดียว ดังแสดงในภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OJ1JKmJmkvMxgCbCMZ0x5g.png" /><figcaption><strong>Global Average Pooling 2D (GAP)</strong></figcaption></figure><p>ซึ่งเราจะเห็นว่าการทำ GAP กับแต่ละ channel ของ feature map ในชั้นสุดท้ายของ CNN เราจะมีจำนวน neuron เท่ากับจำนวน channel ที่ชั้น last conv เช่น ถ้าเรามีจำนวน channel เท่ากับ 512 เราก็จะมีจำนวน neuron เท่ากับ 512 node ที่จะไปเชื่อมต่อเป็น FC กับ output node ซึ่งเราจะลดจำนวน weight ตรงนี้ได้มากช่วยให้โมเดลเรา lightweight ขึ้น และลดปัญหา overfitting ได้ด้วย ดังนั้นเราจะเห็นว่า pre-trained model ปัจจุบันส่วนมากจะมี option เป็น GAP ให้เลือกนอกจาก Flatten</p><p>ซึ่งใน paper <a href="https://arxiv.org/abs/1512.04150"><strong>“Learning Deep Features for Discriminative Localization”</strong></a> พบว่า การทำ Flatten หรือยืดทุกพิกเซลออกและใช้ FC layer ในการแปลงจาก 2D เป็น 1D เพื่อทำ classification จะทำให้ความสามารถดั่งเดิมในเชิงพื้นที่และตำแหน่งของ CNN เสียไป และ paper ได้ทำการ revisit ตัว GAP และพบว่า GAP ไม่เพียงแต่จะเป็น regularizer แต่ยังช่วยรักษาความสามารถของ localization ไปจนถึง conv ชั้นสุดท้ายได้ดีมากขึ้น โดยนำไปประยุกต์ในการ ระบุ discriminative image regions ด้วย forwardpass เพียงครั้งเดียว ซึ่งนำไปสู่การทำ weakly supervised object localization ซึ่งสามารถนำไปปรับใช้กับ CNN model ได้หลากหลาย โดย paper ได้นำเสนอสิ่งที่เรียก ว่า Class Activation Map (CAM)</p><p>โดยที่ CAM จะประยุกต์ใช้ Global Average Pooling (GAP) กับ CNN ซึ่ง CAM จะแสดงพื้นที่ในภาพที่มีการจำแนกแยกแยะออกเป็นประเภทอย่างเฉพาะเจาะจง กับแต่ละคลาส ตามที่แสดงในภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XDR8NDL-PiMD1y7Cr97GIA.png" /><figcaption>ภาพจาก <strong>paper </strong><a href="https://arxiv.org/abs/1512.04150"><strong>Learning Deep Features for Discriminative Localization</strong></a></figcaption></figure><p>จากภาพเราจะเห็นว่า หลังจากทำ GAP เราจะมีจำนวน weight สำหรับแต่ละ output node (แต่ละคลาส) เท่ากับจำนวน channel ที่ last conv และเมื่อเชื่อมต่อเป็น FC layer เราก็จะมีชุดของ weight สำหรับแต่ละคลาสแยกกัน โดยที่ใช้ conv layer ชั้นสุดท้ายเพราะเป็น high level feature ที่สามารถใช้เพื่อระบุตำแหน่ง feature ที่สำคัญของแต่ละคลาสได้ ถ้าเราไปใช้ conv ชั้นแรกๆ จะเป็น low level feature ซึ่งเราอาจไม่ได้ feature ที่มีความเจาะจงกับคลาสที่เป็นคำตอบได้</p><p>โดยหลักการและขั้นตอนการสร้าง CAM จะเป็นดังนี้ คือ</p><ol><li>กระบวนการนี้จะทำหลังจากที่เรา train model เสร็จแล้วนั่นหมายถึง weight จะผ่านกระบวนการเรียน อัพเดต weight ให้มีการตอบสนองต่อข้อมูล input</li><li>เมื่อเราป้อน input เข้าไป feature ของ input คลาสนั้นๆก็จะผ่านเข้าไปใน CNN ซึ่งจะผ่านชั้น filter ต่างๆ ที่จะมีการตอบสนองต่อ input feature ต่างกัน ดังนั้นถ้าเราไม่ได้ป้อน input ของคลาสๆหนึ่งก็ยอมไม่มี feature ของคลาสนั้นและ weight ที่ response กับคลาสนั้นก็จะไม่ activate มากนัก</li><li>เราจะดูว่าโมเดลมีผลการ ทำนายต่อ input เป็นคลาสใด หรือ คลาสใดที่มีการ activate ซึ่ง class ที่ activate คือคลาสที่มีค่าความน่าจะเป็นสูงสุดที่ประมาณได้จาก softmax ซึ่งการที่ class นั้นมีค่าความน่าจะเป็นสูงสุดแสดงว่า raw logits (ค่าก่อนเข้า softmax) ก็จะมีค่าสูงที่สุด ซึ่ง ก็คือค่า weighted sum ของ feature ที่เข้ามา คูณกับชุด weight ของคลาสนั้น ดังนั้น input feature ที่เข้ามาเกิด high response กับ weight ของคลาสนั้น ก็จะหมายถึง feature ของ input นั้นเป็นของคลาสนั้น นั่นเอง</li><li>จากนั้นเราจะเลือกชุดของ weight ที่เชื่อมต่อกับคลาสที่เป็นคำตอบ(เลือกตามความน่าจะเป็นสูงสุดที่ได้จาก softmax activation function) เช่นจากในภาพด้านบน จะเห็นว่าโมเดลตอบออกมาเป็นคลาส Australian terrier ซึ่งเป็นชื่อ สายพันธ์ของสุนัขในภาพ weight ชุดนี้จะนำไปคูณกับแต่ละ channel ของ last conv ดังแสดงในภาพ เราจะเห็นว่าบางตำแหน่งของแต่ละ channel ก็มีการ activate ขึ้นมาต่อ input feature ของตำแหน่งนั้น ดังนั้นการที่เราทำ GAP จะทำให้ค่า average รวมมีค่าสูงขึ้นมา แต่การที่เรานำ weight ของคลาสที่ activate ไปคูณก็เพื่อเป็นการ weighted feature ที่ส่งผลหรือมีความสำคัญต่อคำตอบที่ได้ ซึ่งถ้าค่า weight ที่นำไปคูณมีค่าน้อยก็จะส่งผลให้ feature map ตำแหน่งนั้นมีค่าลดลง เกิดการหักล้างหรือเสริมกันจนสุดท้ายเราจะได้ ผลรวมออกมาเหลือ 1 channel ซึ่งนั่นก็คือ CAM ที่คำนวณได้นั่นเอง</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8H1nolqZFbs2f9AQTFwdlg.png" /></figure><p>โดยจากสมการใน paper fk ก็คือ feature map ที่ชั้น k และ wk คือ weight ที่สอดคล้องกับ feature map ชั้นที่ k และ c หมายถึง weight ชุดนี้ เชื่อมต่อไปที่คลาส c และ x, y ก็คือตำแหน่งพิกเซล 2D ซึ่งอธิบายด้วยภาพด้านบนนั่นเอง</p><p>โดยถ้าเราจะมองเป็น 3D กระบวนการสร้าง CAM จะเป็นดังภาพนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qfjPIfOYX7GQx3ma44QqVg.png" /><figcaption>Last Conv with GAP</figcaption></figure><p>สมมติว่า คลาสที่ activate เป็น class 1 ซึ่ง weight ที่เชื่อมต่อกับคลาส 1 แสดงด้วยเส้นสีแดง ดังนั้นเราจะใช้ weight ชุดนี้ไปคูณกับ last conv เพื่อสร้าง CAM</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NZZWI97Wn4cyVlYTFotqhg.gif" /><figcaption>Simulation of the CAM generation in 3D</figcaption></figure><p>ดังนั้นถ้างานของเราเป็น multi-label คือใน 1 ภาพ มีคลาสหรือวัตถุที่สนใจมากกว่าหนึ่งคลาส เราก็สามารถ generate CAM ที่สอดคล้องกับแต่ละคลาสที่เป็นคำตอบได้เราก็จะได้ discriminative image regions ที่เป็นการทำ localization ของแต่ละวัตถุหรือคลาสที่ปรากฎในภาพได้</p><p>แต่จะเห็นว่า CAM ถูกสร้างจาก feature map ที่ชั้น last conv ซึ่งมีการทำ pooling หรือลดขนาดด้วย strided convolution หลายครั้ง ซึ่งจะมีขนาดที่เล็กกว่าขนาด input image ดั่งเดิม ดังนั้น CAM ที่ได้จะเป็นภาพ low resolution ซึ่งในการนำไปพล็อตเทียบตำแหน่งกับ input ก็จะนำไปทำการ interpolate สเกลให้มีขนาดเท่ากับขนาด input แต่ก็จะทำให้ ภาพที่ได้ไม่คมเป็นพิกเซลแตก</p><p>ดังนั้นวิธีหนึ่งที่แก้ได้กรณีที่เราต้องการ ภาพ CAM ที่คมชัดมีตำแหน่งที่สอดคล้องกับ input มากขึ้น ก็คือการออกแบบให้ CNN มีการ upsampling กลับมาขนาดเท่าเดิม โดยถ้าค่อยๆ up ก็จะช่วยให้ภาพคมชัดขึ้นได้ เพราะ feature map ที่ last conv ก็จะมีขนาดเท่ากับ input image ซึ่งก็จะคล้ายกับโครงสร้างอย่าง Auto-encoder ที่มีช่วง downsampling ขนาดและ upsampling ก็จะคล้ายกับโครงสร้างโมเดลสำหรับงาน semantic segmentation หรือ image segmentaion แต่การเพิ่มช่วง decoder ก็ต้องแลกกับจำนวน parameter หรือ weight ที่เพิ่มขึ้น</p><p>กลับมาต่อประเด็นสุดท้ายที่ติดไว้ ว่าทำไม CAM ที่ได้จาก sigmoid 1 node output ถึงต่างกับ softmax 2 node ถึงตรงนี้เพื่อนๆคงจะเห็นความแตกต่างที่สำคัญอย่างหนึ่งคือ ถ้าเราใช้ softmax เราจะมีชุดของ weight สองชุด สำหรับสอง class แต่กรณี sigmoid เราจะมี weight เพียงชุดเดียว</p><p>ซึ่งถ้าเราลองพิจารณาถึง สมการ sigmoid ในการแปลง logists (real-valued scalar) ไปเป็นค่าความน่าจะเป็นในช่วง [0, 1] ซึ่งก็คือ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*xN9aUo5-QagSWLfInmYHcA.jpeg" /><figcaption>sigmoid activation function</figcaption></figure><h4>โดยที่</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ChI6g3txhWJqYE-jN0mz7g.jpeg" /></figure><p>เมื่อ E คือ Event ดังนั้น สำหรับกรณี binary classification ในที่นี้ <strong>class bad</strong> หรือเรียกว่า <strong>positive class</strong> และอีกคลาสก็คือ <strong>Not class bad (good class)(ซึ่งเป็น complement ของ class bad หรือเรียกว่า negative class)</strong></p><p>ดังนั้น σ(z(x)) เมื่อ x คือ input image ก็จะเป็น probability ที่ x จะเป็นของ <strong>positive class </strong>และ 1−σ(z(x)) ก็จะเป็น probability ที่ x จะเป็นของ <strong>negative class</strong></p><blockquote>ดังนั้น x จะสามารถเป็นคลาสใดคลาสหนึ่งก็ได้แต่ไม่ใช่ทั้งสองคลาสพร้อมกัน และ probability ของทั้งสองคลาสรวมกันจะเท่ากับ 1</blockquote><p>ซึ่งจะเห็นว่า CAM ที่เราได้จากกรณี sigmoid ก็จะสะท้อนออกมาให้เราเห็นในเชิงพื้นที่และตำแหน่ง จากภาพเราจะเห็นว่า input image เมื่อเป็น positive class บริเวณ feature สำคัญที่บ่งบอกก็จะเป็นรอยตำหนิ ดังนั้น พื้นที่โดยรอบรอยตำหนิ ก็คือ บริเวณที่เป็น complement ของ positive class ซึ่งก็คือ เมื่อเป็น good class นั่นเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*0JuNFI-kpmGvAlDQF9afxQ.gif" /><figcaption>CAM Visualization with <strong>pre-trained</strong> EfficientNet model <strong>(Sigmoid)</strong></figcaption></figure><p>เราจะเห็นว่ากรณี softmax คลาสที่ activate ไม่ว่าจะเป็นกรณี class bad หรือ class good ก็จะโฟกัสไปที่บริเวณของ hazelnut ซึ่งจากการที่ softmax มี weight แยกเป็น 2 ชุด เมื่อคลาสใดคลาสหนึ่งเป็น activate class ก็จะทำให้อีกคลาสเป็น deactivate เนื่องจาก probability ของทั้งสองคลาสรวมกันเท่ากับ 1 นั่นเท่ากับว่าทั้งสองเป็น complement ซึ่งกันและกัน เมื่อคลาสหนึ่ง activate CAM ที่สร้างจาก weight ของคลาสนั้นจะโฟกัสไปที่ตรงกลาง ขณะที่ถ้าเรานำ weight ของคลาสที่ deactivate มา สร้าง CAM ก็จะโฟกัสบริเวณโดยรอบแทน เหมือนกับกรณีของ sigmoid</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*AHjrlzfejyfbccxB5TH6uw.gif" /><figcaption>deactivate class of softmax</figcaption></figure><p>ภาพที่แสดงนี้ ได้จากการที่ผมพล็อต CAM ของ deactivate class ผมจะดูว่าโมเดลตอบคลาสใด(activate class) ผมก็จะไปเอา weight ของอีกคลาสซึ่งก็คือ deactivate class มาสร้าง CAM แทน และถ้าพิจารณาสมการของ softmax กรณีมี 2 คลาส ก็จะพบว่าเหมือนกับสมการของ sigmoid (จากภาพสมการเป็นแค่การแสดงให้เห็นความสัมพันธ์ ดังนั้นตอนคำนวณจริงค่าอะไรก็ได้ แต่ก็จะได้เป็น σ(z(x)) และ 1−σ(z(x))</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*PI5IwyPZoWHqocLXGL-Cpw.jpeg" /><figcaption>when class = 2 the softmax is identical to the sigmoid</figcaption></figure><p>ลองแทนค่าดู จะพบว่า เป็น σ(z(x)) และ 1−σ(z(x))</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D50i-WLEqhuO6ro7Iha5kQ.jpeg" /></figure><h3>Conclusion</h3><p>CAM นอกจากจะใช้ในการ visualization ได้แล้ว ยังสามารถนำไปประยุกต์ในงานต่างๆได้ เช่นการทำ object localization ที่เราต้องการระบุตำแหน่งคร่าวๆ ของจุดที่สำคัญเช่น ภาพถ่ายทางการแพทย์, การทำ inspection พื้นผิววัตถุด้วยภาพ และอื่นๆอีกมากมาย โดยที่เราไม่จำเป็นต้องทำเฉลยด้วยการตีกรอบเป็น bounding box ให้เสียเวลา ช่วยประหยัดเวลาในการเตรียมข้อมูลรวมถึงประหยัดค่าใช้จ่าย ดังนั้นก่อนจะนำ AI ไปใช้แก้ปัญหาใด ควรมีการวิเคราะห์ให้ดีว่า แท้จริงแล้วงานของเราหรือระบบของเราต้องการอะไร จะช่วยให้เราแก้ปัญหาได้ตรงจุดโดยไม่เสียเวลาหรือต้นทุนต่างๆโดยไม่จำเป็น</p><p>สุดท้ายเพื่อนๆคนใดอยากลองสร้าง CAM จากข้อมูลที่มีหรือสนใจนำไปประยุกต์ใช้ลองดูตัวอย่างจาก colab ซึ่งเป็น demo code สำหรับการทดลองนี้ได้ครับ เขียนด้วย Tensorflow2 &amp; Keras</p><p>หมายเหตุ:</p><ol><li>ที่เลือกใช้ EfficientNet เนื่องจากทดลองด้วย CNN ง่ายๆ ที่เขียนขึ้นมา แล้ว CAM ที่ได้มีการโฟกัสไม่ดีเท่า EfficientNet ซึ่งผมอยากให้เห็นภาพที่ชัดเจนจึงเลือกโมเดลนี้ รวมถึง Accuracy ที่ได้ก็สูงกว่าและการเทรน converge เร็วกว่ามาก</li><li>การทดลองและการวิเคราะห์ทั้งหมดเกิดจากความรู้ความเข้าใจของผม ดังนั้นถ้ามีข้อผิดพลาดประการใดต้องขออภัยไว้ ณ ที่นี้ด้วยครับ</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac7c17686eee2d285ec57d20a7354ed8/href">https://medium.com/media/ac7c17686eee2d285ec57d20a7354ed8/href</a></iframe><p>ขอขอบคุณทุกๆท่านที่ให้ความสนใจ หวังว่าบทความนี้จะมีประโยชน์ต่อผู้ที่สนใจไม่มากก็น้อยครับ</p><h3>References</h3><p><a href="https://www.mvtec.com/company/research/datasets/mvtec-ad">[1] Paul Bergmann, Michael Fauser, David Sattlegger, Carsten Steger. <strong>MVTec AD — A Comprehensive Real-World Dataset for Unsupervised Anomaly Detection</strong>; in: <em>IEEE Conference on Computer Vision and Pattern Recognition (CVPR), June 2019</em></a></p><p><a href="https://arxiv.org/abs/1512.04150">[2] https://arxiv.org/abs/1512.04150</a></p><p><a href="https://deepai.org/machine-learning-glossary-and-terms/softmax-layer">[3] https://deepai.org/machine-learning-glossary-and-terms/softmax-layer</a></p><p><a href="http://web.stanford.edu/~nanbhas//blog/sigmoid-softmax.html">[4] http://web.stanford.edu/~nanbhas//blog/sigmoid-softmax.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=829c128f048f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/super-ai-engineer/better-understand-why-ai-made-such-decisions-with-cam-class-activation-map-829c128f048f">Better understand why “AI” made such decisions with CAM (Class Activation Map)</a> was originally published in <a href="https://medium.com/super-ai-engineer">Super AI Engineer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[รีวิว paper: Unsupervised Anomaly Detection with Generative Adversarial Networks to Guide Marker…]]></title>
            <link>https://medium.com/super-ai-engineer/%E0%B8%A3%E0%B8%B5%E0%B8%A7%E0%B8%B4%E0%B8%A7-paper-unsupervised-anomaly-detection-with-generative-adversarial-networks-to-guide-marker-eb2793613f0?source=rss-b546586d119b------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb2793613f0</guid>
            <category><![CDATA[expsuperai]]></category>
            <category><![CDATA[anomaly-detection]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[super-ai-engineer]]></category>
            <category><![CDATA[generative-adversarial]]></category>
            <dc:creator><![CDATA[Mail'Suesarn Wilainuch]]></dc:creator>
            <pubDate>Tue, 28 Jul 2020 12:19:09 GMT</pubDate>
            <atom:updated>2021-02-02T22:57:11.989Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Unsupervised Anomaly Detection with Generative Adversarial Networks to Guide Marker Discovery (AnoGAN)" src="https://cdn-images-1.medium.com/max/780/1*djx-3AcPiADc1O6Vg2wcJg.jpeg" /></figure><h3>การตรวจจับความผิดปกติในภาพด้วย Generative Adversarial Network (GAN)</h3><p>สวัสดีครับ วันนี้ผมจะมา รีวิว paper งานวิจัยที่ใช้ โมเดลอย่าง Generative Adversarial Network (GAN) มาทำการตรวจจับความผิดปกติในข้อมูลประเภทรูปภาพครับ ถ้าพร้อมแล้วเริ่มกันเลยครับ!</p><h4>paper ที่จะมารีวิวในวันนี้ชื่อ Unsupervised Anomaly Detection with Generative Adversarial Networks to Guide Marker Discovery (หรือ AnoGAN)</h4><p>สำหรับใครที่ยังไม่รู้จักว่า Generative Adversarial Network (GAN) คืออะไรผมแนะนำให้ลองหาอ่านศึกษาได้จากที่อื่นก่อนครับ เพราะผมจะไม่ได้ลงรายละเอียดเกี่ยวกับ GAN เนื่องจากในงานนี้จะเป็นการนำ GAN มาประยุกต์ใช้งานครับ ถ้าเพื่อนๆมีความเข้าใจในตัว GAN ระดับนึงแล้วจะเข้าใจงานนี้ได้ไม่ยากครับ</p><p>ก่อนอื่นขอบอกว่าในงาน Anomaly Detection นั้นมีหลายประเภท ขึ้นอยู่กับข้อมูลที่เราสนใจ หรือลักษณะงาน แต่ในงานนี้จะเป็นข้อมูลรูปภาพครับ และ จริงๆแล้วนิยามของคำว่า “Anomaly” หรือ ความผิดปกตินั้นก็มีหลายความหมาย เพราะขึ้นอยู่กับมุมมอง</p><p>ขอเท้าความถึง GAN เล็กน้อย GAN หรือ Generative Adversarial Network เป็นโมเดลตระกูล Generative model ที่เรียนรู้ ที่จะสร้างของ หรือข้อมูลที่เราต้องการ ข้อมูลของเราในที่นี้คือรูปภาพ โดย GAN จะประกอบด้วย AI 2 ตัว หรือ 2 โมเดล ที่แข่งกัน ตัวหนึ่งทำหน้าที่สร้างภาพ เรียก Generator จะเรียนรู้ที่จะสร้างภาพให้เหมือนกับใน Dataset ซึ่งก็คือภาพที่เราใช้เทรน และอีกตัวทำหน้าที่ ตรวจสอบว่าภาพที่มันได้รับเป็นภาพจริงใน Dataset หรือภาพที่ถูกสร้างจาก Generator เรียก Discriminator ซึ่งจะเรียนรู้ที่จะแยกระหว่างภาพจาก Dataset กับ ภาพที่ถูกสร้างจาก Generator ดังนั้นถ้าการเทรนเป็นไปด้วยดีสุดท้าย Generator จะสามารถสร้างภาพที่เหมือนกับใน Dataset มากจนแม้แต่บางทีเราก็แยกไม่ออก</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*emPq-LX2oxJL90VS-z72TQ.jpeg" /><figcaption>รูปที่ 1 : GAN Architecture I<a href="https://mc.ai/learning-generative-adversarial-networks-gans/">mage source</a></figcaption></figure><p>จากรูป GAN Architecture จะเห็นว่า Discriminator รับ Input เป็นภาพจาก Training set หรือก็คือ Dataset ของเรา บางครั้งเราจะเรียกว่า ภาพจริง(Real image) และรับ ภาพจาก Generator หรือเรียกว่า ภาพปลอม(Fake image) และทายว่าภาพนั้นจริงหรือปลอม ส่วน Generator จะรับ Input เป็น Random noise หรือก็คือ ชุดตัวเลขจำนวนจริงที่เราสุ่มขึ้นมา โดยปกติจะเป็นการสุ่มแบบ <a href="https://sites.google.com/site/mystatistics01/chapter1/probability-distribution">Gaussian-distribution</a> หรือ <a href="https://sites.google.com/site/mystatistics01/chapter1/probability-distribution">Uniform-distribution</a> บางครั้งเราจะเรียกชุดตัวเลขนี้ว่า latent space หรือ latent code มักแทนด้วยตัวแปร z (ชุดตัวเลข 1 มิติ หรือมองเป็น vector ก็ได้ แต่ถ้าเรานำไปพล็อต 2 มิติ ก็จะเห็นเป็นจุดๆ (noise image) อย่างในรูปที่ 1)</p><blockquote><em>ดังนั้นเราจะเห็นว่า Generator เรียนรู้ที่จะจับคู่ชุดตัวเลข (latent space) ไปเป็นภาพ ( image space) หรือเรามองกลับกันได้ว่า ภาพถูกเข้ารหัสไปเป็นชุดตัวเลขผ่าน Generator ซึ่งนี้คือไอเดียหลักที่ paper ใช้ในการประยุกต์ใช้กับการตรวจจับความผิดปกติ</em></blockquote><h3>Introduction</h3><p>งานวิจัยนี้ได้ใช้ข้อมูลภาพถ่ายทางการแพทย์ เกี่ยวกับดวงตา <a href="https://www.paolohospital.com/th-TH/phahol/Article/Details/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1-%E0%B9%82%E0%B8%A3%E0%B8%84%E0%B8%95%E0%B8%B2/%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88%E0%B8%88%E0%B8%AD%E0%B8%9B%E0%B8%A3%E0%B8%B0%E0%B8%AA%E0%B8%B2%E0%B8%97%E0%B8%95%E0%B8%B2%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B9%80%E0%B8%97%E0%B8%84%E0%B9%82%E0%B8%99%E0%B9%82%E0%B8%A5%E0%B8%A2%E0%B8%B5-OCT-%E0%B8%AA%E0%B8%B0%E0%B8%94%E0%B8%A7%E0%B8%81-%E0%B8%A3%E0%B8%A7%E0%B8%94%E0%B9%80%E0%B8%A3%E0%B9%87%E0%B8%A7-%E0%B9%81%E0%B8%A1%E0%B9%88%E0%B8%99%E0%B8%A2%E0%B8%B3#:~:text=%E0%B9%80%E0%B8%84%E0%B8%A3%E0%B8%B7%E0%B9%88%E0%B8%AD%E0%B8%87%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88%E0%B8%A7%E0%B8%B4%E0%B9%80%E0%B8%84%E0%B8%A3%E0%B8%B2%E0%B8%B0%E0%B8%AB%E0%B9%8C%E0%B8%A0%E0%B8%B2%E0%B8%9E%E0%B8%95%E0%B8%B1%E0%B8%94,2%20%E0%B9%81%E0%B8%A5%E0%B8%B0%203%20%E0%B8%A1%E0%B8%B4%E0%B8%95%E0%B8%B4%20%E0%B9%83%E0%B8%AB%E0%B9%89">optical coherence tomography (OCT) scan</a> โดยจะมีการทำ preprocessing คือสกัดเอาเฉพาะบริเวณที่ต้องการ และทำ normalization</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*3HYG77Iuc5K3AvUQVB9zYQ.jpeg" /><figcaption>รูปที่ 2 : OCT <a href="https://imagebank.asrs.org/file/17083/oct-myopic-staphyloma-with-schisis-and-erm">Image source</a></figcaption></figure><p>ดังนั้นเป้าหมายของการตรวจจับความผิดปกติในที่นี้ ก็คือ ความผิดปกติในจอประสาทตานั่นเอง (Retina) ซึ่งความผิดปกติที่เกิดขึ้น ก็จะถูกแพทย์วินิจฉัยต่อไปว่าเป็นโรคใด เนื่องจากวิธีการที่งานนี้ใช้จัดเป็น Unsupervised learning ดังนั้นเราจะไม่ได้ระบุคลาสหรือระบุว่าเป็นลักษณะของโรคใด แต่จะเป็นการระบุว่าพบความผิดปกติ</p><p>โดยปกติโมเดลทั่วไปที่เป็น classification จะต้องใช้ข้อมูลที่มีเฉลยจำนวนมาก เพื่อที่จะเทรนให้สามารถทำงานได้ดี แต่ในบางครั้งเราก็ไม่สามารถหาข้อมูลได้มากพอ ในบางคลาส ดังนั้น วิธี Unsupervised learning จะไม่มีข้อจำกัดนี้ และเราอาจจะตรวจพบความผิดปกติใหม่ที่เราไม่รู้จักมาก่อนก็เป็นได้</p><h3>Proposed method</h3><p>วิธีการคือ เราจะสร้าง Dataset ที่มีเฉพาะ healthy cases คือภาพของ Retina ที่ปกติสุขภาพดีไม่มีโรค หรือความผิดปกติใดๆ โดยที่เราจะ ใช้ Dataset นี้ในการเทรน GAN จนกระทั่ง Generator สามารถสร้างภาพที่เหมือนกับใน Dataset ได้ นั่นคือ Generator เรียนรู้ที่จะทำการ mapping จาก latent space ไปเป็น image space</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SjFYsid5ZifUBxxtpjkDSw.jpeg" /><figcaption>รูปที่ 3 : เทรน GAN ด้วย healthy cases</figcaption></figure><p>ดังนั้นเป้าหมายคือการ หา <strong>z</strong> (latent space) ที่สอดคล้องกับ G(z) (ภาพที่สร้างโดย Generator) ที่มีความคล้ายกับภาพที่เราต้องการทดสอบมากที่สุด พูดง่ายๆคือ เราต้องการค้นหา <strong>z</strong> ของภาพที่เราต้องการจะทดสอบ <strong>x</strong> (query image) ถ้าภาพที่เราทดสอบ <strong>(x)</strong> คือ 1 ในภาพที่เราใช้เทรน (โมเดลเคยเห็นแล้ว) เราก็จะสามารถหา <strong>z</strong> ที่เป็นเหมือนรหัสของภาพนั้นได้ แต่ถ้าเราใช้ภาพที่โมเดลไม่เคยเห็นมาก่อน (Testset) แต่ภาพนั้นอยู่ใน Domain เดียวกัน คือภาพ healthy cases เราก็น่าจะสามารถค้นหา <strong>z</strong> ที่ใกล้เคียงที่สุดได้ แต่ถ้าภาพที่เราทดสอบแตกต่างจาก trainset มากก็จะไม่สามารถหา <strong>z</strong> ที่ใกล้เคียงได้ หรืออาจกล่าวได้ว่าภาพนั้น เป็นภาพที่ผิดปกติไปจาก healthy cases ดังนั้นถ้าเรามี Dataset ที่ครอบคลุมกรณีของ healthy cases มากเท่าใดก็ยิ่งทำให้โมเดลสามารถแยกระหว่างภาพปกติที่สุขภาพดีกับภาพที่มีความผิดปกติได้ดีมากขึ้นเท่านั้น</p><p><strong>ส่วนขั้นตอนการค้นหา z นั้นจะทำผ่านการ อัพเดต z ที่เราสุ่มขึ้นมาจนกระทั่งได้ค่าที่ใกล้เคียงที่สุด โดยกระบวนการนี้ จะใช้ loss function 2 ตัวในการอัพเดต z</strong></p><h3>1. Residual Loss</h3><p>residual loss จะวัดความแตกต่างระหว่าง query image <strong>x</strong> กับ generated image G(<strong>z</strong>) ใน image space กำหนดโดย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*x41QbHteSbT_HdcYZvxdbA.jpeg" /><figcaption>Residual Loss</figcaption></figure><p>ซึ่งก็คือการนำภาพที่ต้องการทดสอบ <strong>x </strong>มาลบกับภาพที่สร้างจาก Generator โดย <strong>z1 </strong>จะสุ่มขึ้นมา จากสมการนี้เราจะพบว่า ถ้า <strong>x เป็นรูปปกติไม่มีความผิดปกติ ก็จะมีความแตกต่างน้อยส่งผลให้ค่าที่ได้น้อย</strong></p><h3>2. Discrimination Loss</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*aRsCIIi1OVOTM_wqTByvDg.jpeg" /><figcaption>Discrimination Loss</figcaption></figure><p>discrimination loss ใช้ประโยชน์จาก discriminator ที่เรียนรู้ที่จะแยกความแตกต่างระหว่างภาพจริงจาก Dataset และภาพปลอมจาก Generator ซึ่งก็คือภาพใน Domain ของภาพปกติสุขภาพดี ดังนั้น parameter ของ discriminator ก็จะสอดคล้องกับภาพปกติ ดังนั้นจึงใช้เทคนิค feature matching ที่ last convolution layer ของ discriminator ที่เป็น richer intermediate feature ดังนั้นถ้าภาพทดสอบเป็นภาพปกติสุขภาพดีก็จะทำให้ intermediate feature ของ discriminator มีความคล้ายกับภาพจริงใน Dataset เป็นเหมือนการใช้ discriminator เป็น feature extractor</p><p>สุดท้ายเราจะนำ loss function ทั้งสองมารวมเข้าด้วยกันเป็น overall loss และทำการ weight ผลรวมของทั้ง 2 เทอม และใช้ในการอัพเดต <strong>z</strong> หรือก็คือการ mapping จาก image space ไปเป็น latent space</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LkvtongbFWO63RqZhENzoQ.jpeg" /><figcaption>overall loss</figcaption></figure><p>โดยจะมีเพียง z ที่จะถูกอัพเดตผ่าน backpropagation ส่วน parameter หรือ weight ของ Generator และ Discriminator จะถูก fixed ไว้</p><h3>Detection of Anomalies</h3><p>ส่วนวิธีการประเมินและระบุว่าภาพที่ทดสอบเป็น ภาพปกติหรือผิดปกติสามารถใช้ overall loss ที่รอบสุดท้ายของการอัพเดต <strong>z</strong> เป็น anomaly score ได้โดยตรง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*PUt8h1m1YMdcEm2DmX_6Yw.jpeg" /><figcaption>anomaly score</figcaption></figure><blockquote>anomaly score จะมีค่ามากสำหรับ ภาพที่ผิดปกติ แต่ถ้ามีค่าน้อยหมายความว่าภาพนั้นมีความคล้ายกับ Dataset มากซึ่งก็คือ ภาพปกติสุขภาพดี</blockquote><p>นอกจาก anomaly score ยังสามารถระบุตำแหน่งบริเวณของภาพที่มีความผิดปกติได้ จากการ นำภาพที่ต้องการทดสอบลบกับ ภาพที่สร้างจาก generator โดยใช้ <strong>z</strong> ที่ถูกอัพเดตในรอบสุดท้าย เรียกว่า residual image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*RCnBz71cYcGatAqMFvRMkA.jpeg" /><figcaption>residual image</figcaption></figure><p>จากการทดลอง paper นี้รัน 500 รอบ backpropagation ในการอัพเดต <strong>z </strong>และใช ้λ = 0.1</p><h3>Results</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GW8mM46Z7XjAcVzKP-HAsw.jpeg" /><figcaption>รูปที่ 4 : Results</figcaption></figure><p>จากรูปที่ 4 สามารถอธิบายได้ดังนี้ แถวแรกหรือ Query image คือรูปที่ใช้ทดสอบ แถวที่สอง Generated image คือรูปที่ได้จาก Generator ที่มี input เป็น <strong>z </strong>ที่ถูกอัพเดตรอบสุดท้าย แถวที่สาม Residual overlay เกิดจากการนำ Query image แถวที่ 1 ลบกับ Generated image แถวที่ 2 บริเวณที่ไฮไลท์สีแดงคือ บริเวณที่มีความผิดปกติ แถวที่สี่ คือ Ground truth ของโรค retinal fluid</p><p>ซึ่งเราจะเห็นว่า ภาพปกติแม้โมเดลจะไม่เคยเห็นมาก่อน (Testset)แต่ก็สามารถหา <strong>z</strong> ที่ใกล้เคียงกับภาพที่ใช้เทรนได้ ดูได้จาก generated image ที่ได้มีความคล้ายกับภาพทดสอบ ซึ่งก็คือภาพปกติสุขภาพดี ส่วนภาพที่มีความผิดปกติ โมเดลไม่สามารถหา <strong>z </strong>ที่ใกล้เคียงได้ จึงทำให้ generated image ที่ได้ไม่เหมือนกับภาพทดสอบซึ่งก็คือภาพผิดปกติ</p><p>นอกจากอาการ retinal fluid ที่สนใจเราจะเห็นว่าโมเดลสามารถตรวจพบความผิดปกติอย่างอื่นที่เราไม่ได้ระบุไว้ใน ground truth (กรอบสีเขียว) เรียก Hyperreﬂective foci</p><p><strong>เพื่อให้เห็นภาพของการ อัพเดต z ลองดูตัวอย่างต่อไปนี้ สมมติว่าเราเทรน GAN ด้วย MNIST Dataset ซึ่งเป็นภาพตัวเลข 0–9 ในรอบแรกเราสุ่ม z1 และป้อนให้ Generator ปรากฎว่าได้เป็น เลข 1 ออกมา โดยที่ภาพที่เราต้องการทดลองคือ ภาพเลข 7 ที่โมเดลไม่เคยเห็นมาก่อน</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*POGHjG05XAeR5qRouXeNLw.jpeg" /></figure><p>จากนั้นเราอัพเดต <strong>z1 </strong>ด้วย residual loss และ discriminative loss โดย fixed weight ของ Generator และ Discriminator</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SOwHeVwDqCIR_vp3yhBs1A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AS6O7AXWJzuTkqg218TZkA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*36VnM8M93UxJTehzXqcTaA.jpeg" /></figure><p>สุดท้ายเราจะได้ generated image ที่คล้ายกับ query image ซึ่งถึงแม้ว่าโมเดลจะไม่เคยเห็นมาก่อน แต่เราก็สามารถหา <strong>z</strong> ที่ใกล้เคียงมากๆได้ เนื่องจากภาพเลข 7 เป็นภาพใน domain เดียวกันกับที่เราใช้เทรนโมเดล ซึ่งสามารถมองเป็นภาพปกติได้เช่นกัน</p><h3>Conclusion</h3><p>ข้อดีของวิธีนี้ คือการที่เราไม่จำเป็นต้องใช้ label ในการเทรนโมเดลและข้อมูลภาพปกติสุขภาพดีก็ยังหาได้ง่ายกว่าภาพผิดปกติของแต่ละโรค ทำให้การวิเคราะห์ความผิดปกติในเบื้องต้นเป็นไปได้อย่างรวดเร็ว ถึงแม้ว่าเราจะไม่รู้ว่าเป็นโรคอะไรในตอนแรกแต่เราก็จะบอกได้ว่ามีความผิดปกติเกิดขึ้น ภาพที่ถูกทำนายว่ามีความผิดปกติก็จะเป็น candidate ให้แพทย์ได้วินิจฉัยต่อไป</p><p>ส่วนข้อเสียที่เห็นได้ชัดจะเป็นเรื่องของความเร็วในการประมวลผล ที่ใช้เวลานานเนื่องจากต้องใช้เวลาในการอัพเดต z สำหรับแต่ละ query image และอีกประเด็นที่สำคัญคือจะเห็นว่าวิธีนี้จะใช้งานตัว generator ที่ต้องสามารถสร้าง image ที่เหมือนกับ dataset ได้ หรือพูดอีกอย่างก็คือ การ train โมเดล GAN จะต้องสำเร็จ และนั่นก็เป็นเรื่องที่ยากเหมือนกัน ขึ้นกับหลายปัจจัย ทั้งความซับซ้อนของตัว image, ขนาดภาพและ ข้อจำกัดต่างๆ ที่แต่ละโจทย์ไม่เหมือนกัน ดังนั้นถ้าจะนำวิธีนี้ไปประยุกต์ใช้ คงต้องคิดวิเคราะห์ให้ดีว่าเหมาะสมหรือไม่</p><p>ขอขอบคุณทุกๆท่านที่ให้ความสนใจ หวังว่าบทความนี้จะมีประโยชน์ต่อผู้ที่สนใจไม่มากก็น้อย ถ้ามีข้อผิดพลาดประการใดต้องขออภัยไว้ ณ ที่นี้ด้วยครับ</p><blockquote>“A MAN IS NOT WHAT HE OWNS.<br>A MAN IS WHAT HE LOVES.”<br>-CHATRI SITYODTONG</blockquote><h3>Reference</h3><p>[1] <a href="https://arxiv.org/abs/1703.05921">https://arxiv.org/abs/1703.05921</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb2793613f0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/super-ai-engineer/%E0%B8%A3%E0%B8%B5%E0%B8%A7%E0%B8%B4%E0%B8%A7-paper-unsupervised-anomaly-detection-with-generative-adversarial-networks-to-guide-marker-eb2793613f0">รีวิว paper: Unsupervised Anomaly Detection with Generative Adversarial Networks to Guide Marker…</a> was originally published in <a href="https://medium.com/super-ai-engineer">Super AI Engineer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>