经典卷积神经网络---VGG16详解
一.VGG概述
VGGNet是牛津大學(xué)視覺(jué)幾何組(Visual Geometry Group)提出的模型,該模型在2014ImageNet圖像分類(lèi)與定位挑戰(zhàn)賽 ILSVRC-2014中取得在分類(lèi)任務(wù)第二,定位任務(wù)第一的優(yōu)異成績(jī)。VGGNet突出的貢獻(xiàn)是證明了很小的卷積,通過(guò)增加網(wǎng)絡(luò)深度可以有效提高性能。VGG很好的繼承了Alexnet的衣缽?fù)瑫r(shí)擁有著鮮明的特點(diǎn)。即網(wǎng)絡(luò)層次較深。
VGGNet結(jié)構(gòu)
VGGNet模型有A-E五種結(jié)構(gòu)網(wǎng)絡(luò),深度分別為11,11,13,16,19。其中較為典型的網(wǎng)絡(luò)結(jié)構(gòu)主要有vgg16和vgg19,本篇文章主要講VGG16,并分享VGG16的Keras實(shí)現(xiàn)。其網(wǎng)絡(luò)結(jié)構(gòu)如下圖中D列(紅色方框):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? VGG16網(wǎng)絡(luò)結(jié)構(gòu)
vggnet對(duì)輸入圖像的默認(rèn)大小是224*224*3 (從表中input可以看出)。vgg16網(wǎng)絡(luò)結(jié)構(gòu)含有參數(shù)的網(wǎng)絡(luò)層一共有16層,即13個(gè)卷積層,5個(gè)池化層,3個(gè)全連接層,不包括激活層。
vgg16網(wǎng)絡(luò)結(jié)構(gòu)可以劃分為6個(gè)模塊層次加1個(gè)輸入模塊,分別如下
| ? ? ? ? ? ? ? ? ? ? ? ?模塊 | ? ? ? ? ? ?各模塊的涉及的層次 |
| ? ? ? ? ? ? ? ? 輸入模塊 | ? ? ? ? ? ??224*224*3 |
| ? ? ? ? ? ? ? ? 第一個(gè)模塊 | ? ? ? ? ? ? ? conv3-64 |
| ? ? ? ? ? ? ? conv3-64 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? ? ? 第二個(gè)模塊 | ? ? ? ? ? ? ? conv3-128 |
| ? ? ? ? ? ? ? conv3-128 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? ? ?第三個(gè)模塊 | ? ? ? ? ? ? ? conv3-256 |
| ? ? ? ? ? ? ? conv3-256 | |
| ? ? ? ? ? ? ? conv3-256 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? ? 第四個(gè)模塊 | ? ? ? ? ? ? ? conv3-512 |
| ? ? ? ? ? ? ? conv3-512 | |
| ? ? ? ? ? ? ? conv3-512 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? ?第五個(gè)模塊 | ? ? ? ? ? ? ? conv-512 |
| ? ? ? ? ? ? ? conv3-512 | |
| ? ? ? ? ? ? ? conv3-512 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? 第六個(gè)模塊(全連接層和輸出層) | ? ? ? ? ? ? ? FC-4096 (實(shí)際上前面需要加一個(gè)Flatten層) |
| ? ? ? ? ? ? ? FC-4096 | |
| ? ? ? ? ? ? ? FC-1000 (負(fù)責(zé)分類(lèi)) | |
| ? ? ? ? ? ? ? softmax(輸出層函數(shù)) |
二.vgg16實(shí)現(xiàn)MNIST分類(lèi)
(基于keras框架)
代碼實(shí)現(xiàn):
#從keras.model中導(dǎo)入model模塊,為函數(shù)api搭建網(wǎng)絡(luò)做準(zhǔn)備 from keras.models import Model from keras.layers import Flatten,Dense,Dropout,MaxPooling2D,Conv2D,BatchNormalization,Input,ZeroPadding2D,Concatenate from keras.layers.convolutional import AveragePooling2D from keras import regularizers #正則化 from keras.optimizers import RMSprop #優(yōu)化選擇器 from keras.layers import AveragePooling2D from keras.datasets import mnist from keras.utils import np_utils import matplotlib.pyplot as plt import numpy as np#數(shù)據(jù)處理 (X_train,Y_train),(X_test,Y_test)=mnist.load_data() X_test1=X_test Y_test1=Y_test X_train=X_train.reshape(-1,28,28,1).astype("float32")/255.0 X_test=X_test.reshape(-1,28,28,1).astype("float32")/255.0 Y_train=np_utils.to_categorical(Y_train,10) Y_test=np_utils.to_categorical(Y_test,10) print(X_train.shape) print(Y_train.shape) print(X_train.shape)def vgg16():x_input = Input((28, 28, 1)) # 輸入數(shù)據(jù)形狀28*28*1# Block 1x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(x_input)x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)# Block 2x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)# Block 3x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)# Block 4x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)# Block 5x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)#BLOCK 6x=Flatten()(x)x=Dense(256,activation="relu")(x)x=Dropout(0.5)(x)x = Dense(256, activation="relu")(x)x = Dropout(0.5)(x)#搭建最后一層,即輸出層x = Dense(10, activation="softmax")(x)# 調(diào)用MDOEL函數(shù),定義該網(wǎng)絡(luò)模型的輸入層為X_input,輸出層為x.即全連接層model = Model(inputs=x_input, outputs=x)# 查看網(wǎng)絡(luò)模型的摘要model.summary()return model model=vgg16() optimizer=RMSprop(lr=1e-4) model.compile(loss="binary_crossentropy",optimizer=optimizer,metrics=["accuracy"]) #訓(xùn)練加評(píng)估模型 n_epoch=4 batch_size=128 def run_model(): #訓(xùn)練模型training=model.fit(X_train,Y_train,batch_size=batch_size,epochs=n_epoch,validation_split=0.25,verbose=1)test=model.evaluate(X_train,Y_train,verbose=1)return training,test training,test=run_model() print("誤差:",test[0]) print("準(zhǔn)確率:",test[1])def show_train(training_history,train, validation):plt.plot(training.history[train],linestyle="-",color="b")plt.plot(training.history[validation] ,linestyle="--",color="r")plt.title("training history")plt.xlabel("epoch")plt.ylabel("accuracy")plt.legend(["training","validation"],loc="lower right")plt.show() show_train(training,"accuracy","val_accuracy")def show_train1(training_history,train, validation):plt.plot(training.history[train],linestyle="-",color="b")plt.plot(training.history[validation] ,linestyle="--",color="r")plt.title("training history")plt.xlabel("epoch")plt.ylabel("loss")plt.legend(["training","validation"],loc="upper right")plt.show() show_train1(training,"loss","val_loss")prediction=model.predict(X_test) def image_show(image):fig=plt.gcf() #獲取當(dāng)前圖像fig.set_size_inches(2,2) #改變圖像大小plt.imshow(image,cmap="binary") #顯示圖像plt.show() def result(i):image_show(X_test1[i])print("真實(shí)值:",Y_test1[i])print("預(yù)測(cè)值:",np.argmax(prediction[i])) result(0) result(1)?
總結(jié)
以上是生活随笔為你收集整理的经典卷积神经网络---VGG16详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 外螺纹对照表_紧固件螺纹直径与螺距对照表
- 下一篇: 聚焦和增强卷积神经网络