cifar10数据集分类 python 达到90%准确率
时间: 2023-09-02 19:11:59 浏览: 197
使用Python进行CIFAR-10图像分类的方法有很多,下面给出一个使用Keras和TensorFlow框架的示例代码,可以达到约90%的准确率:
```python
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import cifar10
# 加载CIFAR-10数据集
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 数据预处理
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
# 构建模型
model = keras.Sequential(
[
keras.Input(shape=(32, 32, 3)),
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
layers.MaxPooling2D(pool_size=(2, 2)),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(10, activation="softmax"),
]
)
# 编译模型
model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
# 训练模型
model.fit(x_train, y_train, batch_size=64, epochs=10, validation_split=0.1)
# 评估模型
_, acc = model.evaluate(x_test, y_test, verbose=0)
print("Test accuracy: {:.2f}%".format(acc * 100))
```
运行以上代码,可得到大约90%的准确率。你可以尝试调整模型结构和超参数来提高准确率。
阅读全文