谁来用低阶api写个vgg瞅瞅呗
收藏
谁来用低阶api写个vgg瞅瞅呗
看源码看不懂。
就洗个vgg19
https://aistudio.baidu.com/aistudio/projectdetail/1540993
0
收藏
请登录后评论
搞好了。
还是不行,醉了。。。
vgg就是直接码ConvBN
然后该pool就pool,没啥啊
不行用paddleclass套件里的vgg试试
import paddle
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
__all__ = ["VGG11", "VGG13", "VGG16", "VGG19"]
class ConvBlock(nn.Layer):
def __init__(self, input_channels, output_channels, groups, name=None):
super(ConvBlock, self).__init__()
self.groups = groups
self._conv_1 = Conv2D(
in_channels=input_channels,
out_channels=output_channels,
kernel_size=3,
stride=1,
padding=1,
weight_attr=ParamAttr(name=name + "1_weights"),
bias_attr=False)
if groups == 2 or groups == 3 or groups == 4:
self._conv_2 = Conv2D(
in_channels=output_channels,
out_channels=output_channels,
kernel_size=3,
stride=1,
padding=1,
weight_attr=ParamAttr(name=name + "2_weights"),
bias_attr=False)
if groups == 3 or groups == 4:
self._conv_3 = Conv2D(
in_channels=output_channels,
out_channels=output_channels,
kernel_size=3,
stride=1,
padding=1,
weight_attr=ParamAttr(name=name + "3_weights"),
bias_attr=False)
if groups == 4:
self._conv_4 = Conv2D(
in_channels=output_channels,
out_channels=output_channels,
kernel_size=3,
stride=1,
padding=1,
weight_attr=ParamAttr(name=name + "4_weights"),
bias_attr=False)
self._pool = MaxPool2D(kernel_size=2, stride=2, padding=0)
def forward(self, inputs):
x = self._conv_1(inputs)
x = F.relu(x)
if self.groups == 2 or self.groups == 3 or self.groups == 4:
x = self._conv_2(x)
x = F.relu(x)
if self.groups == 3 or self.groups == 4:
x = self._conv_3(x)
x = F.relu(x)
if self.groups == 4:
x = self._conv_4(x)
x = F.relu(x)
x = self._pool(x)
return x
class VGGNet(nn.Layer):
def __init__(self, layers=11, class_dim=1000):
super(VGGNet, self).__init__()
self.layers = layers
self.vgg_configure = {
11: [1, 1, 2, 2, 2],
13: [2, 2, 2, 2, 2],
16: [2, 2, 3, 3, 3],
19: [2, 2, 4, 4, 4]
}
assert self.layers in self.vgg_configure.keys(), \
"supported layers are {} but input layer is {}".format(
vgg_configure.keys(), layers)
self.groups = self.vgg_configure[self.layers]
self._conv_block_1 = ConvBlock(3, 64, self.groups[0], name="conv1_")
self._conv_block_2 = ConvBlock(64, 128, self.groups[1], name="conv2_")
self._conv_block_3 = ConvBlock(128, 256, self.groups[2], name="conv3_")
self._conv_block_4 = ConvBlock(256, 512, self.groups[3], name="conv4_")
self._conv_block_5 = ConvBlock(512, 512, self.groups[4], name="conv5_")
self._drop = Dropout(p=0.5, mode="downscale_in_infer")
self._fc1 = Linear(
7 * 7 * 512,
4096,
weight_attr=ParamAttr(name="fc6_weights"),
bias_attr=ParamAttr(name="fc6_offset"))
self._fc2 = Linear(
4096,
4096,
weight_attr=ParamAttr(name="fc7_weights"),
bias_attr=ParamAttr(name="fc7_offset"))
self._out = Linear(
4096,
class_dim,
weight_attr=ParamAttr(name="fc8_weights"),
bias_attr=ParamAttr(name="fc8_offset"))
def forward(self, inputs):
x = self._conv_block_1(inputs)
x = self._conv_block_2(x)
x = self._conv_block_3(x)
x = self._conv_block_4(x)
x = self._conv_block_5(x)
x = paddle.reshape(x, [0, -1])
x = self._fc1(x)
x = F.relu(x)
x = self._drop(x)
x = self._fc2(x)
x = F.relu(x)
x = self._drop(x)
x = self._out(x)
return x
def VGG11(**args):
model = VGGNet(layers=11, **args)
return model
def VGG13(**args):
model = VGGNet(layers=13, **args)
return model
def VGG16(**args):
model = VGGNet(layers=16, **args)
return model
def VGG19(**args):
model = VGGNet(layers=19, **args)
return model
社区升级了,直接粘代码不吃掉缩进了~~
就想按照它那个写。。。
---------------------------------------------------------------------------
Layer (type) Input Shape Output Shape Param #
===========================================================================
Conv2D-1 [[1, 3, 224, 224]] [1, 64, 224, 224] 1,792
Conv2D-2 [[1, 64, 224, 224]] [1, 64, 224, 224] 36,928
MaxPool2D-1 [[1, 64, 224, 224]] [1, 64, 112, 112] 0
ConvPool-1 [[1, 3, 224, 224]] [1, 64, 112, 112] 0
Conv2D-3 [[1, 64, 112, 112]] [1, 128, 112, 112] 73,856
Conv2D-4 [[1, 128, 112, 112]] [1, 128, 112, 112] 147,584
MaxPool2D-2 [[1, 128, 112, 112]] [1, 128, 56, 56] 0
ConvPool-2 [[1, 64, 112, 112]] [1, 128, 56, 56] 0
Conv2D-5 [[1, 128, 56, 56]] [1, 256, 56, 56] 295,168
Conv2D-6 [[1, 256, 56, 56]] [1, 256, 56, 56] 590,080
Conv2D-7 [[1, 256, 56, 56]] [1, 256, 56, 56] 590,080
MaxPool2D-3 [[1, 256, 56, 56]] [1, 256, 28, 28] 0
ConvPool-3 [[1, 128, 56, 56]] [1, 256, 28, 28] 0
Conv2D-8 [[1, 256, 28, 28]] [1, 512, 28, 28] 1,180,160
Conv2D-9 [[1, 512, 28, 28]] [1, 512, 28, 28] 2,359,808
Conv2D-10 [[1, 512, 28, 28]] [1, 512, 28, 28] 2,359,808
MaxPool2D-4 [[1, 512, 28, 28]] [1, 512, 14, 14] 0
ConvPool-4 [[1, 256, 28, 28]] [1, 512, 14, 14] 0
Conv2D-11 [[1, 512, 14, 14]] [1, 512, 14, 14] 2,359,808
Conv2D-12 [[1, 512, 14, 14]] [1, 512, 14, 14] 2,359,808
Conv2D-13 [[1, 512, 14, 14]] [1, 512, 14, 14] 2,359,808
MaxPool2D-5 [[1, 512, 14, 14]] [1, 512, 7, 7] 0
ConvPool-5 [[1, 512, 14, 14]] [1, 512, 7, 7] 0
Linear-1 [[1, 25088]] [1, 4096] 102,764,544
Linear-2 [[1, 4096]] [1, 4096] 16,781,312
Linear-3 [[1, 4096]] [1, 5] 20,485
===========================================================================
Total params: 134,281,029
Trainable params: 134,281,029
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 126.77
Params size (MB): 512.24
Estimated Total Size (MB): 639.59
---------------------------------------------------------------------------
{'total_params': 134281029, 'trainable_params': 134281029}
Epoch 9/10
step 48/48 [==============================] - loss: 1.1617 - acc_top1: 0.7626 - acc_top2: 0.8937 - 849ms/step
save checkpoint at /home/aistudio/chk_points/8
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 7/7 [==============================] - loss: 1.1522 - acc_top1: 0.7727 - acc_top2: 0.8955 - 624ms/step
Eval samples: 440
Epoch 10/10
step 48/48 [==============================] - loss: 1.1092 - acc_top1: 0.7750 - acc_top2: 0.9003 - 846ms/step
save checkpoint at /home/aistudio/chk_points/9
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 7/7 [==============================] - loss: 1.1537 - acc_top1: 0.7864 - acc_top2: 0.9045 - 617ms/step
Eval samples: 440
save checkpoint at /home/aistudio/chk_points/final
1
样本ID:2, 真实标签:river, 预测值:river
2
样本ID:38, 真实标签:lawn, 预测值:river
0
样本ID:56, 真实标签:ice, 预测值:ice
2
样本ID:92, 真实标签:lawn, 预测值:lawn
0
样本ID:100, 真实标签:ice, 预测值:river
1
样本ID:303, 真实标签:river, 预测值:river
这是直接用paddleclas训的吧,效果不错啊
vgg19
手写的
打印风格太paddleclas了~
反正看图识字描完了。
没有现成得吗?
现成的没有现做的香
哈哈 那倒是
根据他那个 改改