2019年2月16日

ResNet:网络复现

手画网络结构终归不如直接实现一遍搞得清楚,直接上网络结构代码,Res-152改良版(根据后续版本,残差块里的“卷积、批量归一化和激活”结构改成了“批量归一化、激活和卷积”)

# Mxnet 实现
# 残差模块
class Residual(nn.Block):
    def __init__(self, num_channels, num_channels_out, use_1x1conv=False, strides=1, **kwargs):
        super(Residual, self).__init__(**kwargs)
        self.conv1 = nn.Conv2D(num_channels, kernel_size=1)
        self.conv2 = nn.Conv2D(num_channels, kernel_size=3, padding=1,
                               strides=strides)
        self.conv3 = nn.Conv2D(num_channels, kernel_size=1)
        if use_1x1conv:
            self.conv4 = nn.Conv2D(num_channels, kernel_size=1,
                                   strides=strides)
        else:
            self.conv4 = None
        self.bn1 = nn.BatchNorm()
        self.bn2 = nn.BatchNorm()
        self.bn3 = nn.BatchNorm()
    def forward(self, X):
        Y = self.conv1(nd.relu(self.bn1(X)))
        Y = self.conv2(nd.relu(self.bn2(Y)))
        Y = self.conv3(nd.relu(self.bn3(Y)))
        if self.conv4:
            X = self.conv4(X)
        return nd.relu(Y + X)
# 残差层
def resnet_block(num_channels, num_channels_out, num_residuals, first_block=False):
    blk = nn.Sequential()
    for i in range(num_residuals):
        if i == 0 and not first_block:
            blk.add(Residual(num_channels, num_channels_out, use_1x1conv=True, strides=2))
        else:
            blk.add(Residual(num_channels, num_channels_out))
    return blk
# 网络实现
net = nn.Sequential()
net.add(nn.Conv2D(64, kernel_size=7, strides=2, padding=3),
        nn.BatchNorm(), nn.Activation('relu'),
        nn.MaxPool2D(pool_size=3, strides=2, padding=1))
net.add(resnet_block(64, 256, 3, first_block=True),
        resnet_block(128, 512, 8),
        resnet_block(256, 1024, 36),
        resnet_block(512, 2048, 3))
net.add(nn.GlobalAvgPool2D(), nn.Dense(10))
# 打印结构
X = nd.random.uniform(shape=(1, 1, 224, 224))
net.initialize()
for layer in net:
    X = layer(X)
    print(layer.name, 'output shape:\t', X.shape)
Share

You may also like...

发表评论

您的电子邮箱地址不会被公开。