Problems
Loading...
Step 4 of 6
Dense Block
Hard

Implement a DenseNet block with concatenative connectivity. Layer ℓ\ell receives the original block input and every preceding layer output concatenated along the channel axis.

xℓ=Hℓ ⁣([x0,x1,…,xℓ−1])x_{\ell}=H_{\ell}\!\left([x_0,x_1,\ldots,x_{\ell-1}]\right) Hℓ(x)=Conv⁡3×3 ⁣(ReLU⁡(BN⁡(x)))H_{\ell}(x)=\operatorname{Conv}_{3\times3}\!\left(\operatorname{ReLU}(\operatorname{BN}(x))\right)

Here, each layer dictionary supplies its batch-normalization parameters and convolution weight, while growth_rate is the number KK of channels added by every layer. Return [x0,x1,…,xL][x_0,x_1,\ldots,x_L] as one float64 PyTorch tensor with shape (N,C+LK,H,W)(N,C+LK,H,W).

Loading visualization...

Examples

Input: x = [[[[1,0],[0,-1]]]], layers = [{"bn_gamma":[1],"bn_beta":[0],"bn_mean":[0],"bn_var":[1],"conv_weight":[[[[0,0.1,0],[0.1,0.5,0.1],[0,0.1,0]]]]}], growth_rate = 1, eps = 0.00001

Output: [[[[1,0],[0,-1]],[[0.499998,0.1],[0.1,0]]]]

Explanation: The output retains the original channel and appends the new feature map produced from it.

Input: x.shape = (1, 1, 2, 3), layers = 2 supplied layer dictionaries, growth_rate = 1, eps = 0.00001

Output: [[[[-0.05157,-0.03165,-0.09038],[-0.48279,0.6395,-0.21491]],[[0.065917,0.072333,-0.073062],[0.020997,-0.038131,0.046715]],[[0.253784,0.352888,-0.253893],[-0.105019,0.094921,0.140759]]]]

Input: x.shape = (1, 2, 3, 2), layers = 1 supplied layer dictionaries, growth_rate = 2, eps = 0.00001

Output: [[[[-0.07919,0.83936],[0.46152,0.72792],[-0.4711,-0.12916]],[[0.68561,0.93799],[0.10141,0.88331],[0.70686,-0.21615]],[[-0.253079,-0.2722],[-0.073621,-0.315352],[-0.17462,0.106576]],[[-0.289511,0.0074],[0.108911,0.033796],[-0.009493,0.217671]]]]

Hint 1

Keep a list initialized with x and concatenate it along dim=1 before each layer.

Hint 2

Use padding=1 so every new feature map can be concatenated spatially.

Requirements

  • Use PyTorch.
  • Concatenate the block input and all preceding layer outputs before every composite transformation.
  • Apply each supplied layer in order.
  • Preserve the input before appending the new feature maps.
  • Return the documented float64 tensor.

Constraints

  • x has shape (N, C, H, W) and dtype torch.float64.
  • layers contains L dictionaries in execution order.
  • At layer l, each batch-normalization vector has shape (C + lK).
  • At layer l, conv_weight has shape (K, C + lK, 3, 3).
  • growth_rate equals K, and eps is positive.
  • N, C, H, W, K, and L are positive.