经典卷积神经网络---VGG16详解
生活随笔
收集整理的這篇文章主要介紹了
经典卷积神经网络---VGG16详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一.VGG概述
VGGNet是牛津大學視覺幾何組(Visual Geometry Group)提出的模型,該模型在2014ImageNet圖像分類與定位挑戰(zhàn)賽 ILSVRC-2014中取得在分類任務第二,定位任務第一的優(yōu)異成績。VGGNet突出的貢獻是證明了很小的卷積,通過增加網絡深度可以有效提高性能。VGG很好的繼承了Alexnet的衣缽同時擁有著鮮明的特點。即網絡層次較深。
VGGNet結構
VGGNet模型有A-E五種結構網絡,深度分別為11,11,13,16,19。其中較為典型的網絡結構主要有vgg16和vgg19,本篇文章主要講VGG16,并分享VGG16的Keras實現(xiàn)。其網絡結構如下圖中D列(紅色方框):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? VGG16網絡結構
vggnet對輸入圖像的默認大小是224*224*3 (從表中input可以看出)。vgg16網絡結構含有參數(shù)的網絡層一共有16層,即13個卷積層,5個池化層,3個全連接層,不包括激活層。
vgg16網絡結構可以劃分為6個模塊層次加1個輸入模塊,分別如下
| ? ? ? ? ? ? ? ? ? ? ? ?模塊 | ? ? ? ? ? ?各模塊的涉及的層次 |
| ? ? ? ? ? ? ? ? 輸入模塊 | ? ? ? ? ? ??224*224*3 |
| ? ? ? ? ? ? ? ? 第一個模塊 | ? ? ? ? ? ? ? conv3-64 |
| ? ? ? ? ? ? ? conv3-64 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? ? ? 第二個模塊 | ? ? ? ? ? ? ? conv3-128 |
| ? ? ? ? ? ? ? conv3-128 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? ? ?第三個模塊 | ? ? ? ? ? ? ? conv3-256 |
| ? ? ? ? ? ? ? conv3-256 | |
| ? ? ? ? ? ? ? conv3-256 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? ? 第四個模塊 | ? ? ? ? ? ? ? conv3-512 |
| ? ? ? ? ? ? ? conv3-512 | |
| ? ? ? ? ? ? ? conv3-512 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? ?第五個模塊 | ? ? ? ? ? ? ? conv-512 |
| ? ? ? ? ? ? ? conv3-512 | |
| ? ? ? ? ? ? ? conv3-512 | |
| ? ? ? ? ? ? ? maxpool | |
| ? ? ? ? ? ? 第六個模塊(全連接層和輸出層) | ? ? ? ? ? ? ? FC-4096 (實際上前面需要加一個Flatten層) |
| ? ? ? ? ? ? ? FC-4096 | |
| ? ? ? ? ? ? ? FC-1000 (負責分類) | |
| ? ? ? ? ? ? ? softmax(輸出層函數(shù)) |
二.vgg16實現(xiàn)MNIST分類
(基于keras框架)
代碼實現(xiàn):
#從keras.model中導入model模塊,為函數(shù)api搭建網絡做準備 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)# 調用MDOEL函數(shù),定義該網絡模型的輸入層為X_input,輸出層為x.即全連接層model = Model(inputs=x_input, outputs=x)# 查看網絡模型的摘要model.summary()return model model=vgg16() optimizer=RMSprop(lr=1e-4) model.compile(loss="binary_crossentropy",optimizer=optimizer,metrics=["accuracy"]) #訓練加評估模型 n_epoch=4 batch_size=128 def run_model(): #訓練模型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("準確率:",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() #獲取當前圖像fig.set_size_inches(2,2) #改變圖像大小plt.imshow(image,cmap="binary") #顯示圖像plt.show() def result(i):image_show(X_test1[i])print("真實值:",Y_test1[i])print("預測值:",np.argmax(prediction[i])) result(0) result(1)?
總結
以上是生活随笔為你收集整理的经典卷积神经网络---VGG16详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 外螺纹对照表_紧固件螺纹直径与螺距对照表
- 下一篇: vue php跨域,Vue+php处理跨