[Python] What modifications should i add in this Machine Learning Question (CNN layer)?

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    How to approach and solve this exercise?


    Below you will find the input, filter and resulting output of a CNN layer. These are plotted both as just grayscale images and with the exact pixel values written out. For the pixels on the very edge of the output image, any coordinates outside of the input image are assumed to be 0 (zero-padding). Some of the values in the output are missing. Compute the output values for pixels a, b and c using the given input and convolutional filter.

    enter image description here

    What are the values of pixels a, b and c?
    ​

    This is the furthest I got, but the values are still not correct:

    import numpy as np

    input_image = np.array([
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 1, 1, 0, 0, 0],
    [0, 3, 4, 4, 4, 3, 0, 0],
    [0, 1, 4, 4, 4, 1, 0, 0],
    [0, 0, 1, 3, 4, 4, 4, 2],
    [0, 0, 1, 2, 3, 4, 4, 3],
    [2, 4, 4, 4, 4, 3, 3, 3],
    [1, 2, 3, 2, 0, 2, 2, 2],
    [0, 0, 0, 0, 0, 0, 0, 0]
    ])

    filter = np.array([
    [2, 3, 0],
    [2, 2, 3],
    [0, 0, 2]
    ])

    # Function to perform convolution
    def convolution(input_image, filter, x, y):
    filter_size = filter.shape[0]
    result = 0
    for i in range(filter_size):
    for j in range(filter_size):
    if x + i < input_image.shape[0] and y + j < input_image.shape[1]:
    result += input_image[x + i, y + j] * filter[i, j]
    return result

    # Compute missing pixel values
    a = convolution(input_image, filter, 7, 1)
    b = convolution(input_image, filter, 5, 3)
    c = convolution(input_image, filter, 2, 1)

    print(f"a={a}, b={b}, c={c}")


    output: a=13, b=42, c=46 ✘ The value of pixel a is not correct.

    Continue reading...

Compartilhe esta Página