import paddle.nn as nn
class ResidualBlock(nn.Layer):
def __init__(self, in_channels, out_channels, stride = 1, downsample = None):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2D(in_channels, out_channels, kernel_size = 3, stride = stride, padding = 1),
nn.BatchNorm2D(out_channels),
nn.ReLU())
self.conv2 = nn.Sequential(
nn.Conv2D(out_channels, out_channels, kernel_size = 3, stride = 1, padding = 1),
nn.BatchNorm2D(out_channels))
self.downsample = downsample
self.relu = nn.ReLU()
self.out_channels = out_channels
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.conv2(out)
if self.downsample:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Layer):
def __init__(self, block, layers, num_classes = 1000):
super(ResNet, self).__init__()
self.inplanes = 64
self.conv1 = nn.Sequential(
nn.Conv2D(3, 64, kernel_size = 7, stride = 2, padding = 3),
nn.BatchNorm2D(64),
nn.ReLU())
self.maxpool = nn.MaxPool2D(kernel_size = 3, stride = 2, padding = 1)
self.layer0 = self._make_layer(block, 64, layers[0], stride = 1)
self.layer1 = self._make_layer(block, 128, layers[1], stride = 2)
self.layer2 = self._make_layer(block, 256, layers[2], stride = 2)
self.layer3 = self._make_layer(block, 512, layers[3], stride = 2)
self.avgpool = nn.AvgPool2D(7, stride=1)
self.fc = nn.Linear(2048, num_classes)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes:
downsample = nn.Sequential(
nn.Conv2D(self.inplanes, planes, kernel_size=1, stride=stride),
nn.BatchNorm2D(planes),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.maxpool(x)
x = self.layer0(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.avgpool(x)
# x = x.view(x.size(0), -1)
x = paddle.reshape(x, [x.shape[0],-1])
x = self.fc(x)
return x
model = ResNet(ResidualBlock, [3, 4, 6, 3], num_classes=2)#模型实例化
paddle.Model(model).summary((-1, 3, 256, 256))
W0505 09:07:12.146911 5588 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0505 09:07:12.151273 5588 device_context.cc:465] device: 0, cuDNN Version: 7.6.
----------------------------------------------------------------------------
Layer (type) Input Shape Output Shape Param #
============================================================================
Conv2D-1 [[1, 3, 256, 256]] [1, 64, 128, 128] 9,472
BatchNorm2D-1 [[1, 64, 128, 128]] [1, 64, 128, 128] 256
ReLU-1 [[1, 64, 128, 128]] [1, 64, 128, 128] 0
MaxPool2D-1 [[1, 64, 128, 128]] [1, 64, 64, 64] 0
Conv2D-2 [[1, 64, 64, 64]] [1, 64, 64, 64] 36,928
BatchNorm2D-2 [[1, 64, 64, 64]] [1, 64, 64, 64] 256
ReLU-2 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
Conv2D-3 [[1, 64, 64, 64]] [1, 64, 64, 64] 36,928
BatchNorm2D-3 [[1, 64, 64, 64]] [1, 64, 64, 64] 256
ReLU-3 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
ResidualBlock-1 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
Conv2D-4 [[1, 64, 64, 64]] [1, 64, 64, 64] 36,928
BatchNorm2D-4 [[1, 64, 64, 64]] [1, 64, 64, 64] 256
ReLU-4 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
Conv2D-5 [[1, 64, 64, 64]] [1, 64, 64, 64] 36,928
BatchNorm2D-5 [[1, 64, 64, 64]] [1, 64, 64, 64] 256
ReLU-5 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
ResidualBlock-2 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
Conv2D-6 [[1, 64, 64, 64]] [1, 64, 64, 64] 36,928
BatchNorm2D-6 [[1, 64, 64, 64]] [1, 64, 64, 64] 256
ReLU-6 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
Conv2D-7 [[1, 64, 64, 64]] [1, 64, 64, 64] 36,928
BatchNorm2D-7 [[1, 64, 64, 64]] [1, 64, 64, 64] 256
ReLU-7 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
ResidualBlock-3 [[1, 64, 64, 64]] [1, 64, 64, 64] 0
Conv2D-9 [[1, 64, 64, 64]] [1, 128, 32, 32] 73,856
BatchNorm2D-9 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
ReLU-8 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
Conv2D-10 [[1, 128, 32, 32]] [1, 128, 32, 32] 147,584
BatchNorm2D-10 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
Conv2D-8 [[1, 64, 64, 64]] [1, 128, 32, 32] 8,320
BatchNorm2D-8 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
ReLU-9 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
ResidualBlock-4 [[1, 64, 64, 64]] [1, 128, 32, 32] 0
Conv2D-11 [[1, 128, 32, 32]] [1, 128, 32, 32] 147,584
BatchNorm2D-11 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
ReLU-10 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
Conv2D-12 [[1, 128, 32, 32]] [1, 128, 32, 32] 147,584
BatchNorm2D-12 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
ReLU-11 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
ResidualBlock-5 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
Conv2D-13 [[1, 128, 32, 32]] [1, 128, 32, 32] 147,584
BatchNorm2D-13 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
ReLU-12 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
Conv2D-14 [[1, 128, 32, 32]] [1, 128, 32, 32] 147,584
BatchNorm2D-14 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
ReLU-13 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
ResidualBlock-6 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
Conv2D-15 [[1, 128, 32, 32]] [1, 128, 32, 32] 147,584
BatchNorm2D-15 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
ReLU-14 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
Conv2D-16 [[1, 128, 32, 32]] [1, 128, 32, 32] 147,584
BatchNorm2D-16 [[1, 128, 32, 32]] [1, 128, 32, 32] 512
ReLU-15 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
ResidualBlock-7 [[1, 128, 32, 32]] [1, 128, 32, 32] 0
Conv2D-18 [[1, 128, 32, 32]] [1, 256, 16, 16] 295,168
BatchNorm2D-18 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-16 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-19 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-19 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
Conv2D-17 [[1, 128, 32, 32]] [1, 256, 16, 16] 33,024
BatchNorm2D-17 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-17 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
ResidualBlock-8 [[1, 128, 32, 32]] [1, 256, 16, 16] 0
Conv2D-20 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-20 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-18 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-21 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-21 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-19 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
ResidualBlock-9 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-22 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-22 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-20 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-23 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-23 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-21 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
ResidualBlock-10 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-24 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-24 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-22 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-25 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-25 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-23 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
ResidualBlock-11 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-26 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-26 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-24 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-27 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-27 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-25 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
ResidualBlock-12 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-28 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-28 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-26 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-29 [[1, 256, 16, 16]] [1, 256, 16, 16] 590,080
BatchNorm2D-29 [[1, 256, 16, 16]] [1, 256, 16, 16] 1,024
ReLU-27 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
ResidualBlock-13 [[1, 256, 16, 16]] [1, 256, 16, 16] 0
Conv2D-31 [[1, 256, 16, 16]] [1, 512, 8, 8] 1,180,160
BatchNorm2D-31 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-28 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
Conv2D-32 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,359,808
BatchNorm2D-32 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
Conv2D-30 [[1, 256, 16, 16]] [1, 512, 8, 8] 131,584
BatchNorm2D-30 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-29 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
ResidualBlock-14 [[1, 256, 16, 16]] [1, 512, 8, 8] 0
Conv2D-33 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,359,808
BatchNorm2D-33 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-30 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
Conv2D-34 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,359,808
BatchNorm2D-34 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-31 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
ResidualBlock-15 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
Conv2D-35 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,359,808
BatchNorm2D-35 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-32 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
Conv2D-36 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,359,808
BatchNorm2D-36 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-33 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
ResidualBlock-16 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
AvgPool2D-1 [[1, 512, 8, 8]] [1, 512, 2, 2] 0
Linear-1 [[1, 2048]] [1, 2] 4,098
============================================================================
Total params: 21,314,306
Trainable params: 21,280,258
Non-trainable params: 34,048
----------------------------------------------------------------------------
Input size (MB): 0.75
Forward/backward pass size (MB): 125.77
Params size (MB): 81.31
Estimated Total Size (MB): 207.82
----------------------------------------------------------------------------
{'total_params': 21314306, 'trainable_params': 21280258}