Skip to content

Conv2D

Conv2d is the standard layer for image processing within a neural network

This layer performs a convolution by sliding a filter (The kernel) across the image. A new, scaled down image is output, calculated by the weighted sum of the pixels under the kernel at each position as well as adding a bias term.

The out_channels parameter determins the number of learned features the model will extract from the image.

Output Dimensions

The dimensions of the output image is determined by the values for kernel_size, stride, and padding The formula for each dimension is the following

\[ Output\ Size = \left \lfloor \frac{input\ size + 2\times padding - kernel\ size}{stride} \right \rfloor + 1 \]

Bases: Module

Standard convolutional layer

Applies a kernel to an image output a scaled down image of feature channels

Parameters:

Name Type Description Default
in_channels int

Number of channels in the input image

required
out_channels int

Number of channels in the output image

required
kernel_size int or tuple

Shape of kernel (h, w). If integer then defaults to (a, a)

required
stride int or tuple

Number of pixels to skip in each direction (h, w). If integer then defaults to (a, a)

required
padding int or tuple

number of zeros to apply to the input image vertically and horizontally (h, w). If integer then defaults to (a, a)

required

Shapes

Input:
    (N, in_channels, in_height, in_width)

Output:
    (N, out_channels, out_height, out_width)
Example

layer = ntk.Conv2d(3, 32, 2, 1, 0)

y = layer(x)

implementation

import neuraltoolkit as ntk
import numpy as np

image_count = 100
in_channels = 3
height = 28
width = 28

x = ntk.Tensor(
    np.random.rand(image_count, in_channels, height, width)
)

layer = ntk.Conv2d(
    in_channels=in_channels,
    out_channels=32,
    kernel_size=2,
    stride=1,
    padding=0
)

y = layer(x)