PaddlePaddle 飞桨复现 VGG16

2023-06-25,,

import paddle.nn as nn
class VGG16(nn.Layer):
def __init__(self, num_classes=1000):
super(VGG16, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2D(3, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(64),
nn.ReLU(),
)
self.layer2 = nn.Sequential(
nn.Conv2D(64, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(64),
nn.ReLU(),
nn.MaxPool2D(kernel_size=2, stride=2)
) self.layer3 = nn.Sequential(
nn.Conv2D(64, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(128),
nn.ReLU(),
)
self.layer4 = nn.Sequential(
nn.Conv2D(128, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(128),
nn.ReLU(),
nn.MaxPool2D(kernel_size=2, stride=2)
) self.layer5 = nn.Sequential(
nn.Conv2D(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(256),
nn.ReLU(),
)
self.layer6 = nn.Sequential(
nn.Conv2D(256, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(256),
nn.ReLU(),
nn.MaxPool2D(kernel_size=2, stride=2)
)
self.layer7 = nn.Sequential(
nn.Conv2D(256, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(512),
nn.ReLU(),
)
self.layer8 = nn.Sequential(
nn.Conv2D(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(512),
nn.ReLU(),
nn.MaxPool2D(kernel_size=2, stride=2)
) self.layer9 = nn.Sequential(
nn.Conv2D(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(512),
nn.ReLU(),
)
self.layer10 = nn.Sequential(
nn.Conv2D(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(512),
nn.ReLU(),
nn.MaxPool2D(kernel_size=2, stride=2)
) self.layer11 = nn.Sequential(
nn.Conv2D(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(512),
nn.ReLU(),
)
self.layer12 = nn.Sequential(
nn.Conv2D(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(512),
nn.ReLU(),
)
self.layer13 = nn.Sequential(
nn.Conv2D(512, 512, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2D(512),
nn.ReLU(),
nn.MaxPool2D(kernel_size=2, stride=2)
) self.fc = nn.Sequential(
nn.Dropout(0.5),
nn.Linear(4*4*512, 4096),
nn.ReLU())
self.fc1 = nn.Sequential(
nn.Dropout(0.5),
nn.Linear(4096, 512),
nn.ReLU())
self.fc2= nn.Sequential(
nn.Linear(512, num_classes)) def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.layer5(out)
out = self.layer6(out)
out = self.layer7(out)
out = self.layer8(out)
out = self.layer9(out)
out = self.layer10(out)
out = self.layer11(out)
out = self.layer12(out)
out = self.layer13(out)
out = paddle.reshape(out, [out.shape[0],-1])
out = self.fc(out)
out = self.fc1(out)
out = self.fc2(out)
return out paddle.Model(VGG16(num_classes=2)).summary((-1,3,256,256))
W0505 00:38:12.705672 18379 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0505 00:38:12.711607 18379 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, 256, 256] 1,792
BatchNorm2D-1 [[1, 64, 256, 256]] [1, 64, 256, 256] 256
ReLU-1 [[1, 64, 256, 256]] [1, 64, 256, 256] 0
Conv2D-2 [[1, 64, 256, 256]] [1, 64, 256, 256] 36,928
BatchNorm2D-2 [[1, 64, 256, 256]] [1, 64, 256, 256] 256
ReLU-2 [[1, 64, 256, 256]] [1, 64, 256, 256] 0
MaxPool2D-1 [[1, 64, 256, 256]] [1, 64, 128, 128] 0
Conv2D-3 [[1, 64, 128, 128]] [1, 128, 128, 128] 73,856
BatchNorm2D-3 [[1, 128, 128, 128]] [1, 128, 128, 128] 512
ReLU-3 [[1, 128, 128, 128]] [1, 128, 128, 128] 0
Conv2D-4 [[1, 128, 128, 128]] [1, 128, 128, 128] 147,584
BatchNorm2D-4 [[1, 128, 128, 128]] [1, 128, 128, 128] 512
ReLU-4 [[1, 128, 128, 128]] [1, 128, 128, 128] 0
MaxPool2D-2 [[1, 128, 128, 128]] [1, 128, 64, 64] 0
Conv2D-5 [[1, 128, 64, 64]] [1, 256, 64, 64] 295,168
BatchNorm2D-5 [[1, 256, 64, 64]] [1, 256, 64, 64] 1,024
ReLU-5 [[1, 256, 64, 64]] [1, 256, 64, 64] 0
Conv2D-6 [[1, 256, 64, 64]] [1, 256, 64, 64] 590,080
BatchNorm2D-6 [[1, 256, 64, 64]] [1, 256, 64, 64] 1,024
ReLU-6 [[1, 256, 64, 64]] [1, 256, 64, 64] 0
MaxPool2D-3 [[1, 256, 64, 64]] [1, 256, 32, 32] 0
Conv2D-7 [[1, 256, 32, 32]] [1, 512, 32, 32] 1,180,160
BatchNorm2D-7 [[1, 512, 32, 32]] [1, 512, 32, 32] 2,048
ReLU-7 [[1, 512, 32, 32]] [1, 512, 32, 32] 0
Conv2D-8 [[1, 512, 32, 32]] [1, 512, 32, 32] 2,359,808
BatchNorm2D-8 [[1, 512, 32, 32]] [1, 512, 32, 32] 2,048
ReLU-8 [[1, 512, 32, 32]] [1, 512, 32, 32] 0
MaxPool2D-4 [[1, 512, 32, 32]] [1, 512, 16, 16] 0
Conv2D-9 [[1, 512, 16, 16]] [1, 512, 16, 16] 2,359,808
BatchNorm2D-9 [[1, 512, 16, 16]] [1, 512, 16, 16] 2,048
ReLU-9 [[1, 512, 16, 16]] [1, 512, 16, 16] 0
Conv2D-10 [[1, 512, 16, 16]] [1, 512, 16, 16] 2,359,808
BatchNorm2D-10 [[1, 512, 16, 16]] [1, 512, 16, 16] 2,048
ReLU-10 [[1, 512, 16, 16]] [1, 512, 16, 16] 0
MaxPool2D-5 [[1, 512, 16, 16]] [1, 512, 8, 8] 0
Conv2D-11 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,359,808
BatchNorm2D-11 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-11 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
Conv2D-12 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,359,808
BatchNorm2D-12 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-12 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
Conv2D-13 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,359,808
BatchNorm2D-13 [[1, 512, 8, 8]] [1, 512, 8, 8] 2,048
ReLU-13 [[1, 512, 8, 8]] [1, 512, 8, 8] 0
MaxPool2D-6 [[1, 512, 8, 8]] [1, 512, 4, 4] 0
Dropout-1 [[1, 8192]] [1, 8192] 0
Linear-1 [[1, 8192]] [1, 4096] 33,558,528
ReLU-14 [[1, 4096]] [1, 4096] 0
Dropout-2 [[1, 4096]] [1, 4096] 0
Linear-2 [[1, 4096]] [1, 512] 2,097,664
ReLU-15 [[1, 512]] [1, 512] 0
Linear-3 [[1, 512]] [1, 2] 1,026
===========================================================================
Total params: 52,159,554
Trainable params: 52,141,634
Non-trainable params: 17,920
---------------------------------------------------------------------------
Input size (MB): 0.75
Forward/backward pass size (MB): 383.73
Params size (MB): 198.97
Estimated Total Size (MB): 583.45
--------------------------------------------------------------------------- {'total_params': 52159554, 'trainable_params': 52141634}

PaddlePaddle 飞桨复现 VGG16的相关教程结束。

《PaddlePaddle 飞桨复现 VGG16.doc》

下载本文的Word格式文档,以方便收藏与打印。