用python实现 识别图片手写数字
时间: 2023-06-19 17:04:12 浏览: 101
python 实现识别图片上的数字
4星 · 用户满意度95%
要使用 Python 实现手写数字识别,最常用的方法是使用深度学习模型。以下是一个基于 TensorFlow 的示例代码,它使用卷积神经网络(Convolutional Neural Network, CNN)来实现手写数字识别:
首先,我们需要导入必要的库:
```python
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
import matplotlib.pyplot as plt
```
然后,我们需要加载 MNIST 数据集:
```python
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
```
现在,我们可以先预处理数据,将输入数据归一化为 0 到 1 之间的值:
```python
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
```
接下来,我们需要将标签进行 one-hot 编码:
```python
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
```
现在,我们可以构建 CNN 模型:
```python
model = keras.Sequential(
[
keras.Input(shape=(28, 28, 1)),
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"),
]
)
```
然后,我们可以编译模型并开始训练:
```python
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=128, epochs=15, validation_split=0.1)
```
最后,我们可以使用测试集来评估模型的性能:
```python
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
```
完整的代码如下:
```python
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
import matplotlib.pyplot as plt
# Load MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Preprocess data
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
# One-hot encode labels
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
# Build CNN model
model = keras.Sequential(
[
keras.Input(shape=(28, 28, 1)),
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"),
]
)
# Compile model
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
# Train model
model.fit(x_train, y_train, batch_size=128, epochs=15, validation_split=0.1)
# Evaluate model
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
```
这个模型可以达到 99% 的准确率,可以用来识别手写数字。
阅读全文