1. Home
  2. Explore
  3. Computer Science
  4. Image Recognition Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Image Recognition Basics

🧠 Introduction to Neural Networks: Structure and Function

Our brains effortlessly recognize patterns, like various forms of the digit "3", despite significant pixel variations. This ability is comically trivial for humans but dauntingly difficult for computers to replicate. Machine learning and neural networks aim to solve such complex pattern recognition problems. This introduction focuses on the structural component of neural networks, demystifying them as a piece of mathematics rather than a buzzword.

The goal is to understand how a neural network is structured and what it means for it to "learn." We will explore a simple, "plain vanilla" neural network designed to recognize handwritten digits, a classic example in the field. This foundational understanding is crucial for grasping more powerful modern variants of neural networks.


💡 What is a Neuron?

In the context of neural networks, a neuron is simply a container for a number, specifically a value between 0 and 1. This number is called the activation of the neuron. A higher activation value suggests the neuron is "lit up" or more active.

🖼️ Layers of a Neural Network

Neural networks are organized into layers of these neurons.

  1. Input Layer:

    Corresponds to the raw input data.

  2. For a 28x28 pixel image, there are 784 neurons (28 * 28).

  3. Each input neuron holds a number representing the grayscale value of a corresponding pixel, where 0 is black and 1 is white.

  4. Output Layer:

    Represents the network's final decision or classification.

  5. For handwritten digit recognition (0-9), there are 10 neurons, one for each digit.

  6. The activation of an output neuron indicates how much the network believes the input image corresponds to that specific digit. The brightest neuron in this layer is the network's chosen digit.

  7. Hidden Layers:

    Layers positioned between the input and output layers.

  8. These layers are where the network performs its complex processing to recognize patterns.

  9. The exact number of hidden layers and neurons within them is often an arbitrary or experimental choice. In our example, there are two hidden layers, each with 16 neurons.

  10. The activations in one layer determine the activations of the next layer, forming the core information processing mechanism. This is loosely analogous to how biological neurons firing cause others to fire.


🧠 How a Network Operates: Information Flow

Once a neural network has been trained (which means its internal parameters have been adjusted), it processes information by propagating activations through its layers.

  1. An image is fed into the input layer, "lighting up" the 784 neurons according to pixel brightness.

  2. This pattern of activations in the input layer causes a specific pattern in the first hidden layer.

  3. This pattern then causes a pattern in the second hidden layer.

  4. Finally, this leads to a pattern in the output layer, where the most active neuron indicates the network's prediction.

🌟 The Hope for Hidden Layers: Abstraction

The design of a layered structure is motivated by the idea of hierarchical feature extraction or abstraction.

  • Human Recognition: When humans recognize digits, we break them down into subcomponents (e.g., a "9" has a loop and a line; an "8" has two loops).

  • Ideal Network Behavior:

    We hope that neurons in the second-to-last hidden layer might correspond to these larger subcomponents (e.g., an "upper loop" neuron, a "long vertical line" neuron). An image containing an upper loop (like a 9 or 8) would activate the "upper loop" neuron.

  • This would simplify the final step: the output layer just needs to learn which combination of subcomponents corresponds to which digit.

  • Further, neurons in an earlier hidden layer (e.g., the second layer) might correspond to even simpler, more fundamental components like small edges (horizontal, vertical, diagonal).

  • The network would then build up complexity: pixels combine into edges, edges combine into patterns (like loops), and patterns combine into digits.

This layered abstraction is also useful for other tasks, such as parsing speech, which involves layers of abstraction from raw audio to sounds, syllables, words, and phrases.


⚙️ The Mechanics: Weights and Biases

The core of how activations in one layer determine activations in the next lies in weights and biases. These are the "dials and knobs" that the network can tweak to learn patterns.

🔗 Connections and Weighted Sums

  • Each connection between a neuron in one layer and a neuron in the next layer has an associated weight, which is just a number.

  • To calculate the activation of a neuron in a subsequent layer, we compute a weighted sum of the activations from the previous layer's neurons.

    For example, if we want a neuron in the second layer to detect an edge in a specific region, we would assign positive weights to pixels within that region and negative weights to surrounding pixels. The weighted sum would be largest when the central pixels are bright and surrounding pixels are dark, indicating an edge.

📈 The Sigmoid Function (Activation Function)

  • The weighted sum can be any real number, but neuron activations are desired to be between 0 and 1.

  • Therefore, the weighted sum is "squished" by an activation function. A common one, historically, is the sigmoid function (also known as the logistic curve).

    sigmoid(x): Very negative inputs become close to 0, very positive inputs become close to 1, and it steadily increases around x=0.

  • The neuron's activation is thus a measure of how positive its relevant weighted sum is.

🎯 Bias

  • A bias is an additional number added to the weighted sum before it's passed through the sigmoid function.

  • It allows the neuron to become active only when the weighted sum exceeds a certain threshold, effectively controlling the neuron's "eagerness" to fire. For example, a bias of -10 means the weighted sum needs to be greater than 10 before the neuron meaningfully activates.

  • Weights tell the neuron what pixel pattern it's picking up on.

  • Bias tells the neuron how high that weighted sum needs to be before it becomes active.

📊 Network Complexity: Parameters

  • Every neuron in a hidden layer is connected to all neurons in the previous layer, each with its own weight. Each neuron also has its own bias.

  • For our example network:

    Input to first hidden layer: 784 (input neurons) * 16 (hidden neurons) = 12,544 weights + 16 biases.

  • Combined with connections between other layers, this network has almost 13,000 total weights and biases.

These 13,000 "knobs and dials" are the parameters that are tweaked during the learning process.


🧮 Mathematical Representation: Vectorization

Writing out the calculations for each neuron individually is cumbersome. A more notationally compact and efficient way uses linear algebra.

  1. Activations as Vectors: Organize all activations from one layer into a column vector.

  2. Weights as a Matrix: Organize all weights connecting two layers into a matrix (W). Each row of this matrix corresponds to the connections to a single neuron in the next layer.

  3. Biases as a Vector: Organize all biases for a layer into a column vector (b).

  4. Matrix-Vector Product: The weighted sum for all neurons in the next layer can be computed by a matrix-vector product of the weight matrix and the activation vector from the previous layer (W * a).

  5. Adding Bias: Add the bias vector to the result (W * a + b).

  6. Applying Sigmoid: Apply the sigmoid function component-wise to each element of the resulting vector: sigmoid(W * a + b).

This compact expression a_next = sigmoid(W * a_prev + b) represents the full transition of activations from one layer to the next, making the code simpler and faster due to optimized matrix multiplication libraries.

The entire neural network is fundamentally just a function that takes 784 numbers (pixel values) as input and outputs 10 numbers (digit probabilities). It is an absurdly complicated function involving thousands of parameters, matrix-vector products, and activation functions, but a function nonetheless.


🎯 Modern Activation Functions: ReLU

While the sigmoid function was historically common, modern neural networks often use different activation functions because they can be easier to train.

  • Rectified Linear Unit (ReLU):

    A common modern alternative to sigmoid.

  • Defined as max(0, a), where a is the weighted sum plus bias.

  • This function outputs 0 if the input is negative (inactive) and the input value itself if it's positive (active).

  • ReLU was partly motivated by a biological analogy of neurons being either activated or not, simplifying the activation process. It proved very effective for training deep neural networks.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent