VGG在中间层进行输出的Pytorch实现

简介

搭建网络时有时需要使用到VGG网络的部分结构(如图1所示),这时需要在VGG网络的中间层输出。图1所示的鉴别器网络就分别需要从Relu3_1和Relu5_1层进行输出。
VGG在中间层进行输出的Pytorch实现_第1张图片
图1 鉴别器网络

参考下述博文VGG的Pytorch实现代码,修改部分代码实现VGG网络在中间层输出。

https://blog.csdn.net/zhanghao3389/article/details/85038252

VGG网络不同层的名称描述可参照博文:

https://blog.csdn.net/dudek/article/details/115425783

修改后的代码实现

class VGG(nn.Module):
    def __init__(
        self,
        features: nn.Module,
        num_classes: int = 1000,
        init_weights: bool = True,
        layer_name: str = None
    ) -> None:
        super(VGG, self).__init__()
        self.features = features
        self.layer_name = layer_name
        self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
        self.classifier = nn.Sequential(
            nn.Linear(512 * 7 * 7, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )
        if init_weights:
            self._initialize_weights()

    def forward(self, x: torch.Tensor, layer_name) -> torch.Tensor:
        self.layer_name = layer_name
        if self.layer_name is None:
            x = self.features(x)
            x = self.avgpool(x)
            x = torch.flatten(x, 1)
            x = self.classifier(x)
        else:
            x = self.features(x)
        return x

    def _initialize_weights(self) -> None:
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)

def make_layers(cfg, layer_name=None, batch_norm=False):
    layers = []
    in_channels = 1
    layer_count = 0
    if layer_name == 'Relu3_1':
        layer_count_stop = 7
    elif layer_name == 'Relu5_1':
        layer_count_stop = 17
    for v in cfg:
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
            if batch_norm:
                layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
            else:
                layers += [conv2d, nn.ReLU(inplace=True)]
            in_channels = v
        layer_count += 1
        if layer_count == layer_count_stop:
            break
    return nn.Sequential(*layers)
    
cfg = {
    'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
    'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
    'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
    'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}

def vgg19_bn(layer_name, **kwargs):
    # *args和 **kwargs经常在函数定义中被使用,用来传递可变长参数。*args表示任意多个无名参数,是一个tuple,**kwargs表示关键字参数,是一个dict
    # 当*args和 **kwargs同时是使用时,*args参数列表要放在 **kwargs前边
    model = VGG(make_layers(cfg['E'], layer_name=layer_name, batch_norm=True), **kwargs)
    return model

只需调用vgg19_bn函数并输入希望输出层的名称layer_name即可实现仅使用VGG部分网络结构的目的。调用vgg19_bn函数的示例代码如下:

vgg19_relu3_1_net = vgg19_bn(layer_name='Relu3_1')
vgg19_relu3_1_net = vgg19_relu3_1_net.cuda()
vgg19_relu3_1 = vgg19_relu3_1_net(input, layer_name='Relu3_1')

你可能感兴趣的:(Pytorch,Modules,python,pytorch)