ENع
→CNNs
الدرس 15 · التعلّم العميق

Convolutional networks

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

A convolutional network (CNN) reads an image through small filters instead of wiring every pixel to every neuron. Slide one 3×3 filter over a 6×6 image, multiply and add at each of 16 positions to build a feature map, then apply ReLU and max pooling. See why sharing nine weights beats 576, and how stacked, learned filters turn edges into objects.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 15 · تفاعلي
01 / 10

An image is numbers

To a computer, an image is a grid of numbers, one per pixel (picture dot). Our tiny 6×6 image uses 1 for bright and 0 for dark. It shows a short vertical stroke, two pixels wide.

x (6x6), 1 = bright, 0 = dark
    0  0  1  1  0  0
    0  0  1  1  0  0
    0  0  1  1  0  0
    0  0  1  1  0  0
    0  0  0  0  0  0
    0  0  0  0  0  0

a real photo: 3 numbers per pixel (red, green, blue)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. An image is numbers

    To a computer, an image is a grid of numbers, one per pixel (picture dot). Our tiny 6×6 image uses 1 for bright and 0 for dark. It shows a short vertical stroke, two pixels wide.

    x (6x6), 1 = bright, 0 = dark
        0  0  1  1  0  0
        0  0  1  1  0  0
        0  0  1  1  0  0
        0  0  1  1  0  0
        0  0  0  0  0  0
        0  0  0  0  0  0
    
    a real photo: 3 numbers per pixel (red, green, blue)
  2. A filter: nine weights

    A convolutional network (CNN) looks at small patches, not the whole image at once. It uses a filter, also called a kernel: a 3×3 grid of weights. Ours, +1 left, 0 middle, −1 right, looks for bright-left, dark-right edges.

    k (3x3 filter)
        1  0 −1
        1  0 −1
        1  0 −1
    
    big positive answer: bright on the left, dark on the right
    big negative answer: dark on the left, bright on the right
  3. Multiply and add

    Lay the filter on the top-left 3×3 patch. Multiply each pixel by the weight on it, then add the nine products. Every row gives 0 + 0 − 1, so the total is −3. That one number starts the feature map.

    patch (rows 0-2, columns 0-2) times k:
        0  0 −1
        0  0 −1
        0  0 −1
    
    sum = (0 + 0 + −1) + (0 + 0 + −1) + (0 + 0 + −1)
        = −3   -> fmap[0][0]
  4. Slide it everywhere

    Slide the filter one pixel right and repeat, row by row. It fits 6 − 3 + 1 = 4 ways across and 4 down: 16 positions, 16 numbers. Over the stroke's right edge it gives 3. These numbers form a 4×4 feature map.

    for i in range(4):          # 4 rows of positions
      for j in range(4):        # 4 columns
    
    at (0, 2): (1 + 0 + 0) + (1 + 0 + 0) + (1 + 0 + 0) = 3
    
    fmap
       −3 −3  3  3
       −3 −3  3  3
       −2 −2  2  2
       −1 −1  1  1
  5. One filter, shared

    The same nine weights were used at all 16 positions. That is weight sharing: this layer has just 9 weights plus 1 bias (here 0), whatever the image size. Linking all 36 pixels to all 16 outputs would need 576 weights.

    convolution  9 weights + 1 bias = 10 numbers to learn
    dense        36 inputs x 16 outputs = 576 weights
    
    and the filter finds the edge wherever it is in the image
  6. ReLU drops negatives

    Next, an activation function called ReLU turns every negative number into 0 and keeps the rest: max(0, v). The −3s marked the stroke's other edge, dark-to-bright, which this filter is not looking for. Now only its edge remains.

    relu(v) = max(0, v)
    
    before
       −3 −3  3  3
       −3 −3  3  3
       −2 −2  2  2
       −1 −1  1  1
    after
        0  0  3  3
        0  0  3  3
        0  0  2  2
        0  0  1  1
  7. Pooling shrinks the map

    Max pooling splits the map into 2×2 blocks and keeps the biggest number in each. The 4×4 map becomes 2×2: 0, 3, 0, 2. It is four times smaller and still says: the edge is on the right, strongest at the top.

    maxpool: the largest of each 2x2 block
    
    relu(fmap)
        0  0  3  3
        0  0  3  3
        0  0  2  2
        0  0  1  1
    
    out
        0  3
        0  2
  8. Many filters, many maps

    A real layer has many filters, each making its own feature map: one finds vertical edges, another horizontal ones, another colour blobs. AlexNet's first layer has 96. Deeper layers filter those maps, finding corners, then textures, then parts of objects.

    layer 1   edges, colour blobs
    layer 2   corners, simple textures
    deeper    parts of objects: eyes, wheels, letters
    
    AlexNet (2012), first layer: 96 filters of 11x11x3
  9. From maps to an answer

    At the end, the small maps are flattened into one list of numbers and fed to an ordinary dense layer. It gives a score to each class, here "vertical" or "horizontal" stroke, and the highest score is the answer.

    flatten  [[0, 3], [0, 2]]  ->  [0, 3, 0, 2]
    dense    one score per class
               vertical    <- highest
               horizontal
    
    conv -> ReLU -> pool, repeated, then dense
  10. The filters are learned

    We chose our filter by hand. In a real CNN the filter weights start random. Training compares the answer with the label, and backpropagation with gradient descent nudges every weight to lower the loss. Edge-finding filters appear on their own.

    start    random weights in every filter
    repeat   predict, measure the loss,
             backpropagate the slopes,
             nudge every weight downhill
    
    nobody writes "look for edges": it is learned

اختبار يتاح بعد الخطوة الأخيرة.