人工智能实践Tensorflow2.0 第五章--4.八股法搭建VGGNet网络--北京大学慕课

第五章–卷积神经网络基础–八股法搭建VGGNet网络
本讲目标:
  介绍八股法搭建VGGNet网络的流程。参考视频。

八股法搭建VGGNet网络

  • 1.VGGNet网络介绍
    • 1.1-网络分析
    • 1.2-卷积层搭建
  • 2.六步法训练VGGNet网络
    • 2.1六步法回顾
    • 2.2完整代码
    • 2.3输出结果

1.VGGNet网络介绍

1.1-网络分析

  VGGNet诞生于2014年,当年ImageNet竞赛的亚军,Top5错误率减小到7.3%
 借鉴点:小卷积核减少参数的同时,提高识别准确率;网络结构规整,适合并行加速。(但是不能做的太深入,直到有了残差网络。)
 VGGNet16 和 VGGNet19 并没有本质上的区别,只是网络深度不同,前者 16 层(13 层卷积、3 层全连接),后者 19 层(16 层卷积、3 层全连接)。
  卷积就是特征提取器:CBAPB

Conv2D、 BatchNormalization、 Activation、 Pooling、 Dropout

 特征提取器(卷积层):
C1:
  C(核:64x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(None)
  D(None)
C2:
  C(核:64x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(max,核:2x2,步长:2)
  D(0.2)
C3:
  C(核:128x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(None)
  D(None)
C4:
  C(核:128x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(max,核:2x2,步长:2)
  D(0.2)
C5:
  C(核:256x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(None)
  D(None)
C6:
  C(核:256x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(None)
  D(None)
C7:
  C(核:256x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(max,核:2x2,步长:2)
  D(0.2)
C8:
  C(核:512x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(None)
  D(None)
C9:
  C(核:512x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(None)
  D(None)
C10:
  C(核:512x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(max,核:2x2,步长:2)
  D(0.2)
C11:
  C(核:512x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(None)
  D(None)
C12:
  C(核:512x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(None)
  D(None)
C13:
  C(核:512x3x3,步长:1,填充:same)
  B(Yes)
  A(relu)
  P(max,核:2x2,步长:2)
  D(0.2)

 分类器(全连接层):
D1:
  Dense(神经元:512,激活:relu,Dropout:0.2)
D2:
  Dense(神经元:512,激活:relu,Dropout:0.2)
D3:
  Dense(神经元: 10,激活:softmax)

1.2-卷积层搭建

人工智能实践Tensorflow2.0 第五章--4.八股法搭建VGGNet网络--北京大学慕课_第1张图片

  上图中,紫色的部分为卷积层(即特征提取器CBAPD),红色为全连接层(即分类器)。总体来看,VGGNet的结构是相当规整的,它继承了AlexNet中的Relu激活函数、Dropout操作等有效的方法,同时采用了单一尺寸的 3 * 3 小卷积核,形成了规整的 C(Convolution,卷积)、B(Batch normalization)、A(Activation,激活)、P(Pooling,池化)、D(Dropout)结构,这一典型结构在卷积神经网络中的应用是非常广的。

2.六步法训练VGGNet网络

2.1六步法回顾


import
train,test	
model=tf.keras.Sequantial()/ class
model.compile
model.fit
model.summary

2.2完整代码

import tensorflow as tf
import os
import numpy as np
from matplotlib import pyplot as plt
from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Dropout, Flatten, Dense
from tensorflow.keras import Model

np.set_printoptions(threshold=np.inf)

cifar10 = tf.keras.datasets.cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0


class VGG16(Model):
    def __init__(self):
        super(VGG16, self).__init__()
        self.c1 = Conv2D(filters=64, kernel_size=(3, 3), padding='same')  # 卷积层1
        self.b1 = BatchNormalization()  # BN层1
        self.a1 = Activation('relu')  # 激活层1
        self.c2 = Conv2D(filters=64, kernel_size=(3, 3), padding='same', )
        self.b2 = BatchNormalization()  # BN层1
        self.a2 = Activation('relu')  # 激活层1
        self.p1 = MaxPool2D(pool_size=(2, 2), strides=2, padding='same')
        self.d1 = Dropout(0.2)  # dropout层

        self.c3 = Conv2D(filters=128, kernel_size=(3, 3), padding='same')
        self.b3 = BatchNormalization()  # BN层1
        self.a3 = Activation('relu')  # 激活层1
        self.c4 = Conv2D(filters=128, kernel_size=(3, 3), padding='same')
        self.b4 = BatchNormalization()  # BN层1
        self.a4 = Activation('relu')  # 激活层1
        self.p2 = MaxPool2D(pool_size=(2, 2), strides=2, padding='same')
        self.d2 = Dropout(0.2)  # dropout层

        self.c5 = Conv2D(filters=256, kernel_size=(3, 3), padding='same')
        self.b5 = BatchNormalization()  # BN层1
        self.a5 = Activation('relu')  # 激活层1
        self.c6 = Conv2D(filters=256, kernel_size=(3, 3), padding='same')
        self.b6 = BatchNormalization()  # BN层1
        self.a6 = Activation('relu')  # 激活层1
        self.c7 = Conv2D(filters=256, kernel_size=(3, 3), padding='same')
        self.b7 = BatchNormalization()
        self.a7 = Activation('relu')
        self.p3 = MaxPool2D(pool_size=(2, 2), strides=2, padding='same')
        self.d3 = Dropout(0.2)

        self.c8 = Conv2D(filters=512, kernel_size=(3, 3), padding='same')
        self.b8 = BatchNormalization()  # BN层1
        self.a8 = Activation('relu')  # 激活层1
        self.c9 = Conv2D(filters=512, kernel_size=(3, 3), padding='same')
        self.b9 = BatchNormalization()  # BN层1
        self.a9 = Activation('relu')  # 激活层1
        self.c10 = Conv2D(filters=512, kernel_size=(3, 3), padding='same')
        self.b10 = BatchNormalization()
        self.a10 = Activation('relu')
        self.p4 = MaxPool2D(pool_size=(2, 2), strides=2, padding='same')
        self.d4 = Dropout(0.2)

        self.c11 = Conv2D(filters=512, kernel_size=(3, 3), padding='same')
        self.b11 = BatchNormalization()  # BN层1
        self.a11 = Activation('relu')  # 激活层1
        self.c12 = Conv2D(filters=512, kernel_size=(3, 3), padding='same')
        self.b12 = BatchNormalization()  # BN层1
        self.a12 = Activation('relu')  # 激活层1
        self.c13 = Conv2D(filters=512, kernel_size=(3, 3), padding='same')
        self.b13 = BatchNormalization()
        self.a13 = Activation('relu')
        self.p5 = MaxPool2D(pool_size=(2, 2), strides=2, padding='same')
        self.d5 = Dropout(0.2)

        self.flatten = Flatten()
        self.f1 = Dense(512, activation='relu')
        self.d6 = Dropout(0.2)
        self.f2 = Dense(512, activation='relu')
        self.d7 = Dropout(0.2)
        self.f3 = Dense(10, activation='softmax')

    def call(self, x):
        x = self.c1(x)
        x = self.b1(x)
        x = self.a1(x)
        x = self.c2(x)
        x = self.b2(x)
        x = self.a2(x)
        x = self.p1(x)
        x = self.d1(x)

        x = self.c3(x)
        x = self.b3(x)
        x = self.a3(x)
        x = self.c4(x)
        x = self.b4(x)
        x = self.a4(x)
        x = self.p2(x)
        x = self.d2(x)

        x = self.c5(x)
        x = self.b5(x)
        x = self.a5(x)
        x = self.c6(x)
        x = self.b6(x)
        x = self.a6(x)
        x = self.c7(x)
        x = self.b7(x)
        x = self.a7(x)
        x = self.p3(x)
        x = self.d3(x)

        x = self.c8(x)
        x = self.b8(x)
        x = self.a8(x)
        x = self.c9(x)
        x = self.b9(x)
        x = self.a9(x)
        x = self.c10(x)
        x = self.b10(x)
        x = self.a10(x)
        x = self.p4(x)
        x = self.d4(x)

        x = self.c11(x)
        x = self.b11(x)
        x = self.a11(x)
        x = self.c12(x)
        x = self.b12(x)
        x = self.a12(x)
        x = self.c13(x)
        x = self.b13(x)
        x = self.a13(x)
        x = self.p5(x)
        x = self.d5(x)

        x = self.flatten(x)
        x = self.f1(x)
        x = self.d6(x)
        x = self.f2(x)
        x = self.d7(x)
        y = self.f3(x)
        return y


model = VGG16()

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

checkpoint_save_path = "./checkpoint/VGG16.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')
    model.load_weights(checkpoint_save_path)

cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
                                                 save_weights_only=True,
                                                 save_best_only=True)

history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,
                    callbacks=[cp_callback])
model.summary()

# print(model.trainable_variables)
file = open('./weights.txt', 'w')
for v in model.trainable_variables:
    file.write(str(v.name) + '\n')
    file.write(str(v.shape) + '\n')
    file.write(str(v.numpy()) + '\n')
file.close()

###############################################    show   ###############################################

# 显示训练集和验证集的acc和loss曲线
acc = history.history['sparse_categorical_accuracy']
val_acc = history.history['val_sparse_categorical_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

plt.subplot(1, 2, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()

2.3输出结果

Epoch 1/5
This message will be only logged once.
   1/1563 [..............................] - ETA: 0s - loss: 3.3310 - sparse_categorical_accuracy: 0.0625WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0087s vs `on_train_batch_end` time: 0.0140s). Check your callbacks.
1563/1563 [==============================] - 43s 27ms/step - loss: 1.9394 - sparse_categorical_accuracy: 0.2222 - val_loss: 1.7778 - val_sparse_categorical_accuracy: 0.2746
Epoch 2/5
1563/1563 [==============================] - 34s 22ms/step - loss: 1.5630 - sparse_categorical_accuracy: 0.3810 - val_loss: 1.5641 - val_sparse_categorical_accuracy: 0.4195
Epoch 3/5
1563/1563 [==============================] - 34s 22ms/step - loss: 1.2803 - sparse_categorical_accuracy: 0.5446 - val_loss: 1.1285 - val_sparse_categorical_accuracy: 0.6119
Epoch 4/5
1563/1563 [==============================] - 35s 22ms/step - loss: 1.0500 - sparse_categorical_accuracy: 0.6436 - val_loss: 1.4434 - val_sparse_categorical_accuracy: 0.5569
Epoch 5/5
1563/1563 [==============================] - 34s 22ms/step - loss: 0.9020 - sparse_categorical_accuracy: 0.6990 - val_loss: 0.9089 - val_sparse_categorical_accuracy: 0.6919
Model: "vg_g16"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              multiple                  1792     
_________________________________________________________________
batch_normalization (BatchNo multiple                  256       
_________________________________________________________________
activation (Activation)      multiple                  0         
_________________________________________________________________
conv2d_1 (Conv2D)            multiple                  36928     
_________________________________________________________________
batch_normalization_1 (Batch multiple                  256       
_________________________________________________________________
activation_1 (Activation)    multiple                  0         
_________________________________________________________________
max_pooling2d (MaxPooling2D) multiple                  0         
_________________________________________________________________
dropout (Dropout)            multiple                  0         
_________________________________________________________________
conv2d_2 (Conv2D)            multiple                  73856     
_________________________________________________________________
batch_normalization_2 (Batch multiple                  512       
_________________________________________________________________
activation_2 (Activation)    multiple                  0         
_________________________________________________________________
conv2d_3 (Conv2D)            multiple                  147584   
_________________________________________________________________
batch_normalization_3 (Batch multiple                  512       
_________________________________________________________________
activation_3 (Activation)    multiple                  0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 multiple                  0         
_________________________________________________________________
dropout_1 (Dropout)          multiple                  0         
_________________________________________________________________
conv2d_4 (Conv2D)            multiple                  295168   
_________________________________________________________________
batch_normalization_4 (Batch multiple                  1024     
_________________________________________________________________
activation_4 (Activation)    multiple                  0         
_________________________________________________________________
conv2d_5 (Conv2D)            multiple                  590080   
_________________________________________________________________
batch_normalization_5 (Batch multiple                  1024     
_________________________________________________________________
activation_5 (Activation)    multiple                  0         
_________________________________________________________________
conv2d_6 (Conv2D)            multiple                  590080   
_________________________________________________________________
batch_normalization_6 (Batch multiple                  1024     
_________________________________________________________________
activation_6 (Activation)    multiple                  0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 multiple                  0         
_________________________________________________________________
dropout_2 (Dropout)          multiple                  0         
_________________________________________________________________
conv2d_7 (Conv2D)            multiple                  1180160   
_________________________________________________________________
batch_normalization_7 (Batch multiple                  2048     
_________________________________________________________________
activation_7 (Activation)    multiple                  0         
_________________________________________________________________
conv2d_8 (Conv2D)            multiple                  2359808   
_________________________________________________________________
batch_normalization_8 (Batch multiple                  2048     
_________________________________________________________________
activation_8 (Activation)    multiple                  0         
_________________________________________________________________
conv2d_9 (Conv2D)            multiple                  2359808   
_________________________________________________________________
batch_normalization_9 (Batch multiple                  2048     
_________________________________________________________________
activation_9 (Activation)    multiple                  0         
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 multiple                  0         
_________________________________________________________________
dropout_3 (Dropout)          multiple                  0         
_________________________________________________________________
conv2d_10 (Conv2D)           multiple                  2359808   
_________________________________________________________________
batch_normalization_10 (Batc multiple                  2048     
_________________________________________________________________
activation_10 (Activation)   multiple                  0         
_________________________________________________________________
conv2d_11 (Conv2D)           multiple                  2359808   
_________________________________________________________________
batch_normalization_11 (Batc multiple                  2048     
_________________________________________________________________
activation_11 (Activation)   multiple                  0         
_________________________________________________________________
conv2d_12 (Conv2D)           multiple                  2359808   
_________________________________________________________________
batch_normalization_12 (Batc multiple                  2048     
_________________________________________________________________
activation_12 (Activation)   multiple                  0         
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 multiple                  0         
_________________________________________________________________
dropout_4 (Dropout)          multiple                  0         
_________________________________________________________________
flatten (Flatten)            multiple                  0         
_________________________________________________________________
dense (Dense)                multiple                  262656   
_________________________________________________________________
dropout_5 (Dropout)          multiple                  0         
_________________________________________________________________
dense_1 (Dense)              multiple                  262656   
_________________________________________________________________
dropout_6 (Dropout)          multiple                  0         
_________________________________________________________________
dense_2 (Dense)              multiple                  5130     
=================================================================
Total params: 15,262,026
Trainable params: 15,253,578
Non-trainable params: 8,448
_________________________________________________________________

人工智能实践Tensorflow2.0 第五章--4.八股法搭建VGGNet网络--北京大学慕课_第2张图片

你可能感兴趣的:(Tensorflow学习笔记,tensorflow,卷积神经网络,VGGNet)