tensorflow入门案例手写数字识别人工智能界的helloworld项目落地1

参考
https://tensorflow.google.cn/?hl=zh-cn
https://tensorflow.google.cn/tutorials/keras/classification?hl=zh-cn

项目资源
https://download.csdn.net/download/AnalogElectronic/89872174

文章目录

一、案例学习

1、导入测试和训练数据集,定义模型,编译模型,模型训练,模型评估

数据集市28*28单通道灰度图像

py 复制代码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
  loss='sparse_categorical_crossentropy',
  metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

2、模型经过训练后,您可以使用它对一些图像进行预测。附加一个 Softmax 层,将模型的线性输出 logits 转换成更容易理解的概率。

py 复制代码
probability_model = tf.keras.Sequential([
  model,
  tf.keras.layers.Softmax()
])

#保存模型
probability_model.save('./mnist.model.keras')

#测试前五张图片的测试概率
probability_model(x_test[:5])

预测结果是一个包含 10 个数字的数组。它们代表模型对 10 种不同服装中每种服装的"置信度"。您可以看到哪个标签的置信度值最大:

3、绘图显示某一张测试图片

复制代码
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(x_test[1])
plt.colorbar()
plt.grid(False)
plt.show()

4、Windows上画图工具手写数字图片保存到本地文件夹,像素也是28*28

5、使用模型预测手写图片

py 复制代码
from tensorflow.keras.preprocessing.image import img_to_array, load_img
img_path = './img/7.png'
img = load_img(img_path, target_size=(28, 28), color_mode='grayscale')
img = img_to_array(img) # 灰度化
img = img.reshape(1,28,28)
img = 255-img
predictions  = probability_model(img)
np.argmax(predictions[0])

测试多次屡试不爽都是对应数字

二、项目落地

使用python搭建一个web项目,项目中加载保存的模型,预测用户上传的图片,通过接口上传图片返回图片对应的数字

1、在IDEA工具Pycharm中运行如下代码

py 复制代码
from flask import Flask, request
from PIL import Image
import numpy as np
from werkzeug.utils import secure_filename
from tensorflow import keras

model = keras.models.load_model('C:/Users/DTW/AI/digitRecognize/mnist.model.keras')


app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = './path/to/the/uploads'  # 替换为你的上传文件夹路径

@app.route('/')
def hello_world():
    return '欢迎来到我的Python Web程序!'


@app.route('/upload', methods=['POST'])
def upload_image():
    if 'file' not in request.files:
        return 'No file part'
    file = request.files['file']
    if file.filename == '':
        return 'No selected file'
    if file:  # 这里可以加文件类型判断逻辑
        filename = secure_filename(file.filename)
        # file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        img = Image.open(file.stream)
        channels = len(img.getbands())
        print('图像通道数为', channels)
        img = img.convert("L")  # 转换为灰度
        # img = img.convert("RGB")  # 转换为3通道
        img_matrix = np.asarray(img)
        img_matrix = 255 - img_matrix
        img_matrix = img_matrix.reshape(1, 28, 28)
        predictions = model.predict(img_matrix)
        result = np.argmax(predictions[0])
        return 'File uploaded successfully filename='+filename + 'predict result='+str(result)

if __name__ == '__main__':
    app.run(debug=True)

2、运行日志如下

复制代码
2024-10-10 18:45:12.611150: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-10-10 18:45:13.735215: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-10-10 18:45:16.360099: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
 * Serving Flask app 'webtest01'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
2024-10-10 18:45:17.159241: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-10-10 18:45:18.305527: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-10-10 18:45:21.072051: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
 * Debugger is active!
 * Debugger PIN: 459-068-596
图像通道数为 4
127.0.0.1 - - [10/Oct/2024 18:45:21] "POST /upload HTTP/1.1" 200 -
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 57ms/step

3、使用postman调用接口


相关推荐
老胖闲聊3 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1183 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之3 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
DFminer4 小时前
【LLM】fast-api 流式生成测试
人工智能·机器人
lyaihao4 小时前
使用python实现奔跑的线条效果
python·绘图
郄堃Deep Traffic4 小时前
机器学习+城市规划第十四期:利用半参数地理加权回归来实现区域带宽不同的规划任务
人工智能·机器学习·回归·城市规划
ai大师5 小时前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4
GIS小天5 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
小小爬虾5 小时前
关于datetime获取时间的问题
python
阿部多瑞 ABU5 小时前
主流大语言模型安全性测试(三):阿拉伯语越狱提示词下的表现与分析
人工智能·安全·ai·语言模型·安全性测试