<?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 Matthew Bishop on Medium]]></title>
        <description><![CDATA[Stories by Matthew Bishop on Medium]]></description>
        <link>https://medium.com/@matthewbishop58?source=rss-831f8549ec5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Matthew Bishop on Medium</title>
            <link>https://medium.com/@matthewbishop58?source=rss-831f8549ec5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:55:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matthewbishop58/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[Convolutional Neural Networks (CNNs)]]></title>
            <link>https://medium.com/@matthewbishop58/convolutional-neural-networks-cnns-db669a813f30?source=rss-831f8549ec5------2</link>
            <guid isPermaLink="false">https://medium.com/p/db669a813f30</guid>
            <category><![CDATA[convolutional-network]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Matthew Bishop]]></dc:creator>
            <pubDate>Mon, 12 Sep 2022 17:58:59 GMT</pubDate>
            <atom:updated>2022-09-12T18:02:24.423Z</atom:updated>
            <content:encoded><![CDATA[<p>Convolutional Neural Networks (CNNs) originate from study of the brain’s visual cortex and have been used in image recognition since the 1980s. Recently, due to increases in computational power and availability of training data. CNNs power self-driving cars, image search services and more. CNNs are not restricted to visual perception and can also be used for voice recognition and natural language processing.</p><p>This blog post contains:</p><p>· The origins of CNNs</p><p>· The architecture of CNNs</p><p>· How to implement CNNs in TensorFlow 2 and Keras.</p><p>· Notable CNN architectures</p><h3>The Architecture of the Visual Cortex</h3><p>A series of experiments performed on cats by David H. Hubel and Torsten Wiesel in the 1950s gave key insights into the structure of the visual cortex. They showed that many neurons within the visual cortex have a small local receptive field i.e., they react only to visual stimuli located within a limited region of the visual field. Receptive fields may overlap, and together their piece together the whole visual field.</p><p>It was also shown that some neurons react only to images of horizontal lines and others react specifically to lines in different orientations. Two neurons with the same receptive field may react to lines of different orientations. Some neurons however had larger receptive fields and react to more complex pattern which are combinations of lower-level patterns. This led to the idea that higher-level neurons are based on the outputs of neighbouring lower-level neurons, illustrated in <strong>Figure 1</strong>. This architecture can detect complex patterns in any area of the visual field.</p><h4>Figure 1 — Layers of pattern identification</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*QVDtnC7WflnVtYcXMypEhg.png" /></figure><p>Image Source (Geron, 2019)</p><p>An important milestone occurred in 1998 when Yann LeCun published the famous LeNet-5 architecture. As well as using fully connected layers and sigmoid activation functions, LeNet-5 introduced two new components, <strong>convolutional layers </strong>and <strong>pooling layers</strong>.</p><h3>Convolutional Layers</h3><p>The key component of a CNN is a <strong>convolutional layer</strong>. Each neuron in the first convolutional layer is not connected to every pixel in the input image. Instead, neurons are connected to the pixels in their receptive fields. Each neuron in the second convolutional layer is only connected to neurons located within the receptive field of the first layer, illustrated in <strong>Figure 2</strong>.</p><p>In this architecture the first convolutional layer identifies low-level features from the input and passes them on to the second convolutional layer which assembles them into higher-level features. This process is continued through each convolutional layer in the network.</p><p>CCNs are highly effective at image recognition as hierarchical structure is a common feature of real-world images. Neural networks with fully connected (dense) layers can work on small images but break down on larger images due to the number of parameters. A 100 x 100 px image contains 10,000 pixels. If the first layer of the network contains just 1,000 neurons that means a total of 10 million connections. CNNs overcome this problem through the use of partially connected layers.</p><h4>Figure 2 — Convolutional layers</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*_urx4OvqGeSEpUirwiy5Qw.png" /></figure><p>Image Source (Geron, 2019)</p><p>In a CNN each layer is represented in 2D. A neuron in a given layer, located in row <em>i</em>, column<em> j</em> is connected to neruons in rows <em>i</em> to <em>i + fh-1</em> and columns <em>j</em> to <em>j + fw-1</em> of the previous layer. Where <em>fh</em> and <em>fw</em> are the height and width of the receptive field.</p><p>To allow a layer to have the same height and width as the previous layer, zeros are commonly added around the the inputs. This is known as <strong>zero padding, </strong>shown in<strong> Figure 3</strong>.</p><h4>Figure 3 — Zero padding</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*BwtfPw23egXDOLggnuMnbg.png" /></figure><p>Image Source (Geron, 2019</p><p>A large input layer can be connected to a smaller convolutional layer by spacing out the receptive fields. The shift from one receptive field to the next is known as a <strong>stride</strong> and dramatically reduces a model’s complexity. In Figure X, a 5 x 7 input later (plus zero padding) is connected to a 3 x 4 layer using 3 x 3 receptive fields with a stride of 2.</p><p><strong>Note:</strong> In this example the stride is the same on both directions, but this is not a requirement.</p><p>A neuron located in row i, column j of the upper layer is connected to the outputs of neurons in the previous later from rows <em>i x sh </em>to <em>i x sh + fh-1 </em>and columns j x sw to j x sw + fw-1. Where sh and sw are the vertical and horizontal strides, shown in <strong>Figure 4</strong>.</p><h4>Figure 4 — Strides</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*1kOcYP2gtfkyRMOQMtz43Q.png" /></figure><p>Image Source (Geron, 2019)</p><h3>Filters</h3><p>The weights of a neuron can be represented as a small image the size of the receptive field. <strong>Figure 5</strong> shows two possible sets of weights (called filters or convolution kernels). The first is represented as a black square with a white line in the middle. It is a 7 x 7 matrix of 0s except for the middle column which is full of 1s. Neurons which use these weights will ignore everything in their receptive field except f the central vertical line as all other inputs will be multiplied by 0. The second filter is a black square with a horizontal white line in the middle, neurons using these weights will ignore everything in their receptive field except for the central horizontal line.</p><p>If all neurons in a layer use the same vertical line filter and the same bias term. Feeding in the bottom image of <strong>Figure 5</strong> as in input image will output the top-left image. In this output image the vertical white lines are enhanced, and the rest are blurred. The top-right image shows the output if all neurons use the same horizontal line filter. A layer full of neurons using the same filter output a feature map which highlight the areas in an image which activate that feature the most.</p><p>The filters do not have to be defined manually. During training the convolutional layer will automatically learn the most useful filters for its task, and the layers above will learn to combine them into more complex patterns.</p><h4>Figure 5 — Vertical and horizontal line feature maps</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*_alwnKUiWP4BIqiy_5xKnw.png" /></figure><p>Image Source (Geron, 2019</p><h3>Stacking Multiple Feature Maps</h3><p>For simplicity, each convolutional layer has been presented as a 2D layer. In reality, a convolutional layer uses multiple filters, outputting one feature map per filter. A more accurate representation of a convolutional layer is in 3D (shown in <strong>Figure 6</strong>). The 3D later has one neuron per pixel in each feature map, all neurons within a given feature map share the same parameters (weights and bias) and neurons of different feature maps use different parameters. A neuron’s receptive field is as described but extends across the previous layer’s feature maps. A convolutional layer simultaneously applied multiple trainable filters to its inputs and can detect multiple</p><p>As all neurons in a feature map share the same parameters, the number of parameters in the model are greatly reduced. Once a CNN has learned to recognise a pattern in one location, it can recognise it in any other. By contrast once a deep neural network has learned to recognise a pattern in once location, it can only recognise it in that specific location.</p><p>Input images are also composed of multiple sublayers, one for every colour channel. Typically, there are three: red, green and blue (RGB). Greyscale images have just one colour channel. Some images such as satellite images which capture extra light frequencies (such as infrared) can have many more.</p><h4>Figure 6 — Multiple feature maps</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*YHC1ieQ_06Fp6MooFCHLcA.png" /></figure><p>Image Source (Geron, 2019)</p><h3>Pooling Layers</h3><p>Pooling layers subsample (shrink) the image to reduce computational load, memory usage and the number of parameters, which minimises the risk of overfitting. Similarly, to convolutional layers, each neuron of a pooling later is connected to the outputs of a select number of neurons in the previous layer, defined by a rectangular receptive field.</p><p>A pooling neuron has no weights, it just aggregates the inputs using an aggregate function such as the max or mean. <strong>Figure 7</strong> illustrates a max pooling layer, using a 2x2 pooling kernel, a stride of 2 and no padding. Only the maximum input of each receptive field is transferred to the net later, all other inputs are dropped.</p><p>As there is a stride of 2, the output image is half the width and height of the input image (rounded down as no padding is used).</p><h4>Figure 7 — Max pooling later with 2x2 pooling kernel.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*jxJT5rNNKO94Obx0Pjkhug.png" /></figure><p>Image Source (Geron, 2019)</p><p>Beyond reducing the number of parameters and computational load, max pooling layers also add invariance to small translations. If we assume dark pixels have a higher value than light pixels and consider three images passing through a max pooling layer (A, B, C) shown in <strong>Figure 8</strong>. Images B and C are the same as A but shifted one and two pixels to the right. The max pooling output for A and B are the same, showing translation invariance. Image C is shifted one pixel to the right, a 50% invariance.</p><p>By inserting a max pooling layer every few layers in a CNN, it is possible to get some level of translation invariance at a larger scale. Moreover, max pooling offers a small amount of rotational invariance and a slight scale invariance. Such invariance (even if it is limited) can be useful in cases where the prediction should not depend on these details, such as in classification tasks.</p><h4>Figure 8 — Max pooling invariance</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*oP7aQE4RFe1bB6Dl4gl_pQ.png" /></figure><p>Image Source (Geron, 2019</p><p>There are downsides to max pooling. A very small kernel (2 x 2) with a stride of 2 will produce an output with a length and width that is two times smaller, resulting in an area a quarter of the original size. This is highly destructive, dropping 75% of the input values. Invariance is also not always desirable, for example in semantic segmentation where the task is to classify each pixel according to the object it belongs to.</p><h3>CNN Architectures</h3><p>CNNs typically stack a few convolutional layers, each with ReLU activations followed by a pooling later, then more convolutional layers, another pooling layer and so on. As the image progresses through the network it gets smaller and smaller. It also gets deeper and deeper (i.e., with more feature maps) due to the convolutional layers. At the end of the stack, a regular feedforward network is added with a few dense layers (with ReLU activation) and a final output layer. A typical CNN architecture is shown in <strong>Figure 9</strong>.</p><h4>Figure 9 — A typical CNN Architecture</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*7zc9X2NmspW995lNiLdcIg.png" /></figure><p>Image Source (Geron, 2019</p><p>A common mistake is to use kernels that are too large. It is better to stack two convolutional layers with 3x3 kernels than a single later with a 5 x 5 kernel. They will use fewer parameters and require fewer computations. One exception is the first convolutional layer which can typically have a large kernel (for example 5 x 5) and a stride of 2 or more. This will reduce the spatial dimension of the image without losing too much information.</p><p>A simple CNN can be implemented in TensorFlow as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*8F5pxNuUS6YzgmDST3SM4w.png" /></figure><h3>Data Augmentation</h3><p>Data augmentation is a regularization technique which aims to reduce overfitting by artificially increasing the size of the training set through generating many realistic variants of each training image. Augmented data should be as realistic as possible, a human should not be able to tell whether a given image was augmented or not.</p><p>Examples of augmentation (shown in <strong>Figure 10</strong>) include shifting, rotating and resizes every picture in the training set by a varying amount and adding the resulting pictures to the training set. This makes the model more robust and toleration to variation to changes in sizes, position and orientation of test images.</p><p>To make a model more tolerant to different lighting conditions, images can be generated with various contrasts. Images that do not contain text can also be flipped horizontally. Combining these transformations can greatly increase the size of a training set.</p><h4>Figure 10 — Examples of data augmentation</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*JRx_V1xARkZSBmtcTw9VJQ.png" /></figure><p>Image Source (Geron, 2019)</p><h3>References</h3><p>Geron, A. (2019) <em>Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems</em>. Sebastopol: O’Reilly Media, Incorporated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db669a813f30" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>