使用TensorFlow(Keras)搭建卷积神经网络VGG,实现手写数字识别。
时间: 2023-06-19 16:08:29 浏览: 119
好的,下面是使用TensorFlow和Keras搭建VGG卷积神经网络实现手写数字识别的步骤:
1. 导入必要的库和数据集
```python
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
# 加载mnist数据集
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
```
2. 对数据进行预处理
```python
# 将像素值缩放到0到1的范围内
train_images, test_images = train_images / 255.0, test_images / 255.0
# 将标签转换为one-hot向量
train_labels = tf.keras.utils.to_categorical(train_labels, 10)
test_labels = tf.keras.utils.to_categorical(test_labels, 10)
```
3. 定义VGG网络模型
```python
# 定义VGG网络模型
model = models.Sequential()
# 添加卷积层1和池化层1
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(28, 28, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加卷积层2和池化层2
model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加卷积层3和池化层3
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加卷积层4和池化层4
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加卷积层5和池化层5
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加全连接层和输出层
model.add(layers.Flatten())
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
# 打印模型结构
model.summary()
```
4. 编译模型
```python
# 编译模型
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
```
5. 训练模型
```python
# 训练模型
history = model.fit(train_images.reshape(-1, 28, 28, 1), train_labels, epochs=5, batch_size=64,
validation_data=(test_images.reshape(-1, 28, 28, 1), test_labels))
```
6. 评估模型
```python
# 评估模型
test_loss, test_acc = model.evaluate(test_images.reshape(-1, 28, 28, 1), test_labels)
print('Test accuracy:', test_acc)
```
完整代码如下:
```python
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
# 加载mnist数据集
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
# 将像素值缩放到0到1的范围内
train_images, test_images = train_images / 255.0, test_images / 255.0
# 将标签转换为one-hot向量
train_labels = tf.keras.utils.to_categorical(train_labels, 10)
test_labels = tf.keras.utils.to_categorical(test_labels, 10)
# 定义VGG网络模型
model = models.Sequential()
# 添加卷积层1和池化层1
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(28, 28, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加卷积层2和池化层2
model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加卷积层3和池化层3
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加卷积层4和池化层4
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加卷积层5和池化层5
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(layers.MaxPooling2D((2, 2), strides=(2, 2)))
# 添加全连接层和输出层
model.add(layers.Flatten())
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dense(4096, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
# 打印模型结构
model.summary()
# 编译模型
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# 训练模型
history = model.fit(train_images.reshape(-1, 28, 28, 1), train_labels, epochs=5, batch_size=64,
validation_data=(test_images.reshape(-1, 28, 28, 1), test_labels))
# 评估模型
test_loss, test_acc = model.evaluate(test_images.reshape(-1, 28, 28, 1), test_labels)
print('Test accuracy:', test_acc)
```
运行完后,输出模型准确率和损失函数。
阅读全文