当前位置: 首页 > news >正文

超值的网站建设哪里有html5网站建设

超值的网站建设,哪里有html5网站建设,合肥做网站的的公司有哪些,手机网站生成app软件一、概述 1、本文主要介绍如何用S32DS在NXP S32K344 中部署Tensorflow; 2、示例使用了Tensorflow入门代码,主要功能是识别28 * 28 的手写图片的数字; 3、在MCU上开启DSP功能后,最终运行时间在 7ms(64神经元&#xf…

一、概述

1、本文主要介绍如何用S32DS在NXP S32K344 中部署Tensorflow;

2、示例使用了Tensorflow入门代码,主要功能是识别28 * 28 的手写图片的数字;

3、在MCU上开启DSP功能后,最终运行时间在 7ms(64神经元),准确率在 90%左右;

4、Tensorflow Lite Micro为嵌入式环境运行的设计,参考以下链接:

开始使用 TensorFlow Lite

二、资源需求

1、库文件

库文件资源地址说明
CMSIS 6Release CMSIS 6.1.0 · ARM-software/CMSIS_6 · GitHubARM提供,会使用部分头文件
CMSIS-NNhttps://codeload.github.com/ARM-software/CMSIS-NN/zip/refs/heads/mainARM提供ARM Cortex-M 系列微控制器设计的神经网络库
CMSIS-DSPRTE_Components.h在micro_time.cpp中调用,可手动修改
https://github.com/ARM-software/CMSIS-DSP/tree/main
ARM提供,DSP运算库
tensorflow-lite-microhttps://codeload.github.com/tensorflow/tflite-micro/zip/refs/heads/mainGoogle tensorflowlite基础版,同宗永好,可以借点东西
tensorflow-lite-microhttps://www.keil.arm.com/packs/tensorflow-lite-micro-tensorflow/versions/

ARM提供的tensorflowlite ARM版,我们用这个,直接解压缩使用

Flatbuffershttps://github.com/google/flatbuffers/tree/master
Gemmlowp头文件低精度计算
https://github.com/google/gemmlowp/tree/master
Ruyinstrumentation.h 需要Ruy中提供,矩阵计算
https://github.com/google/ruy

2、软件工具

工具描述
S32DS3.4版本,GCC 10.2编译
Python3.7版本
VS Code + copilot可选,测试程序由AI帮忙生成,再手动修改
豆包遇到不会的问问他吧,比自己查资料快多了
Tensorflow2.x 机器学习模型构建
Netron可选,打开tflite文件
Trace32和 Lauterbach.rc可选,Python操作trace32,更新输入数据(原始数据28*28很大)

三、模型制作

1、机器学习模型

参考 关于TensorFlow | TensorFlow中文官网

import tensorflow as tf
import oscurr_path = os.path.dirname(__file__)
model_path = os.path.join(curr_path, 'model.tflite')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
first_index_data = x_train[0]# 128 = 12ms
# 64 = 7ms
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(input_shape=(28, 28)),tf.keras.layers.Dense(64, 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)# 计算需要的缓存大小,main.cc 会设置
total_memory = 0
for layer in model.layers:for tensor in layer.weights:shape = tensor.shapeelement_size = tensor.dtype.sizetensor_memory = 1for dim in shape:if dim is not None:tensor_memory *= dimtensor_memory *= element_sizetotal_memory += tensor_memory
print(f"Estimated memory usage: {total_memory} bytes")# 转换模型
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()# 保存转换后的模型
with open(model_path, 'wb') as f:f.write(tflite_model)

2、转换模型

缓存区大小和转换成TFlite 模型由AI生成。

converter = tf.lite.TFLiteConverter.from_keras_model(model)

tflite_model = converter.convert()

3、量化

生成模型数组,在嵌入式软件中调用。下列脚本保存到model.cc中。

第一种方法

Convert.py 实现,该脚本由豆包生成

import numpy as np
import tensorflow as tf
import oscurr_path = os.path.dirname(__file__)
model_path = os.path.join(curr_path, 'model.tflite')
output_path = os.path.join(curr_path, 'model.cc')def convert_tflite_to_c_array(tflite_model_path, output_c_file_path):# Load the TFLite modelwith open(tflite_model_path, 'rb') as f:tflite_model = f.read()# Convert the model to a numpy arraymodel_array = np.frombuffer(tflite_model, dtype=np.uint8)# Create the C array as a stringc_array_str = "const unsigned char model_data[] = {\n"c_array_str += ',\n'.join('    ' + ', '.join(f'0x{byte:02x}' for byte in model_array[i:i+12]) for i in range(0, len(model_array), 12))c_array_str += "\n};\n"c_array_str += f"const unsigned int model_len = {len(model_array)};\n"# Write the C array to the output filewith open(output_c_file_path, 'w') as f:f.write(c_array_str)# Example usage
convert_tflite_to_c_array(model_path, output_path)
print(f"Model converted to C array and saved to {output_path}")

第二种方法

许多微控制器平台没有本地文件系统的支持。从程序中使用一个模型最简单的方式是将其以一个 C 数组的形式包含并编译进你的程序。

xxd是个工具(linux/Cygwin/git等中包含)

四、嵌入式软件

1、库创建

使用tensorflow-lite-micro为基础,创建库工程(C++)。可直接使用编译号的库

已编译的库地址下载(使用以下内容,可跳过“库创建”)

【免费】S32DS编译的S32K3tensorflowlite库,o3优化,DSP开启资源-CSDN文库

A、需要的头都放进去,安装Tensorflow引用的头路径方式

使用DSP编译选项的设置,设置方式参考下一章;S32K3支持该协处理器。

B、可能的问题

库路径配置,记得是 C++

ethosu是平台的, AI推理,可以删除

schema_generated.h 屏蔽版本检查

///static_assert(FLATBUFFERS_VERSION_MAJOR == 23 &&

///              FLATBUFFERS_VERSION_MINOR == 5 &&

///              FLATBUFFERS_VERSION_REVISION == 26,

///             "Non-compatible flatbuffers version included");

blocking_counter.h error: 'condition_variable' in namespace 'std' does not name a type,直接屏蔽代码(这个是多线程系统时才有操作实体)

array.h 从tflite-micro-main拷到ARM下载的中,而不是#include "flatbuffers/array.h",

arm_nnfunctions.h  在CMSIS-NN中

instrumentation.h 需要Ruy中提供,矩阵计算

FixPonit 头文件 Gemmlowp 低精度计算

C、重定义问题解决

右击工程“Build configurations Explorer”,这几个文件在其他文件已经“include”了

D、库引用设置(在测试程序中配置)

路径设置:

2、测试程序

创建工程(C++)

Main.c 代码

/** main implementation: use this 'C++' sample to create your own application**/
#include "S32K344.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"// 包含模型数据
#include "model.cc"
#include "input_data.cc"volatile int predicted_class_index = 0;
float input_data[28 * 28];
volatile bool test_run = false;
const int tensor_arena_size = 40 * 1024;
uint8_t tensor_arena[tensor_arena_size];int main() 
{// 定义错误报告器tflite::MicroErrorReporter micro_error_reporter;tflite::ErrorReporter* error_reporter = &micro_error_reporter;// 定义操作解析器tflite::MicroMutableOpResolver<10> resolver;resolver.AddAdd();resolver.AddMul();resolver.AddSub();resolver.AddDiv();resolver.AddReshape();resolver.AddFullyConnected();resolver.AddSoftmax();resolver.AddRelu();// 定义模型const tflite::Model* tfmodel = tflite::GetModel(model_data);if (tfmodel == nullptr) {TF_LITE_REPORT_ERROR(error_reporter, "Failed to build tfmodel from buffer");while (1);}// 定义解释器tflite::MicroInterpreter interpreter = tflite::MicroInterpreter(tfmodel, resolver, tensor_arena, tensor_arena_size);// 分配张量TfLiteStatus allocate_status = interpreter.AllocateTensors();if (allocate_status != kTfLiteOk) {TF_LITE_REPORT_ERROR(error_reporter, "Tensor allocation failed");while (1);}// 初始化第一个输入张量for (int i = 0; i < 28 * 28; ++i) {input_data[i] = x_train[i];}// 准备输入数据TfLiteTensor* input_tensor = interpreter.input(0);for (;;) {// 填充输入数据for (int i = 0; i < 28 * 28; ++i) {input_tensor->data.f[i] = input_data[i];}// 运行推理TfLiteStatus invoke_status = interpreter.Invoke();if (invoke_status != kTfLiteOk) {TF_LITE_REPORT_ERROR(error_reporter, "Invoke failed");while (1);}// 获取输出结果TfLiteTensor* output_tensor = interpreter.output(0);// 处理输出数据:找到概率最大的类别索引int num_classes = output_tensor->bytes / sizeof(float);float max_prob = output_tensor->data.f[0];for (int i = 1; i < num_classes; ++i) {if (output_tensor->data.f[i] > max_prob) {max_prob = output_tensor->data.f[i];predicted_class_index = i;}}test_run = false;while(!test_run);}return 0;
}

解释器

MicroInterpreter为该示例的解释器,另外一个不使用

增加操作

resolver.AddRelu();根据Netron图中的操作或HelloAi中模型层确定

设置缓存区

uint8_t tensor_arena[tensor_arena_size];

tensor_arena_size根据Hellow.py  中的 total_memory

3、性能测试

输入 28 * 28 个数据,输出 10 个数据;该结果通过TRACE32读取。

隐藏层神经元个数48MHz

48MHz

O3优化

DSP使用

160MHz

O3优化

DSP使用

128560ms32ms12ms
64//7ms

使用DSP编译选项的设置

五、模型预测的正确率测试

1、测试脚本(先加载训练数据,再传给TRACE32)

import lauterbach.trace32.rcl as t32rc
import tensorflow as tf
import timeif __name__ == "__main__":mnist = tf.keras.datasets.mnist(x_train, y_train),(x_test, y_test) = mnist.load_data()x_train = x_train / 255.0t32debug = t32rc.connect(node = "localhost",port = 20000,packlen = 1024)right_cnt = 0calcu_cnt = 0# input data updatei = 0for x in x_train:calcu_cnt += 1input_datas = x.flatten()varDict = {}j = 0input_checksum = 0readback_checksum = 0for input_data in input_datas:signalName = 'input_data[' + str(j) + ']' # 构建信号名input_data = float(input_data)input_checksum += input_dataj += 1t32debug.variable.write(signalName, input_data)#time.sleep(0.001)readback_checksum += t32debug.variable.read(signalName).value#t32debug.go()# model predict in mircrocontroller, wait finsht32debug.variable.write('test_run', 1)while t32debug.variable.read('test_run').value != 0:time.sleep(0.1)#0.1s#t32debug.trace32_break()# read output dataout_signalname = 'predicted_class_index'predicted_class = t32debug.variable.read(out_signalname).valueif  predicted_class == y_train[i]:right_cnt += 1else:print("predict error!", 'input checksum: ', input_checksum, 'readback checksum', readback_checksum)print(right_cnt, '/', calcu_cnt, ' [predicted class]:', predicted_class, '[real class]:', y_train[i])i += 1print('Accuracy: ', right_cnt / calcu_cnt)print('Total number of test data: ', calcu_cnt)

2、加载训练数据

获取结果与实际进行对比。

3、TRACE32需要使能Port

4、TRACE32增加交互的数据

5、执行测试

运行可能不是太快

http://www.dtcms.com/a/531204.html

相关文章:

  • 在线包车网站建设长沙微网站电话号码
  • 广州手机网站建设公司哪家好开发什么软件有市场
  • 如何用h5做网站芜湖做公司网站
  • 指定网站怎么设置路由器只访问合肥建设工程交易网站
  • 国内网站设计制作长安镇做网站
  • 网站定制电话贴吧网站建设
  • 国防教育网站建设方案文昌市建设局网站
  • 南京网站制作报价微信小程序注册要钱吗
  • 免费建站工具wordpress安装linux
  • 什么是网站推广?线上网站建设需求
  • 流放之路做装备词缀网站seo排名优化推荐
  • nginx 做网站色彩搭配比较好的网站
  • 网站做跳转在后天那个文件里做网站icp备案费用
  • 东莞网站建没刚做的网站 搜不到
  • 即墨区建设局网站外国网站怎么进入
  • 企业网站备案注销太原做网站设计
  • 广州华优_网站建设公司0基础学网站开发
  • 中英文网站如何做思路如何建设淘宝客网站
  • 温州多语言网站建设仿站网站
  • 网站后台怎么上传图片产品店面设计费一般多少钱一平
  • 手机网站拒绝访问怎么解决东莞制作公司网站的公司
  • 淄博网站推广网站域名分类
  • 怎么自己创立网站移动健康app下载
  • 上海怎样建设网站seo顾问合同
  • 商城网站建设用乐云seo系统山东省服务外包网
  • html5门户网站模板云建站模板
  • 上海制作企业网站网站建设和推广话术6
  • cnzz网站代做网站建设课程中的收获
  • 怎样注册网站帐号申请哪个网站可以做破案h5
  • 仿门户网站多功能js相册画廊源码 支持缩略小图浏览网站怎么加留言