Downloads · 30 days
8
13% of all-time downloads
lemonhall/heater-switch-detector
heater-switch-detector is a audio classification model from lemonhall. Use it for the audio classification task on the model card, and read the license before you ship it in a product. It is set up for transformers. The card lists the license as mit.
基于Wav2Vec2的热水器开关声音实时检测模型。这是一个少样本学习项目,仅用6个音频样本就能达到100%的检测准确率。
Downloads · 30 days
8
13% of all-time downloads
All-time downloads
63
Public
Repo size
378 MB
Likes
0
Public
Click a slice to open those files.
.pth378 MB · 100%
From the Hugging Face model README
基于Wav2Vec2的热水器开关声音实时检测模型。这是一个少样本学习项目,仅用6个音频样本就能达到100%的检测准确率。
该模型使用Facebook的Wav2Vec2预训练模型作为特征提取器,在热水器开关声音数据上进行微调,实现对开关按下声音的精确识别。
原始音频 [48000 samples]
↓ Wav2Vec2特征编码器 (7层1D卷积)
局部特征 [1199, 768]
↓ Wav2Vec2上下文网络 (12层Transformer)
上下文特征 [1199, 768]
↓ 全局平均池化
固定特征 [768]
↓ 分类头 (2层全连接)
分类结果 [2] (开关/背景)
| 样本类型 | 时长范围 | RMS能量 | 频谱质心 | 过零率 |
|---|---|---|---|---|
| 开关声音 | 3.2-5.2s | 0.0079-0.0115 | 1587-1992Hz | 0.0657-0.1215 |
| 背景噪音 | 2.0-4.0s | 0.005-0.02 | 500-1500Hz | 0.05-0.15 |
| 指标 | 数值 |
|---|---|
| 准确率 | 100% |
| 精确率 | 100% |
| 召回率 | 100% |
| F1分数 | 100% |
| 训练轮数 | 15 epochs |
| 模型大小 | 361MB |
| 推理延迟 | <100ms |
实际\预测 无开关 有开关
无开关 2 0
有开关 0 2
pip install torch torchaudio transformers huggingface_hub
from huggingface_hub import hf_hub_download
import torch
import torchaudio
from transformers import Wav2Vec2Model
# 下载模型
model_path = hf_hub_download(
repo_id="lemonhall/heater-switch-detector",
filename="switch_detector_model.pth"
)
# 加载模型
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
checkpoint = torch.load(model_path, map_location=device)
# 重建模型架构
wav2vec2_model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-base")
classifier = torch.nn.Sequential(
torch.nn.Linear(768, 256),
torch.nn.ReLU(),
torch.nn.Dropout(0.3),
torch.nn.Linear(256, 2)
)
# 加载权重
classifier.load_state_dict(checkpoint['classifier_state_dict'])
classifier.eval()
def predict_audio(audio_path):
# 加载音频
waveform, sample_rate = torchaudio.load(audio_path)
# 重采样到16kHz
if sample_rate != 16000:
resampler = torchaudio.transforms.Resample(sample_rate, 16000)
waveform = resampler(waveform)
# 转为单声道
if waveform.shape[0] > 1:
waveform = waveform.mean(dim=0, keepdim=True)
# 特征提取
with torch.no_grad():
features = wav2vec2_model(waveform).last_hidden_state
pooled_features = features.mean(dim=1) # 全局平均池化
# 分类预测
logits = classifier(pooled_features)
probabilities = torch.softmax(logits, dim=-1)
prediction = torch.argmax(probabilities, dim=-1)
return {
'prediction': '开关按下' if prediction.item() == 1 else '背景声音',
'confidence': probabilities.max().item(),
'probabilities': {
'背景声音': probabilities[0][0].item(),
'开关按下': probabilities[0][1].item()
}
}
# 使用示例
result = predict_audio("test_audio.wav")
print(f"预测结果: {result['prediction']}")
print(f"置信度: {result['confidence']:.3f}")
import pyaudio
import numpy as np
def realtime_detection():
# 音频参数
SAMPLE_RATE = 16000
CHUNK_SIZE = 1024
DETECTION_WINDOW = 3.0 # 3秒检测窗口
# 初始化音频流
audio = pyaudio.PyAudio()
stream = audio.open(
format=pyaudio.paFloat32,
channels=1,
rate=SAMPLE_RATE,
input=True,
frames_per_buffer=CHUNK_SIZE
)
print("🎤 开始实时检测...")
buffer = []
window_size = int(DETECTION_WINDOW * SAMPLE_RATE)
try:
while True:
# 读取音频数据
data = stream.read(CHUNK_SIZE)
audio_chunk = np.frombuffer(data, dtype=np.float32)
buffer.extend(audio_chunk)
# 保持窗口大小
if len(buffer) > window_size:
buffer = buffer[-window_size:]
# 检测
if len(buffer) == window_size:
waveform = torch.FloatTensor(buffer).unsqueeze(0)
with torch.no_grad():
features = wav2vec2_model(waveform).last_hidden_state
pooled_features = features.mean(dim=1)
logits = classifier(pooled_features)
probabilities = torch.softmax(logits, dim=-1)
switch_prob = probabilities[0][1].item()
if switch_prob > 0.93: # 高置信度阈值
print(f"🔥 检测到开关按下! 置信度: {switch_prob:.3f}")
except KeyboardInterrupt:
print("\n⏹️ 检测停止")
finally:
stream.stop_stream()
stream.close()
audio.terminate()
# 运行实时检测
realtime_detection()
如果您使用了这个模型,请引用:
@misc{heater-switch-detector-2024,
title={基于Wav2Vec2的热水器开关声音检测器},
author={lemonhall},
year={2024},
howpublished={\url{https://huggingface.co/lemonhall/heater-switch-detector}}
}
MIT License
如有问题或建议,请通过以下方式联系:
该模型仅用于研究和教育目的。在生产环境中使用前,请进行充分的测试和验证。