经典卷积神经⽹络算法(2):AlexNet
上⽂中实现了LeNet-5卷积⽹络,不可否认这是⼀个⼗分经典的⽹络结构,但是迫于种种复杂的现实场景限制,只能在⼀些领域应⽤,所以在接下来的10年间,卷积⽹络的锋芒⼀度被SVN算法覆盖,直到2012年AlexNet的提出。2012年Imagenet图像识别⼤赛中,Alext提出的AlexNet⽹络模型⼀鸣惊⼈,引爆了神经⽹络的应⽤热潮,并且赢得了2012届图像识别⼤赛的冠军,这也使得卷积神经⽹络真正意义上成为图像处理上的核⼼算法。AlexNet⽹络相⽐于之前的卷积⽹络模型有以下⼏点创新:
(1)使⽤relu作为激活函数。在上世纪末,神经⽹络⼤多还是使⽤sigmod或tanh作为激活函数,使⽤relu作为激活函数是AlexNet⽹络开创的先河。
(2)使⽤GPU并⾏运算。AlexNet使⽤两张GPU同时训练模型,⼤⼤加快了模型训练的速度。
(3)最⼤池化。在AlexNet出现以前,⼤多使⽤平均池化。
(4)dropout操作。随机杀死部分神经元,防⽌过拟合。
(5)数据增强。从原始图⽚中随机截取固定⼤⼩的⼦图,构建更⼤规模数据集。初一阅读题及答案
AlexNet⽹络结构如下图所⽰(注:图⽚来源于)。
接下来,我们使⽤TensorFlow实现AlexNet,数据集为cifar100。
In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import datats, layers, optimizers, Sequential ,metrics
加载数据并查看数据情况:
In [2]:
(x, y), (x_test, y_test) = datats.cifar100.load_data()
In [3]:
print(x.shape, y.shape)
print(x_test.shape, y_test.shape)
(50000, 32, 32, 3) (50000, 1)
(10000, 32, 32, 3) (10000, 1)
index =1
fig, axes = plt.subplots(4, 3, figsize=(8, 4), tight_layout=True)
for row in range(4):
for col in range(3):
axes[row, col].imshow(x[index])
axes[row, col].axis('off')
axes[row, col].t_title(y[index][0])
redisindex +=1
plt.show()
从上述y值shape可出,其元素也是是数组形式,进⾏squeeze 对元素转换为具体值,⽅便后续操作:
In [5]:
y = tf.squeeze(y, axis=1)
y_test = tf.squeeze(y_test, axis=1)
巧克力的拼音定义预处理函数:
In [6]:
def preprocess(x, y):
x = tf.cast(x, dtype=tf.float32) /255.# 将每个像素值映射到[0, 1]内
y = tf.cast(y, dtype=tf.float32)
return x, y
In [7]:
train_db = tf.data.Datat.from_tensor_slices((x, y))
train_db = train_db.shuffle(1000).map(preprocess).batch(64)
test_db = tf.data.Datat.from_tensor_slices((x_test, y_test))
test_db = test_db.shuffle(1000).map(preprocess).batch(64)
开始创建模型:
model = Sequential([
layers.Conv2D(96,(11,11),strides=(2,2),# input_shape=(in_shape[1],in_shape[2],in_shape[3]),
padding='same',activation='relu',kernel_initializer='uniform'),
丰溪里layers.MaxPooling2D(pool_size=(3,3),strides=(2,2)),
layers.Conv2D(256,(5,5),strides=(1,1),padding='same',activation='relu',kernel_initializer='uniform'),
白酒英语layers.MaxPooling2D(pool_size=(3,3),strides=(2,2)),
layers.Conv2D(384,(3,3),strides=(1,1),padding='same',activation='relu',kernel_initializer='uniform'),
layers.Conv2D(384,(3,3),strides=(1,1),padding='same',activation='relu',kernel_initializer='uniform'),
layers.Conv2D(256,(3,3),strides=(1,1),padding='same',activation='relu',kernel_initializer='uniform'),
layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
winter sweet
layers.Flatten(),
layers.Den(2048,activation='relu'),
layers.Dropout(0.5),
layers.Den(2048,activation='relu'),
layers.Dropout(0.5),
出国留学 流程
layers.Den(100,activation='softmax')
])
In [15]:
model.build(input_shape=[None, 32, 32, 3]) # 指定输⼊
检测⼀下输出shape 是否正常:
In [17]:
x = al([1, 32, 32, 3]) # 1是指输⼊⼀张图像,两个32是图像长宽,3是指3通道
out = model(x)
out.shape
Out[17]:
TensorShape([1, 100])
In [19]:
model.fit(train_db,epochs=5, validation_data=test_db) # 训练模型
Train for 782 steps, validate for 157 steps
Epoch 1/5
782/782 [==============================] - 301s 386ms/step - loss: 4.3788 - accuracy: 0.0253 - val_loss: 4.2012 - val _accuracy: 0.0426
Epoch 2/5
782/782 [==============================] - 301s 385ms/step - loss: 4.1368 - accuracy: 0.0516 - val_loss: 4.0282 - val _accuracy: 0.0688历年四级听力下载
Epoch 3/5
782/782 [==============================] - 301s 385ms/step - loss: 4.0186 - accuracy: 0.06
90 - val_loss: 3.9264 - val _accuracy: 0.0859
Epoch 4/5
emoji是什么意思
782/782 [==============================] - 301s 385ms/step - loss: 3.9193 - accuracy: 0.0870 - val_loss: 3.8942 - val _accuracy: 0.0957
Epoch 5/5
782/782 [==============================] - 301s 385ms/step - loss: 3.8103 - accuracy: 0.1084 - val_loss: 3.7646 - val _accuracy: 0.1143
Out[19]:
<tensorflow.python.keras.callbacks.History at 0x7f7a0c223710>
intermediate从上⾯训练过程输出看⼀看出,每⼀次迭代loss都在降低⽽准确率逐渐升⾼,经过5轮迭代之后准确率达到
11.43%。