Implement a Dense Block as used in DenseNet architectures. In a Dense Block, each layer receives feature maps from all preceding layers (via concatenation) and passes its output to all subsequent layers. Each layer applies BN-ReLU-Conv.
import numpy as np
def batch_norm(x, eps=1e-5):
mean = np.mean(x, axis=(0, 2, 3), keepdims=True)
var = np.var(x, axis=(0, 2, 3), keepdims=True)
return (x - mean) / np.sqrt(var + eps)
def relu(x):
return np.maximum(0, x)
def conv2d(x, kernel, bias, stride=1, pad=1):
if pad > 0:
x = np.pad(x, ((0,0),(0,0),(pad,pad),(pad,pad)), mode='constant')
N, C_in, H, W = x.shape
C_out, _, kH, kW = kernel.shape
out_h = (H - kH) // stride + 1
out_w = (W - kW) // stride + 1
out = np.zeros((N, C_out, out_h, out_w))
for n in range(N):
for co in range(C_out):
for i in range(out_h):
for j in range(out_w):
hs, ws = i * stride, j * stride
out[n, co, i, j] = np.sum(x[n, :, hs:hs+kH, ws:ws+kW] * kernel[co]) + bias[co]
return out
class DenseLayer:
def __init__(self, in_channels, growth_rate):
self.kernel = np.random.randn(growth_rate, in_channels, 3, 3) * np.sqrt(2.0 / (in_channels * 9))
self.bias = np.zeros(growth_rate)
def forward(self, x):
out = batch_norm(x)
out = relu(out)
out = conv2d(out, self.kernel, self.bias, stride=1, pad=1)
return out
class DenseBlock:
def __init__(self, in_channels, growth_rate, num_layers):
self.layers = []
for i in range(num_layers):
layer_in = in_channels + i * growth_rate
self.layers.append(DenseLayer(layer_in, growth_rate))
def forward(self, x):
features = [x]
for layer in self.layers:
concat_input = np.concatenate(features, axis=1)
new_features = layer.forward(concat_input)
features.append(new_features)
return np.concatenate(features, axis=1)growth_rate new feature maps.in_channels + num_layers * growth_rate.