【机器学习】TensorFlowLite安装和模型训练

运行环境

Linux,部分库不支持Apple芯片

做AI这部分的开发,还是强烈建议装个Linux双系统或虚拟机

这些比折腾Windows和Mac上的移植环境要轻松得多

安装依赖
sudo apt install libportaudio2=19.6.0-1.2
pip3 install tf-models-official==2.3.0
pip3 install tensorflow-hub==0.12
pip3 install numpy==1.23.5
pip3 install pillow==10.1.0
pip3 install sentencepiece==0.1.99
pip3 install tensorflow-datasets==2.1.0
pip3 install fire==0.3.1
pip3 install flatbuffers==23.5.26
pip3 install absl-py==1.4.0
pip3 install urllib3==2.1.0
pip3 install tflite-support==0.4.2
pip3 install tensorflowjs==3.18.0
pip3 install tensorflow==2.15.0
pip3 install numba==0.58.1
pip3 install librosa==0.8.1
pip3 install lxml==4.6.1
pip3 install PyYAML==6.0.1
pip3 install matplotlib==3.4.0
pip3 install six==1.16.0
pip3 install tensorflow-addons==0.23.0
pip3 install neural-structured-learning==1.3.1
pip3 install tensorflow-model-optimization==0.7.5
pip3 install Cython==0.29.13
pip3 install protobuf==3.20.3
pip3 install tensorflow==2.8.4
pip3 install scann==1.2.6
pip3 install tflite-model-maker==0.4.2
准备训练图片
图片存放格式如下
--ModelFolder
----ClassFolder01
------Image01
------Image02
------Image03
----ClassFolder02
------Image01
------Image02
------Image03
----ClassFolder03
------Image01
------Image02
------Image03
TensorFlowLite对训练图片的格式要求非常严格,不仅仅是后缀名正确可以
测试图片和参考文档

https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz

https://www.tensorflow.org/lite/models/modify/model_maker/image_classification

https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/zh-cn/lite/models/modify/model_maker/image_classification.ipynb

模型训练与导出

	import os
	import numpy
	import tensorflow as tf
	import matplotlib.pyplot as plot
	from tflite_model_maker import model_spec as ModelSpec
	from tflite_model_maker import image_classifier as ImageClassifier
	from tflite_model_maker.config import ExportFormat
	from tflite_model_maker.config import QuantizationConfig
	from tflite_model_maker.image_classifier import DataLoader
	from keras.layers import normalization
	
	print("Model Train Started")
	data = DataLoader.from_folder("/home/dev/flower_photos")
	trainData, testData = data.split(0.9)
	model = ImageClassifier.create(trainData)
	loss, accuracy = model.evaluate(testData)
	model.export("/home/dev/flower_photos")
	print("Model Exported")

你可能感兴趣的:(Python,机器学习,人工智能,TensorFlow,TFLite,模型训练)