BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Computer vision25 of 35 in topic

321. Implement a multi-channel grouped conv2d with stride, padding and dilation

HardComputer vision Pro
Asked atMicrosoft

Why it matters Every CNN layer is a conv2d like this; grouped and depthwise convs are the same code with a different groups value.

Real conv layers mix many input channels, and ResNeXt, MobileNet and ConvNeXt use groups. One function covers them all.

Write conv2d_grouped(x, w, b, stride=1, padding=0, dilation=1, groups=1). x is (C_in, H, W), w is (C_out, C_in // groups, kh, kw), b is (C_out,). Zero-pad x by padding on every side. Output channel o belongs to group o // (C_out // groups) and only sees that group's C_in // groups input channels. Kernel taps are dilation apart. Output size is Ho = (H + 2 * padding - dilation * (kh - 1) - 1) // stride + 1, and the same for Wo. Return a (C_out, Ho, Wo) float array (cross-correlation, no kernel flip).

Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.

Functiondef conv2d_grouped(x, w, b, stride=1, padding=0, dilation=1, groups=1)

Example 1

Input: x = ones((2, 3, 3)), w = ones((2, 1, 2, 2)), b = [0, 10], groups = 2

Output: [[[4, 4], [4, 4]], [[14, 14], [14, 14]]]

Example 2

Input: R(conv2d_grouped(X, Wfull, [0.5, -0.5, 0.0], stride=2, padding=1))

Output: [[[7.5, -12.5, -4.5], [-11.5, -9.5, 5.5], [9.5, 19.5, -9.5]], [[-12.5, 6.5, 13.5], [3.5, 10.5, -11.5], [2.5, -12.5, 1.5]], [[14.0, 7.0, -12.0], [0.0, -18.0, 13.0], [-8.0, -3.0, -1.0]]]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Loop over output channels and positions; slice x with steps of dilation to collect the taps.

Hint 2

np.pad with ((0,0),(p,p),(p,p)).

Hint 3

Group of output o is o // (C_out // groups).

Hint 4

Slice r : r + d*(kh-1) + 1 : d.

Common mistakes
  • Wrong group mapping.
  • Forgetting dilation in the slice end.
  • Flipping the kernel (that is true convolution; deep learning layers do not flip).
Understand the idea

A conv layer slides a small filter over the image. With several input channels, the filter has one slice per input channel and the results are added. Groups split the channels into independent teams: each output channel only looks at its own team's inputs, which cuts the cost by the number of groups. Dilation spaces the filter taps apart to see wider without more weights; stride skips positions; padding adds a zero border.

Worked example

two input channels of ones, groups = 2, one 2x2 kernel of ones per output. Each output channel sees only one input channel: 4 ones, so 4, plus bias 0 or 10.

out[o, i, j] = b[o] + sum over the group's channels and taps of x[c, i*s + u*d, j*s + v*d] * w[o, c, u, v]

ResNeXt uses groups, MobileNet uses groups = C_in (depthwise), and DeepLab uses dilation.

Shapes and data flow
x (C_in,H,W), w (C_out, C_in/g, kh, kw) -> out (C_out, Ho, Wo)
Topics
Computer vision
Companies1
  • Microsoft the topic appears in interview reports Microsoft Interview Prep: Questions, Process & Study Plan

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Remove duplicate detections with IoU and non-max suppressionHard
  • Assign anchor boxes to ground-truth objects for detector trainingHard
  • Implement a single-channel transposed convolutionHard
  • Implement the backward pass of a strided multi-channel conv2dHard
All Computer vision problems
Report
321 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def conv2d_grouped(x, w, b, stride=1, padding=0, dilation=1, groups=1):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS