动手学深度学习第五章,LeNet、AlexNet、VGG

基础教程详见【第一篇】和 【第二篇

LeNet

首先导包:

import tensorflow as tf

print(tf.__version__)

输出:

2.1.0

GPU内存设置:

gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu,True)

模型定义:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters=6,kernel_size=5,activation='relu',input_shape=(28,28,1)))
model.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
model.add(tf.keras.layers.Conv2D(filters=16,kernel_size=5,activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(120,activation='relu'))
model.add(tf.keras.layers.Dense(84,activation='relu'))
model.add(tf.keras.layers.Dense(10,activation='softmax'))

我们输出每一层查看一下:

for layer in model.layers:
    print(layer.name)

输出:

conv2d_6
max_pooling2d_6
conv2d_7
max_pooling2d_7
flatten_3
dense_9
dense_10
dense_11

读取数据集:

fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()

将三维数据变成四维(图片数量,图片长,图片宽,图片channel数),其中由于fashion mnist channel数为1,所以手动加上一维

# 三维变4维
train_images = tf.reshape(train_images,(train_images.shape[0],train_images.shape[1],train_images.shape[2],1))
print(train_images.shape)
test_images = tf.reshape(test_images,(test_images.shape[0],test_images.shape[1],test_images.shape[2],1))
print(test_images.shape)

输出:

(60000, 28, 28, 1)
(10000, 28, 28, 1)

训练:

model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(train_images,train_labels,epochs=5,validation_split=0.2)

输出:

Train on 48000 samples, validate on 12000 samples
Epoch 1/5
48000/48000 [==============================] - 6s 132us/sample - loss: 0.7950 - accuracy: 0.7685 - val_loss: 0.5144 - val_accuracy: 0.8116

Epoch 2/5
48000/48000 [==============================] - 6s 130us/sample - loss: 0.4314 - accuracy: 0.8410 - val_loss: 0.4012 - val_accuracy: 0.8550

Epoch 3/5
48000/48000 [==============================] - 6s 129us/sample - loss: 0.3699 - accuracy: 0.8647 - val_loss: 0.3891 - val_accuracy: 0.8627

Epoch 4/5
48000/48000 [==============================] - 6s 126us/sample - loss: 0.3416 - accuracy: 0.8738 - val_loss: 0.3661 - val_accuracy: 0.8683

Epoch 5/5
48000/48000 [==============================] - 6s 124us/sample - loss: 0.3174 - accuracy: 0.8814 - val_loss: 0.3666 - val_accuracy: 0.8689


验证测试集:

model.evaluate(test_images, test_labels)
10000/10000 [==============================] - 1s 84us/sample - loss: 0.3856 - accuracy: 0.8667

AlexNet

首先导包:

import tensorflow as tf
print(tf.__version__)

输出:

2.1.0

GPU设置:

for gpu in tf.config.experimental.list_physical_devices('GPU'):
    tf.config.experimental.set_memory_growth(gpu, True)

模型:

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=96,kernel_size=11,strides=4,activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=3,strides=2))
model.add(tf.keras.layers.Conv2D(filters=256,kernel_size=5,padding='same',activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=3,strides=2))
model.add(tf.keras.layers.Conv2D(383,kernel_size=3,padding='same',activation='relu'))
model.add(tf.keras.layers.Conv2D(384,kernel_size=3,padding='same',activation='relu'))
model.add(tf.keras.layers.Conv2D(256,kernel_size=3,padding='same',activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=3,strides=2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(4096,activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(4096,activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10,activation='sigmoid'))

输出每一层的名称:

for layer in model.layers:
    print(layer.name)

输出如下:

conv2d
max_pooling2d
conv2d_1
max_pooling2d_1
conv2d_2
conv2d_3
conv2d_4
max_pooling2d_2
flatten
dense
dropout
dense_1
dropout_1
dense_2

这里不做进一步演示了,都差不多

VGG(block)

这里,我们第一次接触block,即n个重复模块的基本单位。

首先,老生常谈,不做赘述。

import tensorflow as tf
print(tf.__version__)
2.1.0
for gpu in tf.config.experimental.list_physical_devices('GPU'):
    tf.config.experimental.set_memory_growth(gpu, True)

然后我们定义block,也就是n个卷积之后连接一个池化层:

def vgg_block(num_convs, num_channels):
blk = tf.keras.models.Sequential()
for _ in range(num_convs):
blk.add(tf.keras.layers.Conv2D(num_channels,kernel_size=3,
padding='same',activation='relu'))

blk.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
return blk

然后我们定义网络,通过conv_arch来对整个网络进行定义:

conv_arch = ((1, 64), (1, 128), (2, 256), (2, 512), (2, 512))
# VGG-11
def vgg(conv_arch):
net = tf.keras.models.Sequential()
for (num_convs, num_channels) in conv_arch:
net.add(vgg_block(num_convs,num_channels))
net.add(tf.keras.models.Sequential([tf.keras.layers.Flatten(),
tf.keras.layers.Dense(4096,activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(4096,activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10,activation='sigmoid')]))
return net

net = vgg(conv_arch)

for blk in net.layers:
print(blk.name)

输出,我们可以看到有5个人block和最后的一个全连接,一共6个部分:

sequential_1
sequential_2
sequential_3
sequential_4
sequential_5
sequential_6

剩下的不赘述,详细代码移步github


0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注