Convultion Neural Network
Convolutional Neural Networks: How Machines Learn to See
How do you recognize a friend's face in a crowd? You don't measure the distance between their eyes. Your brain picks up small cues first, like an edge, a curve, a patch of colour, and then assembles them into bigger things: an eye, a nose, a face, your friend.
A convolutional neural network, or CNN, is built to work the same way. It's the technology behind photo tagging, medical scan analysis, and the cameras that read number plates.
Why images are a problem
A photo is a grid of numbers. A modest 224 × 224 colour image has 150,528 of them. Suppose you flattened that into one long list and connected every number to every neuron in a layer of 1,000. That's about 150 million weights in a single layer, and it has a deeper flaw: flattening throws away the fact that pixels sitting next to each other are related.
Two more things make images awkward. A cat is still a cat if it's in the top-left corner instead of the middle, and the network ought to know that without being shown the cat in every possible position. And most of what matters in a picture is local: an edge is defined by a handful of neighbouring pixels, not by the entire frame.
CNNs are designed around exactly these facts.
The convolution
The core operation is simple. Take a small grid of numbers called a filter (or kernel), typically 3 × 3. Lay it over the top-left corner of the image. Multiply each filter value by the pixel underneath, add up the nine results, and write that single number down. Then slide the filter one step to the right and do it again. Keep going across and down until you've covered the whole image.
The grid of numbers you end up with is a feature map. It lights up wherever the filter's pattern shows up in the image.
What patterns? That's the clever part. You don't design the filters. The network learns them during training. Early filters usually become edge detectors (vertical, horizontal, diagonal), while later ones respond to textures, then to parts like wheels or eyes.
Two ideas make this efficient:
- Local connectivity. Each output number depends only on a small patch, not the whole picture.
- Weight sharing. The same filter is reused at every position. Nine weights can scan an image of any size, and a pattern is recognised wherever it appears. A layer usually has many filters, say 32 or 64, each producing its own feature map, and each learning to spot something different.
Stride and padding
Two knobs control the sliding:
- Stride is how far the filter jumps each step. A stride of 2 skips every other position and shrinks the output.
- Padding adds a border of zeros around the image so the filter can sit on the edges, and so the output doesn't shrink too fast. "Same" padding keeps the size unchanged.
Pooling
After a convolution, many networks add a pooling layer. Max pooling takes each small window (often 2 × 2) and keeps only its largest value. This halves the width and height, cuts computation, and makes the network less fussy about the exact pixel position of a feature. If an edge moves one pixel, the pooled result barely changes.
The full pipeline
A typical CNN stacks these pieces:
- Convolution + ReLU, to detect simple features.
- Pooling, to shrink the map.
- Repeat steps 1 and 2 several times. Each round sees a larger part of the original image and detects more complex shapes.
- Flatten the final feature maps into a list.
- A few dense layers and an output (often softmax) to produce class probabilities. So what you get is a ladder: pixels, then edges, then textures, then parts, then objects.
Code example
python
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Input(shape=(28, 28, 1)), # greyscale digit image
layers.Conv2D(32, 3, activation="relu"),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, activation="relu"),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(10, activation="softmax") # digits 0-9
])
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
Trained on the classic handwritten-digit dataset, a model this small usually reaches around 99% accuracy.
Famous architectures
A few names are worth knowing:
- LeNet (1990s): the early pioneer that read handwritten digits on cheques.
- AlexNet (2012): won a major image-recognition contest by a huge margin and kicked off the modern deep learning wave.
- VGG: showed that stacking many small 3 × 3 filters works well.
- ResNet: introduced skip connections, which let information bypass layers and made networks with over a hundred layers trainable.
- EfficientNet: balances depth, width and image size for strong accuracy at lower cost. ## Practical advice
Use transfer learning. Training from scratch needs enormous amounts of data. Instead, take a network already trained on millions of images, keep its early layers (which have learned general edges and textures), and retrain only the last few layers on your own small dataset. It's often the difference between a project that works and one that doesn't.
Augment your data. Flip, rotate, crop and brighten your training images. The network sees more variety without you collecting a single new photo.
Watch for shortcuts. CNNs are famous for latching onto the wrong clue, like recognising a boat by the water around it. Check what the model is actually looking at, using techniques like Grad-CAM heatmaps.
Beyond photos
The same machinery works on anything with local structure on a grid: medical scans, satellite imagery, audio turned into spectrograms, even short stretches of text or sensor data using one-dimensional filters.
Takeaways
- A small filter slides across the image, detecting one pattern wherever it appears.
- Local connections and shared weights keep the model compact.
- Pooling shrinks the data and adds tolerance to small shifts.
- Stacked layers build from edges up to objects.
- Transfer learning is your best friend when data is limited.
原文:https://www.a8q9.cn/en/article/convultion-neural-network-421114.html