VGG继承了AlexNet的思路,建立了一个层次更多、深度更深的网络。结构同样是5个卷积层和3个全连接层,但是每个卷积层做2~4次卷积,卷积核的大小全是3*3的,使得参数变少了。
import tflearn
from tflearn.layers.core import input_data,dropout,fully_connected
from tflearn.layers.conv import conv_2d,max_pool_2d
from tflearn.layers.estimator import regression
#数据导入
import tflearn.datasets.oxflower17 as oxflower17
X,Y=oxflower17.load_data(one_hot=True)
#建立模型
#1
network=input_data(shape=[None,224,224,3])
network=conv_2d(network,64,3,activation='relu')
network=conv_2d(network,64,3,activation='relu')
network=max_pool_2d(network,2,strides=2)
#2
network=conv_2d(network,128,3,activation='relu')
network=conv_2d(network,128,3,activation='relu')
network=max_pool_2d(network,2,strides=2)
#3
network=conv_2d(network,256,3,activation='relu')
network=conv_2d(network,256,3,activation='relu')
network=conv_2d(network,256,3,activation='relu')
network=max_pool_2d(network,2,strides=2)
#4
network=conv_2d(network,512,3,activation='relu')
network=conv_2d(network,512,3,activation='relu')
network=conv_2d(network,512,3,activation='relu')
network=max_pool_2d(network,2,strides=2)
#5
network=conv_2d(network,512,3,activation='relu')
network=conv_2d(network,512,3,activation='relu')
network=conv_2d(network,512,3,activation='relu')
network=max_pool_2d(network,2,strides=2)
network=fully_connected(network,4096,activation='relu')
network=dropout(network,0.5)
network=fully_connected(network,4096,activation='relu')
network=dropout(network,0.5)
network=fully_connected(network,17,activation='softmax')
network=dropout(network,0.5)
#损失函数
network=regression(network,optimizer='rmsprop',loss='categorical_crossentropy',learning_rate=0.001)
model=tflearn.DNN(network,checkpoint_path='model_vgg',max_checkpoints=1,tensorboard_verbose=0)
model.fit(X,Y,n_epoch=500,shuffle=True,show_metric=True,
batch_size=32,snapshot_step=500,snapshot_epoch=False,run_id='vgg_oxflowers17')
论文全部使用了3*3的卷积核和2*2的池化核,通过不断加深网络结构来提升性能(有正则化的效果)
其中出现多个完全一样的3*3的卷积层堆叠在一起,2个3*3相当于1个5*5的卷积核,3个3*3相当于1个7*7的。串联拥有更少的参数
拥有更多的非线性变换,使得CNN对特征的学习能力更强