Growing the Grid Back

Using Vision ModelsMedium+20 XP+20

Growing the Grid Back

A segmentation network's encoder shrinks a photo down to a small grid of features. The decoder then has to grow that grid back to full size. One common way is a transposed convolution: a layer with a learned kernel that works like convolution run backwards.

Ordinary convolution reads a window of inputs and writes one output value. A transposed convolution does the reverse: each input value writes a whole window.

  • Multiply every entry of the k×kk \times kk×k kernel by one input value x[i][j]x[i][j]x[i][j]. Call the result a stamp.
  • Place the stamp on the output with its top-left corner at row i⋅si \cdot si⋅s and column j⋅sj \cdot sj⋅s, where sss is the stride.
  • Where stamps overlap, their values add.

The output starts as all zeros and is exactly large enough to hold every stamp. Notice that the stride now spreads the stamps apart instead of skipping positions.

Task: write grow_grid(x, kernel, stride).

  • x is the input grid, a list of rows. It need not be square.
  • kernel is a k×kk \times kk×k grid, a list of rows.
  • stride is a positive integer.
  • Return the output grid as a list of rows of floats, each rounded to 4 decimal places (so 3 is reported as 3.0).

Examples

x = [[1, 2], [3, 4]]
kernel = [[1, 1], [1, 1]]
stride = 1
returns [[1.0, 3.0, 2.0], [4.0, 10.0, 6.0], [3.0, 7.0, 4.0]]
x = [[2, -1], [0, 3]]
kernel = [[1, 0], [2, 1]]
stride = 2
returns [[2.0, 0.0, -1.0, 0.0], [4.0, 2.0, -2.0, -1.0], [0.0, 0.0, 3.0, 0.0], [0.0, 0.0, 6.0, 3.0]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run