Problems
Loading...
Step 2 of 6
U-Net Decoder Block
Medium

Implement one numerical U-Net decoder block in NHWC layout. Repeat each spatial value twice along height and width, apply the supplied channel projection and ReLU, center-crop the skip tensor to the upsampled size, concatenate skip channels before decoder channels, then apply two same-padded convolutions with ReLU.

U=ReLU⁡(Upsample⁡(X)Wup+bup)U = \operatorname{ReLU}(\operatorname{Upsample}(X)W_{\mathrm{up}} + b_{\mathrm{up}}) Y=ConvBlock⁡(Concat⁡(CenterCrop⁡(S),U))Y = \operatorname{ConvBlock}(\operatorname{Concat}(\operatorname{CenterCrop}(S), U))

Here, XX is the lower-resolution decoder input, SS is the encoder skip tensor, UU is the projected upsampled tensor, and YY is the decoder output. Return YY as a float64 NumPy array.

Loading visualization...

Examples

Input: x = [[[[1]]]], skip = [[[[0.5],[1]],[[-0.5],[2]]]], W_up = [[2]], b_up = [0], kernel1 = [[[[0.5],[1]]]], bias1 = [0.1], kernel2 = [[[[1]]]], bias2 = [-0.2]

Output: [[[[2.15],[2.4]],[[1.65],[2.9]]]]

Explanation: Upsampling restores spatial resolution before skip features and decoder features are fused.

Input: x = [[[[1,-1]]]], skip = [[[[0],[1]],[[2],[3]]]], W_up = [[1],[0.5]], b_up = [0.2], kernel1 = [[[[1,-1],[0.5,1]]]], bias1 = [0,0.1], kernel2 = [[[[0.5],[1]]]], bias2 = [0]

Output: [[[[0.975],[0.675]],[[1.175],[1.675]]]]

Input: x = [[[[0.5]]]], skip = [[[[1],[2],[3],[4]],[[5],[6],[7],[8]],[[9],[10],[11],[12]],[[13],[14],[15],[16]]]], W_up = [[1]], b_up = [0], kernel1 = [[[[1],[1]]]], bias1 = [0], kernel2 = [[[[1]]]], bias2 = [0]

Output: [[[[6.5],[7.5]],[[10.5],[11.5]]]]

Hint 1

Use np.repeat twice to upsample the spatial axes.

Hint 2

Project the upsampled last axis before concatenating the centered skip crop.

Hint 3

Reuse the same-padded convolution operation for both final stages.

Requirements

  • Use NumPy.
  • Upsample by repeating values along both spatial axes.
  • Apply the supplied channel projection and ReLU.
  • Center-crop and concatenate the skip tensor.
  • Apply both supplied same-padded convolutions with ReLU.
  • Return a float64 NumPy array.

Constraints

  • x and skip use NHWC layout and dtype float64.
  • The skip height and width are at least twice those of x.
  • W_up and b_up match the decoder input and projected channel widths.
  • Both convolution kernels have positive odd spatial size.
  • Every supplied numeric array has dtype float64.