<?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 pju on Medium]]></title>
        <description><![CDATA[Stories by pju on Medium]]></description>
        <link>https://medium.com/@phuvnqn24?source=rss-0308b583e7ea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*irpzJF2bgbAw9M2U</url>
            <title>Stories by pju on Medium</title>
            <link>https://medium.com/@phuvnqn24?source=rss-0308b583e7ea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 03:21:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@phuvnqn24/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[Object detection: Convolutional Implementation of Sliding Windows]]></title>
            <link>https://medium.com/@phuvnqn24/object-detection-convolutional-implementation-of-sliding-windows-5d209f023718?source=rss-0308b583e7ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d209f023718</guid>
            <category><![CDATA[object-detection]]></category>
            <dc:creator><![CDATA[pju]]></dc:creator>
            <pubDate>Thu, 25 Jul 2024 17:26:28 GMT</pubDate>
            <atom:updated>2024-07-26T12:04:53.275Z</atom:updated>
            <content:encoded><![CDATA[<p><em>In the previous chapter, we&#39;ve studied about Sliding Windows and their concept. Their most disadvantages are Computational Cost, now we will find out the way to address this problem due to Convolutional layers.</em></p><p><strong>Table of contents:</strong></p><ul><li>Problems of Sliding Windows</li><li>Convolutional Implementation of sliding Windows</li><li>How it addresses the problems</li><li>Another problems: Accuracy of object in these windows</li><li>Conclusions</li></ul><ol><li><strong>Problems of Sliding Windows:</strong></li></ol><p>— Sliding windows requires a larges amount of computational costs because it has to pass through all the position of the image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/0*LC6OJe0dOB-TuS6a" /></figure><p>This is just a small image but in the case of a larger image, it can be very expensive.</p><p><strong>2. Convolutional Implementation of sliding Windows:</strong></p><p>— The convolutional implementation of sliding windows is an approach that addresses the computational inefficiency of the traditional sliding windows algorithm. It leverages the power of convolutional neural networks (CNNs) to make the object detection process more efficient and accurate.</p><p>— Let&#39;s take an example for a 14×14×3 image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g28KGpuqmSD7gjuLgqve2g.png" /></figure><p>— First, we use a Conv layers with 16 kernels 5×5 with the image then the results of this is 10×10×16 image, right after that is a max pooling layer 2x2 and the result is a 5×5×16 image. Then 2 FCs are right after that, which bring the image 14x14x3 into a 1x1x4 image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4FmnH3orqJltdlD9zenysQ.png" /></figure><p>This&#39;s another way to visualize the results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vlljCUbI-nealLyIxQmXSA.png" /></figure><p>This application even works for a bigger image 16x16x3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M1xPlc0UewwixVeYzfCdyw.png" /></figure><p>So a 16x16x3 image → 2x2x4 with the same concepts.</p><p>— If there is a object in the blue part of 2x2x4 image → object appear on the same part of the 16x16x3 image.</p><p>In practice, this is how they&#39;re applied. From the 28x28x3 image, it is converted to 8x8x4 image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ehLYifaDpdKsh3b0FEuJfQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*578tU-zPHDNQ7MDdnDwraA.png" /></figure><p><strong>3. How it addresses the problems</strong></p><p>— The main reason for the birth of this revolution is that it we can run the algorithm on the entire image at the same time, which means that we can reduce a lot of computational cost.</p><p>— Scalability: The convolutional implementation is more scalable than the classic sliding windows algorithm. It can handle larger input sizes efficiently by processing the entire image in a single forward pass. This scalability is achieved by leveraging the parallelism and shared computation capabilities of CNNs.</p><p><strong>4. Another problems: Accuracy of object in these windows</strong></p><p>— Not all the algorithms are perfect, so this algorithm is not an exception.</p><p>— Although it can address the previous algorithm, but the accuracy of this algorithm is low because the position of the bounding boxes is not going to be accurate.</p><p>5. <strong>Conclusions:</strong></p><p>— In conclusion, the convolutional implementation of sliding windows is an approach that<strong> addresses the limitations of the classic sliding windows</strong> algorithm. It improves computational efficiency by <strong>applying the convolutional neural network (CNN) to the entire image at once</strong>, reducing redundant computation and improving scalability. It also enhances the accuracy of bounding box positions by capturing <strong>contextual information and leveraging shared computation</strong>. However, it may <strong>sacrifice fine-grained localization, have limited flexibility in handling object sizes</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d209f023718" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object detection: Sliding Windows technique]]></title>
            <link>https://medium.com/@phuvnqn24/object-detection-f0d76c8f01fb?source=rss-0308b583e7ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0d76c8f01fb</guid>
            <category><![CDATA[object-detection]]></category>
            <dc:creator><![CDATA[pju]]></dc:creator>
            <pubDate>Thu, 25 Jul 2024 11:56:06 GMT</pubDate>
            <atom:updated>2024-07-25T11:57:38.708Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>Table of contents:</em></strong></p><ul><li><em>Data Labeling</em></li><li><em>Sliding windows technique</em></li><li><em>Disadvantage of sliding windows technique</em></li><li><em>Conclusions</em></li></ul><ol><li><strong>Data Labeling:</strong></li></ol><p>Context: If you want to detect a car in this picture</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*fLNCget5KjckuW4kd7nAfQ.png" /></figure><p>— To detect a car in an image, the machine needs to learn from labeled examples. Data labeling involves providing the machine with images containing cars (labeled as 1) and images without cars (labeled as 0). This labeled data is then used to train a convolutional neural network (ConvNet) to predict whether new images contain cars.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*QgHY3W8wx52T0pFoCj3RNw.png" /></figure><p>— 3 images above have cars, so we&#39;ll label them as 1 meanwhile the images at the bottom don&#39;t have cars, so their labels are 0. This process is called <strong>data labeling.</strong></p><p>— Then you can put a new image of a car into a ConvNets can predict their images as a car or not (y=1 or 0)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*d8NjAPV6GsJg7kNwPf-FLA.png" /></figure><p>2. <strong>Sliding Windows technique</strong>:<br> — The sliding windows technique involves creating a small rectangular window that is moved across the image to cover every possible position. Each window is fed into a ConvNet to predict whether it contains a car.</p><p>— To detect objects of different sizes, we use various sizes of rectangular windows. For larger objects like cars, larger windows are used. This ensures that the ConvNet can detect cars at different scales and positions within the image.</p><p>— First, we&#39;ll create a small rectangular like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*Tco8rFzwlyPCFWb-KrPXdQ.png" /></figure><p>— Then it is put on the image and run through every position of the image.</p><p>— And this is an application of CNNs, so every rectangular will be put in a ConvNets to predict if there is a car or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/0*-7wxlSvRYMZGqlod.gif" /></figure><p>Next, we will create a larger rectangular and do the same</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*gLGuBIPaM88BAoW4aPGvxw.png" /></figure><p>— Depends on the size of the object we want to detect, the size of the rectangular can be varied, in this case we will need bigger rectangular for a car detection</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*a1S-sX3-CpL_RQ_G-EPIVA.png" /></figure><p>3. <strong>Disadvantage of sliding windows technique:</strong></p><p>— <strong>Computational cost</strong>: The algorithm can be computationally expensive, especially when using a small stride or fine granularity, leading to a high computational burden.</p><p>— <strong>Localization accuracy</strong>: The sliding windows approach may not provide precise localization of objects within the image, resulting in less accurate object detection.</p><p>— <strong>Scale and aspect ratio limitations</strong>: Using fixed-size rectangular windows may not effectively capture objects with significant scale or aspect ratio variations.</p><p>— <strong>Redundant computations</strong>: The algorithm involves running the ConvNet on multiple overlapping windows, leading to redundant computations and increased computational requirements.</p><p>— <strong>Performance trade-offs</strong>: There is a trade-off between computational cost and detection accuracy, where using a larger stride reduces accuracy, while using a smaller stride increases computational requirements. Which means that if you use a bigger window, it will take less time and computational cost for the task, but it will hurt the performance.</p><p>4. <strong>Conclusions:</strong></p><p>— In the past, this technique is widely used but with the birth of some modern techniques like region-based methods, anchor-based approaches. This technique is not popular nowadays. Anyway, we need to learn about the past to go to the future :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0d76c8f01fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CNNs:[Part 2] Implement on happy house dataset with TensorFlow]]></title>
            <link>https://medium.com/@phuvnqn24/cnns-part-2-implement-on-happy-house-dataset-with-tensorflow-83b714ca8983?source=rss-0308b583e7ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/83b714ca8983</guid>
            <category><![CDATA[happy-house]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[cnn]]></category>
            <dc:creator><![CDATA[pju]]></dc:creator>
            <pubDate>Mon, 17 Jun 2024 04:20:52 GMT</pubDate>
            <atom:updated>2024-06-17T04:20:52.824Z</atom:updated>
            <content:encoded><![CDATA[<p>Classify the smiling face by CNNs with the help of TensorFlow</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/0*yvwRR9YEvmj4z5MY" /></figure><p>T<strong>ables of content:</strong></p><ol><li><strong>Import the lib:</strong></li></ol><ul><li><strong>Resource</strong></li><li><strong>Libraries</strong></li></ul><p><strong>2. Splitting train/ test dataset:</strong></p><p><strong>3. Normalize and reshape</strong></p><p><strong>4. Visualization</strong></p><p><strong>5. Defining model</strong></p><p><strong>6. Evaluating model</strong></p><p><strong>7. Testing</strong></p><p><strong>8.Conclusion</strong></p><h4><strong>1.Import the lib:</strong></h4><p><strong>— Context</strong>: This dataset comprises about 750 examples of human faces. Your mission is detecting whether this is a smiling face or not.</p><p><strong>— Resource:</strong> <a href="https://www.kaggle.com/datasets/iarunava/happy-house-dataset/data">https://www.kaggle.com/datasets/iarunava/happy-house-dataset/data</a></p><p>I&#39;ll use CNNs, which is a powerful tool for computer visions for this mission. This is a beginner example, but for me, it&#39;s like the first step in the world of computer visions. And like Maya Angelou said:</p><blockquote>Every journey begins with a single step</blockquote><p>So if you&#39;re a newbie like me :0, let&#39;s join me in this journey.</p><p>— Libraries needed for this project:</p><ul><li>Tensorflow for CNNs model</li><li>numpy and pandas for normalizing and reshaping</li><li>matplotlib for visualizing data</li><li>h5py for extract the file h5py</li></ul><pre>import tensorflow as tf<br>import numpy as np<br>import pandas as pd<br>import matplotlib.pyplot as plt<br>import h5py</pre><h4><strong>2. Splitting train/ test dataset:</strong></h4><p>— At the core of h5py is the HDF5 file format, a versatile data model that can store complex data relationships and metadata. Unlike traditional flat file formats, HDF5 supports large, multidimensional datasets and hierarchically organized data structures, making it ideal for handling extensive and intricate data collections found in scientific computing, machine learning, and high-performance computing.</p><p>— In essence, h5py can store multi-type like text, image ,… So that&#39;s the reason the dataset in h5py format. Ok ! Now we&#39;ll use h5py lib to extract this.</p><pre>train=h5py.File(&#39;/content/train_happy.h5&#39;)<br>test=h5py.File(&#39;/content/test_happy.h5&#39;)</pre><p>— And next we will assign training and test dataset:</p><pre>X_train=train[&#39;train_set_x&#39;][:]#[:] is used for creating copy of the train_set_x list from the train dictionary and assigns it to the variable X_train.<br>y_train=train[&#39;train_set_y&#39;][:]<br><br>X_test=test[&#39;test_set_x&#39;][:]<br>y_test=test[&#39;test_set_y&#39;][:]</pre><p>— Overview of the this part:</p><pre>print (&quot;number of training examples = &quot; + str(X_train.shape[0]))<br>print (&quot;number of test examples = &quot; + str(X_test.shape[0]))<br>print (&quot;X_train shape: &quot; + str(X_train.shape))<br>print (&quot;Y_train shape: &quot; + str(y_train.shape))<br>print (&quot;X_test shape: &quot; + str(X_test.shape))<br>print (&quot;Y_test shape: &quot; + str(y_test.shape))</pre><pre>number of training examples = 600<br>number of test examples = 150<br>X_train shape: (600, 64, 64, 3)<br>Y_train shape: (600,)<br>X_test shape: (150, 64, 64, 3)<br>Y_test shape: (150,)</pre><p>To interpret it, in training dataset:</p><ul><li>There are 600 images in 64x64x3 (height=64, width=64 and RGB) in training dataset</li><li>There are 150 images in 64x64x3 (height=64, width=64 and RGB) in test dataset.</li></ul><h4><strong>3. Normalize and reshape:</strong></h4><p>—Normalizing is essential for this task because the values on the image range from (0 to 255) for the grayscale image nowadays.</p><blockquote>Why 256 bits ?</blockquote><blockquote>Because normally grayscale in digital world in 8-bits.</blockquote><p>So it’s 2⁸=256 range from 0 to 255</p><p>We’ll apply MinMaxScaler for this standardization. To simplify, we’ll divide it for 255.</p><p>— The second question:</p><blockquote>Why we need to reshape, why we can’t keep the dim the same</blockquote><blockquote>— Y_train shape: (600,)</blockquote><p>— You can see the y_train shape just keep the number of training examples but not the dim of the feature (which is smiling or not)</p><pre>#Reshape<br>y_train=y_train.reshape(X_train.shape[0],1)<br>y_test=y_test.reshape(X_test.shape[0],1)<br><br>#Normalization<br>X_train=X_train/255<br>X_test=X_test/255</pre><pre>print(y_train.shape)<br>print(y_test.shape)<br>#(600, 1)<br>#(150, 1)</pre><pre>print (&quot;X_train shape: &quot; + str(X_train.shape))<br>print (&quot;Y_train shape: &quot; + str(y_train.shape))<br>print (&quot;X_test shape: &quot; + str(X_test.shape))<br>print (&quot;Y_test shape: &quot; + str(y_test.shape))<br>#X_train shape: (600, 64, 64, 3)<br>#Y_train shape: (600, 1)<br>#X_test shape: (150, 64, 64, 3)<br>#Y_test shape: (150, 1)</pre><h4><strong>4. Visualization:</strong></h4><p><strong>One random image and its label:</strong></p><pre>plt.imshow(X_train[3])<br>plt.title(y_train[3])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KlUw5bZfNInaJ-CoPiyq0Q.png" /><figcaption>Smiling face which has been labeled =1</figcaption></figure><p>Multi images and their images:</p><pre>fig,axes=plt.subplots(2,2, figsize=(4,4))<br><br>for i,ax in enumerate(axes.flat):<br>  index=np.random.randint(X_train.shape[0])<br>  ax.imshow(X_train[index])<br>  if y_train[index]==1:<br>    ax.set_title(&#39;Happy&#39;)<br>  else:<br>    ax.set_title(&#39;Not Happy&#39;)<br>  fig.suptitle(&#39;Happy or not&#39;)<br>plt.tight_layout()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*eAQZDy4k0JGbYrD_Bu7q0A.png" /></figure><h4><strong>5. Defining model:</strong></h4><p>My model has 6 layers: 4 Conv-pool layer and 2 densely-connected NN layers</p><pre>from keras.models import Sequential, load_model<br>from keras.layers import Conv2D, MaxPool2D, Dense, Dropout, Flatten<br>from keras.layers import BatchNormalization<br>from keras.layers import Activation<br><br>model= Sequential()<br>model.add(Conv2D(24, (3,3), padding=&#39;valid&#39;, input_shape=(64,64,3) ))<br>model.add(BatchNormalization())<br>model.add(Activation(&#39;leaky_relu&#39;))<br>model.add(MaxPool2D(pool_size=(2,2)))<br><br>model.add(Conv2D(28, (3,3), padding=&#39;valid&#39; ))<br>model.add(BatchNormalization())<br>model.add(Activation(&#39;leaky_relu&#39;))<br>model.add(MaxPool2D(pool_size=(2,2)))<br><br>model.add(Conv2D(64, (3,3), padding=&#39;valid&#39; ))<br>model.add(BatchNormalization())<br>model.add(Activation(&#39;leaky_relu&#39;))<br>model.add(MaxPool2D(pool_size=(2,2)))<br><br>model.add(Conv2D(128, (3,3), padding=&#39;valid&#39; ))<br>model.add(BatchNormalization())<br>model.add(Activation(&#39;leaky_relu&#39;))<br>model.add(MaxPool2D(pool_size=(2,2)))<br><br><br>model.add(Flatten())<br><br>model.add(Dense(units=256, activation=&#39;leaky_relu&#39;))<br>model.add(Dropout(0.2))<br><br>model.add(Dense(units=128, activation=&#39;leaky_relu&#39;))<br>model.add(Dropout(0.2))<br><br>model.add(Dense(units=1, activation=&#39;sigmoid&#39;))<br><br><br>model.summary()</pre><pre>Model: &quot;sequential&quot;<br>_________________________________________________________________<br> Layer (type)                Output Shape              Param #   <br>=================================================================<br> conv2d (Conv2D)             (None, 62, 62, 24)        672       <br>                                                                 <br> batch_normalization (Batch  (None, 62, 62, 24)        96        <br> Normalization)                                                  <br>                                                                 <br> activation (Activation)     (None, 62, 62, 24)        0         <br>                                                                 <br> max_pooling2d (MaxPooling2  (None, 31, 31, 24)        0         <br> D)                                                              <br>                                                                 <br> conv2d_1 (Conv2D)           (None, 29, 29, 28)        6076      <br>                                                                 <br> batch_normalization_1 (Bat  (None, 29, 29, 28)        112       <br> chNormalization)                                                <br>                                                                 <br> activation_1 (Activation)   (None, 29, 29, 28)        0         <br>                                                                 <br> max_pooling2d_1 (MaxPoolin  (None, 14, 14, 28)        0         <br> g2D)                                                            <br>                                                                 <br> conv2d_2 (Conv2D)           (None, 12, 12, 64)        16192     <br>                                                                 <br> batch_normalization_2 (Bat  (None, 12, 12, 64)        256       <br> chNormalization)                                                <br>                                                                 <br> activation_2 (Activation)   (None, 12, 12, 64)        0         <br>                                                                 <br> max_pooling2d_2 (MaxPoolin  (None, 6, 6, 64)          0         <br> g2D)                                                            <br>                                                                 <br> conv2d_3 (Conv2D)           (None, 4, 4, 128)         73856     <br>                                                                 <br> batch_normalization_3 (Bat  (None, 4, 4, 128)         512       <br> chNormalization)                                                <br>                                                                 <br> activation_3 (Activation)   (None, 4, 4, 128)         0         <br>                                                                 <br> max_pooling2d_3 (MaxPoolin  (None, 2, 2, 128)         0         <br> g2D)                                                            <br>                                                                 <br> flatten (Flatten)           (None, 512)               0         <br>                                                                 <br> dense (Dense)               (None, 256)               131328    <br>                                                                 <br> dropout (Dropout)           (None, 256)               0         <br>                                                                 <br> dense_1 (Dense)             (None, 128)               32896     <br>                                                                 <br> dropout_1 (Dropout)         (None, 128)               0         <br>                                                                 <br> dense_2 (Dense)             (None, 1)                 129       <br>                                                                 <br>=================================================================<br>Total params: 262125 (1023.93 KB)<br>Trainable params: 261637 (1022.02 KB)<br>Non-trainable params: 488 (1.91 KB)<br>_________________________________________________________________</pre><p>We can see the overview of the model by &#39;plot_model&#39; of keras.</p><pre>from keras.utils import plot_model<br>plot_model(model,<br>           show_shapes=True<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*kWty7qutcSNOcLXHbZsopA.png" /></figure><p>Now we will train the model like this:</p><ul><li>loss: &#39;binary_crossentropy&#39; because there&#39;re 2 classes: 1 (smiling ) and 0 ( not smiling.</li><li>optimizers: &#39;Adam&#39;</li></ul><pre>model.compile(loss=&#39;binary_crossentropy&#39;,<br>              optimizer= tf.keras.optimizers.Adam(),<br>              metrics=[&#39;accuracy&#39;])<br><br><br>hist=model.fit(X_train,y_train,<br>            epochs=500,<br>            validation_data=(X_test,y_test))</pre><h4><strong>6. Evaluating model:</strong></h4><pre>err_ts,acc_ts=model.evaluate(X_test,y_test)<br>err_tr,acc_tr=model.evaluate(X_train,y_train)</pre><pre>5/5 [==============================] - 0s 5ms/step - loss: 0.1518 - accuracy: 0.9800<br>19/19 [==============================] - 0s 4ms/step - loss: 2.5059e-04 - accuracy: 1.0000</pre><p>— Visualize the results:</p><pre><br>plt.plot(hist.history[&#39;val_loss&#39;],color=&#39;r&#39;,label=&#39;val_loss&#39;)<br>plt.plot(hist.history[&#39;loss&#39;],color=&#39;g&#39;,label=&#39;loss&#39;)<br>plt.title(&#39;validation loss per epoch&#39;)<br>plt.xlabel(&#39;epochs&#39;)<br>plt.ylabel(&#39;val loss&#39;)<br>plt.legend()<br>plt.tight_layout()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2EpCdOnp2naB5KW6wuQzEQ.png" /></figure><pre>plt.plot(hist.history[&#39;val_accuracy&#39;],color=&#39;b&#39;,label=&#39;val_acc&#39;)<br>plt.plot(hist.history[&#39;accuracy&#39;],color=&#39;g&#39;,label=&#39;train_acc&#39;)<br>plt.title(&#39;Validation accuracy per epoch&#39;)<br>plt.xlabel(&#39;epochs&#39;)<br>plt.ylabel(&#39;Accuraccy&#39;)<br>plt.legend()<br>plt.tight_layout()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y3B4Zv6PsLSxDfI7xqiZ4A.png" /></figure><p>— Confusion matrix:</p><pre>from sklearn.metrics import confusion_matrix,classification_report<br>import seaborn as sns<br>y_pred=model.predict(X_test)<br>y_bin=(y_pred&gt;=0.5).astype(int)<br>print(classification_report(y_bin,y_test))<br>print(confusion_matrix(y_bin,y_test))<br><br>sns.heatmap(confusion_matrix(y_bin,y_test),annot=True,cmap=&#39;Greens&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M2vM7PyV524paOod02u3rw.png" /></figure><p>— Classification report:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r0BjQ5TyKAbD4wZwZGS4kg.png" /></figure><p><strong>— Conclusion on this part:</strong></p><p>— Overall, the results are quite good (about 98% on validation set) but the training is not good because there&#39;s an epoch which has the loss very high(25) then it stable again I think the reason for this problem is:Scale of the test dataset is quite small( 150 samples)</p><p><strong>7. Testing:</strong></p><p>— One image:</p><pre>plt.imshow(X_test[5])<br>y_proba_5=model.predict(X_test[5].reshape(1,64,64,3))<br>thresh=0.5<br>y_binary_5=(y_proba&gt;=thresh).astype(int)<br>print(f&#39;predicted_value={y_binary_5}&#39;)<br>print(f&#39;actual value={y_test[5]}&#39;)<br>plt.title(y_binary_5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KkHtdGToxc9cZmzXkI9m9A.png" /><figcaption>Image predicted as smiling face :)</figcaption></figure><p>— Multiple Image and their predicted labels:</p><pre>fig,axes=plt.subplots(2,2,figsize=(4,4))<br><br>for i,ax in enumerate(axes.flat):<br>  index=np.random.randint(X_test.shape[0])<br>  ax.imshow(X_test[index])<br>  y_pred=model.predict(X_test[index].reshape(1,64,64,3))<br>  if y_pred &gt;= 0.5:<br>    ax.set_title(&#39;Happy&#39;)<br>  else:<br>    ax.set_title(&#39;Not Happy&#39;)<br>    fig.suptitle(&#39;Happy or not&#39;)<br>  plt.tight_layout()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*CQnaYJ37PEyDcRkVSyyvlw.png" /></figure><h4>8. Conclusion:</h4><p>— Ok!! That is a simple application of CNNs on HappyHouse dataset. This is my tutorial for this problem.</p><p>— Because I&#39;m newbie in DL so this blog may have a lot of error. So just comment when you announce one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83b714ca8983" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CNNs: Complete Convolutional Neural Network [Part 1]]]></title>
            <link>https://medium.com/@phuvnqn24/cnns-complete-convolutional-neural-network-part-1-2d00e7989724?source=rss-0308b583e7ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d00e7989724</guid>
            <category><![CDATA[cnn]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[pju]]></dc:creator>
            <pubDate>Fri, 14 Jun 2024 07:56:12 GMT</pubDate>
            <atom:updated>2024-06-14T07:56:12.698Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Complete theory about CNNs</blockquote><p><strong>Table of contents:</strong></p><p><strong>I) Introduction:</strong></p><ul><li>Problems</li><li>Lenet</li></ul><p><strong>II) Components:</strong></p><ul><li>Convolutional Layer</li><li>Pooling Layer</li><li>Fully-connected Layers</li><li>Summary</li></ul><p><strong>III) Overview of CNNs</strong></p><p><strong>IV)Conclusion:</strong></p><p><strong>Reference</strong></p><h4>I) Introduction:</h4><p><strong>Problems with ANNs:</strong></p><p>— ANNs is one of the first official neural network in the world. And of course, anything which is born soon always have their drawbacks and ANNs is not an exception. I can list some main inconvenient of this kind of Neural Network:</p><ul><li><strong>Too many parameters</strong>!! Why i say so. Because each layers in ANNs is fully-connected, so it takes a lot of parameters. I’ll give you an example so you can have more clearly view about this problem.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*caiODx6VpDj0O1izDRddyg.png" /><figcaption>ANNs with 109 parameters</figcaption></figure><p>ANNs with:</p><ul><li>13 nodes in the input layers</li><li>5 nodes in the first hidden layers</li><li>4 nodes in the second one</li><li>3 nodes in the output layers</li></ul><p>And this is the formula:</p><p><strong>The number of internal parameters in a neural network is total number of weights + the total number of biase</strong></p><p>To apply for this exemple:</p><pre>nb of weights= 13 * 5 ) + ( 5 * 4 ) + ( 4 * 3 ) = 97<br>nb of biases=5 + 4 + 3 = 12<br>So nb of params: 97+12=109 <br></pre><p>Just a small Neural network, but we have 109 parameters!! That’s huge !! And for some more complex task, we’ll need not only 1<strong>00 parameters but a thousand or even a million parameters</strong>, which makes them computationally expensive to train and deploy. This becomes a challenge when dealing with large-scale datasets.</p><ul><li><strong>Losing spatial information</strong> is one of the biggest problems with ANNs. Because the ANNs just can deal with the<strong> flat vector (</strong> after a flatten layer), <strong>disregarding the spatial structure</strong>. This approach is not suitable for tasks that require understanding spatial relationships, such as recognizing objects in images.</li><li><strong>Lack of translation invariance</strong>: ANNs do not inherently possess translation invariance, meaning that they may<strong> not recognize</strong> an object if it <strong>appears in a different location within an image</strong>.</li></ul><p>— So the researchers have to think about different architectures to solve these problems. And that’s the reason CNNs is born.</p><p>LeNet-5:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XEfVORSehny1PnVn.png" /><figcaption>Architecture of LeNet-5</figcaption></figure><p>— LeNet-5 is a classic convolutional neural network (CNN) architecture that was developed by <strong>Yann LeCun</strong> and his colleagues <strong>in the 1990s</strong>. It was one of the pioneering models in the field of deep learning and played a significant role in the advancement of computer vision tasks, particularly handwritten digit recognition.</p><p>— In the next part, we’ll find out which changes make CNNs is better than ANNs and why it’s the soul of computer vision.</p><h4><strong>II) Components:</strong></h4><p>Note: I&#39;ve clearly explained each layers in different blogs</p><ul><li>Convolution on RGB image: <a href="https://medium.com/@phuvnqn24/cnns-convolution-over-the-volume-c06e9cf25fd0">https://medium.com/@phuvnqn24/cnns-convolution-over-the-volume-c06e9cf25fd0</a></li><li>Pooling layers: <a href="https://medium.com/@phuvnqn24/cnns-pooling-layer-3660f39aaccd">https://medium.com/@phuvnqn24/cnns-pooling-layer-3660f39aaccd</a></li></ul><p>So in this blog i&#39;ll show it in an example because in my point of view, example is easier to understand than the theory :)</p><figure><img alt="State of the Art Convolutional Neural Networks (CNNs)… – Towards AI" src="https://cdn-images-1.medium.com/proxy/1*QPRC1lcfYxcWWPAC2hrQgg.gif" /><figcaption>CNNs on MNIST dataset</figcaption></figure><p>I&#39;ll take a image 32x32x3 which is quite common on the internet and try to classify it (number 7 )</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/1*PwfeeoLlnlnUrc_8xEHjig.png" /></figure><p><strong>1.Convolutional layers(Conv1)</strong></p><p>— With n=32,f=5,s=1,nb of kernel=6, we can compute the results image:</p><pre>m=((32+2*0-5)/1)+1=28<br>nc=nb of kernel=6<br>--&gt; 28x28x6</pre><p>Number of parameters:</p><pre>pa=(5*5*6)+6=156</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*JaxWXS3fVItQRK0_bInkCw.png" /></figure><p><strong>2.Pooling layers(Maxpool1):</strong></p><p>I take n=28, s=2,f=2 so the image after this layer is :</p><pre>m&#39;=((28+2*0-2)/2)+1=14<br>n&#39;c=nb of channel of the previous layer(6)<br>--&gt; 14x14x6</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/1*sLwCbh2GNrUsxXNnKXXZjg.png" /></figure><p>No parameters is needed</p><pre>pa=0</pre><p>=&gt; Normally, we&#39;ll consider Conv layer and Max pool layer as one layer because Maxpool layer doesn&#39;t have the parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*T3d6iXP1WMkJXuj41YPW5w.png" /><figcaption>Layer 1</figcaption></figure><p><strong>3.Convolutional layers(Conv2):</strong></p><p>— With n=14,f=5,s=1,nc=16 we&#39;ll have:</p><pre>m=((14+2*0-5)/1)+1=10<br>nc=16<br>--&gt;10x10x16</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*Mk6Lzq8wUAEjxjlH_azVHg.png" /></figure><p>— Number of parameters:</p><pre>pa=((5*5)*16)+16=416</pre><p><strong>4.Pooling Layer(MaxPool2):</strong></p><p>— n=10,f=2,s=2,n_c=16:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/166/1*M7ifhi7QE4cYn54nzVRniw.png" /></figure><p>And the results is:</p><pre>m&#39;=((10-2)/2)+1=5<br>n_c&#39;=n_c=16</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*4y86fZz5kI7LD9vATdMUyQ.png" /><figcaption>maxpool2</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*Vp6tgFJbF8oHhlNxQk7CfA.png" /><figcaption>Layer 2</figcaption></figure><p><strong>5.Flatten layers:</strong></p><p>— The Fully-connected layers(FCs) ,which also means ANNs, just can handle the flat vector so Flatten layer appear in the middle for this mission</p><p>— After the previous layer we&#39;ll have 5x5x16 image.</p><pre>#after flatten<br>k=5*5*16=400 pixels</pre><p>— 400 pixels after the flatten layer</p><p><strong>6.Fully-Connected layers:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*hGFB9cbAfi91eGeuWJJ6Kw.png" /><figcaption>2 FCs layers</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*Ukl0WRmA54jWLQHVHeMiEw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rsxmQHbn2QZcaRXm.png" /><figcaption>softmax function</figcaption></figure><p>— In this example, 2 FCs is enough because it&#39;s not quite complicated.</p><ul><li>One hidden layer(FC1) : 120 units</li><li>Another layers(FC2): 84 units</li></ul><p>— Finally, we will use &#39;softmax&#39; activaiton function to classify them with 10 outputs <strong>(from 0 to 9)</strong></p><p>Number of parameters after these layers is:</p><pre>nb of w=(400*120)+(120*84)+(84*10)=58920<br>nb of bias=120+84+10=214<br>nb of params = 59134!!<br></pre><p><strong>7.Summary</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cOQ_q2yKHOodf548gmwxEQ.png" /><figcaption>Summary table</figcaption></figure><p>— If you look at this table, you can see the image is smaller after each layer in CNNs(Conv1,Pool1,Conv2,Pool2):</p><p>[32–28–14–10–16–5]</p><p>— By contrast, their channels are increased:</p><p>[3–6–6–16–16]</p><p>— And one more significant thing is the parameters in CNNs are much more smaller than ANNs</p><p>CNNs (max=2416) while ANNs (max=48120)</p><p><strong>III) Overview of CNNs:</strong></p><p><strong>Pros:</strong></p><p>—P<strong>arameter sharing</strong>. As you can see, the weights of the kernel can apply for multiple position on the image It can reduce much number of parameters in the Neural Network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uItNQvPKuy6yf3_n80jHFg.png" /></figure><p>— Without the need of flatten layers like ANNs, CNNs can reserve the spatial information which is essential for the image. F<strong>or me, it means that image through a CNNs not an array.</strong></p><p>— <strong>Spatial Invariance</strong>: CNNs are inherently <strong>spatially invariant</strong>, <strong>meaning they can recognize objects regardless of their position in the input image.</strong> This is achieved through the use of shared weights in convolutional layers, allowing the network to detect the same features regardless of their location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/0*Ayu7NPo7-0jnDa5Z.png" /></figure><p><strong>Cons:</strong></p><p>— Large Training Data Requirements: CNNs typically require a large amount of labeled training data to generalize well. Without sufficient data, CNNs may overfit and fail to perform accurately on unseen examples.</p><p>— Lack of Interpretability: CNNs are often considered as black-box models, making it challenging to interpret the learned features and understand the decision-making process. This lack of interpretability can be a drawback in certain domains where explainability is crucial.</p><p><strong>IV)Conclusion:</strong></p><p>— In conclusion, Convolutional Neural Networks (CNNs) have proven to be highly effective in various computer vision tasks, thanks to their ability to automatically learn hierarchical representations from visual data.</p><p>— The cons of CNN are requirement for large training data, so it&#39;s difficult for training CNNs on our own. That&#39;s the reason for transfer learning.</p><p>— In the next part, I will come to the implement of CNN on the happy dataset ( classify the smiling face) and SIGN dataset (classification with 5 sign number)</p><p><strong>Reference:</strong></p><p><a href="https://www.coursera.org/learn/convolutional-neural-networks/lecture/uRYL1/cnn-example">CNN Example - Foundations of Convolutional Neural Networks | Coursera</a></p><p><a href="https://viso.ai/deep-learning/convolutional-neural-networks/#:~:text=The%20History%20of%20CNNs,-Convolutional%20Neural%20Networks&amp;text=It%20started%20back%20in%20the,layers%2C%20and%20fully%20connected%20layers">https://viso.ai/deep-learning/convolutional-neural-networks/#:~:text=The%20History%20of%20CNNs,-Convolutional%20Neural%20Networks&amp;text=It%20started%20back%20in%20the,layers%2C%20and%20fully%20connected%20layers</a>.</p><p><a href="https://stats.stackexchange.com/questions/208936/what-is-translation-invariance-in-computer-vision-and-convolutional-neural-netwo">https://stats.stackexchange.com/questions/208936/what-is-translation-invariance-in-computer-vision-and-convolutional-neural-netwo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d00e7989724" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CNNs: Convolution over the volume]]></title>
            <link>https://medium.com/@phuvnqn24/cnns-convolution-over-the-volume-c06e9cf25fd0?source=rss-0308b583e7ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/c06e9cf25fd0</guid>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[cnn]]></category>
            <category><![CDATA[rgb]]></category>
            <dc:creator><![CDATA[pju]]></dc:creator>
            <pubDate>Thu, 13 Jun 2024 05:08:31 GMT</pubDate>
            <atom:updated>2024-06-14T04:40:20.633Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Convolutions on 3D Image</strong></blockquote><p><strong>Table of contents:</strong></p><p>I) <strong>Introduction:</strong></p><ul><li>Image RGB</li></ul><p>II) <strong>Convolution layers:</strong></p><ul><li>The core part of convolution layers</li><li>Some important notations</li><li>Full Conv layers</li><li>Implement on Tensorflow</li></ul><p>III) <strong>Conclusion</strong>:</p><p><strong>Reference:</strong></p><p>I)<strong>Introduction</strong>:</p><ol><li><strong>Image RGB:</strong></li></ol><p>The image on the Internet nowadays is normally in RGB (red, green, blue). <br>It means that their size is not only height and width but also the channels.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*7ewYZ5lUHna9Buqeh02V9w.png" /><figcaption>Image dimensions: height x width x channels</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/0*OjF2cYyVGcRkicL2.PNG" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/0*eDywxdonP40VMJTQ" /><figcaption>3 channels: Red Green Blue (RGB)</figcaption></figure><p>And so the kernel size is in 3 dimensions: height x width x channels:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*-sjuCVRKRuLSqT63qrNZFA.png" /><figcaption>kernel size in 3d</figcaption></figure><p>You can imagine the image is just like a cube</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*X6KQ58Ub_NWpHhlhcXghqQ.png" /></figure><p>And so do the kernel:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*TqqnrzbUL5bi3qyW1z_xog.png" /></figure><p>For this problem, you can simply imagine like a convolution between 2 cubes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*YwHbCSr3l1t4tfm683W-5w.png" /></figure><p><strong><em>Caution: the channels of the image is must the same as the channels of the kernels ( normally this is equal to 3(RGB) )</em></strong></p><p><strong>II) Convolution layers</strong></p><p><strong>1) The core part of convolution layers:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eBg2npDwlATpWEUP7T8ALg.png" /></figure><p>— I will demonstrate the process through an exemple:</p><ul><li>The initial image dim is 6x6x3 (RGB)</li><li>The kernel dim is 3x3x3 ( the channel of the filter is 3 which is equal to the one in intial image)</li><li>The results image dim is 4x4 image.</li></ul><p>— Consider the RGB image as a stack of three 2D images, where each image represents one color channel (red, green, and blue).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*KTEUZfwQ6Rz62KORi-awPg.png" /></figure><p>— The 3x3x3 kernel also has three layers, corresponding to the red, green, and blue channels.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*RbZaViYZ391Ri16RCS1Bcw.png" /></figure><p>— Place the kernel in the upper leftmost position of the image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*5iz_5oFcqh6SSyDQVY1Bng.png" /></figure><p>— Multiply each element of the kernel with the corresponding element in the image’s color channels. Sum up the results of the multiplications to get the first output value. This step is just like we convoluted each channels with corresponding kernel.</p><p>Note: If we want the red channel, we can modify the kernel parameter like this :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*Q3mUdDmpcFTO-HFTJc2erg.png" /><figcaption>Red kernel parameter is (1,0,-1) and the other kernel parameter is full of zero.</figcaption></figure><p>If we want to take three channels, we can take like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AiPoOVhAKUBNxFL7PfERgg.png" /><figcaption>We have the parameters on 3 channels</figcaption></figure><p>— Slide the kernel over by one position and repeat the multiplication and summation process to get the next output value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*0dnb2IshadvzVX-22rE9Yw.png" /></figure><p>— Continue sliding the kernel and computing the output values until you cover the entire image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*BBRi-w-6toFp_LQnMK-IJg.png" /></figure><p>— The resulting output will be a 2D image with dimensions of (n — f + 1) x (n — f + 1), where n is the size of the input image and f is the size of the kernel.</p><pre>n=6,f=3----&gt;m=4x4</pre><p>— The output image will have a single channel, as indicated by the absence of the third dimension.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*gxOhJzvtnD7pJ8sq_nIs-w.png" /><figcaption>The result image dim=4x4</figcaption></figure><p>— But is that enough ? Sometimes we need more than 1 filters for a convolution (for example vertical edge and horizontal edge filters)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/0*UZaUfrMGElUR0QeJ.jpg" /></figure><p>Which means we have to use different kernels:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*qcNy1sZAqMMGY7Tr7F1UXw.png" /><figcaption>Kernel for vertical edge</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*sogHrghRP8UpRnt9OHG3lQ.png" /><figcaption>Kernel for horizontal edge</figcaption></figure><p>— Now we need more filters: one for vertical edges and the other for horizontal edges</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MSITBeNYb_T7PAhlBPW-aA.png" /></figure><p>— As the results, we have achieved 2 output images. Then we will combine it together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/156/1*ansE5fl4tEu1ZWtfGCZeWQ.png" /><figcaption>The final output</figcaption></figure><p>One important thing you&#39;ve to remember is the number of parameters in one Conv layer:</p><pre>pa=(f*f)*n_c+n_c<br>f:size of kernel<br>n_c:number of kernel<br></pre><p>For example, if we apply a kernel (5x5) and 2 kernel( one for horizontal and one for vertical edges), we&#39;ll have:</p><pre>pa=(5*5)*2+2=52</pre><p>But we have to remember the formula? No. You just remember a simple quote for this calculation:</p><pre>pa=(nb of w)*(nb of kernel)+nb of biases<br></pre><p>Each of number in the kernel is a weight and ,of course, 5x5 kernel has 25 weights and for 2 kernels, we&#39;ll have 50 weights, but don&#39;t forget the biases, after the conv layers we&#39;ve to plus a biases b and pass through an activation function to obtain the final results, so each kernel we&#39;ll have one bias. And that&#39;s explanation for this formule.</p><p>In conclusion, we have this formula to simulate this process:</p><pre>               (nxnxnc) * (fxfxnc)---&gt; (n-f+1)x(n-f+1)xn&#39;c<br>n: inital image size<br>f: kernels size<br>nc: nb of channels<br>n&#39;c: nb of kernels</pre><p>This is an essential part of the soul of the convolution layers.</p><p>3) <strong>Full Conv layers</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5z7TPghOJsu6WZpwZ5VSZw.png" /></figure><p>— After passing the first part of convolution layer, you will get 2 4x4 image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*UX46HOWZ-SkqKxFZRor96A.png" /></figure><p>— Then you add a bias for each image. And applying an activation function for this(normally &#39;relu&#39; function).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*txjVEXxil-zLfWLbZ8y70A.png" /></figure><p>— Finally, you&#39;ve achieved the final image, and then you combine them together</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*fkP8yol3RDQwIilFAtG3hA.png" /><figcaption>The final image</figcaption></figure><p>4) <strong>Some important notations:</strong></p><p>If l is a convolution filter, then:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*0JTW81riGwbxfpSW5D63lQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*ovAN6gzxphZSriaPxdEYDA.png" /><figcaption>l-1 : mean the previous layer</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*5i7f4LyM7KW2SI6PkzJMCg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*X9eI9uSHatJX4s9oNlscdw.png" /><figcaption>Formula for height</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*3O5m1y0KXTlMzf4Y2yJiag.png" /><figcaption>Formula for width</figcaption></figure><p>Dimension of the filter:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*VlPdGnwZR1J41-LoGHhmjw.png" /><figcaption>Nc[l-1]: number of channels</figcaption></figure><p>So the number of channels of the kernel is equal to the number of channels of the initial image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lA__GDuqHYsyckUsA8sG5g.png" /><figcaption>a: is normally &#39;relu&#39; function</figcaption></figure><p>— Of course, the activation functions are the same dimension with the output image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*droML3PGpwV9eBH_RhC1sQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*tZWVRjrFjcBGou6f4-lOiQ.png" /><figcaption>Weights a.k.a the number on the kernel</figcaption></figure><p>— The first nc is a number of channels, the second one is number of filter.</p><p>— Bias is included by each filter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/198/1*0hcFS8uW0IQFRu8_j_b3zg.png" /></figure><p>— When we come to the real problems , we&#39;ll have a parameter m(training example)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qZSDa6mQ6uUoFcCr6mpQRA.png" /></figure><p>5) <strong>Implement on Tensorflow</strong>:</p><pre>x = np.random.rand(4, 10, 10, 128)<br>y = keras.layers.Conv2D(32, 3, activation=&#39;relu&#39;)(x)<br>print(y.shape)<br>#(4,8,8,2)<br></pre><pre>import tensorflow as tf<br>from tensorflow.keras.models import Sequential<br>from tensorflow.keras.layers import Conv2D<br><br>model=Sequential()<br>model.add(Conv2D(filters=32,kernel_size=(3,3),strides=(1,1),activation=&#39;relu&#39;,input_shape=()<br># For Conv2D the most important parmeters of Conv2D is filters,kernel_size,activation<br>model.summary()</pre><p>III) <strong>Conclusions</strong>:</p><p>— This blog just included the essential part of the CONV2D. There are some other parts like MaxPool layer or Dense layer will be included in the following path.</p><p><strong>Reference:</strong></p><ul><li><a href="https://www.coursera.org/learn/convolutional-neural-networks/lecture/nsiuW/one-layer-of-a-convolutional-network">One Layer of a Convolutional Network - Foundations of Convolutional Neural Networks | Coursera</a></li><li><a href="https://animatedai.github.io/">Animated AI</a></li><li><a href="https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D">tf.keras.layers.Conv2D | TensorFlow v2.16.1</a></li></ul><p><a href="https://www.linkedin.com/learning/computer-vision-for-data-scientists/overview-of-cnns?resume=false&amp;u=114782108">https://www.linkedin.com/learning/computer-vision-for-data-scientists/overview-of-cnns?resume=false&amp;u=114782108</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c06e9cf25fd0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CNNs: Pooling layer]]></title>
            <link>https://medium.com/@phuvnqn24/cnns-pooling-layer-3660f39aaccd?source=rss-0308b583e7ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/3660f39aaccd</guid>
            <category><![CDATA[max-pooling]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[cnn]]></category>
            <dc:creator><![CDATA[pju]]></dc:creator>
            <pubDate>Thu, 13 Jun 2024 04:58:20 GMT</pubDate>
            <atom:updated>2024-06-13T05:06:17.828Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Knowledge about pooling layer</blockquote><figure><img alt="Introduction To Pooling Layers In CNN – Towards AI" src="https://cdn-images-1.medium.com/proxy/1*ypIfJX7iWX6h6Kbkfq85Kg.png" /><figcaption>2 types of pooling layers</figcaption></figure><blockquote><strong>Table of contents:</strong></blockquote><p><strong>I) Introduction</strong></p><p><strong>II) Pooling layer</strong></p><ul><li>Definition</li><li>Process</li><li>Type of pooling layer</li><li>Benefits</li><li>Implement on TensorFlow lib</li></ul><p><strong>III) Conclusion:</strong></p><blockquote><strong>Reference</strong></blockquote><p>I)<strong> Introduction</strong>:</p><p>— Pooling is one of essential layers in ConvNets, but not all people has a clear knowledge about this layer. So I write this article to share more clearly view about this layer.</p><p>II) <strong>Pooling layer:</strong></p><h4><strong>1. Definition</strong>:</h4><p>— A pooling layer is a type of layer in Convolutional Neural Networks (ConvNets) that is used to reduce the spatial dimensions (width and height) of the input feature maps.</p><h4>2. <strong>Process</strong>:</h4><p>— I will pass through an example to give you an intuition of this layer</p><p>For exemple,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*tHzRIKgvDFktQOnlfup3ng.png" /><figcaption>4x4 matrix</figcaption></figure><p>— I have a kernel:</p><pre>f=2( kernel size)<br>s=2 (stride=2)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*6-5M3lL_8E9SbZeLKKC3rA.png" /><figcaption>kernel position</figcaption></figure><p>— At each position of the kernel, we will take the largest number of that square. For example, in the first kernel [1,3,2,9] →9. And we&#39;ll have this result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*6tUPp8qE0hzd1tPlWub2GQ.png" /><figcaption>After pooling layer</figcaption></figure><p>Imagine like the kernel has 2 hyperparameters:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*S9k9FthRIc2nLVEVXowLJg.png" /></figure><p>Do you remember the formula that i&#39;ve written in the previous article. The result matrix will have the dimension like this</p><pre>m=((n+2p-f)/s)+1<br>n: initial size<br>p: padding<br>f:kernel size<br>s: stride</pre><p>In this example, n=4, p=0, f=2, s=2 →m=2</p><p>— But the size of the kernel is always 2? No</p><p>We can customize it if we want. I will take another example for customized pooling layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*YCO0cQ1-ZavrLxTzG08GBg.png" /></figure><p>This is the matrix we want to pooling. We can customize the f, s like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*f6oDN1cl7wtnYQGwuN_IHA.png" /></figure><p>with this parameter, we will have the size of the result matrix :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*piRfRgHR3nUXF0Boln7Zkw.png" /><figcaption>3x3 matrix</figcaption></figure><p>Ok now we just fill the matrix by taking the largest number in each square</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*DBxEjbiMU_Z-nDJdwOh48g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*WQazJVwHjlzUmJcHeJVdVA.png" /><figcaption>The first one</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*Z4fIfg6oerPhByaJBEnmBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*cTrzbvT7A7zfavEGKs391A.png" /><figcaption>The second one</figcaption></figure><p>We will do it until, we&#39;ve filled the matrix:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*zsekZ5yfisELLGScR81fxA.png" /></figure><p>That&#39;s it! We&#39;ve finished. This is one type of pooling layers which is called <strong>Maxpooling</strong>. This type of pooling just takes the largest number in the &#39;pool&#39; so we&#39;ll lose a part of the information which can lead to overfitting.</p><p>There is another type of pooling layer which takes all the information in the pool. Let&#39;s pass to another part to discover it.</p><h4>3)<strong> Type of pooling layer</strong>:</h4><p>— <strong>Max Pooling</strong>: In max pooling, the maximum value within each pooling region is selected as the output. This helps to capture the most salient features in the input and provides a form of translation invariance.</p><p>— <strong>Average Pooling</strong>: In average pooling, the average value within each pooling region is computed as the output. This can help to reduce the impact of outliers and provide a smoother representation of the input.</p><p>— So as you can see, the average pooling will take<strong> all the information in the pool and take the averages of it</strong>. This can solve the problems of<strong> overfitting, but</strong> it may take some <strong>inessential information (noise)</strong> which is als<strong>o needed to be reduced</strong>. So normally, we will use the <strong>Max Pooling for Pooling layer.</strong></p><h4>4) <strong>Benefits:</strong></h4><ul><li><strong>Dimensionality reduction</strong>: Pooling layers reduce the spatial dimensions (width and height) of the input feature maps, which helps to reduce the computational complexity of the network. By downsampling the feature maps, pooling layers make the subsequent layers more efficient to compute.</li><li><strong>Translation invariance</strong>: Pooling layers make the network more robust to small translations in the input. By taking the maximum or average value within a pooling region, pooling layers can capture the presence of a feature regardless of its exact location in the input. This helps the network to learn features that are more invariant to translation.</li><li><strong>Feature selection</strong>: Pooling layers help to select the most important features from the input. By taking the maximum or average value within a pooling region, pooling layers emphasize the most salient features and suppress less important ones. This can help to reduce overfitting and improve generalization.</li><li><strong>Parameter reduction</strong>: Pooling layers have no parameters to learn, which means they do not contribute to the overall number of trainable parameters in the network. This can help to prevent overfitting, especially in cases where the network has a large number of parameters.</li></ul><h4><strong>5. Implement on TensorFlow lib:</strong></h4><p>— This is the description on the page of TensorFlow Lib:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*0uNSLj-BSHuA5iOvZE8tuw.png" /></figure><pre>import tensorflow as tf<br>from tensorflow.keras.models import Sequential<br>from tensorflow.keras.layers import MaxPool2d,Conv2D<br><br>model=Sequential()<br>model.add(Conv2D(32,kernel_size=(3,3),activation=&#39;relu&#39;,input_shape(28,28,3))<br>model.add(MaxPool2D(pool_size=(2,2),<br>                     strides=2,<br>                      padding=&#39;same&#39;)<br>model.summary()</pre><p><strong>III) Conclusion:</strong></p><p>— Overall, pooling layers play a crucial role in ConvNets by reducing dimensions, improving translation invariance, selecting essential features, and reducing the number of parameters. They are an essential tool for building effective and efficient convolutional neural networks. I hope this blog can help you enhance your knowledge about Pooling Layer.</p><p>Reference:</p><p><a href="https://www.coursera.org/learn/convolutional-neural-networks/lecture/hELHk/pooling-layers">Pooling Layers - Foundations of Convolutional Neural Networks | Coursera</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3660f39aaccd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CNNs: Padding and Stride]]></title>
            <link>https://medium.com/@phuvnqn24/cnns-padding-and-stride-ada1100351d2?source=rss-0308b583e7ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/ada1100351d2</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[convolutional-layer]]></category>
            <dc:creator><![CDATA[pju]]></dc:creator>
            <pubDate>Thu, 13 Jun 2024 02:51:11 GMT</pubDate>
            <atom:updated>2024-06-13T05:07:08.221Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>What do you know about padding and stride in CNNs</blockquote><p>I) Padding:</p><ol><li><strong>Why we need padding?</strong></li></ol><p>When the CNNs are applied on the image, there are 2 main inconvenient:</p><p>— It&#39;ll shrink after each layer, meaning that the image will become very small if the CNNs is so deep(for example, 100 layers). For example, if you take a 6×6 image pass through a Convolution layer, you can see the size of this is 4×4. There is an equation for this size reduction:</p><pre>                            m=(n-f+1)x(n-f+1)<br>m: size of the image after the layer<br>n: size of the initial image<br>f: size of the filter(kernel)<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FJyqtHaSyLEC1D9XT_otKw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*wyXKB2meaZX3MNmXU99NlA.png" /><figcaption>Figure 1: n=6x6, f=3×3 — — → m=(6–3+1)x(6–3+1)=4x4</figcaption></figure><p>— The second one is losing the information from the edge of the image. The pixels which is in the middle of the image will be overlapped (means that will be used multiple times) meanwhile the pixel is near the edges of the image will be used once. This will lead to the information in these pixels disappear after a few layers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*niAgU2_WsESCSMyus6om4g.png" /><figcaption>Figure 2: The pixel in green which near the edge of the image is used once.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*G90H0Y-QVFhqe-Rvca7dYw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*WriPKrZ6PG8CHMWUYLKRkw.png" /><figcaption>Figure 3: The pixel in red is used for multiple times by different kernels.</figcaption></figure><p><strong>2. Solutions:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*9uFAZ1mxYOaiR4ZDmfDu3w.png" /><figcaption>Figure 4: Definition in real life</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*1V0PPxKikjNtRaFEbmhJbg.png" /><figcaption>Figure 5: Padding in CNNs</figcaption></figure><p>— I think the meaning of the &#39;padding&#39; in real life in a real life and in the CNNs are almost the same. Both means the thing which is put outside to protect the insider thing. In CNNs, we just surrounded by one border with &#39;zeros&#39;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*cLHWFbvhDLIJRiUUyd6fQw.png" /><figcaption>Figure 6: By convention, the &#39;padding&#39; is full of zero.</figcaption></figure><p><strong>2.1) Solving the first problem: Shrink output</strong></p><p>— There are one essential parameters in the padding: p (padding amount), in this case p=1(the width of the &#39;padding&#39; border). With the help of the &#39;padding&#39;, the size of the image after convolution is:</p><pre>                        m&#39;=n+2p-f+1 x n+2p-f+1<br>m&#39;:size of the image with the help of &#39;padding&#39; after convolution<br>p: padding amount(in this case p=1)<br>f: size of the kernel<br>n: size of the initial image</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oGQYAQJbgAmJjflbY3dy6w.png" /></figure><p>After the padding the size of the initial image is 8×8 and after the convolution layer the size is 6×6(which is not shrunk).</p><pre>p=1,f=3x3,n=6x6<br>                m&#39;=6+2x1-3+1x6+2x1-3+1<br>                m&#39;=6x6</pre><p>So the first problem is solved. <strong>Do the second problem is solved by this method?</strong></p><p><strong>2.2) Solving the second problem: Losing the information of the pixels near the edge of image:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Uc-86nfEbRxYte9JsZT-g.png" /><figcaption>Figure 7: Convolution with padding</figcaption></figure><p>With the pixel in the top-left corner (which is quite near the edge of the image) is retained after the convolution and now the pixel of &#39;losing-information&#39; is padding border (which is meaningless). And as result, the second issue is resolved.</p><p><strong>3) Some external definitions:</strong></p><p>— Valid Convolutions: The convolutions without the help of &#39;padding&#39;. This is the one which occurs the problem. The size of the output will smaller the size of the input</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b7Epa8SPYNDeP1ZfneOiDQ.png" /></figure><p>— Same Convolutions: The convolutions with the help of &#39;padding&#39;. The size of the output will remain the same as the output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_LdJk9YLxClXK9cmjMI8jA.png" /></figure><p>— If you want to find p ( padding amount), you can use this equation:</p><pre>                    n+2p-f+1=n =&gt;p=(f-1)/2</pre><p>— Normally, in computer vision, f (size of the kernel) is odd(for example 3×3 or 5×5). When f is even, we will use the<strong> asymmetric padding(padding with &#39;more on the left&#39; or &#39;more on the right&#39;)</strong>. Moreover, when we use f=odd number we will have the central point which is to identify the position of the filters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*IB8F2hSKacnw6rRRwHf94w.png" /><figcaption>Figure 8: Central point of the pad</figcaption></figure><p><strong>4)Implement on TensorFlow lib :</strong></p><p>On the TensorFlow lib, we will tune the &#39;padding&#39; parameter:</p><pre>import tensorflow as tf<br>from tensorflow.keras.layers import Conv2D<br>from tensorflow.keras.models import Sequential<br><br>model=Sequential()<br>model.add(Conv2d(filters=32,<br>                kernel_size=(3,3),# parameter f<br>                padding=&#39;same&#39;,<br>                activation=&#39;relu&#39;))<br># in this case we modify padding=&#39;same&#39; to same convolutions<br># padding=&#39;valid&#39; to valid convolutions<br>model.summary()<br></pre><p><strong>II) Strided convolution:</strong></p><ol><li><strong>Definition:</strong></li></ol><p>— Stride convolution is a technique used in (CNNs) to <strong>reduce the spatial dimensions of the input data</strong>. It involves applying a convolution operation with a larger stride value, which means the filter moves across the input data by <strong>skipping some positions.</strong></p><p>— Stride convolution can be used to <strong>extract high-level features</strong> from the input data. By skipping some positions, the network focuses on capturing <strong>more abstract and global patterns in the data</strong>.</p><p>— Stride convolution can reduce the<strong> spatial dimensions</strong> of the input data, which can be useful in reducing the number of parameters in the network and <strong>preventing overfitting.</strong></p><p>2.<strong> How to do it?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zial6wTTZPtpjnKTiiUWMQ.png" /><figcaption>Convolution of m=7x7 and kernel f=3x3</figcaption></figure><p>— In essence, the kernel (blue window) will pass through all the image with steps=1. But with the parameter &#39;stride&#39; we can modify the steps(for example stride=2). This modification will affect on horizontal axis and vertical one.</p><figure><img alt="Image with stride=2" src="https://cdn-images-1.medium.com/max/862/1*CF27YtDM_tPzGGLKRQ6duA.png" /><figcaption>Image with stride(step)=2 (Horizontal axis)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*lxDedoPammycfo0jqzbnhQ.png" /><figcaption>Image with stride(step)=2 (Vertical axis)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n88GwS4rQp-LlWfaNJeXUA.png" /><figcaption>This is the result of strided convolution</figcaption></figure><p>With the strided convolution, the equation to calculate the dimensions of the image after convolution is modified with a parameter s(stride)</p><pre>                    M= ((n+2p-f)/s)+1  x ((n+2p-f)/s)+1<br>n: size of the initial image<br>p: padding amount <br>f: kernel size<br>s: stride (step)<br>M: size of the image after convolution</pre><p>In the following example,</p><pre>n=7<br>p=0<br>f=3<br>s=2<br>====&gt;M=((7+2*0-3)/2)+1=3</pre><p>3) Implement on Tensorflow:</p><pre>import tensorflow as tf<br>from tensorflow.keras.layers import Conv2D<br>from tensorflow.keras.models import Sequential<br><br>model=Sequential()<br>model.add(Conv2d(filters=32,<br>                kernel_size=(3,3),# parameter f<br>                padding=&#39;same&#39;, # padding <br>                stride=2 # stride (steps)<br>                activation=&#39;relu&#39;))<br># in this case we modify stride=2 meaning that step=2 on the horizontal and vertical axis<br>model.summary()</pre><p><strong>III) Conclusion:</strong></p><p>— In reality, the tuning of ‘padding’ parameter is quite simple. But to deeply understand it is also necessary. So I write this blog to help every one can understand the parameter ‘padding’.</p><p>— Strided convolution is essential technique to reduce the spaital dimensions of the image and extracting the essential features in the image.</p><h4>Reference:</h4><p>This blog is highly inspired by the course Deep learning Specialization of Andrew Ng.</p><ul><li><a href="https://www.coursera.org/learn/convolutional-neural-networks/lecture/o7CWi/padding">Padding - Foundations of Convolutional Neural Networks | Coursera</a></li><li><a href="https://www.coursera.org/learn/convolutional-neural-networks/lecture/wfUhx/strided-convolutions">Strided Convolutions - Foundations of Convolutional Neural Networks | Coursera</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ada1100351d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>