工业物联网下基于元学习的边缘设备少样本故障诊断系统。
data_layer:CWRU / HST 数据读取、FFT / STFT / WT 预处理、few-shot 数据构造model_layer:MAML、ProtoNet、CNN的训练与评估deploy_layer:结构化剪枝、恢复微调、ONNX 导出、INT8 PTQ、推理后端选择test_layer:benchmark、实验矩阵、结果聚合、论文表格导出edge_layer:边缘侧信号模拟、轻量预处理、MQTT 上传system_layer:FastAPI、MQTT、WebSocket、历史/报警存储与 Vue + ECharts 控制台
统一训练入口:
python train.py --mode {train|deploy} --algorithm {maml|protonet|cnn} ...完整训练环境:
conda create -n fault_env python=3.9 -y
conda activate fault_env
pip install -r requirements.txt后两层最小联调环境:
conda create -n edge_system python=3.9 -y
conda activate edge_system
pip install -r requirements.edge-system.txt默认数据目录:./data
data/
`-- CWRU_12k/
|-- Drive_end_0/
|-- Drive_end_1/
|-- Drive_end_2/
`-- Drive_end_3/
data/
`-- HST/
|-- 0/
|-- 1/
`-- 2/
Dataset = CWRUPreprocess = STFTTask = 5-way 5-shotTrain domains = 0,1,2Test domain = 3Runtime backend = onnxruntimeStructured pruning = 0.4INT8 = PTQ
比较:
CNNMAMLProtoNet
输出:
- 准确率
固定主模型:
MAML
比较:
5-shot10-shot15-shot
输出:
- 准确率
固定主模型:
MAML
比较:
OriginalPrunedPruned + INT8
输出:
- 准确率
- 平均推理时延
- 参数量
- 模型大小
系统链路固定为:
Edge Simulator -> MQTT -> FastAPI -> ONNX -> WebSocket -> Frontend
输出:
preprocess_latency_msinference_latency_msend_to_end_latency_ms
当前系统支持最小可用的运行时原型更新:
- 接口:
POST /adapt - 适用对象:
ProtoNet类型的 prototype deployment
它用于展示“少量 support sample 的运行时原型更新”,不是完整在线再训练系统。
python train.py --mode train --algorithm cnn \
--runtime_backend onnxruntime \
--dataset CWRU \
--preprocess STFT \
--ways 5 \
--shots 5 \
--query_shots 5 \
--train_domains 0,1,2 \
--test_domain 3 \
--epochs 60 \
--enable_compression true \
--prune_ratio 0.4python train.py --mode train --algorithm maml \
--runtime_backend onnxruntime \
--dataset CWRU \
--preprocess STFT \
--ways 5 \
--shots 5 \
--train_domains 0,1,2 \
--test_domain 3 \
--iters 200 \
--enable_compression true \
--prune_ratio 0.4python train.py --mode train --algorithm protonet \
--runtime_backend onnxruntime \
--dataset CWRU \
--preprocess STFT \
--ways 5 \
--shots 5 \
--query_shots 5 \
--train_domains 0,1,2 \
--test_domain 3 \
--iters 200 \
--enable_compression true \
--prune_ratio 0.4每次训练并开启压缩后,会输出到:
deploy_artifacts/<experiment_title>/
主要文件:
compression_summary.json*_baseline_float.onnx*_float.onnx*_int8.onnxProtoNet额外输出*_prototypes.npz
其中 compression_summary.json 现在会统一记录:
- 训练实验描述
- 部署后端与部署类型
- 原始模型、剪枝模型、INT8 模型的精度与时延
- 参数量与压缩比例
- 各部署产物的文件大小
核心文件:
约束边界:
run_controlled_overnight.sh负责执行原本会在终端里手动输入的训练与部署命令test_layer中的 Python 文件只负责读取运行后的logs/、deploy_artifacts/、checkpoints/并做分析导出
bash test_layer/run_controlled_overnight.sh restart这个入口会顺序执行:
CNN / MAML / ProtoNetFFT / STFT / WT5-shot / 10-shot / 15-shot- 自动训练、剪枝恢复、ONNX 导出、INT8 PTQ、benchmark 聚合
- 自动导出
logs/thesis_tables/controlled/下的表格文件
完整矩阵共 27 组:
FFT + CNN/MAML/ProtoNet + 5/10/15-shotSTFT + CNN/MAML/ProtoNet + 5/10/15-shotWT + CNN/MAML/ProtoNet + 5/10/15-shot
受控统一调度:
FFT:MAML / ProtoNet iters = 400STFT:MAML / ProtoNet iters = 80WT:MAML / ProtoNet iters = 80CNN epochs:FFT = 40,STFT = 30,WT = 30test_task_num = 50compression_finetune_iters = 80
日志按预处理和算法分类保存:
logs/overnight_runs/controlled/latest/logs/<preprocess>/<algorithm>/
如需先查看脚本里会顺序执行的全部终端命令:
run_controlled_overnight.sh 现只支持 clean | run | restart。
论文增强实验入口:
bash test_layer/run_seed_extension.sh restart
bash test_layer/run_domain_extension.sh restart
bash test_layer/run_compression_ablation.sh restart
bash test_layer/run_paper_suite.sh restartpython test_layer/benchmark.py \
--summary_path deploy_artifacts/<experiment_title>/compression_summary.jsonpython test_layer/benchmark.py \
--summary_path deploy_artifacts/<experiment_title>/compression_summary.json \
--rows_only \
--output_format csv \
--output_path logs/<experiment_title>_benchmark_row.csvpython test_layer/result_aggregator.py \
--summary_glob "deploy_artifacts/*/compression_summary.json" \
--output_format csv \
--output_path logs/thesis_benchmark_rows.csvpython test_layer/thesis_tables.py \
--summary_glob "deploy_artifacts/*/compression_summary.json" \
--output_dir logs/thesis_tables它会输出:
table1_model_performance.csvtable2_few_shot.csvtable3_compression.csvthesis_tables.jsonthesis_tables.md
其中 thesis_tables.md 是论文草稿和答辩整理最方便看的版本。
python test_layer/paper_tables.py \
--output_dir logs/thesis_tables/paper_balanced先启动系统并让它跑过一轮 MQTT 联调,再执行:
python test_layer/thesis_tables.py \
--summary_glob "deploy_artifacts/*/compression_summary.json" \
--system_stats_url http://127.0.0.1:8000/system/stats \
--system_channel mqtt \
--output_dir logs/thesis_tables这会额外生成:
table4_system_performance.csv
来源:
checkpoints/*_history.json
用于:
- 算法精度对比
- 学习曲线
- 最佳 checkpoint 选择
来源:
deploy_artifacts/*/compression_summary.json
用于:
- 原始 / 剪枝 / INT8 对比
- 压缩前后参数量对比
- 模型大小与平均推理时延分析
来源:
GET /system/statsGET /historyGET /alertsWS /ws/realtime
关键字段:
preprocess_latency_msinference_latency_msend_to_end_latency_ms
语义:
inference_latency_ms:部署层纯推理时延preprocess_latency_ms:系统层在线预处理时延end_to_end_latency_ms:系统层在线端到端时延latency_ms:兼容字段,等于end_to_end_latency_ms
推荐显式指定模型:
MAML_EDGE_MODEL_SUMMARY_PATH=deploy_artifacts/<experiment_title>/compression_summary.json启动后端:
python -m uvicorn system_layer.backend.main:app --host 0.0.0.0 --port 8000启动 MQTT:
mosquitto -p 1883启动边缘模拟器:
python -m edge_layer.simulator.publish_signal \
--source synthetic \
--device_id esp32-sim-01 \
--host 127.0.0.1 \
--port 1883 \
--topic maml-edge/devices/esp32-sim-01/signal \
--count 5 \
--interval 1.0浏览器控制台:
http://127.0.0.1:8000/
主要接口:
GET /healthGET /model/infoGET /benchmark/currentGET /system/statsGET /historyGET /alertsPOST /predictPOST /adaptPOST /simulate/publishWS /ws/realtime
- fyancy/MetaFD
- Yifei20/Few-shot-Fault-Diagnosis-MAML
- Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks
- Meta-learning as a promising approach for few-shot cross-domain fault diagnosis: Algorithms, applications, and prospects
- Prototypical Networks for Few-shot Learning
- Matching Networks for One Shot Learning
- CWRU official Database
MIT