由于我的机器非常老旧,所以采用的是依靠cpu训练的模式搭建的。
- 硬件特性:该机型通常搭载Intel酷睿标压处理器、核显,无独立高性能GPU,训练时优先使用CPU模式,避免显存不足报错。
- 软件环境:安装Python 3.7~3.8版本,搭配TensorFlow 2.x稳定版,避免高版本框架带来的兼容性问题。
- 依赖库安装:执行命令安装所需工具包:
pip install tensorflow opencv-python numpy matplotlib 最后系统选取的是版本如下:pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/从国内镜像站中下载的,没有设置出现了网速不好的情况断开的异常。pip install tensorflow opencv-python numpy matplotlib
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting tensorflow
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8f/a2/6d7e6a738e302530586d484895de2cf3fc158ad9c73b4504a670b2956dd9/tensorflow-2.21.0-cp311-cp311-win_amd64.whl (350.8 MB)
---------------------------------------- 350.8/350.8 MB 292.4 kB/s eta 0:00:00
Collecting opencv-python
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/21/f0/9fa6e85cb10c8eb36a0222d27e50fe381b86ce49a55446bf39f491727564/opencv_python-5.0.0.93-cp37-abi3-win_amd64.whl (44.0 MB)
---------------------------------------- 44.0/44.0 MB 316.4 kB/s eta 0:00:00
Requirement already satisfied: numpy in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (2.4.6)
Collecting matplotlib
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bc/be/fa26ed085b41298f64a8f9b7592c671bbf1acc8b0df124c1c5de96b859f8/matplotlib-3.11.1-cp311-cp311-win_amd64.whl (9.3 MB)
---------------------------------------- 9.3/9.3 MB 322.6 kB/s eta 0:00:00
Collecting absl-py>=1.0.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/58/0a/a10b45aab35b175aded078a462dc8d0c698f5b13946e7cb0869097b78bb6/absl_py-2.5.0-py3-none-any.whl (137 kB)
---------------------------------------- 137.4/137.4 kB 61.7 kB/s eta 0:00:00
Collecting astunparse>=1.6.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2b/03/13dde6512ad7b4557eb792fbcf0c653af6076b81e5941d36ec61f7ce6028/astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Requirement already satisfied: flatbuffers>=25.9.23 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from tensorflow) (25.12.19)
Collecting gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/1d/33/f1c6a276de27b7d7339a34749cc33fa87f077f921969c47185d34a887ae2/gast-0.7.0-py3-none-any.whl (22 kB)
Collecting google_pasta>=0.1.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a3/de/c648ef6835192e6e2cc03f40b19eeda4382c49b5bafb43d88b931c4c74ac/google_pasta-0.2.0-py3-none-any.whl (57 kB)
---------------------------------------- 57.5/57.5 kB 30.2 kB/s eta 0:00:00
Collecting libclang>=13.0.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0b/2d/3f480b1e1d31eb3d6de5e3ef641954e5c67430d5ac93b7fa7e07589576c7/libclang-18.1.1-py2.py3-none-win_amd64.whl (26.4 MB)
---------------------------------------- 26.4/26.4 MB 299.4 kB/s eta 0:00:00
Collecting opt_einsum>=2.3.2
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/23/cd/066e86230ae37ed0be70aae89aabf03ca8d9f39c8aea0dec8029455b5540/opt_einsum-3.4.0-py3-none-any.whl (71 kB)
---------------------------------------- 71.9/71.9 kB 33.2 kB/s eta 0:00:00
Requirement already satisfied: packaging in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from tensorflow) (26.2)
Requirement already satisfied: protobuf<8.0.0,>=6.31.1 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from tensorflow) (7.35.1)
Requirement already satisfied: requests<3,>=2.21.0 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from tensorflow) (2.34.2)
Requirement already satisfied: setuptools in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from tensorflow) (83.0.0)
Requirement already satisfied: six>=1.12.0 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from tensorflow) (1.17.0)
Collecting termcolor>=1.1.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/33/d1/8bb87d21e9aeb323cc03034f5eaf2c8f69841e40e4853c2627edf8111ed3/termcolor-3.3.0-py3-none-any.whl (7.7 kB)
Requirement already satisfied: typing_extensions>=3.6.6 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from tensorflow) (4.16.0)
Collecting wrapt>=1.11.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4a/62/ecc969b13b141fef89b888c9760821cb01a86ac8fc953911592c8e1e1522/wrapt-2.3.0-cp311-cp311-win_amd64.whl (80 kB)
---------------------------------------- 80.9/80.9 kB 37.1 kB/s eta 0:00:00
Requirement already satisfied: grpcio<2.0,>=1.24.3 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from tensorflow) (1.83.0)
Collecting keras>=3.12.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8d/b3/c9b848bbdba18e765a8051917c0cc82585b64278ce87895f2e521a27438f/keras-3.15.1-py3-none-any.whl (2.4 MB)
---------------------------------------- 2.4/2.4 MB 262.2 kB/s eta 0:00:00
Collecting h5py<3.15.0,>=3.11.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/db/0c/6c3f879a0f8e891625817637fad902da6e764e36919ed091dc77529004ac/h5py-3.14.0-cp311-cp311-win_amd64.whl (2.9 MB)
---------------------------------------- 2.9/2.9 MB 347.9 kB/s eta 0:00:00
Collecting ml_dtypes<1.0.0,>=0.5.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b4/24/70bd59276883fdd91600ca20040b41efd4902a923283c4d6edcb1de128d2/ml_dtypes-0.5.4-cp311-cp311-win_amd64.whl (210 kB)
---------------------------------------- 210.7/210.7 kB 366.5 kB/s eta 0:00:00
Collecting contourpy>=1.0.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/98/4b/9bd370b004b5c9d8045c6c33cf65bae018b27aca550a3f657cdc99acdbd8/contourpy-1.3.3-cp311-cp311-win_amd64.whl (225 kB)
---------------------------------------- 225.2/225.2 kB 404.9 kB/s eta 0:00:00
Collecting cycler>=0.10
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e7/05/c19819d5e3d95294a6f5947fb9b9629efb316b96de511b418c53d245aae6/cycler-0.12.1-py3-none-any.whl (8.3 kB)
Collecting fonttools>=4.28.2
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/08/60/defa5e69641db890a63be281f41345f4c33b157824eaf0b9fad3e08b0dcb/fonttools-4.63.0-cp311-cp311-win_amd64.whl (2.4 MB)
---------------------------------------- 2.4/2.4 MB 300.3 kB/s eta 0:00:00
Collecting kiwisolver>=1.3.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/be/6c/28f17390b62b8f2f520e2915095b3c94d88681ecf0041e75389d9667f202/kiwisolver-1.5.0-cp311-cp311-win_amd64.whl (73 kB)
---------------------------------------- 73.5/73.5 kB 30.9 kB/s eta 0:00:00
Requirement already satisfied: pillow>=9 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from matplotlib) (12.3.0)
Collecting pyparsing>=3
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/10/bd/c038d7cc38edc1aa5bf91ab8068b63d4308c66c4c8bb3cbba7dfbc049f9c/pyparsing-3.3.2-py3-none-any.whl (122 kB)
---------------------------------------- 122.8/122.8 kB 57.2 kB/s eta 0:00:00
Requirement already satisfied: python-dateutil>=2.7 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from matplotlib) (2.9.0.post0)
Collecting wheel<1.0,>=0.23.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/87/1b/9e33c09813d65e248f7f773119148a612516a4bea93e9c6f545f78455b7c/wheel-0.47.0-py3-none-any.whl (32 kB)
Requirement already satisfied: rich in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from keras>=3.12.0->tensorflow) (15.0.0)
Collecting namex
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b2/bc/465daf1de06409cdd4532082806770ee0d8d7df434da79c76564d0f69741/namex-0.1.0-py3-none-any.whl (5.9 kB)
Collecting optree
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/22/64/3cc7b08cb1c0f1949895f9490217ca8db6ced7f3bf75c65a5bf31c07bf1e/optree-0.19.1-cp311-cp311-win_amd64.whl (337 kB)
---------------------------------------- 337.6/337.6 kB 261.9 kB/s eta 0:00:00
Requirement already satisfied: charset_normalizer<4,>=2 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from requests<3,>=2.21.0->tensorflow) (3.4.9)
Requirement already satisfied: idna<4,>=2.5 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from requests<3,>=2.21.0->tensorflow) (3.18)
Requirement already satisfied: urllib3<3,>=1.26 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from requests<3,>=2.21.0->tensorflow) (2.7.0)
Requirement already satisfied: certifi>=2023.5.7 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from requests<3,>=2.21.0->tensorflow) (2026.7.22)
Requirement already satisfied: markdown-it-py>=2.2.0 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from rich->keras>=3.12.0->tensorflow) (4.2.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from rich->keras>=3.12.0->tensorflow) (2.20.0)
Requirement already satisfied: mdurl~=0.1 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from markdown-it-py>=2.2.0->rich->keras>=3.12.0->tensorflow) (0.1.2)
Installing collected packages: namex, libclang, wrapt, wheel, termcolor, pyparsing, optree, opt_einsum, opencv-python, ml_dtypes, kiwisolver, h5py, google_pasta, gast, fonttools, cycler, contourpy, absl-py, matplotlib, astunparse, keras, tensorflow
Successfully installed absl-py-2.5.0 astunparse-1.6.3 contourpy-1.3.3 cycler-0.12.1 fonttools-4.63.0 gast-0.7.0 google_pasta-0.2.0 h5py-3.14.0 keras-3.15.1 kiwisolver-1.5.0 libclang-18.1.1 matplotlib-3.11.1 ml_dtypes-0.5.4 namex-0.1.0 opencv-python-5.0.0.93 opt_einsum-3.4.0 optree-0.19.1 pyparsing-3.3.2 tensorflow-2.21.0 termcolor-3.3.0 wheel-0.47.0 wrapt-2.3.0- 准备好工具后下载数据源:
- 数据集获取:下载Kaggle官方的Dogs vs. Cats数据集,从中选取猫狗图片各1000~1500张
- 数据清洗:剔除损坏、非猫狗类的异常图片,避免训练过程中断。
预处理操作:将所有图片统一resize为224×224尺寸,按8:2比例划分为训练集和验证集,完成数值归一化、图像随机翻转/亮度调节等增强操作,提升模型泛化能力。 - 在实现图片统一size的时候我们采用了:批处理
使用 OpenCV(推荐,稳定性高,适合 AI 训练)
OpenCV 在处理图像解码异常和大规模批量任务时比 Pillow 更健壮,且支持断点续传逻辑
pip install opencv-python tqdm
这个脚本实现了转换:
到这里就基本上实现了环境的搭建和数据源的基本准备条件。
接下来就是开始做数据分配和训练的工作。
预处理操作:将所有图片统一resize为224×224尺寸,按8:2比例划分为训练集和验证集,完成数值归一化、图像随机翻转/亮度调节等增强操作,提升模型泛化能力。
由于我的电脑比较老旧,所以我在数据源的处理上使用了1500张图的,设计原则。
那么用于训练就是1200张,用于验证就是300张这样的一个比例操作。
三、基于迁移学习训练基础模型
- 预训练模型选择:选用轻量型的MobileNet V2作为基础模型,冻结大部分预训练层,仅微调最后几层分类头,大幅减少训练耗时。
- 训练配置:设置批量大小为16,初始学习率设为0.0001,训练轮次控制在20~30轮,开启早停机制防止过拟合。
- 训练监控:训练过程中实时观察损失值和准确率变化,在E431的CPU环境下,该规模的数据集完整训练耗时约2~3小时。
- 在这里呢就使用了这个脚本进行的训练。
- 训练数据源是有格式要求的。
- 设置相关参数会得到:
四、转换生成catsvsdogs.tflite文件
- 模型导出:将训练完成的Keras格式模型(.h5或SavedModel格式)保存到本地。
- 格式转换:使用TensorFlow官方的
tf.lite.TFLiteConverter工具加载基础模型,可选择动态范围量化策略,进一步压缩模型体积、提升后续推理速度。 - 导出验证:转换完成后得到最终的catsvsdogs.tflite文件,可通过加载模型输入单张测试图片,验证分类输出结果是否正常。
- 把生成的.h5文件进行转换:
- 这个脚本完成的这个功能。最终我们得到了:
- 当我们训练出来了这个模型后,那么它的识别率有多么高呢?需要通过图片来进行验证。
- 我在网上找了相关的方案,比如colab 在线网站操作比对结果,出现各种问题,不稳定。然后准备使用tf原生的官网方法,应该是没有维护了导致不行。
- 最终使用了streamlit 方案搭建了本地的验证网站。
- 那么接下来就重点介绍这个方式。
- 考虑到 的性能和开发便捷性,推荐使用 Python + Streamlit。它无需编写 HTML/CSS/JS,仅需几十行 Python 代码即可生成交互式网页,且完美支持加载 TFLite 模型
- 安装相关环境:
- pip install streamlit pillow tensorflow
- 这个地方比较坑多。
- 使用如下脚本实现这个功能:
- 错误最多的就是:非字符串什么的错误。
- 运行:
D:\AIsource\archive\PetImages>streamlit run app.py
XXXXX Uvicorn server started on :::8501You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Network URL: http://192.168.106.191:8501Help agents write better Streamlit apps?
Install the official Streamlit skills by running streamlit skills in your terminal.WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
最终的效果:
到这里整个训练验证的工程就完成了。