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
-----------------------------------------------------------------------
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