如何在Python中将传感器原始特征转为Edge Impulse模型训练用处理特征?
实现Edge Impulse原始振动数据到模型输入特征的自动转换
你可以通过以下步骤在Python中自动完成原始传感器数据到模型所需特征的转换:
1. 从Edge Impulse导出预处理管道
在Edge Impulse Studio中,进入项目的Processing blocks页面,点击右上角的Export按钮,选择Python library格式下载。解压后会得到包含预处理逻辑的代码文件,其中ei_runner.py和parameters.json等参数文件会完整复现Studio中的特征提取流程。
2. 使用预处理代码转换原始数据
将解压后的文件放到你的Python项目目录中,用以下代码处理原始振动数据:
import numpy as np from ei_runner import EdgeImpulseRunner # 初始化runner,加载预处理参数 runner = EdgeImpulseRunner('parameters.json') runner.init() # 替换为你的真实原始振动数据(格式需与训练采集时一致,比如单轴数据为一维数组,三轴为[samples, 3]的二维数组) raw_vibration_data = np.random.randn(100) # 示例:100个采样点的单轴振动数据 # 运行预处理,生成模型所需特征 features, _ = runner.get_features(raw_vibration_data) runner.stop()
3. 整合到现有推理流程
将自动生成的features替换原手动填充的数组,完整推理代码如下:
import tensorflow as tf import numpy as np from ei_runner import EdgeImpulseRunner # 加载预处理管道 runner = EdgeImpulseRunner('parameters.json') runner.init() # 加载TFLite模型 interpreter = tf.lite.Interpreter(model_path="/content/ei-testing-nn-classifier-tensorflow-lite-float32-model.lite") input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() interpreter.allocate_tensors() # 处理原始数据得到特征 raw_vibration_data = np.random.randn(100) # 替换为真实传感器数据 features, _ = runner.get_features(raw_vibration_data) runner.stop() # 适配模型输入格式 np_features = np.array(features).astype(input_details[0]['dtype']) np_features = np.expand_dims(np_features, axis=0) # 执行推理 interpreter.set_tensor(input_details[0]['index'], np_features) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index']) print(output_data)
关键注意事项
- 原始数据的采样率、轴数必须与Edge Impulse训练时的采集参数完全一致,否则生成的特征会与模型预期不匹配。
- 若为多轴振动数据,需将数据整理成
[采样点数, 轴数]的二维数组传入get_features方法。
内容的提问来源于stack exchange,提问作者Valeed
相关产品推荐
相关产品推荐

