为什么我的pytorch模型摘要没有参数?
我是pytorch的初学者。
我想自动编码器模型类似于U-NET。
因此,我在下面进行了代码,并使用pytorch_model_summary
参见摘要,但是结果告诉我模型有任何参数...
为什么我的模型有任何参数?
class unet_like(nn.Module):
def __init__(self):
super(unet_like, self).__init__()
def conv2d_block(self, in_channels, out_channels, x):
x = nn.Conv2d(in_channels=in_channels, out_channels = out_channels, kernel_size = 3, padding = "same")(x)
x = nn.BatchNorm2d(out_channels)(x)
x = nn.ReLU()(x)
x = nn.Conv2d(in_channels = out_channels, out_channels = out_channels, kernel_size = 3, padding = "same")(x)
x = nn.BatchNorm2d(out_channels)(x)
x = nn.ReLU()(x)
return x
def forward(self, x):
c1 = self.conv2d_block(3, 16, x)
p1 = nn.MaxPool2d(kernel_size = 2)(c1)
p1 = nn.Dropout2d(0.1)(p1)
c2 = self.conv2d_block(16, 32, p1)
p2 = nn.MaxPool2d(kernel_size = 2)(c2)
p2 = nn.Dropout(0.1)(p2)
c3 = self.conv2d_block(32, 64, p2)
p3 = nn.MaxPool2d(kernel_size = 2)(c3)
p3 = nn.Dropout(0.1)(p3)
c4 = self.conv2d_block(64, 128, p3)
p4 = nn.MaxPool2d(kernel_size = 2)(c4)
p4 = nn.Dropout(0.1)(p4)
c5 = self.conv2d_block(128, 256, p4)
# nn.ConvTranspose2d(in_channels = 16, out_channels = 64, kernel_size = 3, stride = 1, padding = (1, 1)),
# nn.ReLU(),
u6 = nn.ConvTranspose2d(in_channels=256, out_channels=128, kernel_size = 2, stride = 2, output_padding = (0,1))(c5)
print(u6.shape)
print(c4.shape)
u6 = torch.cat([u6, c4], 1) # u6: 128, c4: 128
print(u6.shape)
u6 = nn.Dropout(0.1)(u6)
c6 = self.conv2d_block(256, 128, u6)
u7 = nn.ConvTranspose2d(in_channels = 128, out_channels = 64, kernel_size = 2, stride = 2, output_padding = (1,0))(c6)
u7 = torch.cat([u7, c3], 1)
u7 = nn.Dropout(0.1)(u7)
c7 = self.conv2d_block(128, 64, u7)
u8 = nn.ConvTranspose2d(in_channels = 64, out_channels = 32, kernel_size = 2, stride = 2, output_padding = (0,1))(c7)
u8 = torch.cat([u8, c2], 1)
u8 = nn.Dropout(0.1)(u8)
c8 = self.conv2d_block(64, 32, u8)
u9 = nn.ConvTranspose2d(in_channels = 32, out_channels = 16, kernel_size = 2, stride = 2, output_padding = (0,1))(c8)
u9 = torch.cat([u9, c1], 1)
u9 = nn.Dropout(0.1)(u9)
c9 = self.conv2d_block(32, 16, u9)
# in_channels, kernel_size,
# outputs = Conv2D(1, (1, 1), activation = "sigmoid")(c9)
c9 = nn.Conv2d(in_channels=16, out_channels = 1, kernel_size = 3, padding = (1,1))(c9)
outputs = nn.Sigmoid()(c9)
return outputs
model = unet_like().to("cpu")
print(pytorch_model_summary.summary(model, torch.tensor(train_images[:1], dtype = torch.float32).to("cpu"), show_input=True))
torch.Size([1, 128, 12, 9])
torch.Size([1, 128, 12, 9])
torch.Size([1, 256, 12, 9])
-----------------------------------------------------------------------
Layer (type) Input Shape Param # Tr. Param #
=======================================================================
unet_like-1 [1, 3, 100, 75] 0 0
=======================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
-----------------------------------------------------------------------
im beginer of pytorch.
I want to auto encoder model similar to U-Net.
so I make below code, and see summary using pytorch_model_summary
but the result told me model have any parameters...
why my model have any parameters??
class unet_like(nn.Module):
def __init__(self):
super(unet_like, self).__init__()
def conv2d_block(self, in_channels, out_channels, x):
x = nn.Conv2d(in_channels=in_channels, out_channels = out_channels, kernel_size = 3, padding = "same")(x)
x = nn.BatchNorm2d(out_channels)(x)
x = nn.ReLU()(x)
x = nn.Conv2d(in_channels = out_channels, out_channels = out_channels, kernel_size = 3, padding = "same")(x)
x = nn.BatchNorm2d(out_channels)(x)
x = nn.ReLU()(x)
return x
def forward(self, x):
c1 = self.conv2d_block(3, 16, x)
p1 = nn.MaxPool2d(kernel_size = 2)(c1)
p1 = nn.Dropout2d(0.1)(p1)
c2 = self.conv2d_block(16, 32, p1)
p2 = nn.MaxPool2d(kernel_size = 2)(c2)
p2 = nn.Dropout(0.1)(p2)
c3 = self.conv2d_block(32, 64, p2)
p3 = nn.MaxPool2d(kernel_size = 2)(c3)
p3 = nn.Dropout(0.1)(p3)
c4 = self.conv2d_block(64, 128, p3)
p4 = nn.MaxPool2d(kernel_size = 2)(c4)
p4 = nn.Dropout(0.1)(p4)
c5 = self.conv2d_block(128, 256, p4)
# nn.ConvTranspose2d(in_channels = 16, out_channels = 64, kernel_size = 3, stride = 1, padding = (1, 1)),
# nn.ReLU(),
u6 = nn.ConvTranspose2d(in_channels=256, out_channels=128, kernel_size = 2, stride = 2, output_padding = (0,1))(c5)
print(u6.shape)
print(c4.shape)
u6 = torch.cat([u6, c4], 1) # u6: 128, c4: 128
print(u6.shape)
u6 = nn.Dropout(0.1)(u6)
c6 = self.conv2d_block(256, 128, u6)
u7 = nn.ConvTranspose2d(in_channels = 128, out_channels = 64, kernel_size = 2, stride = 2, output_padding = (1,0))(c6)
u7 = torch.cat([u7, c3], 1)
u7 = nn.Dropout(0.1)(u7)
c7 = self.conv2d_block(128, 64, u7)
u8 = nn.ConvTranspose2d(in_channels = 64, out_channels = 32, kernel_size = 2, stride = 2, output_padding = (0,1))(c7)
u8 = torch.cat([u8, c2], 1)
u8 = nn.Dropout(0.1)(u8)
c8 = self.conv2d_block(64, 32, u8)
u9 = nn.ConvTranspose2d(in_channels = 32, out_channels = 16, kernel_size = 2, stride = 2, output_padding = (0,1))(c8)
u9 = torch.cat([u9, c1], 1)
u9 = nn.Dropout(0.1)(u9)
c9 = self.conv2d_block(32, 16, u9)
# in_channels, kernel_size,
# outputs = Conv2D(1, (1, 1), activation = "sigmoid")(c9)
c9 = nn.Conv2d(in_channels=16, out_channels = 1, kernel_size = 3, padding = (1,1))(c9)
outputs = nn.Sigmoid()(c9)
return outputs
model = unet_like().to("cpu")
print(pytorch_model_summary.summary(model, torch.tensor(train_images[:1], dtype = torch.float32).to("cpu"), show_input=True))
torch.Size([1, 128, 12, 9])
torch.Size([1, 128, 12, 9])
torch.Size([1, 256, 12, 9])
-----------------------------------------------------------------------
Layer (type) Input Shape Param # Tr. Param #
=======================================================================
unet_like-1 [1, 3, 100, 75] 0 0
=======================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
-----------------------------------------------------------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模型定义必须在模块类的初始化器中进行:
__ Init __
,而forward
处理该模块的推理逻辑。您使用的pytorch层是模块 类 ,在用于推理之前需要初始化它们。与Keras这样的框架不同,Pytorch不需要汇编步骤,逻辑是声明定义的。这是让您开始的事情:
Your model definition must go in the initializer of your module class:
__init__
, while theforward
handles the inference logic of that module. The PyTorch layers you are using are module classes, they need to be initialized before being use for inference. Unlike frameworks like keras, PyTorch doesn't need a compilation step, the logic is defined declaratively.Here's something to get you started: