通过CIFAR-10数据集完成AlexNet卷积神经网络分类器的搭建
时间: 2023-09-18 11:08:40 浏览: 112
用于CIFAR-10-图像分类的卷积神经网络
首先,我们需要导入必要的库和数据集:
```python
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
# 加载数据集
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 归一化
x_train, x_test = x_train / 255.0, x_test / 255.0
# 转换标签为独热编码
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)
```
接下来,我们可以开始搭建AlexNet模型。在这里,我们使用TensorFlow的Sequential模型。AlexNet共有8层,包括5个卷积层和3个全连接层。我们可以使用tf.keras.layers下的Conv2D、MaxPooling2D和Dense来构建模型:
```python
model = tf.keras.models.Sequential([
# 第一层卷积层
tf.keras.layers.Conv2D(filters=96, kernel_size=(11,11), strides=(4,4), activation='relu', input_shape=(32,32,3)),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
# 第二层卷积层
tf.keras.layers.Conv2D(filters=256, kernel_size=(5,5), strides=(1,1), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
# 第三层卷积层
tf.keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'),
# 第四层卷积层
tf.keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'),
# 第五层卷积层
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
# 全连接层
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='softmax')
])
```
在这里,我们使用ReLU激活函数,并在全连接层中使用Dropout以避免过拟合。
接下来,我们可以编译模型并开始训练:
```python
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=128, validation_data=(x_test, y_test))
```
最后,我们可以使用测试集来评估模型的准确率:
```python
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)
```
完整代码如下:
```python
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
# 加载数据集
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 归一化
x_train, x_test = x_train / 255.0, x_test / 255.0
# 转换标签为独热编码
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)
model = tf.keras.models.Sequential([
# 第一层卷积层
tf.keras.layers.Conv2D(filters=96, kernel_size=(11,11), strides=(4,4), activation='relu', input_shape=(32,32,3)),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
# 第二层卷积层
tf.keras.layers.Conv2D(filters=256, kernel_size=(5,5), strides=(1,1), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
# 第三层卷积层
tf.keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'),
# 第四层卷积层
tf.keras.layers.Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'),
# 第五层卷积层
tf.keras.layers.Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2)),
# 全连接层
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='softmax')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=128, validation_data=(x_test, y_test))
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)
```
阅读全文