【发布时间】:2020-08-23 09:09:23
【问题描述】:
我正在尝试将一些 resnet 层重用于自定义架构,但遇到了一个我无法弄清楚的问题。这是一个简化的示例;当我跑步时:
import torch
from torchvision import models
from torchsummary import summary
def convrelu(in_channels, out_channels, kernel, padding):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel, padding=padding),
nn.ReLU(inplace=True),
)
class ResNetUNet(nn.Module):
def __init__(self):
super().__init__()
self.base_model = models.resnet18(pretrained=False)
self.base_layers = list(self.base_model.children())
self.layer0 = nn.Sequential(*self.base_layers[:3])
def forward(self, x):
print(x.shape)
output = self.layer0(x)
return output
base_model = ResNetUNet().cuda()
summary(base_model,(3,224,224))
给我:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 112, 112] 9,408
Conv2d-2 [-1, 64, 112, 112] 9,408
BatchNorm2d-3 [-1, 64, 112, 112] 128
BatchNorm2d-4 [-1, 64, 112, 112] 128
ReLU-5 [-1, 64, 112, 112] 0
ReLU-6 [-1, 64, 112, 112] 0
================================================================
Total params: 19,072
Trainable params: 19,072
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 36.75
Params size (MB): 0.07
Estimated Total Size (MB): 37.40
----------------------------------------------------------------
这是复制每一层(有 2 个 convs、2 个 batchnorm、2 个 relu's),而不是每个层。如果我打印出self.base_layers[:3],我会得到:
[Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False), BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True), ReLU(inplace=True)]
仅显示三层而没有重复。为什么它会复制我的图层?
我使用的是 pytorch 1.4.0 版
【问题讨论】:
标签: deep-learning neural-network pytorch