(Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
首先给大家介绍的是网络层的输出维度调试:
from torchsummary import summary...
#在这里就随便用一个网络层了class OutConv(nn.Module):def __init__(self, in_channels, out_channels):super(OutConv, self).__init__()self.conv = nn.Conv3d(in_channels, out_channels, kernel_size=1)def forward(self, x):return self.conv(x)
print('OutConv:', summary(OutConv(1, 128), (1, 128, 128, 128), batch_size=1, device="cpu"))
上述的方法只适用于单输入(当然这是我自己的理解!!!我自己的理解,不一定是正确的!!)
所以如果我们有的层是双输入的话,我们可以这样输入:
class Conv(nn.Module):...这里就省略了def forward(self, x1, x2, x3):.....# conv = Conv(...)
# x1 = torch.randn(1, 32, 32, 32, 32) # 假设输入数据的尺寸是 (batch_size, channels, depth, height, width)
# x2 = torch.randn(1, 32, 64, 64, 64) # x2 的尺寸应该比 x1 的尺寸大
# x3 = torch.randn(1, 64, 128, 128, 128)
# # 通过 Conv 实例
# output = ups(x1, x2, x3)
# # 查看输出的尺寸
# print("Conv Output size:", output.size())
然后当我们把网络结构搭好之后:
from torchsummary import summary...class net(nn.Module):def __init__(self, ...):....device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = net(...).to(device)summary(model, (1, 112, 112, 80))
就像下面这样:
我们可以看到所有的维度信息,还有参数量信息
最后如果我们想调试搭建好的网络的其中一部分,我们可以这样:
def forward(self, x):x1 = self.inc(x)print("x1 shape:", x1.shape)x2 = self.down1(x1)print("x2 shape:", x2.shape)
我们可以看到:
各位小伙伴们,本期内容是针对有一些基础的人,如果大家基础不是很好的话,有可能会看不懂,因为我也是想给大家分享,原谅我没有写的这么细致~
注:里面有一些内容是我自己的理解,如果有理解不正确的,请大家谅解,不是误人子弟!!