Implement the cross-correlation used by an AlexNet-style convolution layer for an NHWC image batch. For image X, kernel K, bias b, stride s, and zero-padding p, each output value is
Yn,i,j,o=bo+u=0∑Kh−1v=0∑Kw−1c=0∑C−1Xn,is+u,js+v,c(p)Ku,v,c,o.Here, n selects a batch item, (i,j) selects an output location, c is an input channel, o is an output channel, and X(p) is the zero-padded image. Return the convolution result as a float64 NumPy array with shape (B,Hout,Wout,Cout).
Examples
Input: image = [[[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]]], kernel = [[[[1]],[[0]]],[[[0]],[[-1]]]], bias = [0], stride = 1, padding = 0
Output: [[[[-4],[-4]],[[-4],[-4]]]]
Explanation: Each kernel window is multiplied elementwise with the image patch, summed, and shifted by the output-channel bias.
Input: image = [[[[1,2],[3,4]],[[5,6],[7,8]]]], kernel = [[[[2],[-1]]]], bias = [0.5], stride = 1, padding = 0
Output: [[[[0.5],[2.5]],[[4.5],[6.5]]]]
Input: image = [[[[1],[2]],[[3],[4]]]], kernel = [[[[1]],[[1]]],[[[1]],[[1]]]], bias = [0], stride = 2, padding = 1
Output: [[[[1],[2]],[[3],[4]]]]
Hint 1
Use np.pad only on the height and width axes.
Hint 2
np.tensordot can contract a patch over its height, width, and input-channel axes.
Hint 3
The output sizes are floor((size + 2 * padding - kernel_size) / stride) + 1.
Requirements
- Compute every output value from the supplied image, kernel, bias, stride, and padding.
- Use NHWC image layout and (Kh,Kw,Cin,Cout) kernel layout.
- Apply zero-padding equally to the height and width dimensions.
- Return a new float64 NumPy array.
Constraints
- image has shape (B,H,W,Cin).
- kernel has shape (Kh,Kw,Cin,Cout).
- bias has shape (Cout,).
- stride is a positive integer and padding is a nonnegative integer.
- H+2p≥Kh and W+2p≥Kw.
All steps in AlexNet
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Accepts: number
Convolutional Layers
Implement the cross-correlation used by an AlexNet-style convolution layer for an NHWC image batch. For image X, kernel K, bias b, stride s, and zero-padding p, each output value is
Yn,i,j,o=bo+u=0∑Kh−1v=0∑Kw−1c=0∑C−1Xn,is+u,js+v,c(p)Ku,v,c,o.Here, n selects a batch item, (i,j) selects an output location, c is an input channel, o is an output channel, and X(p) is the zero-padded image. Return the convolution result as a float64 NumPy array with shape (B,Hout,Wout,Cout).
Examples
Input: image = [[[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]]], kernel = [[[[1]],[[0]]],[[[0]],[[-1]]]], bias = [0], stride = 1, padding = 0
Output: [[[[-4],[-4]],[[-4],[-4]]]]
Explanation: Each kernel window is multiplied elementwise with the image patch, summed, and shifted by the output-channel bias.
Input: image = [[[[1,2],[3,4]],[[5,6],[7,8]]]], kernel = [[[[2],[-1]]]], bias = [0.5], stride = 1, padding = 0
Output: [[[[0.5],[2.5]],[[4.5],[6.5]]]]
Input: image = [[[[1],[2]],[[3],[4]]]], kernel = [[[[1]],[[1]]],[[[1]],[[1]]]], bias = [0], stride = 2, padding = 1
Output: [[[[1],[2]],[[3],[4]]]]
Hint 1
Use np.pad only on the height and width axes.
Hint 2
np.tensordot can contract a patch over its height, width, and input-channel axes.
Hint 3
The output sizes are floor((size + 2 * padding - kernel_size) / stride) + 1.
Requirements
- Compute every output value from the supplied image, kernel, bias, stride, and padding.
- Use NHWC image layout and (Kh,Kw,Cin,Cout) kernel layout.
- Apply zero-padding equally to the height and width dimensions.
- Return a new float64 NumPy array.
Constraints
- image has shape (B,H,W,Cin).
- kernel has shape (Kh,Kw,Cin,Cout).
- bias has shape (Cout,).
- stride is a positive integer and padding is a nonnegative integer.
- H+2p≥Kh and W+2p≥Kw.
All steps in AlexNet
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Accepts: number