搭建VGG13网络,解决CIFAR10数据集上的图片分类,画出这两种网络模型在该数据集上的随epoch的精度变化曲线。 tensorflow代码实现
时间: 2024-03-09 15:49:02 浏览: 54
cnn.zip_CIFAR数据集_CNN_cNN分类_cifar_tensorflow cifar
好的,以下是搭建VGG13网络解决CIFAR10数据集图片分类的TensorFlow代码实现:
```python
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# 加载CIFAR10数据集
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
# 数据预处理
x_train = x_train / 255.0
x_test = x_test / 255.0
# 定义VGG13网络模型
model = tf.keras.Sequential([
Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(32, 32, 3)),
Conv2D(64, (3, 3), activation='relu', padding='same'),
MaxPooling2D((2, 2)),
Conv2D(128, (3, 3), activation='relu', padding='same'),
Conv2D(128, (3, 3), activation='relu', padding='same'),
MaxPooling2D((2, 2)),
Conv2D(256, (3, 3), activation='relu', padding='same'),
Conv2D(256, (3, 3), activation='relu', padding='same'),
Conv2D(256, (3, 3), activation='relu', padding='same'),
MaxPooling2D((2, 2)),
Conv2D(512, (3, 3), activation='relu', padding='same'),
Conv2D(512, (3, 3), activation='relu', padding='same'),
Conv2D(512, (3, 3), activation='relu', padding='same'),
MaxPooling2D((2, 2)),
Conv2D(512, (3, 3), activation='relu', padding='same'),
Conv2D(512, (3, 3), activation='relu', padding='same'),
Conv2D(512, (3, 3), activation='relu', padding='same'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(4096, activation='relu'),
Dense(4096, activation='relu'),
Dense(10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 训练模型
history = model.fit(x_train, y_train, epochs=50, validation_data=(x_test, y_test))
# 画出精度变化曲线
import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
plt.show()
```
以上是VGG13网络在CIFAR10数据集上的TensorFlow代码实现,其中包括数据预处理、模型定义、模型编译、模型训练和精度变化曲线的绘制。
阅读全文