为什么我的pytorch模型摘要没有参数?

发布于 2025-02-09 20:13:44 字数 3404 浏览 3 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2025-02-16 20:13:44

您的模型定义必须在模块类的初始化器中进行:__ Init __,而forward处理该模块的推理逻辑。您使用的pytorch层是模块 ,在用于推理之前需要初始化它们。与Keras这样的框架不同,Pytorch不需要汇编步骤,逻辑是声明定义的。

这是让您开始的事情:

class UNetlike(nn.Module):
    def __init__(self):
      super().__init__()
      
        self.c1 = nn.Sequential(self.conv2d_block(3, 16),
                                nn.MaxPool2d(kernel_size=2),
                                nn.Dropout2d(0.1))

        self.c2 = nn.Sequential(self.conv2d_block(16, 32),
                                nn.MaxPool2d(kernel_size=2),
                                nn.Dropout2d(0.1))
        

        self.c2 = nn.Sequential(self.conv2d_block(32, 64),
                                nn.MaxPool2d(kernel_size=2),
                                nn.Dropout2d(0.1))
        
        self.c4 = nn.Sequential(self.conv2d_block(64, 128),
                                nn.MaxPool2d(kernel_size=2),
                                nn.Dropout2d(0.1))
        
    def conv2d_block(self, in_channels, out_channels):
        return nn.Sequential(
            nn.Conv2d(in_channels=in_channels, out_channels=out_channels, 
                      kernel_size=3, padding="same"),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(),
            nn.Conv2d(in_channels=out_channels, out_channels=out_channels, 
                      kernel_size=3, padding="same"),
            nn.BatchNorm2d(out_channels),
            nn.ReLU())
        
    def forward(self, x):
        p1 = self.c1(x)
        p2 = self.c2(p1)
        p3 = self.c3(p2)
        p4 = self.c3(p3)
        # so on and so forth

Your model definition must go in the initializer of your module class: __init__, while the forward 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:

class UNetlike(nn.Module):
    def __init__(self):
      super().__init__()
      
        self.c1 = nn.Sequential(self.conv2d_block(3, 16),
                                nn.MaxPool2d(kernel_size=2),
                                nn.Dropout2d(0.1))

        self.c2 = nn.Sequential(self.conv2d_block(16, 32),
                                nn.MaxPool2d(kernel_size=2),
                                nn.Dropout2d(0.1))
        

        self.c2 = nn.Sequential(self.conv2d_block(32, 64),
                                nn.MaxPool2d(kernel_size=2),
                                nn.Dropout2d(0.1))
        
        self.c4 = nn.Sequential(self.conv2d_block(64, 128),
                                nn.MaxPool2d(kernel_size=2),
                                nn.Dropout2d(0.1))
        
    def conv2d_block(self, in_channels, out_channels):
        return nn.Sequential(
            nn.Conv2d(in_channels=in_channels, out_channels=out_channels, 
                      kernel_size=3, padding="same"),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(),
            nn.Conv2d(in_channels=out_channels, out_channels=out_channels, 
                      kernel_size=3, padding="same"),
            nn.BatchNorm2d(out_channels),
            nn.ReLU())
        
    def forward(self, x):
        p1 = self.c1(x)
        p2 = self.c2(p1)
        p3 = self.c3(p2)
        p4 = self.c3(p3)
        # so on and so forth
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文