Implement a deterministic DenseNet forward pass. Apply the supplied 3 by 3 stem convolution, then each dense block in order with a transition between adjacent blocks. Finish with batch normalization, ReLU, global average pooling, and the supplied linear classifier.
xℓ=Hℓ([x0,x1,…,xℓ−1]) pn,c=HW1h=1∑Hw=1∑Wzn,c,h,w logits=pWfcT+bfcThe weights dictionary contains stem_conv, blocks, transitions, the four final batch-normalization vectors, fc_weight, and fc_bias. Each block is an ordered list of composite-layer dictionaries, and each transition supplies its own batch-normalization vectors and 1 by 1 convolution. Return the class logits as a float64 PyTorch tensor with shape (N,Cclasses).
Examples
Input: x = [[[[-0.15441,0.38403],[-0.03968,0.04415]]]], weights = {"stem_conv":[[[[-0.19179,-0.20879,-0.10259],[-0.19144,-0.02615,-0.15921],[-0.07057,0.06929,0.08283]]]],"blocks":[[{"bn_gamma":[0.69251],"bn_beta":[-0.1198],"bn_mean":[0.10282],"bn_var":[0.95959],"conv_weight":[[[[-0.13171,-0.19187,0.19535],[-0.04574,0.13067,0.15458],[-0.15993,-0.19898,0.09224]]]]}]],"transitions":[],"final_bn_gamma":[0.89042,0.59945],"final_bn_beta":[0.11602,0.29186],"final_bn_mean":[0.28338,-0.11992],"final_bn_var":[1.27184,0.65231],"fc_weight":[[0.04251,-0.0161],[-0.0053,0.0332]],"fc_bias":[-0.00133,0.11938]}, growth_rate = 1, eps = 0.00001
Output: [[-0.007462,0.132025]]
Explanation: The supplied stem, dense layer, final normalization, pooling, and classifier are applied in order.
Input: x.shape = (2, 1, 4, 4), weights = supplied DenseNet weights, growth_rate = 1, eps = 0.00001
Output: [[-0.110528,-0.069652],[-0.11093,-0.069368]]
Input: x.shape = (1, 2, 3, 3), weights = supplied DenseNet weights, growth_rate = 2, eps = 0.00001
Output: [[0.250917,-0.325549,0.058388]]
Hint 1
Implement one helper for the supplied evaluation-mode BN-ReLU operation.
Hint 2
Keep a feature list inside each block and concatenate it before every layer.
Hint 3
Average the final feature map over dimensions 2 and 3 before the classifier.
Requirements
- Use PyTorch.
- Apply every supplied dense block and transition in order.
- Use dense concatenation inside each block.
- Apply final BN-ReLU, global average pooling, and the supplied classifier.
- Return the documented float64 logits tensor.
Constraints
- x has shape (N, C_in, H, W) and dtype torch.float64.
- weights contains one fewer transition than dense blocks.
- Every composite convolution produces growth_rate channels.
- H and W are divisible by two once for every transition.
- All supplied floating tensors have dtype torch.float64.
- eps is positive.
All steps in DenseNet
Accepts: array
Accepts: object
Accepts: number
Accepts: number
Full Forward
Implement a deterministic DenseNet forward pass. Apply the supplied 3 by 3 stem convolution, then each dense block in order with a transition between adjacent blocks. Finish with batch normalization, ReLU, global average pooling, and the supplied linear classifier.
xℓ=Hℓ([x0,x1,…,xℓ−1]) pn,c=HW1h=1∑Hw=1∑Wzn,c,h,w logits=pWfcT+bfcThe weights dictionary contains stem_conv, blocks, transitions, the four final batch-normalization vectors, fc_weight, and fc_bias. Each block is an ordered list of composite-layer dictionaries, and each transition supplies its own batch-normalization vectors and 1 by 1 convolution. Return the class logits as a float64 PyTorch tensor with shape (N,Cclasses).
Examples
Input: x = [[[[-0.15441,0.38403],[-0.03968,0.04415]]]], weights = {"stem_conv":[[[[-0.19179,-0.20879,-0.10259],[-0.19144,-0.02615,-0.15921],[-0.07057,0.06929,0.08283]]]],"blocks":[[{"bn_gamma":[0.69251],"bn_beta":[-0.1198],"bn_mean":[0.10282],"bn_var":[0.95959],"conv_weight":[[[[-0.13171,-0.19187,0.19535],[-0.04574,0.13067,0.15458],[-0.15993,-0.19898,0.09224]]]]}]],"transitions":[],"final_bn_gamma":[0.89042,0.59945],"final_bn_beta":[0.11602,0.29186],"final_bn_mean":[0.28338,-0.11992],"final_bn_var":[1.27184,0.65231],"fc_weight":[[0.04251,-0.0161],[-0.0053,0.0332]],"fc_bias":[-0.00133,0.11938]}, growth_rate = 1, eps = 0.00001
Output: [[-0.007462,0.132025]]
Explanation: The supplied stem, dense layer, final normalization, pooling, and classifier are applied in order.
Input: x.shape = (2, 1, 4, 4), weights = supplied DenseNet weights, growth_rate = 1, eps = 0.00001
Output: [[-0.110528,-0.069652],[-0.11093,-0.069368]]
Input: x.shape = (1, 2, 3, 3), weights = supplied DenseNet weights, growth_rate = 2, eps = 0.00001
Output: [[0.250917,-0.325549,0.058388]]
Hint 1
Implement one helper for the supplied evaluation-mode BN-ReLU operation.
Hint 2
Keep a feature list inside each block and concatenate it before every layer.
Hint 3
Average the final feature map over dimensions 2 and 3 before the classifier.
Requirements
- Use PyTorch.
- Apply every supplied dense block and transition in order.
- Use dense concatenation inside each block.
- Apply final BN-ReLU, global average pooling, and the supplied classifier.
- Return the documented float64 logits tensor.
Constraints
- x has shape (N, C_in, H, W) and dtype torch.float64.
- weights contains one fewer transition than dense blocks.
- Every composite convolution produces growth_rate channels.
- H and W are divisible by two once for every transition.
- All supplied floating tensors have dtype torch.float64.
- eps is positive.
All steps in DenseNet
Accepts: array
Accepts: object
Accepts: number
Accepts: number