• pytoch-基本卷积网络结构, 参数提取,参数初始化

    基本卷积网络结构net.py 

    from torch import nn
    
    class SimpleCNN(nn.Module):
        def __init__(self):
            super(SimpleCNN, self).__init__()
            layer1 = nn.Sequential() # 将网络模型进行添加
            layer1.add_module(conv1, nn.Conv2d(3, 32, 3, 1, padding=1)) # nn.Conv
            layer1.add_module(relu1, nn.ReLU(True))
            layer1.add_module(pool1, nn.MaxPool2d(2, 2))
            self.layer1 = layer1
    
            layer2 = nn.Sequential()
            layer2.add_module(conv2, nn.Conv2d(32, 64, 3, 1, padding=1))
            layer2.add_module(relu2, nn.ReLU(True))
            layer2.add_module(pool2, nn.MaxPool2d(2, 2))
            self.layer2 = layer2
    
            layer3 = nn.Sequential()
            layer3.add_module(conv3, nn.Conv2d(64, 128, 3, 1, padding=1))
            layer3.add_module(relu3, nn.ReLU(True))
            layer3.add_module(pool3, nn.MaxPool2d(2, 2))
            self.layer3 = layer3
    
            layer4 = nn.Sequential()
            layer4.add_module(fc1, nn.Linear(2048, 512))
            layer4.add_module(fc_relu1, nn.ReLU(True))
            layer4.add_module(fc2, nn.Linear(512, 64))
            layer4.add_module(fc_relu2, nn.ReLU(True))
            layer4.add_module(fc3, nn.Linear(64, 10))
            self.layer4 = layer4
    
        def forward(self, x):
            conv1 = self.layer1(x)
            conv2 = self.layer2(conv1)
            conv3 = self.layer3(conv2)
            fc_input = conv3.view(conv3.size(0), -1)
            fc_out = self.layer4(fc_input)
    
            return fc_out
    
    model = SimpleCNN()
    # print(model) # 打印输出网络结构

    提取前两层的网络结构

    new_model = nn.Sequential(*list(model.children())[:2])  # 提取前两层的网络结构, 构造nn.Sequential网络串接, * 表示将里面的内容一个个传进去

    提取所有层的网络结构

    conv_model = nn.Sequential()
    # 提取所有的卷积层操作, model.name_modules() 提取所有层的网络结构
    for name, layer in model.named_modules():
        if isinstance(layer, nn.Conv2d):
            name = name.replace(., _)
            conv_model.add_module(name, layer)
    print(conv_model)
    相关文章
    相关标签/搜索
    一%2C肖一码′期期准